blob: 035248c08fa2a509e9442ef889f9fd86374da229 [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"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000054using namespace llvm;
Bill Wendlingec041eb2010-03-12 19:20:40 +000055using namespace dwarf;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000056
Evan Chengb1712452010-01-27 06:25:16 +000057STATISTIC(NumTailCalls, "Number of tail calls");
58
Evan Cheng10e86422008-04-25 19:11:04 +000059// Forward declarations.
Owen Andersone50ed302009-08-10 22:56:29 +000060static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +000061 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000062
David Greenea5f26012011-02-07 19:36:54 +000063static SDValue Insert128BitVector(SDValue Result,
64 SDValue Vec,
65 SDValue Idx,
66 SelectionDAG &DAG,
67 DebugLoc dl);
David Greenef125a292011-02-08 19:04:41 +000068
David Greenea5f26012011-02-07 19:36:54 +000069static SDValue Extract128BitVector(SDValue Vec,
70 SDValue Idx,
71 SelectionDAG &DAG,
72 DebugLoc dl);
73
74/// Generate a DAG to grab 128-bits from a vector > 128 bits. This
75/// sets things up to match to an AVX VEXTRACTF128 instruction or a
David Greene74a579d2011-02-10 16:57:36 +000076/// simple subregister reference. Idx is an index in the 128 bits we
77/// want. It need not be aligned to a 128-bit bounday. That makes
78/// lowering EXTRACT_VECTOR_ELT operations easier.
David Greenea5f26012011-02-07 19:36:54 +000079static SDValue Extract128BitVector(SDValue Vec,
80 SDValue Idx,
81 SelectionDAG &DAG,
82 DebugLoc dl) {
83 EVT VT = Vec.getValueType();
84 assert(VT.getSizeInBits() == 256 && "Unexpected vector size!");
David Greenea5f26012011-02-07 19:36:54 +000085 EVT ElVT = VT.getVectorElementType();
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +000086 int Factor = VT.getSizeInBits()/128;
87 EVT ResultVT = EVT::getVectorVT(*DAG.getContext(), ElVT,
88 VT.getVectorNumElements()/Factor);
David Greenea5f26012011-02-07 19:36:54 +000089
90 // Extract from UNDEF is UNDEF.
91 if (Vec.getOpcode() == ISD::UNDEF)
92 return DAG.getNode(ISD::UNDEF, dl, ResultVT);
93
94 if (isa<ConstantSDNode>(Idx)) {
95 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
96
97 // Extract the relevant 128 bits. Generate an EXTRACT_SUBVECTOR
98 // we can match to VEXTRACTF128.
99 unsigned ElemsPerChunk = 128 / ElVT.getSizeInBits();
100
101 // This is the index of the first element of the 128-bit chunk
102 // we want.
103 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits()) / 128)
104 * ElemsPerChunk);
105
106 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
David Greenea5f26012011-02-07 19:36:54 +0000107 SDValue Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, ResultVT, Vec,
108 VecIdx);
109
110 return Result;
111 }
112
113 return SDValue();
114}
115
116/// Generate a DAG to put 128-bits into a vector > 128 bits. This
117/// sets things up to match to an AVX VINSERTF128 instruction or a
David Greene6b381262011-02-09 15:32:06 +0000118/// simple superregister reference. Idx is an index in the 128 bits
119/// we want. It need not be aligned to a 128-bit bounday. That makes
120/// lowering INSERT_VECTOR_ELT operations easier.
David Greenea5f26012011-02-07 19:36:54 +0000121static SDValue Insert128BitVector(SDValue Result,
122 SDValue Vec,
123 SDValue Idx,
124 SelectionDAG &DAG,
125 DebugLoc dl) {
126 if (isa<ConstantSDNode>(Idx)) {
127 EVT VT = Vec.getValueType();
128 assert(VT.getSizeInBits() == 128 && "Unexpected vector size!");
129
130 EVT ElVT = VT.getVectorElementType();
David Greenea5f26012011-02-07 19:36:54 +0000131 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
David Greenea5f26012011-02-07 19:36:54 +0000132 EVT ResultVT = Result.getValueType();
133
134 // Insert the relevant 128 bits.
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +0000135 unsigned ElemsPerChunk = 128/ElVT.getSizeInBits();
David Greenea5f26012011-02-07 19:36:54 +0000136
137 // This is the index of the first element of the 128-bit chunk
138 // we want.
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +0000139 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits())/128)
David Greenea5f26012011-02-07 19:36:54 +0000140 * ElemsPerChunk);
141
142 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
David Greenea5f26012011-02-07 19:36:54 +0000143 Result = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResultVT, Result, Vec,
144 VecIdx);
145 return Result;
146 }
147
148 return SDValue();
149}
150
Chris Lattnerf0144122009-07-28 03:13:23 +0000151static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
Evan Cheng2bffee22011-02-01 01:14:13 +0000152 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
153 bool is64Bit = Subtarget->is64Bit();
NAKAMURA Takumi27635382011-02-05 15:10:54 +0000154
Evan Cheng2bffee22011-02-01 01:14:13 +0000155 if (Subtarget->isTargetEnvMacho()) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000156 if (is64Bit)
157 return new X8664_MachoTargetObjectFile();
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000158 return new TargetLoweringObjectFileMachO();
Michael J. Spencerec38de22010-10-10 22:04:20 +0000159 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000160
Evan Cheng203576a2011-07-20 19:50:42 +0000161 if (Subtarget->isTargetELF())
162 return new TargetLoweringObjectFileELF();
Evan Cheng2bffee22011-02-01 01:14:13 +0000163 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
Chris Lattnere019ec12010-12-19 20:07:10 +0000164 return new TargetLoweringObjectFileCOFF();
Eric Christopher62f35a22010-07-05 19:26:33 +0000165 llvm_unreachable("unknown subtarget type");
Chris Lattnerf0144122009-07-28 03:13:23 +0000166}
167
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +0000168X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +0000169 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +0000170 Subtarget = &TM.getSubtarget<X86Subtarget>();
Bruno Cardoso Lopesaed890b2011-08-01 21:54:05 +0000171 X86ScalarSSEf64 = Subtarget->hasXMMInt() || Subtarget->hasAVX();
172 X86ScalarSSEf32 = Subtarget->hasXMM() || Subtarget->hasAVX();
Evan Cheng25ab6902006-09-08 06:48:29 +0000173 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000174
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000175 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000176 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000177
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000178 // Set up the TargetLowering object.
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000179 static MVT IntVTs[] = { MVT::i8, MVT::i16, MVT::i32, MVT::i64 };
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000180
181 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Duncan Sands03228082008-11-23 15:47:28 +0000182 setBooleanContents(ZeroOrOneBooleanContent);
Eric Christopher471e4222011-06-08 23:55:35 +0000183
Eric Christopherde5e1012011-03-11 01:05:58 +0000184 // For 64-bit since we have so many registers use the ILP scheduler, for
185 // 32-bit code use the register pressure specific scheduling.
186 if (Subtarget->is64Bit())
187 setSchedulingPreference(Sched::ILP);
188 else
189 setSchedulingPreference(Sched::RegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +0000190 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +0000191
Michael J. Spencer92bf38c2010-10-10 23:11:06 +0000192 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing()) {
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000193 // Setup Windows compiler runtime calls.
194 setLibcallName(RTLIB::SDIV_I64, "_alldiv");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000195 setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
Julien Lerougef2960822011-07-08 21:40:25 +0000196 setLibcallName(RTLIB::SREM_I64, "_allrem");
197 setLibcallName(RTLIB::UREM_I64, "_aullrem");
198 setLibcallName(RTLIB::MUL_I64, "_allmul");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000199 setLibcallName(RTLIB::FPTOUINT_F64_I64, "_ftol2");
Michael J. Spencer94f7eeb2010-10-19 07:32:52 +0000200 setLibcallName(RTLIB::FPTOUINT_F32_I64, "_ftol2");
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000201 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer335b8062010-10-11 05:29:15 +0000202 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
Julien Lerougef2960822011-07-08 21:40:25 +0000203 setLibcallCallingConv(RTLIB::SREM_I64, CallingConv::X86_StdCall);
204 setLibcallCallingConv(RTLIB::UREM_I64, CallingConv::X86_StdCall);
205 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::X86_StdCall);
Michael J. Spencer6dad10e2010-10-27 18:52:38 +0000206 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::C);
207 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::C);
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000208 }
209
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000210 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000211 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000212 setUseUnderscoreSetJmp(false);
213 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000214 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000215 // MS runtime is weird: it exports _setjmp, but longjmp!
216 setUseUnderscoreSetJmp(true);
217 setUseUnderscoreLongJmp(false);
218 } else {
219 setUseUnderscoreSetJmp(true);
220 setUseUnderscoreLongJmp(true);
221 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000222
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000223 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000224 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
Dan Gohman71edb242010-04-30 18:30:26 +0000225 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000226 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000227 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000228 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000229
Owen Anderson825b72b2009-08-11 20:47:22 +0000230 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000231
Scott Michelfdc40a02009-02-17 22:15:04 +0000232 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000233 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000234 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000235 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000236 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000237 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
238 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000239
240 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000241 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
242 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
243 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
244 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
245 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
246 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000247
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000248 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
249 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000250 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
251 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
252 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000253
Evan Cheng25ab6902006-09-08 06:48:29 +0000254 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000255 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
256 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Eli Friedman948e95a2009-05-23 09:59:16 +0000257 } else if (!UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000258 // We have an algorithm for SSE2->double, and we turn this into a
259 // 64-bit FILD followed by conditional FADD for other targets.
260 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000261 // We have an algorithm for SSE2, and we turn this into a 64-bit
262 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000263 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000264 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000265
266 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
267 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000268 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
269 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000270
Devang Patel6a784892009-06-05 18:48:29 +0000271 if (!UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000272 // SSE has no i16 to fp conversion, only i32
273 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000274 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000275 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000276 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000277 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000278 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
279 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000280 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000281 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000282 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
283 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000284 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000285
Dale Johannesen73328d12007-09-19 23:55:34 +0000286 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
287 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000288 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
289 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000290
Evan Cheng02568ff2006-01-30 22:13:22 +0000291 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
292 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000293 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
294 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000295
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000296 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000297 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000298 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000299 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000300 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000301 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
302 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000303 }
304
305 // Handle FP_TO_UINT by promoting the destination to a larger signed
306 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000307 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
308 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
309 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000310
Evan Cheng25ab6902006-09-08 06:48:29 +0000311 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000312 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
313 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman948e95a2009-05-23 09:59:16 +0000314 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000315 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000316 // Expand FP_TO_UINT into a select.
317 // FIXME: We would like to use a Custom expander here eventually to do
318 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000319 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000320 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000321 // With SSE3 we can use fisttpll to convert to a signed i64; without
322 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000323 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000324 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000325
Chris Lattner399610a2006-12-05 18:22:22 +0000326 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000327 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000328 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
329 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000330 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000331 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000332 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000333 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000334 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000335 }
Chris Lattner21f66852005-12-23 05:15:23 +0000336
Dan Gohmanb00ee212008-02-18 19:34:53 +0000337 // Scalar integer divide and remainder are lowered to use operations that
338 // produce two results, to match the available instructions. This exposes
339 // the two-result form to trivial CSE, which is able to combine x/y and x%y
340 // into a single instruction.
341 //
342 // Scalar integer multiply-high is also lowered to use two-result
343 // operations, to match the available instructions. However, plain multiply
344 // (low) operations are left as Legal, as there are single-result
345 // instructions for this in x86. Using the two-result multiply instructions
346 // when both high and low results are needed must be arranged by dagcombine.
Chris Lattnere019ec12010-12-19 20:07:10 +0000347 for (unsigned i = 0, e = 4; i != e; ++i) {
348 MVT VT = IntVTs[i];
349 setOperationAction(ISD::MULHS, VT, Expand);
350 setOperationAction(ISD::MULHU, VT, Expand);
351 setOperationAction(ISD::SDIV, VT, Expand);
352 setOperationAction(ISD::UDIV, VT, Expand);
353 setOperationAction(ISD::SREM, VT, Expand);
354 setOperationAction(ISD::UREM, VT, Expand);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000355
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000356 // Add/Sub overflow ops with MVT::Glues are lowered to EFLAGS dependences.
Chris Lattnerd8ff7ec2010-12-20 01:03:27 +0000357 setOperationAction(ISD::ADDC, VT, Custom);
358 setOperationAction(ISD::ADDE, VT, Custom);
359 setOperationAction(ISD::SUBC, VT, Custom);
360 setOperationAction(ISD::SUBE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000361 }
Dan Gohmana37c9f72007-09-25 18:23:27 +0000362
Owen Anderson825b72b2009-08-11 20:47:22 +0000363 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
364 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
365 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
366 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000367 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000368 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
369 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
370 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
371 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
372 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
373 setOperationAction(ISD::FREM , MVT::f32 , Expand);
374 setOperationAction(ISD::FREM , MVT::f64 , Expand);
375 setOperationAction(ISD::FREM , MVT::f80 , Expand);
376 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000377
Owen Anderson825b72b2009-08-11 20:47:22 +0000378 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
379 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000380 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
381 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000382 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
383 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000384 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000385 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
386 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000387 }
388
Benjamin Kramer1292c222010-12-04 20:32:23 +0000389 if (Subtarget->hasPOPCNT()) {
390 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
391 } else {
392 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
393 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
394 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
395 if (Subtarget->is64Bit())
396 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
397 }
398
Owen Anderson825b72b2009-08-11 20:47:22 +0000399 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
400 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000401
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000402 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000403 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000404 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000405 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000406 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000407 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
408 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
409 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
410 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
411 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000412 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000413 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
414 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
415 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
416 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000417 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000418 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
Andrew Trickf6c39412011-03-23 23:11:02 +0000419 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000420 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000421 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000422
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000423 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000424 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
425 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
426 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
427 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000428 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000429 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
430 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000431 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000432 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000433 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
434 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
435 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
436 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000437 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000438 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000439 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000440 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
441 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
442 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000443 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000444 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
445 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
446 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000447 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000448
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000449 if (Subtarget->hasXMM())
Owen Anderson825b72b2009-08-11 20:47:22 +0000450 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000451
Eric Christopher9a9d2752010-07-22 02:48:34 +0000452 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000453 setOperationAction(ISD::ATOMIC_FENCE , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000454
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000455 // On X86 and X86-64, atomic operations are lowered to locked instructions.
456 // Locked instructions, in turn, have implicit fence semantics (all memory
457 // operations are flushed before issuing the locked instruction, and they
458 // are not buffered), so we can fold away the common pattern of
459 // fence-atomic-fence.
460 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000461
Mon P Wang63307c32008-05-05 19:05:59 +0000462 // Expand certain atomics
Chris Lattnere019ec12010-12-19 20:07:10 +0000463 for (unsigned i = 0, e = 4; i != e; ++i) {
464 MVT VT = IntVTs[i];
465 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
466 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
Eli Friedman327236c2011-08-24 20:50:09 +0000467 setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000468 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000469
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000470 if (!Subtarget->is64Bit()) {
Eli Friedmanf8f90f02011-08-24 22:33:28 +0000471 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000472 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
473 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
474 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
475 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
476 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
477 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
478 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000479 }
480
Evan Cheng3c992d22006-03-07 02:02:57 +0000481 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000482 if (!Subtarget->isTargetDarwin() &&
483 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000484 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000485 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000486 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000487
Owen Anderson825b72b2009-08-11 20:47:22 +0000488 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
489 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
490 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
491 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000492 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000493 setExceptionPointerRegister(X86::RAX);
494 setExceptionSelectorRegister(X86::RDX);
495 } else {
496 setExceptionPointerRegister(X86::EAX);
497 setExceptionSelectorRegister(X86::EDX);
498 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000499 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
500 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000501
Owen Anderson825b72b2009-08-11 20:47:22 +0000502 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000503
Owen Anderson825b72b2009-08-11 20:47:22 +0000504 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000505
Nate Begemanacc398c2006-01-25 18:21:52 +0000506 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000507 setOperationAction(ISD::VASTART , MVT::Other, Custom);
508 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000509 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000510 setOperationAction(ISD::VAARG , MVT::Other, Custom);
511 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000512 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000513 setOperationAction(ISD::VAARG , MVT::Other, Expand);
514 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000515 }
Evan Chengae642192007-03-02 23:16:35 +0000516
Owen Anderson825b72b2009-08-11 20:47:22 +0000517 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
518 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
NAKAMURA Takumia2e07622011-03-24 07:07:00 +0000519 setOperationAction(ISD::DYNAMIC_STACKALLOC,
520 (Subtarget->is64Bit() ? MVT::i64 : MVT::i32),
521 (Subtarget->isTargetCOFF()
522 && !Subtarget->isTargetEnvMacho()
523 ? Custom : Expand));
Chris Lattnerb99329e2006-01-13 02:42:53 +0000524
Evan Chengc7ce29b2009-02-13 22:36:38 +0000525 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000526 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000527 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000528 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
529 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000530
Evan Cheng223547a2006-01-31 22:28:30 +0000531 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000532 setOperationAction(ISD::FABS , MVT::f64, Custom);
533 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000534
535 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000536 setOperationAction(ISD::FNEG , MVT::f64, Custom);
537 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000538
Evan Cheng68c47cb2007-01-05 07:55:56 +0000539 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000540 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
541 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000542
Stuart Hastings4fd0dee2011-06-01 04:39:42 +0000543 // Lower this to FGETSIGNx86 plus an AND.
544 setOperationAction(ISD::FGETSIGN, MVT::i64, Custom);
545 setOperationAction(ISD::FGETSIGN, MVT::i32, Custom);
546
Evan Chengd25e9e82006-02-02 00:28:23 +0000547 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000548 setOperationAction(ISD::FSIN , MVT::f64, Expand);
549 setOperationAction(ISD::FCOS , MVT::f64, Expand);
550 setOperationAction(ISD::FSIN , MVT::f32, Expand);
551 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000552
Chris Lattnera54aa942006-01-29 06:26:08 +0000553 // Expand FP immediates into loads from the stack, except for the special
554 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000555 addLegalFPImmediate(APFloat(+0.0)); // xorpd
556 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000557 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000558 // Use SSE for f32, x87 for f64.
559 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000560 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
561 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000562
563 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000564 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000565
566 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000567 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000568
Owen Anderson825b72b2009-08-11 20:47:22 +0000569 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000570
571 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000572 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
573 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000574
575 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000576 setOperationAction(ISD::FSIN , MVT::f32, Expand);
577 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000578
Nate Begemane1795842008-02-14 08:57:00 +0000579 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000580 addLegalFPImmediate(APFloat(+0.0f)); // xorps
581 addLegalFPImmediate(APFloat(+0.0)); // FLD0
582 addLegalFPImmediate(APFloat(+1.0)); // FLD1
583 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
584 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
585
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000586 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000587 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
588 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000589 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000590 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000591 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000592 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000593 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
594 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000595
Owen Anderson825b72b2009-08-11 20:47:22 +0000596 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
597 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
598 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
599 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000600
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000601 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000602 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
603 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000604 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000605 addLegalFPImmediate(APFloat(+0.0)); // FLD0
606 addLegalFPImmediate(APFloat(+1.0)); // FLD1
607 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
608 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000609 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
610 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
611 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
612 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000613 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000614
Cameron Zwarich33390842011-07-08 21:39:21 +0000615 // We don't support FMA.
616 setOperationAction(ISD::FMA, MVT::f64, Expand);
617 setOperationAction(ISD::FMA, MVT::f32, Expand);
618
Dale Johannesen59a58732007-08-05 18:49:15 +0000619 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000620 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000621 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
622 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
623 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000624 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000625 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000626 addLegalFPImmediate(TmpFlt); // FLD0
627 TmpFlt.changeSign();
628 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000629
630 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000631 APFloat TmpFlt2(+1.0);
632 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
633 &ignored);
634 addLegalFPImmediate(TmpFlt2); // FLD1
635 TmpFlt2.changeSign();
636 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
637 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000638
Evan Chengc7ce29b2009-02-13 22:36:38 +0000639 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000640 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
641 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000642 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000643
644 setOperationAction(ISD::FMA, MVT::f80, Expand);
Dale Johannesen2f429012007-09-26 21:10:55 +0000645 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000646
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000647 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000648 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
649 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
650 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000651
Owen Anderson825b72b2009-08-11 20:47:22 +0000652 setOperationAction(ISD::FLOG, MVT::f80, Expand);
653 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
654 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
655 setOperationAction(ISD::FEXP, MVT::f80, Expand);
656 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000657
Mon P Wangf007a8b2008-11-06 05:31:54 +0000658 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000659 // (for widening) or expand (for scalarization). Then we will selectively
660 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000661 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
662 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
663 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
664 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
665 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
666 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
667 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
668 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
669 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
670 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
671 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
672 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
673 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
674 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
675 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
676 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
677 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000678 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
David Greenecfe33c42011-01-26 19:13:22 +0000679 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
680 setOperationAction(ISD::INSERT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000681 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
682 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
683 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
684 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
685 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
686 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
687 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
688 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
689 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
690 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
691 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
692 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
693 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
694 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
695 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
696 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
697 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
698 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
699 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
700 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
701 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
702 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
703 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
704 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
705 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
706 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
707 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
708 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
709 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
710 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
711 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000712 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000713 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
714 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
715 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
716 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
717 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
718 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
719 setTruncStoreAction((MVT::SimpleValueType)VT,
720 (MVT::SimpleValueType)InnerVT, Expand);
721 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
722 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
723 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000724 }
725
Evan Chengc7ce29b2009-02-13 22:36:38 +0000726 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
727 // with -msoft-float, disable use of MMX as well.
Chris Lattner2a786eb2010-12-19 20:19:20 +0000728 if (!UseSoftFloat && Subtarget->hasMMX()) {
Dale Johannesene93d99c2010-10-20 21:32:10 +0000729 addRegisterClass(MVT::x86mmx, X86::VR64RegisterClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000730 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000731 }
732
Dale Johannesen0488fb62010-09-30 23:57:10 +0000733 // MMX-sized vectors (other than x86mmx) are expected to be expanded
734 // into smaller operations.
735 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
736 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
737 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
738 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
739 setOperationAction(ISD::AND, MVT::v8i8, Expand);
740 setOperationAction(ISD::AND, MVT::v4i16, Expand);
741 setOperationAction(ISD::AND, MVT::v2i32, Expand);
742 setOperationAction(ISD::AND, MVT::v1i64, Expand);
743 setOperationAction(ISD::OR, MVT::v8i8, Expand);
744 setOperationAction(ISD::OR, MVT::v4i16, Expand);
745 setOperationAction(ISD::OR, MVT::v2i32, Expand);
746 setOperationAction(ISD::OR, MVT::v1i64, Expand);
747 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
748 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
749 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
750 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
751 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
752 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
753 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
754 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
755 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
756 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
757 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
758 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
759 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000760 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
761 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
762 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
763 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000764
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000765 if (!UseSoftFloat && Subtarget->hasXMM()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000766 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000767
Owen Anderson825b72b2009-08-11 20:47:22 +0000768 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
769 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
770 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
771 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
772 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
773 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
774 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
775 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
776 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
777 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
778 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
779 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000780 }
781
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000782 if (!UseSoftFloat && Subtarget->hasXMMInt()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000783 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000784
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000785 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
786 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000787 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
788 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
789 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
790 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000791
Owen Anderson825b72b2009-08-11 20:47:22 +0000792 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
793 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
794 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
795 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
796 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
797 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
798 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
799 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
800 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
801 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
802 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
803 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
804 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
805 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
806 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
807 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000808
Owen Anderson825b72b2009-08-11 20:47:22 +0000809 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
810 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
811 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
812 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000813
Owen Anderson825b72b2009-08-11 20:47:22 +0000814 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
815 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
816 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
817 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
818 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000819
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000820 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
821 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
822 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
823 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
824 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
825
Evan Cheng2c3ae372006-04-12 21:21:57 +0000826 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000827 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
828 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000829 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000830 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000831 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000832 // Do not attempt to custom lower non-128-bit vectors
833 if (!VT.is128BitVector())
834 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000835 setOperationAction(ISD::BUILD_VECTOR,
836 VT.getSimpleVT().SimpleTy, Custom);
837 setOperationAction(ISD::VECTOR_SHUFFLE,
838 VT.getSimpleVT().SimpleTy, Custom);
839 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
840 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000841 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000842
Owen Anderson825b72b2009-08-11 20:47:22 +0000843 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
844 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
845 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
846 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
847 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
848 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000849
Nate Begemancdd1eec2008-02-12 22:51:28 +0000850 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000851 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
852 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000853 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000854
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000855 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000856 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
857 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000858 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000859
860 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000861 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000862 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000863
Owen Andersond6662ad2009-08-10 20:46:15 +0000864 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000865 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000866 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000867 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000868 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000869 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000870 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000871 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000872 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000873 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000874 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000875
Owen Anderson825b72b2009-08-11 20:47:22 +0000876 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000877
Evan Cheng2c3ae372006-04-12 21:21:57 +0000878 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000879 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
880 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
881 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
882 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000883
Owen Anderson825b72b2009-08-11 20:47:22 +0000884 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
885 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000886 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000887
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000888 if (Subtarget->hasSSE41() || Subtarget->hasAVX()) {
Dale Johannesen54feef22010-05-27 20:12:41 +0000889 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
890 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
891 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
892 setOperationAction(ISD::FRINT, MVT::f32, Legal);
893 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
894 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
895 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
896 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
897 setOperationAction(ISD::FRINT, MVT::f64, Legal);
898 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
899
Nate Begeman14d12ca2008-02-11 04:19:36 +0000900 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000901 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000902
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000903 // Can turn SHL into an integer multiply.
904 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
Nate Begeman51409212010-07-28 00:21:48 +0000905 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000906
Nate Begeman14d12ca2008-02-11 04:19:36 +0000907 // i8 and i16 vectors are custom , because the source register and source
908 // source memory operand types are not the same width. f32 vectors are
909 // custom since the immediate controlling the insert encodes additional
910 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000911 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
912 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
913 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
914 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000915
Owen Anderson825b72b2009-08-11 20:47:22 +0000916 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
917 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
918 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
919 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000920
921 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000922 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
923 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000924 }
925 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000926
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000927 if (Subtarget->hasSSE2() || Subtarget->hasAVX()) {
Nadav Rotem43012222011-05-11 08:12:09 +0000928 setOperationAction(ISD::SRL, MVT::v2i64, Custom);
929 setOperationAction(ISD::SRL, MVT::v4i32, Custom);
930 setOperationAction(ISD::SRL, MVT::v16i8, Custom);
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000931 setOperationAction(ISD::SRL, MVT::v8i16, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +0000932
933 setOperationAction(ISD::SHL, MVT::v2i64, Custom);
934 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
935 setOperationAction(ISD::SHL, MVT::v8i16, Custom);
936
937 setOperationAction(ISD::SRA, MVT::v4i32, Custom);
938 setOperationAction(ISD::SRA, MVT::v8i16, Custom);
939 }
940
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000941 if (Subtarget->hasSSE42() || Subtarget->hasAVX())
Owen Anderson825b72b2009-08-11 20:47:22 +0000942 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000943
David Greene9b9838d2009-06-29 16:47:10 +0000944 if (!UseSoftFloat && Subtarget->hasAVX()) {
Bruno Cardoso Lopesdbd4fe22011-07-21 02:24:08 +0000945 addRegisterClass(MVT::v32i8, X86::VR256RegisterClass);
946 addRegisterClass(MVT::v16i16, X86::VR256RegisterClass);
947 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
948 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
949 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
950 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000951
Owen Anderson825b72b2009-08-11 20:47:22 +0000952 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +0000953 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
954 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +0000955
Owen Anderson825b72b2009-08-11 20:47:22 +0000956 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
957 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
958 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
959 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
960 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
961 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000962
Owen Anderson825b72b2009-08-11 20:47:22 +0000963 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
964 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
965 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
966 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
967 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
968 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000969
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +0000970 setOperationAction(ISD::FP_TO_SINT, MVT::v8i32, Legal);
971 setOperationAction(ISD::SINT_TO_FP, MVT::v8i32, Legal);
Bruno Cardoso Lopes55244ce2011-08-01 21:54:09 +0000972 setOperationAction(ISD::FP_ROUND, MVT::v4f32, Legal);
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +0000973
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +0000974 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4f64, Custom);
975 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i64, Custom);
976 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8f32, Custom);
977 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i32, Custom);
978 setOperationAction(ISD::CONCAT_VECTORS, MVT::v32i8, Custom);
979 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i16, Custom);
980
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000981 setOperationAction(ISD::SRL, MVT::v4i64, Custom);
982 setOperationAction(ISD::SRL, MVT::v8i32, Custom);
983 setOperationAction(ISD::SRL, MVT::v16i16, Custom);
984 setOperationAction(ISD::SRL, MVT::v32i8, Custom);
985
986 setOperationAction(ISD::SHL, MVT::v4i64, Custom);
987 setOperationAction(ISD::SHL, MVT::v8i32, Custom);
988 setOperationAction(ISD::SHL, MVT::v16i16, Custom);
989 setOperationAction(ISD::SHL, MVT::v32i8, Custom);
990
991 setOperationAction(ISD::SRA, MVT::v8i32, Custom);
992 setOperationAction(ISD::SRA, MVT::v16i16, Custom);
993
Craig Toppera5347802011-08-23 04:36:33 +0000994 setOperationAction(ISD::VSETCC, MVT::v32i8, Custom);
995 setOperationAction(ISD::VSETCC, MVT::v16i16, Custom);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +0000996 setOperationAction(ISD::VSETCC, MVT::v8i32, Custom);
997 setOperationAction(ISD::VSETCC, MVT::v4i64, Custom);
998
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +0000999 setOperationAction(ISD::SELECT, MVT::v4f64, Custom);
1000 setOperationAction(ISD::SELECT, MVT::v4i64, Custom);
1001 setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
1002
Craig Topper13894fa2011-08-24 06:14:18 +00001003 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
1004 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
1005 setOperationAction(ISD::ADD, MVT::v16i16, Custom);
1006 setOperationAction(ISD::ADD, MVT::v32i8, Custom);
1007
1008 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
1009 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
1010 setOperationAction(ISD::SUB, MVT::v16i16, Custom);
1011 setOperationAction(ISD::SUB, MVT::v32i8, Custom);
1012
1013 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1014 setOperationAction(ISD::MUL, MVT::v8i32, Custom);
1015 setOperationAction(ISD::MUL, MVT::v16i16, Custom);
1016 // Don't lower v32i8 because there is no 128-bit byte mul
1017
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001018 // Custom lower several nodes for 256-bit types.
David Greene54d8eba2011-01-27 22:38:56 +00001019 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001020 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
1021 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
1022 EVT VT = SVT;
1023
1024 // Extract subvector is special because the value type
1025 // (result) is 128-bit but the source is 256-bit wide.
1026 if (VT.is128BitVector())
1027 setOperationAction(ISD::EXTRACT_SUBVECTOR, SVT, Custom);
1028
1029 // Do not attempt to custom lower other non-256-bit vectors
1030 if (!VT.is256BitVector())
David Greene9b9838d2009-06-29 16:47:10 +00001031 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001032
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001033 setOperationAction(ISD::BUILD_VECTOR, SVT, Custom);
1034 setOperationAction(ISD::VECTOR_SHUFFLE, SVT, Custom);
1035 setOperationAction(ISD::INSERT_VECTOR_ELT, SVT, Custom);
1036 setOperationAction(ISD::EXTRACT_VECTOR_ELT, SVT, Custom);
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00001037 setOperationAction(ISD::SCALAR_TO_VECTOR, SVT, Custom);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001038 setOperationAction(ISD::INSERT_SUBVECTOR, SVT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001039 }
1040
David Greene54d8eba2011-01-27 22:38:56 +00001041 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001042 for (unsigned i = (unsigned)MVT::v32i8; i != (unsigned)MVT::v4i64; ++i) {
1043 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
1044 EVT VT = SVT;
David Greene54d8eba2011-01-27 22:38:56 +00001045
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001046 // Do not attempt to promote non-256-bit vectors
1047 if (!VT.is256BitVector())
David Greene54d8eba2011-01-27 22:38:56 +00001048 continue;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001049
1050 setOperationAction(ISD::AND, SVT, Promote);
1051 AddPromotedToType (ISD::AND, SVT, MVT::v4i64);
1052 setOperationAction(ISD::OR, SVT, Promote);
1053 AddPromotedToType (ISD::OR, SVT, MVT::v4i64);
1054 setOperationAction(ISD::XOR, SVT, Promote);
1055 AddPromotedToType (ISD::XOR, SVT, MVT::v4i64);
1056 setOperationAction(ISD::LOAD, SVT, Promote);
1057 AddPromotedToType (ISD::LOAD, SVT, MVT::v4i64);
1058 setOperationAction(ISD::SELECT, SVT, Promote);
1059 AddPromotedToType (ISD::SELECT, SVT, MVT::v4i64);
David Greene54d8eba2011-01-27 22:38:56 +00001060 }
David Greene9b9838d2009-06-29 16:47:10 +00001061 }
1062
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001063 // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
1064 // of this type with custom code.
1065 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1066 VT != (unsigned)MVT::LAST_VECTOR_VALUETYPE; VT++) {
1067 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT, Custom);
1068 }
1069
Evan Cheng6be2c582006-04-05 23:38:46 +00001070 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001071 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001072
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001073
Eli Friedman962f5492010-06-02 19:35:46 +00001074 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1075 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001076 //
Eli Friedman962f5492010-06-02 19:35:46 +00001077 // FIXME: We really should do custom legalization for addition and
1078 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1079 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001080 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1081 // Add/Sub/Mul with overflow operations are custom lowered.
1082 MVT VT = IntVTs[i];
1083 setOperationAction(ISD::SADDO, VT, Custom);
1084 setOperationAction(ISD::UADDO, VT, Custom);
1085 setOperationAction(ISD::SSUBO, VT, Custom);
1086 setOperationAction(ISD::USUBO, VT, Custom);
1087 setOperationAction(ISD::SMULO, VT, Custom);
1088 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001089 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001090
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001091 // There are no 8-bit 3-address imul/mul instructions
1092 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1093 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001094
Evan Chengd54f2d52009-03-31 19:38:51 +00001095 if (!Subtarget->is64Bit()) {
1096 // These libcalls are not available in 32-bit.
1097 setLibcallName(RTLIB::SHL_I128, 0);
1098 setLibcallName(RTLIB::SRL_I128, 0);
1099 setLibcallName(RTLIB::SRA_I128, 0);
1100 }
1101
Evan Cheng206ee9d2006-07-07 08:33:52 +00001102 // We have target-specific dag combine patterns for the following nodes:
1103 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001104 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +00001105 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +00001106 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001107 setTargetDAGCombine(ISD::SHL);
1108 setTargetDAGCombine(ISD::SRA);
1109 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001110 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001111 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001112 setTargetDAGCombine(ISD::ADD);
1113 setTargetDAGCombine(ISD::SUB);
Chris Lattner149a4e52008-02-22 02:09:43 +00001114 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001115 setTargetDAGCombine(ISD::ZERO_EXTEND);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001116 setTargetDAGCombine(ISD::SINT_TO_FP);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001117 if (Subtarget->is64Bit())
1118 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001119
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001120 computeRegisterProperties();
1121
Evan Cheng05219282011-01-06 06:52:41 +00001122 // On Darwin, -Os means optimize for size without hurting performance,
1123 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001124 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001125 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001126 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001127 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1128 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1129 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengfb8075d2008-02-28 00:43:03 +00001130 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001131 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001132
1133 setPrefFunctionAlignment(4);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001134}
1135
Scott Michel5b8f82e2008-03-10 15:42:14 +00001136
Owen Anderson825b72b2009-08-11 20:47:22 +00001137MVT::SimpleValueType X86TargetLowering::getSetCCResultType(EVT VT) const {
1138 return MVT::i8;
Scott Michel5b8f82e2008-03-10 15:42:14 +00001139}
1140
1141
Evan Cheng29286502008-01-23 23:17:41 +00001142/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1143/// the desired ByVal argument alignment.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001144static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
Evan Cheng29286502008-01-23 23:17:41 +00001145 if (MaxAlign == 16)
1146 return;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001147 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001148 if (VTy->getBitWidth() == 128)
1149 MaxAlign = 16;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001150 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001151 unsigned EltAlign = 0;
1152 getMaxByValAlign(ATy->getElementType(), EltAlign);
1153 if (EltAlign > MaxAlign)
1154 MaxAlign = EltAlign;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001155 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001156 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1157 unsigned EltAlign = 0;
1158 getMaxByValAlign(STy->getElementType(i), EltAlign);
1159 if (EltAlign > MaxAlign)
1160 MaxAlign = EltAlign;
1161 if (MaxAlign == 16)
1162 break;
1163 }
1164 }
1165 return;
1166}
1167
1168/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1169/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001170/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1171/// are at 4-byte boundaries.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001172unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001173 if (Subtarget->is64Bit()) {
1174 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001175 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001176 if (TyAlign > 8)
1177 return TyAlign;
1178 return 8;
1179 }
1180
Evan Cheng29286502008-01-23 23:17:41 +00001181 unsigned Align = 4;
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001182 if (Subtarget->hasXMM())
Dale Johannesen0c191872008-02-08 19:48:20 +00001183 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001184 return Align;
1185}
Chris Lattner2b02a442007-02-25 08:29:00 +00001186
Evan Chengf0df0312008-05-15 08:39:06 +00001187/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001188/// and store operations as a result of memset, memcpy, and memmove
1189/// lowering. If DstAlign is zero that means it's safe to destination
1190/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1191/// means there isn't a need to check it against alignment requirement,
1192/// probably because the source does not need to be loaded. If
1193/// 'NonScalarIntSafe' is true, that means it's safe to return a
1194/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1195/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1196/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001197/// It returns EVT::Other if the type should be determined using generic
1198/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001199EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001200X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1201 unsigned DstAlign, unsigned SrcAlign,
Evan Chengf28f8bc2010-04-02 19:36:14 +00001202 bool NonScalarIntSafe,
Evan Chengc3b0c342010-04-08 07:37:57 +00001203 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001204 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001205 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1206 // linux. This is because the stack realignment code can't handle certain
1207 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001208 const Function *F = MF.getFunction();
Evan Chenga5e13622011-01-07 19:35:30 +00001209 if (NonScalarIntSafe &&
1210 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001211 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001212 (Subtarget->isUnalignedMemAccessFast() ||
1213 ((DstAlign == 0 || DstAlign >= 16) &&
1214 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001215 Subtarget->getStackAlignment() >= 16) {
1216 if (Subtarget->hasSSE2())
1217 return MVT::v4i32;
Evan Chengf28f8bc2010-04-02 19:36:14 +00001218 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001219 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001220 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001221 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001222 Subtarget->getStackAlignment() >= 8 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001223 Subtarget->hasXMMInt()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001224 // Do not use f64 to lower memcpy if source is string constant. It's
1225 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001226 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001227 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001228 }
Evan Chengf0df0312008-05-15 08:39:06 +00001229 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001230 return MVT::i64;
1231 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001232}
1233
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001234/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1235/// current function. The returned value is a member of the
1236/// MachineJumpTableInfo::JTEntryKind enum.
1237unsigned X86TargetLowering::getJumpTableEncoding() const {
1238 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1239 // symbol.
1240 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1241 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001242 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001243
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001244 // Otherwise, use the normal jump table encoding heuristics.
1245 return TargetLowering::getJumpTableEncoding();
1246}
1247
Chris Lattnerc64daab2010-01-26 05:02:42 +00001248const MCExpr *
1249X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1250 const MachineBasicBlock *MBB,
1251 unsigned uid,MCContext &Ctx) const{
1252 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1253 Subtarget->isPICStyleGOT());
1254 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1255 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001256 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1257 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001258}
1259
Evan Chengcc415862007-11-09 01:32:10 +00001260/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1261/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001262SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001263 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001264 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001265 // This doesn't have DebugLoc associated with it, but is not really the
1266 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001267 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001268 return Table;
1269}
1270
Chris Lattner589c6f62010-01-26 06:28:43 +00001271/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1272/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1273/// MCExpr.
1274const MCExpr *X86TargetLowering::
1275getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1276 MCContext &Ctx) const {
1277 // X86-64 uses RIP relative addressing based on the jump table label.
1278 if (Subtarget->isPICStyleRIPRel())
1279 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1280
1281 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001282 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001283}
1284
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001285// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001286std::pair<const TargetRegisterClass*, uint8_t>
1287X86TargetLowering::findRepresentativeClass(EVT VT) const{
1288 const TargetRegisterClass *RRC = 0;
1289 uint8_t Cost = 1;
1290 switch (VT.getSimpleVT().SimpleTy) {
1291 default:
1292 return TargetLowering::findRepresentativeClass(VT);
1293 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
1294 RRC = (Subtarget->is64Bit()
1295 ? X86::GR64RegisterClass : X86::GR32RegisterClass);
1296 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001297 case MVT::x86mmx:
Evan Chengdee81012010-07-26 21:50:05 +00001298 RRC = X86::VR64RegisterClass;
1299 break;
1300 case MVT::f32: case MVT::f64:
1301 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1302 case MVT::v4f32: case MVT::v2f64:
1303 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1304 case MVT::v4f64:
1305 RRC = X86::VR128RegisterClass;
1306 break;
1307 }
1308 return std::make_pair(RRC, Cost);
1309}
1310
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001311bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1312 unsigned &Offset) const {
1313 if (!Subtarget->isTargetLinux())
1314 return false;
1315
1316 if (Subtarget->is64Bit()) {
1317 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1318 Offset = 0x28;
1319 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1320 AddressSpace = 256;
1321 else
1322 AddressSpace = 257;
1323 } else {
1324 // %gs:0x14 on i386
1325 Offset = 0x14;
1326 AddressSpace = 256;
1327 }
1328 return true;
1329}
1330
1331
Chris Lattner2b02a442007-02-25 08:29:00 +00001332//===----------------------------------------------------------------------===//
1333// Return Value Calling Convention Implementation
1334//===----------------------------------------------------------------------===//
1335
Chris Lattner59ed56b2007-02-28 04:55:35 +00001336#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001337
Michael J. Spencerec38de22010-10-10 22:04:20 +00001338bool
Eric Christopher471e4222011-06-08 23:55:35 +00001339X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
1340 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001341 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001342 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001343 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001344 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001345 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001346 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001347}
1348
Dan Gohman98ca4f22009-08-05 01:29:28 +00001349SDValue
1350X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001351 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001352 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001353 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001354 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001355 MachineFunction &MF = DAG.getMachineFunction();
1356 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001357
Chris Lattner9774c912007-02-27 05:28:59 +00001358 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001359 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001360 RVLocs, *DAG.getContext());
1361 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001362
Evan Chengdcea1632010-02-04 02:40:39 +00001363 // Add the regs to the liveout set for the function.
1364 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1365 for (unsigned i = 0; i != RVLocs.size(); ++i)
1366 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1367 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001368
Dan Gohman475871a2008-07-27 21:46:04 +00001369 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001370
Dan Gohman475871a2008-07-27 21:46:04 +00001371 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001372 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1373 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001374 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1375 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001376
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001377 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001378 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1379 CCValAssign &VA = RVLocs[i];
1380 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001381 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001382 EVT ValVT = ValToCopy.getValueType();
1383
Dale Johannesenc4510512010-09-24 19:05:48 +00001384 // If this is x86-64, and we disabled SSE, we can't return FP values,
1385 // or SSE or MMX vectors.
1386 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1387 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001388 (Subtarget->is64Bit() && !Subtarget->hasXMM())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001389 report_fatal_error("SSE register return with SSE disabled");
1390 }
1391 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1392 // llvm-gcc has never done it right and no one has noticed, so this
1393 // should be OK for now.
1394 if (ValVT == MVT::f64 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001395 (Subtarget->is64Bit() && !Subtarget->hasXMMInt()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001396 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001397
Chris Lattner447ff682008-03-11 03:23:40 +00001398 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1399 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001400 if (VA.getLocReg() == X86::ST0 ||
1401 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001402 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1403 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001404 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001405 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001406 RetOps.push_back(ValToCopy);
1407 // Don't emit a copytoreg.
1408 continue;
1409 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001410
Evan Cheng242b38b2009-02-23 09:03:22 +00001411 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1412 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001413 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001414 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001415 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001416 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001417 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1418 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001419 // If we don't have SSE2 available, convert to v4f32 so the generated
1420 // register is legal.
1421 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001422 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001423 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001424 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001425 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001426
Dale Johannesendd64c412009-02-04 00:33:20 +00001427 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001428 Flag = Chain.getValue(1);
1429 }
Dan Gohman61a92132008-04-21 23:59:07 +00001430
1431 // The x86-64 ABI for returning structs by value requires that we copy
1432 // the sret argument into %rax for the return. We saved the argument into
1433 // a virtual register in the entry block, so now we copy the value out
1434 // and into %rax.
1435 if (Subtarget->is64Bit() &&
1436 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1437 MachineFunction &MF = DAG.getMachineFunction();
1438 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1439 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001440 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001441 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001442 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001443
Dale Johannesendd64c412009-02-04 00:33:20 +00001444 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001445 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001446
1447 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001448 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001449 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001450
Chris Lattner447ff682008-03-11 03:23:40 +00001451 RetOps[0] = Chain; // Update chain.
1452
1453 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001454 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001455 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001456
1457 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001458 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001459}
1460
Evan Cheng3d2125c2010-11-30 23:55:39 +00001461bool X86TargetLowering::isUsedByReturnOnly(SDNode *N) const {
1462 if (N->getNumValues() != 1)
1463 return false;
1464 if (!N->hasNUsesOfValue(1, 0))
1465 return false;
1466
1467 SDNode *Copy = *N->use_begin();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001468 if (Copy->getOpcode() != ISD::CopyToReg &&
1469 Copy->getOpcode() != ISD::FP_EXTEND)
Evan Cheng3d2125c2010-11-30 23:55:39 +00001470 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001471
1472 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001473 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001474 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001475 if (UI->getOpcode() != X86ISD::RET_FLAG)
1476 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001477 HasRet = true;
1478 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001479
Evan Cheng1bf891a2010-12-01 22:59:46 +00001480 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001481}
1482
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001483EVT
1484X86TargetLowering::getTypeForExtArgOrReturn(LLVMContext &Context, EVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001485 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001486 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001487 // TODO: Is this also valid on 32-bit?
1488 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001489 ReturnMVT = MVT::i8;
1490 else
1491 ReturnMVT = MVT::i32;
1492
1493 EVT MinVT = getRegisterType(Context, ReturnMVT);
1494 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001495}
1496
Dan Gohman98ca4f22009-08-05 01:29:28 +00001497/// LowerCallResult - Lower the result values of a call into the
1498/// appropriate copies out of appropriate physical registers.
1499///
1500SDValue
1501X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001502 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001503 const SmallVectorImpl<ISD::InputArg> &Ins,
1504 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001505 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001506
Chris Lattnere32bbf62007-02-28 07:09:55 +00001507 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001508 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001509 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001510 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1511 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001512 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001513
Chris Lattner3085e152007-02-25 08:59:22 +00001514 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001515 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001516 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001517 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001518
Torok Edwin3f142c32009-02-01 18:15:56 +00001519 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001520 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001521 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasXMM())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001522 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001523 }
1524
Evan Cheng79fb3b42009-02-20 20:43:02 +00001525 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001526
1527 // If this is a call to a function that returns an fp value on the floating
1528 // point stack, we must guarantee the the value is popped from the stack, so
1529 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001530 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001531 // instead.
1532 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1533 // If we prefer to use the value in xmm registers, copy it out as f80 and
1534 // use a truncate to move it from fp stack reg to xmm reg.
1535 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001536 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001537 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1538 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001539 Val = Chain.getValue(0);
1540
1541 // Round the f80 to the right size, which also moves it to the appropriate
1542 // xmm register.
1543 if (CopyVT != VA.getValVT())
1544 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1545 // This truncation won't change the value.
1546 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001547 } else {
1548 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1549 CopyVT, InFlag).getValue(1);
1550 Val = Chain.getValue(0);
1551 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001552 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001553 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001554 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001555
Dan Gohman98ca4f22009-08-05 01:29:28 +00001556 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001557}
1558
1559
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001560//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001561// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001562//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001563// StdCall calling convention seems to be standard for many Windows' API
1564// routines and around. It differs from C calling convention just a little:
1565// callee should clean up the stack, not caller. Symbols should be also
1566// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001567// For info on fast calling convention see Fast Calling Convention (tail call)
1568// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001569
Dan Gohman98ca4f22009-08-05 01:29:28 +00001570/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001571/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001572static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1573 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001574 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001575
Dan Gohman98ca4f22009-08-05 01:29:28 +00001576 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001577}
1578
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001579/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001580/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001581static bool
1582ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1583 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001584 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001585
Dan Gohman98ca4f22009-08-05 01:29:28 +00001586 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001587}
1588
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001589/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1590/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001591/// the specific parameter attribute. The copy will be passed as a byval
1592/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001593static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001594CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001595 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1596 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001597 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001598
Dale Johannesendd64c412009-02-04 00:33:20 +00001599 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001600 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001601 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001602}
1603
Chris Lattner29689432010-03-11 00:22:57 +00001604/// IsTailCallConvention - Return true if the calling convention is one that
1605/// supports tail call optimization.
1606static bool IsTailCallConvention(CallingConv::ID CC) {
1607 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1608}
1609
Evan Cheng485fafc2011-03-21 01:19:09 +00001610bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1611 if (!CI->isTailCall())
1612 return false;
1613
1614 CallSite CS(CI);
1615 CallingConv::ID CalleeCC = CS.getCallingConv();
1616 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1617 return false;
1618
1619 return true;
1620}
1621
Evan Cheng0c439eb2010-01-27 00:07:07 +00001622/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1623/// a tailcall target by changing its ABI.
1624static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001625 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001626}
1627
Dan Gohman98ca4f22009-08-05 01:29:28 +00001628SDValue
1629X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001630 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001631 const SmallVectorImpl<ISD::InputArg> &Ins,
1632 DebugLoc dl, SelectionDAG &DAG,
1633 const CCValAssign &VA,
1634 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001635 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001636 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001637 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001638 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001639 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001640 EVT ValVT;
1641
1642 // If value is passed by pointer we have address passed instead of the value
1643 // itself.
1644 if (VA.getLocInfo() == CCValAssign::Indirect)
1645 ValVT = VA.getLocVT();
1646 else
1647 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001648
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001649 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001650 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001651 // In case of tail call optimization mark all arguments mutable. Since they
1652 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001653 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001654 unsigned Bytes = Flags.getByValSize();
1655 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1656 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001657 return DAG.getFrameIndex(FI, getPointerTy());
1658 } else {
1659 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001660 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001661 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1662 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001663 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00001664 false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001665 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001666}
1667
Dan Gohman475871a2008-07-27 21:46:04 +00001668SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001669X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001670 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001671 bool isVarArg,
1672 const SmallVectorImpl<ISD::InputArg> &Ins,
1673 DebugLoc dl,
1674 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001675 SmallVectorImpl<SDValue> &InVals)
1676 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001677 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001678 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001679
Gordon Henriksen86737662008-01-05 16:56:59 +00001680 const Function* Fn = MF.getFunction();
1681 if (Fn->hasExternalLinkage() &&
1682 Subtarget->isTargetCygMing() &&
1683 Fn->getName() == "main")
1684 FuncInfo->setForceFramePointer(true);
1685
Evan Cheng1bc78042006-04-26 01:20:17 +00001686 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001687 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001688 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001689
Chris Lattner29689432010-03-11 00:22:57 +00001690 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1691 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001692
Chris Lattner638402b2007-02-28 07:00:42 +00001693 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001694 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001695 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001696 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001697
1698 // Allocate shadow area for Win64
1699 if (IsWin64) {
1700 CCInfo.AllocateStack(32, 8);
1701 }
1702
Duncan Sands45907662010-10-31 13:21:44 +00001703 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001704
Chris Lattnerf39f7712007-02-28 05:46:49 +00001705 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001706 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001707 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1708 CCValAssign &VA = ArgLocs[i];
1709 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1710 // places.
1711 assert(VA.getValNo() != LastVal &&
1712 "Don't support value assigned to multiple locs yet");
1713 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001714
Chris Lattnerf39f7712007-02-28 05:46:49 +00001715 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001716 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001717 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001718 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001719 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001720 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001721 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001722 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001723 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001724 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001725 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001726 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1727 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001728 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001729 RC = X86::VR128RegisterClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001730 else if (RegVT == MVT::x86mmx)
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001731 RC = X86::VR64RegisterClass;
1732 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001733 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001734
Devang Patel68e6bee2011-02-21 23:21:26 +00001735 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001736 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001737
Chris Lattnerf39f7712007-02-28 05:46:49 +00001738 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1739 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1740 // right size.
1741 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001742 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001743 DAG.getValueType(VA.getValVT()));
1744 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001745 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001746 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001747 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001748 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001749
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001750 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001751 // Handle MMX values passed in XMM regs.
1752 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001753 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1754 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001755 } else
1756 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001757 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001758 } else {
1759 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001760 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001761 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001762
1763 // If value is passed via pointer - do a load.
1764 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001765 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
1766 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001767
Dan Gohman98ca4f22009-08-05 01:29:28 +00001768 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001769 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001770
Dan Gohman61a92132008-04-21 23:59:07 +00001771 // The x86-64 ABI for returning structs by value requires that we copy
1772 // the sret argument into %rax for the return. Save the argument into
1773 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001774 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001775 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1776 unsigned Reg = FuncInfo->getSRetReturnReg();
1777 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001778 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001779 FuncInfo->setSRetReturnReg(Reg);
1780 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001781 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001782 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001783 }
1784
Chris Lattnerf39f7712007-02-28 05:46:49 +00001785 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001786 // Align stack specially for tail calls.
1787 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001788 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001789
Evan Cheng1bc78042006-04-26 01:20:17 +00001790 // If the function takes variable number of arguments, make a frame index for
1791 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001792 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001793 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1794 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001795 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001796 }
1797 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001798 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1799
1800 // FIXME: We should really autogenerate these arrays
1801 static const unsigned GPR64ArgRegsWin64[] = {
1802 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001803 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001804 static const unsigned GPR64ArgRegs64Bit[] = {
1805 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1806 };
1807 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001808 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1809 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1810 };
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001811 const unsigned *GPR64ArgRegs;
1812 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001813
1814 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001815 // The XMM registers which might contain var arg parameters are shadowed
1816 // in their paired GPR. So we only need to save the GPR to their home
1817 // slots.
1818 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001819 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001820 } else {
1821 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1822 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001823
1824 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit, TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001825 }
1826 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1827 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001828
Devang Patel578efa92009-06-05 21:57:13 +00001829 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001830 assert(!(NumXMMRegs && !Subtarget->hasXMM()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001831 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001832 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001833 "SSE register cannot be used when SSE is disabled!");
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001834 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasXMM())
Torok Edwin3f142c32009-02-01 18:15:56 +00001835 // Kernel mode asks for SSE to be disabled, so don't push them
1836 // on the stack.
1837 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001838
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001839 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001840 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001841 // Get to the caller-allocated home save location. Add 8 to account
1842 // for the return address.
1843 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001844 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001845 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001846 // Fixup to set vararg frame on shadow area (4 x i64).
1847 if (NumIntRegs < 4)
1848 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001849 } else {
1850 // For X86-64, if there are vararg parameters that are passed via
1851 // registers, then we must store them to their spots on the stack so they
1852 // may be loaded by deferencing the result of va_next.
1853 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1854 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1855 FuncInfo->setRegSaveFrameIndex(
1856 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00001857 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001858 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001859
Gordon Henriksen86737662008-01-05 16:56:59 +00001860 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001861 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001862 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1863 getPointerTy());
1864 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001865 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001866 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1867 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001868 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001869 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001870 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001871 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001872 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001873 MachinePointerInfo::getFixedStack(
1874 FuncInfo->getRegSaveFrameIndex(), Offset),
1875 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001876 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001877 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001878 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001879
Dan Gohmanface41a2009-08-16 21:24:25 +00001880 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1881 // Now store the XMM (fp + vector) parameter registers.
1882 SmallVector<SDValue, 11> SaveXMMOps;
1883 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001884
Devang Patel68e6bee2011-02-21 23:21:26 +00001885 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001886 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1887 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001888
Dan Gohman1e93df62010-04-17 14:41:14 +00001889 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1890 FuncInfo->getRegSaveFrameIndex()));
1891 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1892 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001893
Dan Gohmanface41a2009-08-16 21:24:25 +00001894 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001895 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001896 X86::VR128RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001897 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1898 SaveXMMOps.push_back(Val);
1899 }
1900 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1901 MVT::Other,
1902 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001903 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001904
1905 if (!MemOps.empty())
1906 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1907 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001908 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001909 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001910
Gordon Henriksen86737662008-01-05 16:56:59 +00001911 // Some CCs need callee pop.
Evan Chengef41ff62011-06-23 17:54:54 +00001912 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg, GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001913 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001914 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001915 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001916 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001917 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001918 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001919 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001920
Gordon Henriksen86737662008-01-05 16:56:59 +00001921 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001922 // RegSaveFrameIndex is X86-64 only.
1923 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001924 if (CallConv == CallingConv::X86_FastCall ||
1925 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001926 // fastcc functions can't have varargs.
1927 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001928 }
Evan Cheng25caf632006-05-23 21:06:34 +00001929
Dan Gohman98ca4f22009-08-05 01:29:28 +00001930 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001931}
1932
Dan Gohman475871a2008-07-27 21:46:04 +00001933SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001934X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1935 SDValue StackPtr, SDValue Arg,
1936 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001937 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001938 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001939 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001940 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001941 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001942 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00001943 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001944
1945 return DAG.getStore(Chain, dl, Arg, PtrOff,
1946 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00001947 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001948}
1949
Bill Wendling64e87322009-01-16 19:25:27 +00001950/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001951/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001952SDValue
1953X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001954 SDValue &OutRetAddr, SDValue Chain,
1955 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00001956 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001957 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001958 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001959 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001960
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001961 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00001962 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
1963 false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001964 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001965}
1966
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001967/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001968/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001969static SDValue
1970EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001971 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001972 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001973 // Store the return address to the appropriate stack slot.
1974 if (!FPDiff) return Chain;
1975 // Calculate the new stack slot for the return address.
1976 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001977 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00001978 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001979 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001980 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001981 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00001982 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00001983 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001984 return Chain;
1985}
1986
Dan Gohman98ca4f22009-08-05 01:29:28 +00001987SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001988X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001989 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001990 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001991 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001992 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001993 const SmallVectorImpl<ISD::InputArg> &Ins,
1994 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001995 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001996 MachineFunction &MF = DAG.getMachineFunction();
1997 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00001998 bool IsWin64 = Subtarget->isTargetWin64();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001999 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00002000 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002001
Evan Cheng5f941932010-02-05 02:21:12 +00002002 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002003 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00002004 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
2005 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00002006 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00002007
2008 // Sibcalls are automatically detected tailcalls which do not require
2009 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00002010 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002011 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002012
2013 if (isTailCall)
2014 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002015 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002016
Chris Lattner29689432010-03-11 00:22:57 +00002017 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
2018 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002019
Chris Lattner638402b2007-02-28 07:00:42 +00002020 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002021 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002022 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002023 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002024
2025 // Allocate shadow area for Win64
2026 if (IsWin64) {
2027 CCInfo.AllocateStack(32, 8);
2028 }
2029
Duncan Sands45907662010-10-31 13:21:44 +00002030 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002031
Chris Lattner423c5f42007-02-28 05:31:48 +00002032 // Get a count of how many bytes are to be pushed on the stack.
2033 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002034 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002035 // This is a sibcall. The memory operands are available in caller's
2036 // own caller's stack.
2037 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00002038 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002039 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002040
Gordon Henriksen86737662008-01-05 16:56:59 +00002041 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002042 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002043 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00002044 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00002045 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2046 FPDiff = NumBytesCallerPushed - NumBytes;
2047
2048 // Set the delta of movement of the returnaddr stackslot.
2049 // But only set if delta is greater than previous delta.
2050 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2051 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2052 }
2053
Evan Chengf22f9b32010-02-06 03:28:46 +00002054 if (!IsSibcall)
2055 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002056
Dan Gohman475871a2008-07-27 21:46:04 +00002057 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002058 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002059 if (isTailCall && FPDiff)
2060 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2061 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002062
Dan Gohman475871a2008-07-27 21:46:04 +00002063 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2064 SmallVector<SDValue, 8> MemOpChains;
2065 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002066
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002067 // Walk the register/memloc assignments, inserting copies/loads. In the case
2068 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002069 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2070 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002071 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002072 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002073 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002074 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002075
Chris Lattner423c5f42007-02-28 05:31:48 +00002076 // Promote the value if needed.
2077 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002078 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002079 case CCValAssign::Full: break;
2080 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002081 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002082 break;
2083 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002084 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002085 break;
2086 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002087 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
2088 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002089 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002090 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2091 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002092 } else
2093 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2094 break;
2095 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002096 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002097 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002098 case CCValAssign::Indirect: {
2099 // Store the argument.
2100 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002101 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002102 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002103 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002104 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002105 Arg = SpillSlot;
2106 break;
2107 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002108 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002109
Chris Lattner423c5f42007-02-28 05:31:48 +00002110 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002111 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2112 if (isVarArg && IsWin64) {
2113 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2114 // shadow reg if callee is a varargs function.
2115 unsigned ShadowReg = 0;
2116 switch (VA.getLocReg()) {
2117 case X86::XMM0: ShadowReg = X86::RCX; break;
2118 case X86::XMM1: ShadowReg = X86::RDX; break;
2119 case X86::XMM2: ShadowReg = X86::R8; break;
2120 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002121 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002122 if (ShadowReg)
2123 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002124 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002125 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002126 assert(VA.isMemLoc());
2127 if (StackPtr.getNode() == 0)
2128 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2129 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2130 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002131 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002132 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002133
Evan Cheng32fe1032006-05-25 00:59:30 +00002134 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002135 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002136 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002137
Evan Cheng347d5f72006-04-28 21:29:37 +00002138 // Build a sequence of copy-to-reg nodes chained together with token chain
2139 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002140 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002141 // Tail call byval lowering might overwrite argument registers so in case of
2142 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002143 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002144 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002145 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002146 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002147 InFlag = Chain.getValue(1);
2148 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002149
Chris Lattner88e1fd52009-07-09 04:24:46 +00002150 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002151 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2152 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002153 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002154 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2155 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00002156 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002157 InFlag);
2158 InFlag = Chain.getValue(1);
2159 } else {
2160 // If we are tail calling and generating PIC/GOT style code load the
2161 // address of the callee into ECX. The value in ecx is used as target of
2162 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2163 // for tail calls on PIC/GOT architectures. Normally we would just put the
2164 // address of GOT into ebx and then call target@PLT. But for tail calls
2165 // ebx would be restored (since ebx is callee saved) before jumping to the
2166 // target@PLT.
2167
2168 // Note: The actual moving to ECX is done further down.
2169 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2170 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2171 !G->getGlobal()->hasProtectedVisibility())
2172 Callee = LowerGlobalAddress(Callee, DAG);
2173 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002174 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002175 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002176 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002177
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002178 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002179 // From AMD64 ABI document:
2180 // For calls that may call functions that use varargs or stdargs
2181 // (prototype-less calls or calls to functions containing ellipsis (...) in
2182 // the declaration) %al is used as hidden argument to specify the number
2183 // of SSE registers used. The contents of %al do not need to match exactly
2184 // the number of registers, but must be an ubound on the number of SSE
2185 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002186
Gordon Henriksen86737662008-01-05 16:56:59 +00002187 // Count the number of XMM registers allocated.
2188 static const unsigned XMMArgRegs[] = {
2189 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2190 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2191 };
2192 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00002193 assert((Subtarget->hasXMM() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002194 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002195
Dale Johannesendd64c412009-02-04 00:33:20 +00002196 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002197 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002198 InFlag = Chain.getValue(1);
2199 }
2200
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002201
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002202 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002203 if (isTailCall) {
2204 // Force all the incoming stack arguments to be loaded from the stack
2205 // before any new outgoing arguments are stored to the stack, because the
2206 // outgoing stack slots may alias the incoming argument stack slots, and
2207 // the alias isn't otherwise explicit. This is slightly more conservative
2208 // than necessary, because it means that each store effectively depends
2209 // on every argument instead of just those arguments it would clobber.
2210 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2211
Dan Gohman475871a2008-07-27 21:46:04 +00002212 SmallVector<SDValue, 8> MemOpChains2;
2213 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002214 int FI = 0;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002215 // Do not flag preceding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002216 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002217 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002218 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2219 CCValAssign &VA = ArgLocs[i];
2220 if (VA.isRegLoc())
2221 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002222 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002223 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002224 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002225 // Create frame index.
2226 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002227 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002228 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002229 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002230
Duncan Sands276dcbd2008-03-21 09:14:45 +00002231 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002232 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002233 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002234 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002235 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002236 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002237 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002238
Dan Gohman98ca4f22009-08-05 01:29:28 +00002239 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2240 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002241 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002242 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002243 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002244 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002245 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002246 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002247 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002248 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002249 }
2250 }
2251
2252 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002253 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002254 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002255
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002256 // Copy arguments to their registers.
2257 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002258 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002259 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002260 InFlag = Chain.getValue(1);
2261 }
Dan Gohman475871a2008-07-27 21:46:04 +00002262 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002263
Gordon Henriksen86737662008-01-05 16:56:59 +00002264 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002265 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002266 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002267 }
2268
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002269 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2270 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2271 // In the 64-bit large code model, we have to make all calls
2272 // through a register, since the call instruction's 32-bit
2273 // pc-relative offset may not be large enough to hold the whole
2274 // address.
2275 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002276 // If the callee is a GlobalAddress node (quite common, every direct call
2277 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2278 // it.
2279
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002280 // We should use extra load for direct calls to dllimported functions in
2281 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002282 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002283 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002284 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002285 bool ExtraLoad = false;
2286 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002287
Chris Lattner48a7d022009-07-09 05:02:21 +00002288 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2289 // external symbols most go through the PLT in PIC mode. If the symbol
2290 // has hidden or protected visibility, or if it is static or local, then
2291 // we don't need to use the PLT - we can directly call it.
2292 if (Subtarget->isTargetELF() &&
2293 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002294 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002295 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002296 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002297 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002298 (!Subtarget->getTargetTriple().isMacOSX() ||
2299 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002300 // PC-relative references to external symbols should go through $stub,
2301 // unless we're building with the leopard linker or later, which
2302 // automatically synthesizes these stubs.
2303 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002304 } else if (Subtarget->isPICStyleRIPRel() &&
2305 isa<Function>(GV) &&
2306 cast<Function>(GV)->hasFnAttr(Attribute::NonLazyBind)) {
2307 // If the function is marked as non-lazy, generate an indirect call
2308 // which loads from the GOT directly. This avoids runtime overhead
2309 // at the cost of eager binding (and one extra byte of encoding).
2310 OpFlags = X86II::MO_GOTPCREL;
2311 WrapperKind = X86ISD::WrapperRIP;
2312 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002313 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002314
Devang Patel0d881da2010-07-06 22:08:15 +00002315 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002316 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002317
2318 // Add a wrapper if needed.
2319 if (WrapperKind != ISD::DELETED_NODE)
2320 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2321 // Add extra indirection if needed.
2322 if (ExtraLoad)
2323 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2324 MachinePointerInfo::getGOT(),
2325 false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002326 }
Bill Wendling056292f2008-09-16 21:48:12 +00002327 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002328 unsigned char OpFlags = 0;
2329
Evan Cheng1bf891a2010-12-01 22:59:46 +00002330 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2331 // external symbols should go through the PLT.
2332 if (Subtarget->isTargetELF() &&
2333 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2334 OpFlags = X86II::MO_PLT;
2335 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002336 (!Subtarget->getTargetTriple().isMacOSX() ||
2337 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002338 // PC-relative references to external symbols should go through $stub,
2339 // unless we're building with the leopard linker or later, which
2340 // automatically synthesizes these stubs.
2341 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002342 }
Eric Christopherfd179292009-08-27 18:07:15 +00002343
Chris Lattner48a7d022009-07-09 05:02:21 +00002344 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2345 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002346 }
2347
Chris Lattnerd96d0722007-02-25 06:40:16 +00002348 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002349 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002350 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002351
Evan Chengf22f9b32010-02-06 03:28:46 +00002352 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002353 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2354 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002355 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002356 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002357
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002358 Ops.push_back(Chain);
2359 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002360
Dan Gohman98ca4f22009-08-05 01:29:28 +00002361 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002362 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002363
Gordon Henriksen86737662008-01-05 16:56:59 +00002364 // Add argument registers to the end of the list so that they are known live
2365 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002366 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2367 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2368 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002369
Evan Cheng586ccac2008-03-18 23:36:35 +00002370 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002371 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002372 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2373
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002374 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002375 if (Is64Bit && isVarArg && !IsWin64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002376 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002377
Gabor Greifba36cb52008-08-28 21:40:38 +00002378 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002379 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002380
Dan Gohman98ca4f22009-08-05 01:29:28 +00002381 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002382 // We used to do:
2383 //// If this is the first return lowered for this function, add the regs
2384 //// to the liveout set for the function.
2385 // This isn't right, although it's probably harmless on x86; liveouts
2386 // should be computed from returns not tail calls. Consider a void
2387 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002388 return DAG.getNode(X86ISD::TC_RETURN, dl,
2389 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002390 }
2391
Dale Johannesenace16102009-02-03 19:33:06 +00002392 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002393 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002394
Chris Lattner2d297092006-05-23 18:50:38 +00002395 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002396 unsigned NumBytesForCalleeToPush;
Evan Chengef41ff62011-06-23 17:54:54 +00002397 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg, GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002398 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002399 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002400 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002401 // pops the hidden struct pointer, so we have to push it back.
2402 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002403 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002404 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002405 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002406
Gordon Henriksenae636f82008-01-03 16:47:34 +00002407 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002408 if (!IsSibcall) {
2409 Chain = DAG.getCALLSEQ_END(Chain,
2410 DAG.getIntPtrConstant(NumBytes, true),
2411 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2412 true),
2413 InFlag);
2414 InFlag = Chain.getValue(1);
2415 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002416
Chris Lattner3085e152007-02-25 08:59:22 +00002417 // Handle result values, copying them out of physregs into vregs that we
2418 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002419 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2420 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002421}
2422
Evan Cheng25ab6902006-09-08 06:48:29 +00002423
2424//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002425// Fast Calling Convention (tail call) implementation
2426//===----------------------------------------------------------------------===//
2427
2428// Like std call, callee cleans arguments, convention except that ECX is
2429// reserved for storing the tail called function address. Only 2 registers are
2430// free for argument passing (inreg). Tail call optimization is performed
2431// provided:
2432// * tailcallopt is enabled
2433// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002434// On X86_64 architecture with GOT-style position independent code only local
2435// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002436// To keep the stack aligned according to platform abi the function
2437// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2438// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002439// If a tail called function callee has more arguments than the caller the
2440// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002441// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002442// original REtADDR, but before the saved framepointer or the spilled registers
2443// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2444// stack layout:
2445// arg1
2446// arg2
2447// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002448// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002449// move area ]
2450// (possible EBP)
2451// ESI
2452// EDI
2453// local1 ..
2454
2455/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2456/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002457unsigned
2458X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2459 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002460 MachineFunction &MF = DAG.getMachineFunction();
2461 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002462 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002463 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002464 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002465 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002466 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002467 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2468 // Number smaller than 12 so just add the difference.
2469 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2470 } else {
2471 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002472 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002473 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002474 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002475 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002476}
2477
Evan Cheng5f941932010-02-05 02:21:12 +00002478/// MatchingStackOffset - Return true if the given stack call argument is
2479/// already available in the same position (relatively) of the caller's
2480/// incoming argument stack.
2481static
2482bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2483 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2484 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002485 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2486 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002487 if (Arg.getOpcode() == ISD::CopyFromReg) {
2488 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002489 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002490 return false;
2491 MachineInstr *Def = MRI->getVRegDef(VR);
2492 if (!Def)
2493 return false;
2494 if (!Flags.isByVal()) {
2495 if (!TII->isLoadFromStackSlot(Def, FI))
2496 return false;
2497 } else {
2498 unsigned Opcode = Def->getOpcode();
2499 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2500 Def->getOperand(1).isFI()) {
2501 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002502 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002503 } else
2504 return false;
2505 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002506 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2507 if (Flags.isByVal())
2508 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002509 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002510 // define @foo(%struct.X* %A) {
2511 // tail call @bar(%struct.X* byval %A)
2512 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002513 return false;
2514 SDValue Ptr = Ld->getBasePtr();
2515 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2516 if (!FINode)
2517 return false;
2518 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002519 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002520 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002521 FI = FINode->getIndex();
2522 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002523 } else
2524 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002525
Evan Cheng4cae1332010-03-05 08:38:04 +00002526 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002527 if (!MFI->isFixedObjectIndex(FI))
2528 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002529 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002530}
2531
Dan Gohman98ca4f22009-08-05 01:29:28 +00002532/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2533/// for tail call optimization. Targets which want to do tail call
2534/// optimization should implement this function.
2535bool
2536X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002537 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002538 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002539 bool isCalleeStructRet,
2540 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002541 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002542 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002543 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002544 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002545 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002546 CalleeCC != CallingConv::C)
2547 return false;
2548
Evan Cheng7096ae42010-01-29 06:45:59 +00002549 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002550 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002551 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002552 CallingConv::ID CallerCC = CallerF->getCallingConv();
2553 bool CCMatch = CallerCC == CalleeCC;
2554
Dan Gohman1797ed52010-02-08 20:27:50 +00002555 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002556 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002557 return true;
2558 return false;
2559 }
2560
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002561 // Look for obvious safe cases to perform tail call optimization that do not
2562 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002563
Evan Cheng2c12cb42010-03-26 16:26:03 +00002564 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2565 // emit a special epilogue.
2566 if (RegInfo->needsStackRealignment(MF))
2567 return false;
2568
Evan Chenga375d472010-03-15 18:54:48 +00002569 // Also avoid sibcall optimization if either caller or callee uses struct
2570 // return semantics.
2571 if (isCalleeStructRet || isCallerStructRet)
2572 return false;
2573
Chad Rosier2416da32011-06-24 21:15:36 +00002574 // An stdcall caller is expected to clean up its arguments; the callee
2575 // isn't going to do that.
2576 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2577 return false;
2578
Chad Rosier871f6642011-05-18 19:59:50 +00002579 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002580 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002581 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002582
2583 // Optimizing for varargs on Win64 is unlikely to be safe without
2584 // additional testing.
2585 if (Subtarget->isTargetWin64())
2586 return false;
2587
Chad Rosier871f6642011-05-18 19:59:50 +00002588 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002589 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2590 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002591
Chad Rosier871f6642011-05-18 19:59:50 +00002592 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2593 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2594 if (!ArgLocs[i].isRegLoc())
2595 return false;
2596 }
2597
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002598 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2599 // Therefore if it's not used by the call it is not safe to optimize this into
2600 // a sibcall.
2601 bool Unused = false;
2602 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2603 if (!Ins[i].Used) {
2604 Unused = true;
2605 break;
2606 }
2607 }
2608 if (Unused) {
2609 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002610 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
2611 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002612 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002613 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002614 CCValAssign &VA = RVLocs[i];
2615 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2616 return false;
2617 }
2618 }
2619
Evan Cheng13617962010-04-30 01:12:32 +00002620 // If the calling conventions do not match, then we'd better make sure the
2621 // results are returned in the same way as what the caller expects.
2622 if (!CCMatch) {
2623 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002624 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
2625 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002626 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2627
2628 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002629 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
2630 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002631 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2632
2633 if (RVLocs1.size() != RVLocs2.size())
2634 return false;
2635 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2636 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2637 return false;
2638 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2639 return false;
2640 if (RVLocs1[i].isRegLoc()) {
2641 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2642 return false;
2643 } else {
2644 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2645 return false;
2646 }
2647 }
2648 }
2649
Evan Chenga6bff982010-01-30 01:22:00 +00002650 // If the callee takes no arguments then go on to check the results of the
2651 // call.
2652 if (!Outs.empty()) {
2653 // Check if stack adjustment is needed. For now, do not do this if any
2654 // argument is passed on the stack.
2655 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002656 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2657 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002658
2659 // Allocate shadow area for Win64
2660 if (Subtarget->isTargetWin64()) {
2661 CCInfo.AllocateStack(32, 8);
2662 }
2663
Duncan Sands45907662010-10-31 13:21:44 +00002664 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002665 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002666 MachineFunction &MF = DAG.getMachineFunction();
2667 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2668 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002669
2670 // Check if the arguments are already laid out in the right way as
2671 // the caller's fixed stack objects.
2672 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002673 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2674 const X86InstrInfo *TII =
2675 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002676 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2677 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002678 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002679 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002680 if (VA.getLocInfo() == CCValAssign::Indirect)
2681 return false;
2682 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002683 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2684 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002685 return false;
2686 }
2687 }
2688 }
Evan Cheng9c044672010-05-29 01:35:22 +00002689
2690 // If the tailcall address may be in a register, then make sure it's
2691 // possible to register allocate for it. In 32-bit, the call address can
2692 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002693 // callee-saved registers are restored. These happen to be the same
2694 // registers used to pass 'inreg' arguments so watch out for those.
2695 if (!Subtarget->is64Bit() &&
2696 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002697 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002698 unsigned NumInRegs = 0;
2699 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2700 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002701 if (!VA.isRegLoc())
2702 continue;
2703 unsigned Reg = VA.getLocReg();
2704 switch (Reg) {
2705 default: break;
2706 case X86::EAX: case X86::EDX: case X86::ECX:
2707 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002708 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002709 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002710 }
2711 }
2712 }
Evan Chenga6bff982010-01-30 01:22:00 +00002713 }
Evan Chengb1712452010-01-27 06:25:16 +00002714
Evan Cheng86809cc2010-02-03 03:28:02 +00002715 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002716}
2717
Dan Gohman3df24e62008-09-03 23:12:08 +00002718FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002719X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2720 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002721}
2722
2723
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002724//===----------------------------------------------------------------------===//
2725// Other Lowering Hooks
2726//===----------------------------------------------------------------------===//
2727
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002728static bool MayFoldLoad(SDValue Op) {
2729 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2730}
2731
2732static bool MayFoldIntoStore(SDValue Op) {
2733 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2734}
2735
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002736static bool isTargetShuffle(unsigned Opcode) {
2737 switch(Opcode) {
2738 default: return false;
2739 case X86ISD::PSHUFD:
2740 case X86ISD::PSHUFHW:
2741 case X86ISD::PSHUFLW:
2742 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002743 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002744 case X86ISD::SHUFPS:
2745 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002746 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002747 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002748 case X86ISD::MOVLPS:
2749 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002750 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002751 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002752 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002753 case X86ISD::MOVSS:
2754 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002755 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002756 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002757 case X86ISD::VUNPCKLPSY:
2758 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002759 case X86ISD::PUNPCKLWD:
2760 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002761 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002762 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002763 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002764 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00002765 case X86ISD::VUNPCKHPSY:
2766 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002767 case X86ISD::PUNPCKHWD:
2768 case X86ISD::PUNPCKHBW:
2769 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002770 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00002771 case X86ISD::VPERMILPS:
2772 case X86ISD::VPERMILPSY:
2773 case X86ISD::VPERMILPD:
2774 case X86ISD::VPERMILPDY:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00002775 case X86ISD::VPERM2F128:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002776 return true;
2777 }
2778 return false;
2779}
2780
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002781static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002782 SDValue V1, SelectionDAG &DAG) {
2783 switch(Opc) {
2784 default: llvm_unreachable("Unknown x86 shuffle node");
2785 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002786 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002787 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002788 return DAG.getNode(Opc, dl, VT, V1);
2789 }
2790
2791 return SDValue();
2792}
2793
2794static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002795 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002796 switch(Opc) {
2797 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002798 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002799 case X86ISD::PSHUFHW:
2800 case X86ISD::PSHUFLW:
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00002801 case X86ISD::VPERMILPS:
2802 case X86ISD::VPERMILPSY:
2803 case X86ISD::VPERMILPD:
2804 case X86ISD::VPERMILPDY:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002805 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2806 }
2807
2808 return SDValue();
2809}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002810
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002811static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2812 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2813 switch(Opc) {
2814 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002815 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002816 case X86ISD::SHUFPD:
2817 case X86ISD::SHUFPS:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00002818 case X86ISD::VPERM2F128:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002819 return DAG.getNode(Opc, dl, VT, V1, V2,
2820 DAG.getConstant(TargetMask, MVT::i8));
2821 }
2822 return SDValue();
2823}
2824
2825static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2826 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2827 switch(Opc) {
2828 default: llvm_unreachable("Unknown x86 shuffle node");
2829 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002830 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002831 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002832 case X86ISD::MOVLPS:
2833 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002834 case X86ISD::MOVSS:
2835 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002836 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002837 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002838 case X86ISD::VUNPCKLPSY:
2839 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002840 case X86ISD::PUNPCKLWD:
2841 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002842 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002843 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002844 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002845 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00002846 case X86ISD::VUNPCKHPSY:
2847 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002848 case X86ISD::PUNPCKHWD:
2849 case X86ISD::PUNPCKHBW:
2850 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002851 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002852 return DAG.getNode(Opc, dl, VT, V1, V2);
2853 }
2854 return SDValue();
2855}
2856
Dan Gohmand858e902010-04-17 15:26:15 +00002857SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002858 MachineFunction &MF = DAG.getMachineFunction();
2859 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2860 int ReturnAddrIndex = FuncInfo->getRAIndex();
2861
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002862 if (ReturnAddrIndex == 0) {
2863 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002864 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002865 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002866 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002867 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002868 }
2869
Evan Cheng25ab6902006-09-08 06:48:29 +00002870 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002871}
2872
2873
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002874bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2875 bool hasSymbolicDisplacement) {
2876 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002877 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002878 return false;
2879
2880 // If we don't have a symbolic displacement - we don't have any extra
2881 // restrictions.
2882 if (!hasSymbolicDisplacement)
2883 return true;
2884
2885 // FIXME: Some tweaks might be needed for medium code model.
2886 if (M != CodeModel::Small && M != CodeModel::Kernel)
2887 return false;
2888
2889 // For small code model we assume that latest object is 16MB before end of 31
2890 // bits boundary. We may also accept pretty large negative constants knowing
2891 // that all objects are in the positive half of address space.
2892 if (M == CodeModel::Small && Offset < 16*1024*1024)
2893 return true;
2894
2895 // For kernel code model we know that all object resist in the negative half
2896 // of 32bits address space. We may not accept negative offsets, since they may
2897 // be just off and we may accept pretty large positive ones.
2898 if (M == CodeModel::Kernel && Offset > 0)
2899 return true;
2900
2901 return false;
2902}
2903
Evan Chengef41ff62011-06-23 17:54:54 +00002904/// isCalleePop - Determines whether the callee is required to pop its
2905/// own arguments. Callee pop is necessary to support tail calls.
2906bool X86::isCalleePop(CallingConv::ID CallingConv,
2907 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
2908 if (IsVarArg)
2909 return false;
2910
2911 switch (CallingConv) {
2912 default:
2913 return false;
2914 case CallingConv::X86_StdCall:
2915 return !is64Bit;
2916 case CallingConv::X86_FastCall:
2917 return !is64Bit;
2918 case CallingConv::X86_ThisCall:
2919 return !is64Bit;
2920 case CallingConv::Fast:
2921 return TailCallOpt;
2922 case CallingConv::GHC:
2923 return TailCallOpt;
2924 }
2925}
2926
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002927/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2928/// specific condition code, returning the condition code and the LHS/RHS of the
2929/// comparison to make.
2930static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2931 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002932 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002933 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2934 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2935 // X > -1 -> X == 0, jump !sign.
2936 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002937 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002938 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2939 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002940 return X86::COND_S;
Andrew Trickf6c39412011-03-23 23:11:02 +00002941 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002942 // X < 1 -> X <= 0
2943 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002944 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002945 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002946 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002947
Evan Chengd9558e02006-01-06 00:43:03 +00002948 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002949 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002950 case ISD::SETEQ: return X86::COND_E;
2951 case ISD::SETGT: return X86::COND_G;
2952 case ISD::SETGE: return X86::COND_GE;
2953 case ISD::SETLT: return X86::COND_L;
2954 case ISD::SETLE: return X86::COND_LE;
2955 case ISD::SETNE: return X86::COND_NE;
2956 case ISD::SETULT: return X86::COND_B;
2957 case ISD::SETUGT: return X86::COND_A;
2958 case ISD::SETULE: return X86::COND_BE;
2959 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002960 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002961 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002962
Chris Lattner4c78e022008-12-23 23:42:27 +00002963 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002964
Chris Lattner4c78e022008-12-23 23:42:27 +00002965 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00002966 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
2967 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00002968 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2969 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002970 }
2971
Chris Lattner4c78e022008-12-23 23:42:27 +00002972 switch (SetCCOpcode) {
2973 default: break;
2974 case ISD::SETOLT:
2975 case ISD::SETOLE:
2976 case ISD::SETUGT:
2977 case ISD::SETUGE:
2978 std::swap(LHS, RHS);
2979 break;
2980 }
2981
2982 // On a floating point condition, the flags are set as follows:
2983 // ZF PF CF op
2984 // 0 | 0 | 0 | X > Y
2985 // 0 | 0 | 1 | X < Y
2986 // 1 | 0 | 0 | X == Y
2987 // 1 | 1 | 1 | unordered
2988 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002989 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002990 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002991 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002992 case ISD::SETOLT: // flipped
2993 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002994 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002995 case ISD::SETOLE: // flipped
2996 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002997 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002998 case ISD::SETUGT: // flipped
2999 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003000 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00003001 case ISD::SETUGE: // flipped
3002 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003003 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003004 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003005 case ISD::SETNE: return X86::COND_NE;
3006 case ISD::SETUO: return X86::COND_P;
3007 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003008 case ISD::SETOEQ:
3009 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003010 }
Evan Chengd9558e02006-01-06 00:43:03 +00003011}
3012
Evan Cheng4a460802006-01-11 00:33:36 +00003013/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3014/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003015/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003016static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003017 switch (X86CC) {
3018 default:
3019 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003020 case X86::COND_B:
3021 case X86::COND_BE:
3022 case X86::COND_E:
3023 case X86::COND_P:
3024 case X86::COND_A:
3025 case X86::COND_AE:
3026 case X86::COND_NE:
3027 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003028 return true;
3029 }
3030}
3031
Evan Chengeb2f9692009-10-27 19:56:55 +00003032/// isFPImmLegal - Returns true if the target can instruction select the
3033/// specified FP immediate natively. If false, the legalizer will
3034/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003035bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003036 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3037 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3038 return true;
3039 }
3040 return false;
3041}
3042
Nate Begeman9008ca62009-04-27 18:41:29 +00003043/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3044/// the specified range (L, H].
3045static bool isUndefOrInRange(int Val, int Low, int Hi) {
3046 return (Val < 0) || (Val >= Low && Val < Hi);
3047}
3048
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00003049/// isUndefOrInRange - Return true if every element in Mask, begining
3050/// from position Pos and ending in Pos+Size, falls within the specified
3051/// range (L, L+Pos]. or is undef.
3052static bool isUndefOrInRange(const SmallVectorImpl<int> &Mask,
3053 int Pos, int Size, int Low, int Hi) {
3054 for (int i = Pos, e = Pos+Size; i != e; ++i)
3055 if (!isUndefOrInRange(Mask[i], Low, Hi))
3056 return false;
3057 return true;
3058}
3059
Nate Begeman9008ca62009-04-27 18:41:29 +00003060/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3061/// specified value.
3062static bool isUndefOrEqual(int Val, int CmpVal) {
3063 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003064 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003065 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00003066}
3067
Bruno Cardoso Lopes4002d7e2011-08-12 21:54:42 +00003068/// isSequentialOrUndefInRange - Return true if every element in Mask, begining
3069/// from position Pos and ending in Pos+Size, falls within the specified
3070/// sequential range (L, L+Pos]. or is undef.
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003071static bool isSequentialOrUndefInRange(const SmallVectorImpl<int> &Mask,
3072 int Pos, int Size, int Low) {
3073 for (int i = Pos, e = Pos+Size; i != e; ++i, ++Low)
3074 if (!isUndefOrEqual(Mask[i], Low))
3075 return false;
3076 return true;
3077}
3078
Nate Begeman9008ca62009-04-27 18:41:29 +00003079/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3080/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3081/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00003082static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003083 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003084 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003085 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003086 return (Mask[0] < 2 && Mask[1] < 2);
3087 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003088}
3089
Nate Begeman9008ca62009-04-27 18:41:29 +00003090bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003091 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003092 N->getMask(M);
3093 return ::isPSHUFDMask(M, N->getValueType(0));
3094}
Evan Cheng0188ecb2006-03-22 18:59:22 +00003095
Nate Begeman9008ca62009-04-27 18:41:29 +00003096/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3097/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00003098static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003099 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00003100 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003101
Nate Begeman9008ca62009-04-27 18:41:29 +00003102 // Lower quadword copied in order or undef.
3103 for (int i = 0; i != 4; ++i)
3104 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00003105 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003106
Evan Cheng506d3df2006-03-29 23:07:14 +00003107 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003108 for (int i = 4; i != 8; ++i)
3109 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00003110 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003111
Evan Cheng506d3df2006-03-29 23:07:14 +00003112 return true;
3113}
3114
Nate Begeman9008ca62009-04-27 18:41:29 +00003115bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003116 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003117 N->getMask(M);
3118 return ::isPSHUFHWMask(M, N->getValueType(0));
3119}
Evan Cheng506d3df2006-03-29 23:07:14 +00003120
Nate Begeman9008ca62009-04-27 18:41:29 +00003121/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3122/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00003123static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003124 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00003125 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003126
Rafael Espindola15684b22009-04-24 12:40:33 +00003127 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00003128 for (int i = 4; i != 8; ++i)
3129 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00003130 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003131
Rafael Espindola15684b22009-04-24 12:40:33 +00003132 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003133 for (int i = 0; i != 4; ++i)
3134 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00003135 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003136
Rafael Espindola15684b22009-04-24 12:40:33 +00003137 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003138}
3139
Nate Begeman9008ca62009-04-27 18:41:29 +00003140bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003141 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003142 N->getMask(M);
3143 return ::isPSHUFLWMask(M, N->getValueType(0));
3144}
3145
Nate Begemana09008b2009-10-19 02:17:23 +00003146/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3147/// is suitable for input to PALIGNR.
3148static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
3149 bool hasSSSE3) {
3150 int i, e = VT.getVectorNumElements();
Bruno Cardoso Lopes9065d4b2011-07-29 01:30:59 +00003151 if (VT.getSizeInBits() != 128 && VT.getSizeInBits() != 64)
3152 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003153
Nate Begemana09008b2009-10-19 02:17:23 +00003154 // Do not handle v2i64 / v2f64 shuffles with palignr.
3155 if (e < 4 || !hasSSSE3)
3156 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003157
Nate Begemana09008b2009-10-19 02:17:23 +00003158 for (i = 0; i != e; ++i)
3159 if (Mask[i] >= 0)
3160 break;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003161
Nate Begemana09008b2009-10-19 02:17:23 +00003162 // All undef, not a palignr.
3163 if (i == e)
3164 return false;
3165
Eli Friedman63f8dde2011-07-25 21:36:45 +00003166 // Make sure we're shifting in the right direction.
3167 if (Mask[i] <= i)
3168 return false;
Nate Begemana09008b2009-10-19 02:17:23 +00003169
3170 int s = Mask[i] - i;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003171
Nate Begemana09008b2009-10-19 02:17:23 +00003172 // Check the rest of the elements to see if they are consecutive.
3173 for (++i; i != e; ++i) {
3174 int m = Mask[i];
Eli Friedman63f8dde2011-07-25 21:36:45 +00003175 if (m >= 0 && m != s+i)
Nate Begemana09008b2009-10-19 02:17:23 +00003176 return false;
3177 }
3178 return true;
3179}
3180
Evan Cheng14aed5e2006-03-24 01:18:28 +00003181/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopesaf002d82011-08-24 23:17:55 +00003182/// specifies a shuffle of elements that is suitable for input to 128-bit
3183/// SHUFPS and SHUFPD.
Owen Andersone50ed302009-08-10 22:56:29 +00003184static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003185 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesaf002d82011-08-24 23:17:55 +00003186
3187 if (VT.getSizeInBits() != 128)
3188 return false;
3189
Nate Begeman9008ca62009-04-27 18:41:29 +00003190 if (NumElems != 2 && NumElems != 4)
3191 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003192
Nate Begeman9008ca62009-04-27 18:41:29 +00003193 int Half = NumElems / 2;
3194 for (int i = 0; i < Half; ++i)
3195 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003196 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003197 for (int i = Half; i < NumElems; ++i)
3198 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003199 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003200
Evan Cheng14aed5e2006-03-24 01:18:28 +00003201 return true;
3202}
3203
Nate Begeman9008ca62009-04-27 18:41:29 +00003204bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
3205 SmallVector<int, 8> M;
3206 N->getMask(M);
3207 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003208}
3209
Evan Cheng213d2cf2007-05-17 18:45:50 +00003210/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00003211/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
3212/// half elements to come from vector 1 (which would equal the dest.) and
3213/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00003214static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003215 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003216
3217 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00003218 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003219
Nate Begeman9008ca62009-04-27 18:41:29 +00003220 int Half = NumElems / 2;
3221 for (int i = 0; i < Half; ++i)
3222 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003223 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003224 for (int i = Half; i < NumElems; ++i)
3225 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003226 return false;
3227 return true;
3228}
3229
Nate Begeman9008ca62009-04-27 18:41:29 +00003230static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
3231 SmallVector<int, 8> M;
3232 N->getMask(M);
3233 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003234}
3235
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003236/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3237/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00003238bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003239 EVT VT = N->getValueType(0);
3240 unsigned NumElems = VT.getVectorNumElements();
3241
3242 if (VT.getSizeInBits() != 128)
3243 return false;
3244
3245 if (NumElems != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003246 return false;
3247
Evan Cheng2064a2b2006-03-28 06:50:32 +00003248 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00003249 return isUndefOrEqual(N->getMaskElt(0), 6) &&
3250 isUndefOrEqual(N->getMaskElt(1), 7) &&
3251 isUndefOrEqual(N->getMaskElt(2), 2) &&
3252 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003253}
3254
Nate Begeman0b10b912009-11-07 23:17:15 +00003255/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3256/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3257/// <2, 3, 2, 3>
3258bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003259 EVT VT = N->getValueType(0);
3260 unsigned NumElems = VT.getVectorNumElements();
3261
3262 if (VT.getSizeInBits() != 128)
3263 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003264
Nate Begeman0b10b912009-11-07 23:17:15 +00003265 if (NumElems != 4)
3266 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003267
Nate Begeman0b10b912009-11-07 23:17:15 +00003268 return isUndefOrEqual(N->getMaskElt(0), 2) &&
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003269 isUndefOrEqual(N->getMaskElt(1), 3) &&
3270 isUndefOrEqual(N->getMaskElt(2), 2) &&
3271 isUndefOrEqual(N->getMaskElt(3), 3);
Nate Begeman0b10b912009-11-07 23:17:15 +00003272}
3273
Evan Cheng5ced1d82006-04-06 23:23:56 +00003274/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3275/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003276bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3277 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003278
Evan Cheng5ced1d82006-04-06 23:23:56 +00003279 if (NumElems != 2 && NumElems != 4)
3280 return false;
3281
Evan Chengc5cdff22006-04-07 21:53:05 +00003282 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003283 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003284 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003285
Evan Chengc5cdff22006-04-07 21:53:05 +00003286 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003287 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003288 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003289
3290 return true;
3291}
3292
Nate Begeman0b10b912009-11-07 23:17:15 +00003293/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3294/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3295bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003296 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003297
David Greenea20244d2011-03-02 17:23:43 +00003298 if ((NumElems != 2 && NumElems != 4)
3299 || N->getValueType(0).getSizeInBits() > 128)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003300 return false;
3301
Evan Chengc5cdff22006-04-07 21:53:05 +00003302 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003303 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003304 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003305
Nate Begeman9008ca62009-04-27 18:41:29 +00003306 for (unsigned i = 0; i < NumElems/2; ++i)
3307 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003308 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003309
3310 return true;
3311}
3312
Evan Cheng0038e592006-03-28 00:39:58 +00003313/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3314/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003315static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003316 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003317 int NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003318
3319 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3320 "Unsupported vector type for unpckh");
3321
3322 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8)
Evan Cheng0038e592006-03-28 00:39:58 +00003323 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003324
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003325 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3326 // independently on 128-bit lanes.
3327 unsigned NumLanes = VT.getSizeInBits()/128;
3328 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003329
3330 unsigned Start = 0;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003331 unsigned End = NumLaneElts;
3332 for (unsigned s = 0; s < NumLanes; ++s) {
3333 for (unsigned i = Start, j = s * NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003334 i != End;
3335 i += 2, ++j) {
3336 int BitI = Mask[i];
3337 int BitI1 = Mask[i+1];
3338 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003339 return false;
David Greenea20244d2011-03-02 17:23:43 +00003340 if (V2IsSplat) {
3341 if (!isUndefOrEqual(BitI1, NumElts))
3342 return false;
3343 } else {
3344 if (!isUndefOrEqual(BitI1, j + NumElts))
3345 return false;
3346 }
Evan Cheng39623da2006-04-20 08:58:49 +00003347 }
David Greenea20244d2011-03-02 17:23:43 +00003348 // Process the next 128 bits.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003349 Start += NumLaneElts;
3350 End += NumLaneElts;
Evan Cheng0038e592006-03-28 00:39:58 +00003351 }
David Greenea20244d2011-03-02 17:23:43 +00003352
Evan Cheng0038e592006-03-28 00:39:58 +00003353 return true;
3354}
3355
Nate Begeman9008ca62009-04-27 18:41:29 +00003356bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3357 SmallVector<int, 8> M;
3358 N->getMask(M);
3359 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003360}
3361
Evan Cheng4fcb9222006-03-28 02:43:26 +00003362/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3363/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003364static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003365 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003366 int NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003367
3368 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3369 "Unsupported vector type for unpckh");
3370
3371 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8)
Evan Cheng4fcb9222006-03-28 02:43:26 +00003372 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003373
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003374 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3375 // independently on 128-bit lanes.
3376 unsigned NumLanes = VT.getSizeInBits()/128;
3377 unsigned NumLaneElts = NumElts/NumLanes;
3378
3379 unsigned Start = 0;
3380 unsigned End = NumLaneElts;
3381 for (unsigned l = 0; l != NumLanes; ++l) {
3382 for (unsigned i = Start, j = (l*NumLaneElts)+NumLaneElts/2;
3383 i != End; i += 2, ++j) {
3384 int BitI = Mask[i];
3385 int BitI1 = Mask[i+1];
3386 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003387 return false;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003388 if (V2IsSplat) {
3389 if (isUndefOrEqual(BitI1, NumElts))
3390 return false;
3391 } else {
3392 if (!isUndefOrEqual(BitI1, j+NumElts))
3393 return false;
3394 }
Evan Cheng39623da2006-04-20 08:58:49 +00003395 }
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003396 // Process the next 128 bits.
3397 Start += NumLaneElts;
3398 End += NumLaneElts;
Evan Cheng4fcb9222006-03-28 02:43:26 +00003399 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003400 return true;
3401}
3402
Nate Begeman9008ca62009-04-27 18:41:29 +00003403bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3404 SmallVector<int, 8> M;
3405 N->getMask(M);
3406 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003407}
3408
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003409/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3410/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3411/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003412static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003413 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003414 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003415 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003416
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003417 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3418 // independently on 128-bit lanes.
3419 unsigned NumLanes = VT.getSizeInBits() / 128;
3420 unsigned NumLaneElts = NumElems / NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003421
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003422 for (unsigned s = 0; s < NumLanes; ++s) {
3423 for (unsigned i = s * NumLaneElts, j = s * NumLaneElts;
3424 i != NumLaneElts * (s + 1);
David Greenea20244d2011-03-02 17:23:43 +00003425 i += 2, ++j) {
3426 int BitI = Mask[i];
3427 int BitI1 = Mask[i+1];
3428
3429 if (!isUndefOrEqual(BitI, j))
3430 return false;
3431 if (!isUndefOrEqual(BitI1, j))
3432 return false;
3433 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003434 }
David Greenea20244d2011-03-02 17:23:43 +00003435
Rafael Espindola15684b22009-04-24 12:40:33 +00003436 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003437}
3438
Nate Begeman9008ca62009-04-27 18:41:29 +00003439bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3440 SmallVector<int, 8> M;
3441 N->getMask(M);
3442 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3443}
3444
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003445/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3446/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3447/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003448static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003449 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003450 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3451 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003452
Nate Begeman9008ca62009-04-27 18:41:29 +00003453 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3454 int BitI = Mask[i];
3455 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003456 if (!isUndefOrEqual(BitI, j))
3457 return false;
3458 if (!isUndefOrEqual(BitI1, j))
3459 return false;
3460 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003461 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003462}
3463
Nate Begeman9008ca62009-04-27 18:41:29 +00003464bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3465 SmallVector<int, 8> M;
3466 N->getMask(M);
3467 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3468}
3469
Evan Cheng017dcc62006-04-21 01:05:10 +00003470/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3471/// specifies a shuffle of elements that is suitable for input to MOVSS,
3472/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003473static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003474 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003475 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003476
3477 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003478
Nate Begeman9008ca62009-04-27 18:41:29 +00003479 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003480 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003481
Nate Begeman9008ca62009-04-27 18:41:29 +00003482 for (int i = 1; i < NumElts; ++i)
3483 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003484 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003485
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003486 return true;
3487}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003488
Nate Begeman9008ca62009-04-27 18:41:29 +00003489bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3490 SmallVector<int, 8> M;
3491 N->getMask(M);
3492 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003493}
3494
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003495/// isVPERM2F128Mask - Match 256-bit shuffles where the elements are considered
3496/// as permutations between 128-bit chunks or halves. As an example: this
3497/// shuffle bellow:
3498/// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
3499/// The first half comes from the second half of V1 and the second half from the
3500/// the second half of V2.
3501static bool isVPERM2F128Mask(const SmallVectorImpl<int> &Mask, EVT VT,
3502 const X86Subtarget *Subtarget) {
3503 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256)
3504 return false;
3505
3506 // The shuffle result is divided into half A and half B. In total the two
3507 // sources have 4 halves, namely: C, D, E, F. The final values of A and
3508 // B must come from C, D, E or F.
3509 int HalfSize = VT.getVectorNumElements()/2;
3510 bool MatchA = false, MatchB = false;
3511
3512 // Check if A comes from one of C, D, E, F.
3513 for (int Half = 0; Half < 4; ++Half) {
3514 if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
3515 MatchA = true;
3516 break;
3517 }
3518 }
3519
3520 // Check if B comes from one of C, D, E, F.
3521 for (int Half = 0; Half < 4; ++Half) {
3522 if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) {
3523 MatchB = true;
3524 break;
3525 }
3526 }
3527
3528 return MatchA && MatchB;
3529}
3530
3531/// getShuffleVPERM2F128Immediate - Return the appropriate immediate to shuffle
3532/// the specified VECTOR_MASK mask with VPERM2F128 instructions.
3533static unsigned getShuffleVPERM2F128Immediate(SDNode *N) {
3534 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3535 EVT VT = SVOp->getValueType(0);
3536
3537 int HalfSize = VT.getVectorNumElements()/2;
3538
3539 int FstHalf = 0, SndHalf = 0;
3540 for (int i = 0; i < HalfSize; ++i) {
3541 if (SVOp->getMaskElt(i) > 0) {
3542 FstHalf = SVOp->getMaskElt(i)/HalfSize;
3543 break;
3544 }
3545 }
3546 for (int i = HalfSize; i < HalfSize*2; ++i) {
3547 if (SVOp->getMaskElt(i) > 0) {
3548 SndHalf = SVOp->getMaskElt(i)/HalfSize;
3549 break;
3550 }
3551 }
3552
3553 return (FstHalf | (SndHalf << 4));
3554}
3555
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003556/// isVPERMILPDMask - Return true if the specified VECTOR_SHUFFLE operand
3557/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
3558/// Note that VPERMIL mask matching is different depending whether theunderlying
3559/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3560/// to the same elements of the low, but to the higher half of the source.
3561/// In VPERMILPD the two lanes could be shuffled independently of each other
3562/// with the same restriction that lanes can't be crossed.
3563static bool isVPERMILPDMask(const SmallVectorImpl<int> &Mask, EVT VT,
3564 const X86Subtarget *Subtarget) {
3565 int NumElts = VT.getVectorNumElements();
3566 int NumLanes = VT.getSizeInBits()/128;
3567
3568 if (!Subtarget->hasAVX())
3569 return false;
3570
3571 // Match any permutation of 128-bit vector with 64-bit types
3572 if (NumLanes == 1 && NumElts != 2)
3573 return false;
3574
3575 // Only match 256-bit with 32 types
3576 if (VT.getSizeInBits() == 256 && NumElts != 4)
3577 return false;
3578
3579 // The mask on the high lane is independent of the low. Both can match
3580 // any element in inside its own lane, but can't cross.
3581 int LaneSize = NumElts/NumLanes;
3582 for (int l = 0; l < NumLanes; ++l)
3583 for (int i = l*LaneSize; i < LaneSize*(l+1); ++i) {
3584 int LaneStart = l*LaneSize;
3585 if (!isUndefOrInRange(Mask[i], LaneStart, LaneStart+LaneSize))
3586 return false;
3587 }
3588
3589 return true;
3590}
3591
3592/// isVPERMILPSMask - Return true if the specified VECTOR_SHUFFLE operand
3593/// specifies a shuffle of elements that is suitable for input to VPERMILPS*.
3594/// Note that VPERMIL mask matching is different depending whether theunderlying
3595/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3596/// to the same elements of the low, but to the higher half of the source.
3597/// In VPERMILPD the two lanes could be shuffled independently of each other
3598/// with the same restriction that lanes can't be crossed.
3599static bool isVPERMILPSMask(const SmallVectorImpl<int> &Mask, EVT VT,
3600 const X86Subtarget *Subtarget) {
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003601 unsigned NumElts = VT.getVectorNumElements();
3602 unsigned NumLanes = VT.getSizeInBits()/128;
3603
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003604 if (!Subtarget->hasAVX())
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003605 return false;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003606
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003607 // Match any permutation of 128-bit vector with 32-bit types
3608 if (NumLanes == 1 && NumElts != 4)
3609 return false;
3610
3611 // Only match 256-bit with 32 types
3612 if (VT.getSizeInBits() == 256 && NumElts != 8)
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003613 return false;
3614
3615 // The mask on the high lane should be the same as the low. Actually,
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003616 // they can differ if any of the corresponding index in a lane is undef
3617 // and the other stays in range.
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003618 int LaneSize = NumElts/NumLanes;
3619 for (int i = 0; i < LaneSize; ++i) {
3620 int HighElt = i+LaneSize;
Bruno Cardoso Lopes155a92a2011-08-10 01:54:17 +00003621 bool HighValid = isUndefOrInRange(Mask[HighElt], LaneSize, NumElts);
3622 bool LowValid = isUndefOrInRange(Mask[i], 0, LaneSize);
3623
3624 if (!HighValid || !LowValid)
3625 return false;
3626 if (Mask[i] < 0 || Mask[HighElt] < 0)
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003627 continue;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003628 if (Mask[HighElt]-Mask[i] != LaneSize)
3629 return false;
3630 }
3631
3632 return true;
3633}
3634
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003635/// getShuffleVPERMILPSImmediate - Return the appropriate immediate to shuffle
3636/// the specified VECTOR_MASK mask with VPERMILPS* instructions.
3637static unsigned getShuffleVPERMILPSImmediate(SDNode *N) {
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003638 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3639 EVT VT = SVOp->getValueType(0);
3640
3641 int NumElts = VT.getVectorNumElements();
3642 int NumLanes = VT.getSizeInBits()/128;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003643 int LaneSize = NumElts/NumLanes;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003644
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003645 // Although the mask is equal for both lanes do it twice to get the cases
3646 // where a mask will match because the same mask element is undef on the
3647 // first half but valid on the second. This would get pathological cases
3648 // such as: shuffle <u, 0, 1, 2, 4, 4, 5, 6>, which is completely valid.
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003649 unsigned Mask = 0;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003650 for (int l = 0; l < NumLanes; ++l) {
3651 for (int i = 0; i < LaneSize; ++i) {
3652 int MaskElt = SVOp->getMaskElt(i+(l*LaneSize));
3653 if (MaskElt < 0)
3654 continue;
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003655 if (MaskElt >= LaneSize)
3656 MaskElt -= LaneSize;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003657 Mask |= MaskElt << (i*2);
3658 }
Bruno Cardoso Lopes377baa52011-07-29 01:31:04 +00003659 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003660
3661 return Mask;
3662}
3663
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003664/// getShuffleVPERMILPDImmediate - Return the appropriate immediate to shuffle
3665/// the specified VECTOR_MASK mask with VPERMILPD* instructions.
3666static unsigned getShuffleVPERMILPDImmediate(SDNode *N) {
3667 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3668 EVT VT = SVOp->getValueType(0);
3669
3670 int NumElts = VT.getVectorNumElements();
3671 int NumLanes = VT.getSizeInBits()/128;
3672
3673 unsigned Mask = 0;
3674 int LaneSize = NumElts/NumLanes;
3675 for (int l = 0; l < NumLanes; ++l)
Bruno Cardoso Lopes377baa52011-07-29 01:31:04 +00003676 for (int i = l*LaneSize; i < LaneSize*(l+1); ++i) {
3677 int MaskElt = SVOp->getMaskElt(i);
3678 if (MaskElt < 0)
3679 continue;
3680 Mask |= (MaskElt-l*LaneSize) << i;
3681 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003682
3683 return Mask;
3684}
3685
Evan Cheng017dcc62006-04-21 01:05:10 +00003686/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3687/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003688/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003689static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003690 bool V2IsSplat = false, bool V2IsUndef = false) {
3691 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003692 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003693 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003694
Nate Begeman9008ca62009-04-27 18:41:29 +00003695 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003696 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003697
Nate Begeman9008ca62009-04-27 18:41:29 +00003698 for (int i = 1; i < NumOps; ++i)
3699 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3700 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3701 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003702 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003703
Evan Cheng39623da2006-04-20 08:58:49 +00003704 return true;
3705}
3706
Nate Begeman9008ca62009-04-27 18:41:29 +00003707static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003708 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003709 SmallVector<int, 8> M;
3710 N->getMask(M);
3711 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003712}
3713
Evan Chengd9539472006-04-14 21:59:03 +00003714/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3715/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003716/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
3717bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N,
3718 const X86Subtarget *Subtarget) {
3719 if (!Subtarget->hasSSE3() && !Subtarget->hasAVX())
Evan Chengd9539472006-04-14 21:59:03 +00003720 return false;
3721
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003722 // The second vector must be undef
3723 if (N->getOperand(1).getOpcode() != ISD::UNDEF)
3724 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003725
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003726 EVT VT = N->getValueType(0);
3727 unsigned NumElems = VT.getVectorNumElements();
3728
3729 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3730 (VT.getSizeInBits() == 256 && NumElems != 8))
3731 return false;
3732
3733 // "i+1" is the value the indexed mask element must have
3734 for (unsigned i = 0; i < NumElems; i += 2)
3735 if (!isUndefOrEqual(N->getMaskElt(i), i+1) ||
3736 !isUndefOrEqual(N->getMaskElt(i+1), i+1))
Nate Begeman9008ca62009-04-27 18:41:29 +00003737 return false;
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003738
3739 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003740}
3741
3742/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3743/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003744/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
3745bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N,
3746 const X86Subtarget *Subtarget) {
3747 if (!Subtarget->hasSSE3() && !Subtarget->hasAVX())
Evan Chengd9539472006-04-14 21:59:03 +00003748 return false;
3749
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003750 // The second vector must be undef
3751 if (N->getOperand(1).getOpcode() != ISD::UNDEF)
3752 return false;
3753
3754 EVT VT = N->getValueType(0);
3755 unsigned NumElems = VT.getVectorNumElements();
3756
3757 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3758 (VT.getSizeInBits() == 256 && NumElems != 8))
3759 return false;
3760
3761 // "i" is the value the indexed mask element must have
3762 for (unsigned i = 0; i < NumElems; i += 2)
3763 if (!isUndefOrEqual(N->getMaskElt(i), i) ||
3764 !isUndefOrEqual(N->getMaskElt(i+1), i))
Nate Begeman9008ca62009-04-27 18:41:29 +00003765 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003766
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003767 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003768}
3769
Evan Cheng0b457f02008-09-25 20:50:48 +00003770/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3771/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003772bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
3773 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00003774
Nate Begeman9008ca62009-04-27 18:41:29 +00003775 for (int i = 0; i < e; ++i)
3776 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003777 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003778 for (int i = 0; i < e; ++i)
3779 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003780 return false;
3781 return true;
3782}
3783
David Greenec38a03e2011-02-03 15:50:00 +00003784/// isVEXTRACTF128Index - Return true if the specified
3785/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3786/// suitable for input to VEXTRACTF128.
3787bool X86::isVEXTRACTF128Index(SDNode *N) {
3788 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3789 return false;
3790
3791 // The index should be aligned on a 128-bit boundary.
3792 uint64_t Index =
3793 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3794
3795 unsigned VL = N->getValueType(0).getVectorNumElements();
3796 unsigned VBits = N->getValueType(0).getSizeInBits();
3797 unsigned ElSize = VBits / VL;
3798 bool Result = (Index * ElSize) % 128 == 0;
3799
3800 return Result;
3801}
3802
David Greeneccacdc12011-02-04 16:08:29 +00003803/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
3804/// operand specifies a subvector insert that is suitable for input to
3805/// VINSERTF128.
3806bool X86::isVINSERTF128Index(SDNode *N) {
3807 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3808 return false;
3809
3810 // The index should be aligned on a 128-bit boundary.
3811 uint64_t Index =
3812 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
3813
3814 unsigned VL = N->getValueType(0).getVectorNumElements();
3815 unsigned VBits = N->getValueType(0).getSizeInBits();
3816 unsigned ElSize = VBits / VL;
3817 bool Result = (Index * ElSize) % 128 == 0;
3818
3819 return Result;
3820}
3821
Evan Cheng63d33002006-03-22 08:01:21 +00003822/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003823/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00003824unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003825 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3826 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
3827
Evan Chengb9df0ca2006-03-22 02:53:00 +00003828 unsigned Shift = (NumOperands == 4) ? 2 : 1;
3829 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003830 for (int i = 0; i < NumOperands; ++i) {
3831 int Val = SVOp->getMaskElt(NumOperands-i-1);
3832 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00003833 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00003834 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00003835 if (i != NumOperands - 1)
3836 Mask <<= Shift;
3837 }
Evan Cheng63d33002006-03-22 08:01:21 +00003838 return Mask;
3839}
3840
Evan Cheng506d3df2006-03-29 23:07:14 +00003841/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003842/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003843unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003844 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003845 unsigned Mask = 0;
3846 // 8 nodes, but we only care about the last 4.
3847 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003848 int Val = SVOp->getMaskElt(i);
3849 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003850 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00003851 if (i != 4)
3852 Mask <<= 2;
3853 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003854 return Mask;
3855}
3856
3857/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003858/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003859unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003860 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003861 unsigned Mask = 0;
3862 // 8 nodes, but we only care about the first 4.
3863 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003864 int Val = SVOp->getMaskElt(i);
3865 if (Val >= 0)
3866 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00003867 if (i != 0)
3868 Mask <<= 2;
3869 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003870 return Mask;
3871}
3872
Nate Begemana09008b2009-10-19 02:17:23 +00003873/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3874/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
3875unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
3876 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3877 EVT VVT = N->getValueType(0);
3878 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
3879 int Val = 0;
3880
3881 unsigned i, e;
3882 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
3883 Val = SVOp->getMaskElt(i);
3884 if (Val >= 0)
3885 break;
3886 }
Eli Friedman63f8dde2011-07-25 21:36:45 +00003887 assert(Val - i > 0 && "PALIGNR imm should be positive");
Nate Begemana09008b2009-10-19 02:17:23 +00003888 return (Val - i) * EltSize;
3889}
3890
David Greenec38a03e2011-02-03 15:50:00 +00003891/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
3892/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
3893/// instructions.
3894unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
3895 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3896 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
3897
3898 uint64_t Index =
3899 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3900
3901 EVT VecVT = N->getOperand(0).getValueType();
3902 EVT ElVT = VecVT.getVectorElementType();
3903
3904 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00003905 return Index / NumElemsPerChunk;
3906}
3907
David Greeneccacdc12011-02-04 16:08:29 +00003908/// getInsertVINSERTF128Immediate - Return the appropriate immediate
3909/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
3910/// instructions.
3911unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
3912 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3913 llvm_unreachable("Illegal insert subvector for VINSERTF128");
3914
3915 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00003916 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00003917
3918 EVT VecVT = N->getValueType(0);
3919 EVT ElVT = VecVT.getVectorElementType();
3920
3921 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00003922 return Index / NumElemsPerChunk;
3923}
3924
Evan Cheng37b73872009-07-30 08:33:02 +00003925/// isZeroNode - Returns true if Elt is a constant zero or a floating point
3926/// constant +0.0.
3927bool X86::isZeroNode(SDValue Elt) {
3928 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00003929 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00003930 (isa<ConstantFPSDNode>(Elt) &&
3931 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
3932}
3933
Nate Begeman9008ca62009-04-27 18:41:29 +00003934/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
3935/// their permute mask.
3936static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
3937 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003938 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003939 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00003940 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00003941
Nate Begeman5a5ca152009-04-29 05:20:52 +00003942 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003943 int idx = SVOp->getMaskElt(i);
3944 if (idx < 0)
3945 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003946 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003947 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003948 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003949 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003950 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003951 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
3952 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003953}
3954
Evan Cheng779ccea2007-12-07 21:30:01 +00003955/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3956/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00003957static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00003958 unsigned NumElems = VT.getVectorNumElements();
3959 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003960 int idx = Mask[i];
3961 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003962 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003963 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003964 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003965 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003966 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003967 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003968}
3969
Evan Cheng533a0aa2006-04-19 20:35:22 +00003970/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3971/// match movhlps. The lower half elements should come from upper half of
3972/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003973/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00003974static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00003975 EVT VT = Op->getValueType(0);
3976 if (VT.getSizeInBits() != 128)
3977 return false;
3978 if (VT.getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00003979 return false;
3980 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003981 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003982 return false;
3983 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003984 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003985 return false;
3986 return true;
3987}
3988
Evan Cheng5ced1d82006-04-06 23:23:56 +00003989/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00003990/// is promoted to a vector. It also returns the LoadSDNode by reference if
3991/// required.
3992static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00003993 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3994 return false;
3995 N = N->getOperand(0).getNode();
3996 if (!ISD::isNON_EXTLoad(N))
3997 return false;
3998 if (LD)
3999 *LD = cast<LoadSDNode>(N);
4000 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004001}
4002
Evan Cheng533a0aa2006-04-19 20:35:22 +00004003/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4004/// match movlp{s|d}. The lower half elements should come from lower half of
4005/// V1 (and in order), and the upper half elements should come from the upper
4006/// half of V2 (and in order). And since V1 will become the source of the
4007/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004008static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
4009 ShuffleVectorSDNode *Op) {
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004010 EVT VT = Op->getValueType(0);
4011 if (VT.getSizeInBits() != 128)
4012 return false;
4013
Evan Cheng466685d2006-10-09 20:57:25 +00004014 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004015 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00004016 // Is V2 is a vector load, don't do this transformation. We will try to use
4017 // load folding shufps op.
4018 if (ISD::isNON_EXTLoad(V2))
4019 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004020
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004021 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004022
Evan Cheng533a0aa2006-04-19 20:35:22 +00004023 if (NumElems != 2 && NumElems != 4)
4024 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004025 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004026 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004027 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004028 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004029 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004030 return false;
4031 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004032}
4033
Evan Cheng39623da2006-04-20 08:58:49 +00004034/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4035/// all the same.
4036static bool isSplatVector(SDNode *N) {
4037 if (N->getOpcode() != ISD::BUILD_VECTOR)
4038 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004039
Dan Gohman475871a2008-07-27 21:46:04 +00004040 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00004041 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4042 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00004043 return false;
4044 return true;
4045}
4046
Evan Cheng213d2cf2007-05-17 18:45:50 +00004047/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00004048/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00004049/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00004050static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00004051 SDValue V1 = N->getOperand(0);
4052 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004053 unsigned NumElems = N->getValueType(0).getVectorNumElements();
4054 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004055 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004056 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004057 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00004058 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4059 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004060 if (Opc != ISD::BUILD_VECTOR ||
4061 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00004062 return false;
4063 } else if (Idx >= 0) {
4064 unsigned Opc = V1.getOpcode();
4065 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4066 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004067 if (Opc != ISD::BUILD_VECTOR ||
4068 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00004069 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00004070 }
4071 }
4072 return true;
4073}
4074
4075/// getZeroVector - Returns a vector of specified type with all zero elements.
4076///
Owen Andersone50ed302009-08-10 22:56:29 +00004077static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00004078 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004079 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004080
Dale Johannesen0488fb62010-09-30 23:57:10 +00004081 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004082 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00004083 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00004084 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004085 if (HasSSE2) { // SSE2
4086 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4087 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4088 } else { // SSE1
4089 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4090 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4091 }
4092 } else if (VT.getSizeInBits() == 256) { // AVX
4093 // 256-bit logic and arithmetic instructions in AVX are
4094 // all floating-point, no support for integer ops. Default
4095 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00004096 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004097 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4098 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00004099 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004100 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004101}
4102
Chris Lattner8a594482007-11-25 00:24:49 +00004103/// getOnesVector - Returns a vector of specified type with all bits set.
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004104/// Always build ones vectors as <4 x i32>. For 256-bit types, use two
4105/// <4 x i32> inserted in a <8 x i32> appropriately. Then bitcast to their
4106/// original type, ensuring they get CSE'd.
Owen Andersone50ed302009-08-10 22:56:29 +00004107static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004108 assert(VT.isVector() && "Expected a vector type");
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004109 assert((VT.is128BitVector() || VT.is256BitVector())
4110 && "Expected a 128-bit or 256-bit vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004111
Owen Anderson825b72b2009-08-11 20:47:22 +00004112 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004113 SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
4114 Cst, Cst, Cst, Cst);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004115
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004116 if (VT.is256BitVector()) {
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004117 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, MVT::v8i32),
4118 Vec, DAG.getConstant(0, MVT::i32), DAG, dl);
4119 Vec = Insert128BitVector(InsV, Vec,
4120 DAG.getConstant(4 /* NumElems/2 */, MVT::i32), DAG, dl);
4121 }
4122
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004123 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00004124}
4125
Evan Cheng39623da2006-04-20 08:58:49 +00004126/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4127/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00004128static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004129 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004130 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004131
Evan Cheng39623da2006-04-20 08:58:49 +00004132 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00004133 SmallVector<int, 8> MaskVec;
4134 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00004135
Nate Begeman5a5ca152009-04-29 05:20:52 +00004136 for (unsigned i = 0; i != NumElems; ++i) {
4137 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004138 MaskVec[i] = NumElems;
4139 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00004140 }
Evan Cheng39623da2006-04-20 08:58:49 +00004141 }
Evan Cheng39623da2006-04-20 08:58:49 +00004142 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00004143 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
4144 SVOp->getOperand(1), &MaskVec[0]);
4145 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00004146}
4147
Evan Cheng017dcc62006-04-21 01:05:10 +00004148/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4149/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00004150static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004151 SDValue V2) {
4152 unsigned NumElems = VT.getVectorNumElements();
4153 SmallVector<int, 8> Mask;
4154 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00004155 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004156 Mask.push_back(i);
4157 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00004158}
4159
Nate Begeman9008ca62009-04-27 18:41:29 +00004160/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004161static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004162 SDValue V2) {
4163 unsigned NumElems = VT.getVectorNumElements();
4164 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00004165 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004166 Mask.push_back(i);
4167 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00004168 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004169 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00004170}
4171
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004172/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004173static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004174 SDValue V2) {
4175 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00004176 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00004177 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00004178 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004179 Mask.push_back(i + Half);
4180 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00004181 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004182 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004183}
4184
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004185// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004186// a generic shuffle instruction because the target has no such instructions.
4187// Generate shuffles which repeat i16 and i8 several times until they can be
4188// represented by v4f32 and then be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004189static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004190 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004191 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004192 DebugLoc dl = V.getDebugLoc();
Rafael Espindola15684b22009-04-24 12:40:33 +00004193
Nate Begeman9008ca62009-04-27 18:41:29 +00004194 while (NumElems > 4) {
4195 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004196 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004197 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004198 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004199 EltNo -= NumElems/2;
4200 }
4201 NumElems >>= 1;
4202 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004203 return V;
4204}
Eric Christopherfd179292009-08-27 18:07:15 +00004205
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004206/// getLegalSplat - Generate a legal splat with supported x86 shuffles
4207static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
4208 EVT VT = V.getValueType();
4209 DebugLoc dl = V.getDebugLoc();
4210 assert((VT.getSizeInBits() == 128 || VT.getSizeInBits() == 256)
4211 && "Vector size not supported");
4212
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004213 if (VT.getSizeInBits() == 128) {
4214 V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004215 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004216 V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
4217 &SplatMask[0]);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004218 } else {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004219 // To use VPERMILPS to splat scalars, the second half of indicies must
4220 // refer to the higher part, which is a duplication of the lower one,
4221 // because VPERMILPS can only handle in-lane permutations.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004222 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
4223 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004224
4225 V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
4226 V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
4227 &SplatMask[0]);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004228 }
4229
4230 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4231}
4232
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004233/// PromoteSplat - Splat is promoted to target supported vector shuffles.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004234static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4235 EVT SrcVT = SV->getValueType(0);
4236 SDValue V1 = SV->getOperand(0);
4237 DebugLoc dl = SV->getDebugLoc();
4238
4239 int EltNo = SV->getSplatIndex();
4240 int NumElems = SrcVT.getVectorNumElements();
4241 unsigned Size = SrcVT.getSizeInBits();
4242
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004243 assert(((Size == 128 && NumElems > 4) || Size == 256) &&
4244 "Unknown how to promote splat for type");
4245
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004246 // Extract the 128-bit part containing the splat element and update
4247 // the splat element index when it refers to the higher register.
4248 if (Size == 256) {
4249 unsigned Idx = (EltNo > NumElems/2) ? NumElems/2 : 0;
4250 V1 = Extract128BitVector(V1, DAG.getConstant(Idx, MVT::i32), DAG, dl);
4251 if (Idx > 0)
4252 EltNo -= NumElems/2;
4253 }
4254
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004255 // All i16 and i8 vector types can't be used directly by a generic shuffle
4256 // instruction because the target has no such instruction. Generate shuffles
4257 // which repeat i16 and i8 several times until they fit in i32, and then can
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004258 // be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004259 EVT EltVT = SrcVT.getVectorElementType();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004260 if (EltVT == MVT::i8 || EltVT == MVT::i16)
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004261 V1 = PromoteSplati8i16(V1, DAG, EltNo);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004262
4263 // Recreate the 256-bit vector and place the same 128-bit vector
4264 // into the low and high part. This is necessary because we want
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004265 // to use VPERM* to shuffle the vectors
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004266 if (Size == 256) {
4267 SDValue InsV = Insert128BitVector(DAG.getUNDEF(SrcVT), V1,
4268 DAG.getConstant(0, MVT::i32), DAG, dl);
4269 V1 = Insert128BitVector(InsV, V1,
4270 DAG.getConstant(NumElems/2, MVT::i32), DAG, dl);
4271 }
4272
4273 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004274}
4275
Evan Chengba05f722006-04-21 23:03:30 +00004276/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004277/// vector of zero or undef vector. This produces a shuffle where the low
4278/// element of V2 is swizzled into the zero/undef vector, landing at element
4279/// 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 +00004280static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00004281 bool isZero, bool HasSSE2,
4282 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004283 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00004284 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00004285 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
4286 unsigned NumElems = VT.getVectorNumElements();
4287 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004288 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004289 // If this is the insertion idx, put the low elt of V2 here.
4290 MaskVec.push_back(i == Idx ? NumElems : i);
4291 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004292}
4293
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004294/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4295/// element of the result of the vector shuffle.
Benjamin Kramer050db522011-03-26 12:38:19 +00004296static SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
4297 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004298 if (Depth == 6)
4299 return SDValue(); // Limit search depth.
4300
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004301 SDValue V = SDValue(N, 0);
4302 EVT VT = V.getValueType();
4303 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004304
4305 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4306 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
4307 Index = SV->getMaskElt(Index);
4308
4309 if (Index < 0)
4310 return DAG.getUNDEF(VT.getVectorElementType());
4311
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004312 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004313 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004314 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004315 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004316
4317 // Recurse into target specific vector shuffles to find scalars.
4318 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004319 int NumElems = VT.getVectorNumElements();
4320 SmallVector<unsigned, 16> ShuffleMask;
4321 SDValue ImmN;
4322
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004323 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004324 case X86ISD::SHUFPS:
4325 case X86ISD::SHUFPD:
4326 ImmN = N->getOperand(N->getNumOperands()-1);
4327 DecodeSHUFPSMask(NumElems,
4328 cast<ConstantSDNode>(ImmN)->getZExtValue(),
4329 ShuffleMask);
4330 break;
4331 case X86ISD::PUNPCKHBW:
4332 case X86ISD::PUNPCKHWD:
4333 case X86ISD::PUNPCKHDQ:
4334 case X86ISD::PUNPCKHQDQ:
4335 DecodePUNPCKHMask(NumElems, ShuffleMask);
4336 break;
4337 case X86ISD::UNPCKHPS:
4338 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00004339 case X86ISD::VUNPCKHPSY:
4340 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004341 DecodeUNPCKHPMask(NumElems, ShuffleMask);
4342 break;
4343 case X86ISD::PUNPCKLBW:
4344 case X86ISD::PUNPCKLWD:
4345 case X86ISD::PUNPCKLDQ:
4346 case X86ISD::PUNPCKLQDQ:
David Greenec4db4e52011-02-28 19:06:56 +00004347 DecodePUNPCKLMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004348 break;
4349 case X86ISD::UNPCKLPS:
4350 case X86ISD::UNPCKLPD:
David Greenec4db4e52011-02-28 19:06:56 +00004351 case X86ISD::VUNPCKLPSY:
4352 case X86ISD::VUNPCKLPDY:
4353 DecodeUNPCKLPMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004354 break;
4355 case X86ISD::MOVHLPS:
4356 DecodeMOVHLPSMask(NumElems, ShuffleMask);
4357 break;
4358 case X86ISD::MOVLHPS:
4359 DecodeMOVLHPSMask(NumElems, ShuffleMask);
4360 break;
4361 case X86ISD::PSHUFD:
4362 ImmN = N->getOperand(N->getNumOperands()-1);
4363 DecodePSHUFMask(NumElems,
4364 cast<ConstantSDNode>(ImmN)->getZExtValue(),
4365 ShuffleMask);
4366 break;
4367 case X86ISD::PSHUFHW:
4368 ImmN = N->getOperand(N->getNumOperands()-1);
4369 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4370 ShuffleMask);
4371 break;
4372 case X86ISD::PSHUFLW:
4373 ImmN = N->getOperand(N->getNumOperands()-1);
4374 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4375 ShuffleMask);
4376 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004377 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004378 case X86ISD::MOVSD: {
4379 // The index 0 always comes from the first element of the second source,
4380 // this is why MOVSS and MOVSD are used in the first place. The other
4381 // elements come from the other positions of the first source vector.
4382 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004383 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
4384 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004385 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00004386 case X86ISD::VPERMILPS:
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00004387 ImmN = N->getOperand(N->getNumOperands()-1);
Bruno Cardoso Lopes2eb4c2b2011-07-29 01:31:11 +00004388 DecodeVPERMILPSMask(4, cast<ConstantSDNode>(ImmN)->getZExtValue(),
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00004389 ShuffleMask);
Bruno Cardoso Lopes2eb4c2b2011-07-29 01:31:11 +00004390 break;
4391 case X86ISD::VPERMILPSY:
4392 ImmN = N->getOperand(N->getNumOperands()-1);
4393 DecodeVPERMILPSMask(8, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4394 ShuffleMask);
4395 break;
4396 case X86ISD::VPERMILPD:
4397 ImmN = N->getOperand(N->getNumOperands()-1);
4398 DecodeVPERMILPDMask(2, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4399 ShuffleMask);
4400 break;
4401 case X86ISD::VPERMILPDY:
4402 ImmN = N->getOperand(N->getNumOperands()-1);
4403 DecodeVPERMILPDMask(4, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4404 ShuffleMask);
4405 break;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00004406 case X86ISD::VPERM2F128:
4407 ImmN = N->getOperand(N->getNumOperands()-1);
4408 DecodeVPERM2F128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4409 ShuffleMask);
4410 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004411 default:
4412 assert("not implemented for target shuffle node");
4413 return SDValue();
4414 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004415
4416 Index = ShuffleMask[Index];
4417 if (Index < 0)
4418 return DAG.getUNDEF(VT.getVectorElementType());
4419
4420 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
4421 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
4422 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004423 }
4424
4425 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004426 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004427 V = V.getOperand(0);
4428 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004429 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004430
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004431 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004432 return SDValue();
4433 }
4434
4435 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4436 return (Index == 0) ? V.getOperand(0)
4437 : DAG.getUNDEF(VT.getVectorElementType());
4438
4439 if (V.getOpcode() == ISD::BUILD_VECTOR)
4440 return V.getOperand(Index);
4441
4442 return SDValue();
4443}
4444
4445/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4446/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004447/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004448static
4449unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
4450 bool ZerosFromLeft, SelectionDAG &DAG) {
4451 int i = 0;
4452
4453 while (i < NumElems) {
4454 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004455 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004456 if (!(Elt.getNode() &&
4457 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4458 break;
4459 ++i;
4460 }
4461
4462 return i;
4463}
4464
4465/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
4466/// MaskE correspond consecutively to elements from one of the vector operands,
4467/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4468static
4469bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
4470 int OpIdx, int NumElems, unsigned &OpNum) {
4471 bool SeenV1 = false;
4472 bool SeenV2 = false;
4473
4474 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
4475 int Idx = SVOp->getMaskElt(i);
4476 // Ignore undef indicies
4477 if (Idx < 0)
4478 continue;
4479
4480 if (Idx < NumElems)
4481 SeenV1 = true;
4482 else
4483 SeenV2 = true;
4484
4485 // Only accept consecutive elements from the same vector
4486 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4487 return false;
4488 }
4489
4490 OpNum = SeenV1 ? 0 : 1;
4491 return true;
4492}
4493
4494/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4495/// logical left shift of a vector.
4496static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4497 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4498 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4499 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4500 false /* check zeros from right */, DAG);
4501 unsigned OpSrc;
4502
4503 if (!NumZeros)
4504 return false;
4505
4506 // Considering the elements in the mask that are not consecutive zeros,
4507 // check if they consecutively come from only one of the source vectors.
4508 //
4509 // V1 = {X, A, B, C} 0
4510 // \ \ \ /
4511 // vector_shuffle V1, V2 <1, 2, 3, X>
4512 //
4513 if (!isShuffleMaskConsecutive(SVOp,
4514 0, // Mask Start Index
4515 NumElems-NumZeros-1, // Mask End Index
4516 NumZeros, // Where to start looking in the src vector
4517 NumElems, // Number of elements in vector
4518 OpSrc)) // Which source operand ?
4519 return false;
4520
4521 isLeft = false;
4522 ShAmt = NumZeros;
4523 ShVal = SVOp->getOperand(OpSrc);
4524 return true;
4525}
4526
4527/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4528/// logical left shift of a vector.
4529static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4530 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4531 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4532 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4533 true /* check zeros from left */, DAG);
4534 unsigned OpSrc;
4535
4536 if (!NumZeros)
4537 return false;
4538
4539 // Considering the elements in the mask that are not consecutive zeros,
4540 // check if they consecutively come from only one of the source vectors.
4541 //
4542 // 0 { A, B, X, X } = V2
4543 // / \ / /
4544 // vector_shuffle V1, V2 <X, X, 4, 5>
4545 //
4546 if (!isShuffleMaskConsecutive(SVOp,
4547 NumZeros, // Mask Start Index
4548 NumElems-1, // Mask End Index
4549 0, // Where to start looking in the src vector
4550 NumElems, // Number of elements in vector
4551 OpSrc)) // Which source operand ?
4552 return false;
4553
4554 isLeft = true;
4555 ShAmt = NumZeros;
4556 ShVal = SVOp->getOperand(OpSrc);
4557 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004558}
4559
4560/// isVectorShift - Returns true if the shuffle can be implemented as a
4561/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004562static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004563 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004564 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4565 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4566 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004567
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004568 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004569}
4570
Evan Chengc78d3b42006-04-24 18:01:45 +00004571/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4572///
Dan Gohman475871a2008-07-27 21:46:04 +00004573static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004574 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004575 SelectionDAG &DAG,
4576 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004577 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004578 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004579
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004580 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004581 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004582 bool First = true;
4583 for (unsigned i = 0; i < 16; ++i) {
4584 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4585 if (ThisIsNonZero && First) {
4586 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004587 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004588 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004589 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004590 First = false;
4591 }
4592
4593 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004594 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004595 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4596 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004597 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004598 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004599 }
4600 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004601 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4602 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4603 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004604 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004605 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004606 } else
4607 ThisElt = LastElt;
4608
Gabor Greifba36cb52008-08-28 21:40:38 +00004609 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004610 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004611 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004612 }
4613 }
4614
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004615 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004616}
4617
Bill Wendlinga348c562007-03-22 18:42:45 +00004618/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004619///
Dan Gohman475871a2008-07-27 21:46:04 +00004620static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004621 unsigned NumNonZero, unsigned NumZero,
4622 SelectionDAG &DAG,
4623 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004624 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004625 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004626
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004627 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004628 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004629 bool First = true;
4630 for (unsigned i = 0; i < 8; ++i) {
4631 bool isNonZero = (NonZeros & (1 << i)) != 0;
4632 if (isNonZero) {
4633 if (First) {
4634 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004635 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004636 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004637 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004638 First = false;
4639 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004640 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004641 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004642 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004643 }
4644 }
4645
4646 return V;
4647}
4648
Evan Chengf26ffe92008-05-29 08:22:04 +00004649/// getVShift - Return a vector logical shift node.
4650///
Owen Andersone50ed302009-08-10 22:56:29 +00004651static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004652 unsigned NumBits, SelectionDAG &DAG,
4653 const TargetLowering &TLI, DebugLoc dl) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004654 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00004655 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004656 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4657 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004658 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004659 DAG.getConstant(NumBits,
4660 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004661}
4662
Dan Gohman475871a2008-07-27 21:46:04 +00004663SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004664X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004665 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004666
Evan Chengc3630942009-12-09 21:00:30 +00004667 // Check if the scalar load can be widened into a vector load. And if
4668 // the address is "base + cst" see if the cst can be "absorbed" into
4669 // the shuffle mask.
4670 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4671 SDValue Ptr = LD->getBasePtr();
4672 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4673 return SDValue();
4674 EVT PVT = LD->getValueType(0);
4675 if (PVT != MVT::i32 && PVT != MVT::f32)
4676 return SDValue();
4677
4678 int FI = -1;
4679 int64_t Offset = 0;
4680 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4681 FI = FINode->getIndex();
4682 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004683 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004684 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4685 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4686 Offset = Ptr.getConstantOperandVal(1);
4687 Ptr = Ptr.getOperand(0);
4688 } else {
4689 return SDValue();
4690 }
4691
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004692 // FIXME: 256-bit vector instructions don't require a strict alignment,
4693 // improve this code to support it better.
4694 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00004695 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004696 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00004697 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004698 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00004699 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004700 // Can't change the alignment. FIXME: It's possible to compute
4701 // the exact stack offset and reference FI + adjust offset instead.
4702 // If someone *really* cares about this. That's the way to implement it.
4703 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004704 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004705 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00004706 }
4707 }
4708
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004709 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00004710 // Ptr + (Offset & ~15).
4711 if (Offset < 0)
4712 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004713 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00004714 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004715 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00004716 if (StartOffset)
4717 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4718 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4719
4720 int EltNo = (Offset - StartOffset) >> 2;
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004721 int NumElems = VT.getVectorNumElements();
4722
4723 EVT CanonVT = VT.getSizeInBits() == 128 ? MVT::v4i32 : MVT::v8i32;
4724 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
4725 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00004726 LD->getPointerInfo().getWithOffset(StartOffset),
David Greene67c9d422010-02-15 16:53:33 +00004727 false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004728
4729 // Canonicalize it to a v4i32 or v8i32 shuffle.
4730 SmallVector<int, 8> Mask;
4731 for (int i = 0; i < NumElems; ++i)
4732 Mask.push_back(EltNo);
4733
4734 V1 = DAG.getNode(ISD::BITCAST, dl, CanonVT, V1);
4735 return DAG.getNode(ISD::BITCAST, dl, NVT,
4736 DAG.getVectorShuffle(CanonVT, dl, V1,
4737 DAG.getUNDEF(CanonVT),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00004738 }
4739
4740 return SDValue();
4741}
4742
Michael J. Spencerec38de22010-10-10 22:04:20 +00004743/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4744/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004745/// load which has the same value as a build_vector whose operands are 'elts'.
4746///
4747/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004748///
Nate Begeman1449f292010-03-24 22:19:06 +00004749/// FIXME: we'd also like to handle the case where the last elements are zero
4750/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4751/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004752static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004753 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004754 EVT EltVT = VT.getVectorElementType();
4755 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004756
Nate Begemanfdea31a2010-03-24 20:49:50 +00004757 LoadSDNode *LDBase = NULL;
4758 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004759
Nate Begeman1449f292010-03-24 22:19:06 +00004760 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004761 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004762 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004763 for (unsigned i = 0; i < NumElems; ++i) {
4764 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004765
Nate Begemanfdea31a2010-03-24 20:49:50 +00004766 if (!Elt.getNode() ||
4767 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4768 return SDValue();
4769 if (!LDBase) {
4770 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4771 return SDValue();
4772 LDBase = cast<LoadSDNode>(Elt.getNode());
4773 LastLoadedElt = i;
4774 continue;
4775 }
4776 if (Elt.getOpcode() == ISD::UNDEF)
4777 continue;
4778
4779 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4780 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4781 return SDValue();
4782 LastLoadedElt = i;
4783 }
Nate Begeman1449f292010-03-24 22:19:06 +00004784
4785 // If we have found an entire vector of loads and undefs, then return a large
4786 // load of the entire vector width starting at the base pointer. If we found
4787 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004788 if (LastLoadedElt == NumElems - 1) {
4789 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00004790 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004791 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004792 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00004793 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004794 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004795 LDBase->isVolatile(), LDBase->isNonTemporal(),
4796 LDBase->getAlignment());
Eli Friedman61cc47e2011-07-26 21:02:58 +00004797 } else if (NumElems == 4 && LastLoadedElt == 1 &&
4798 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004799 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
4800 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Chris Lattner88641552010-09-22 00:34:38 +00004801 SDValue ResNode = DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys,
4802 Ops, 2, MVT::i32,
4803 LDBase->getMemOperand());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004804 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00004805 }
4806 return SDValue();
4807}
4808
Evan Chengc3630942009-12-09 21:00:30 +00004809SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004810X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004811 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00004812
David Greenef125a292011-02-08 19:04:41 +00004813 EVT VT = Op.getValueType();
4814 EVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00004815 unsigned NumElems = Op.getNumOperands();
4816
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00004817 // Vectors containing all zeros can be matched by pxor and xorps later
4818 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
4819 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
4820 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004821 if (Op.getValueType() == MVT::v4i32 ||
4822 Op.getValueType() == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00004823 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004824
Dale Johannesenace16102009-02-03 19:33:06 +00004825 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00004826 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004827
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00004828 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
4829 // vectors or broken into v4i32 operations on 256-bit vectors.
4830 if (ISD::isBuildVectorAllOnes(Op.getNode())) {
4831 if (Op.getValueType() == MVT::v4i32)
4832 return Op;
4833
4834 return getOnesVector(Op.getValueType(), DAG, dl);
4835 }
4836
Owen Andersone50ed302009-08-10 22:56:29 +00004837 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004838
Evan Cheng0db9fe62006-04-25 20:13:52 +00004839 unsigned NumZero = 0;
4840 unsigned NumNonZero = 0;
4841 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004842 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004843 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004844 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00004845 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00004846 if (Elt.getOpcode() == ISD::UNDEF)
4847 continue;
4848 Values.insert(Elt);
4849 if (Elt.getOpcode() != ISD::Constant &&
4850 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004851 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00004852 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00004853 NumZero++;
4854 else {
4855 NonZeros |= (1 << i);
4856 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004857 }
4858 }
4859
Chris Lattner97a2a562010-08-26 05:24:29 +00004860 // All undef vector. Return an UNDEF. All zero vectors were handled above.
4861 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00004862 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004863
Chris Lattner67f453a2008-03-09 05:42:06 +00004864 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00004865 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004866 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00004867 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00004868
Chris Lattner62098042008-03-09 01:05:04 +00004869 // If this is an insertion of an i64 value on x86-32, and if the top bits of
4870 // the value are obviously zero, truncate the value to i32 and do the
4871 // insertion that way. Only do this if the value is non-constant or if the
4872 // value is a constant being inserted into element 0. It is cheaper to do
4873 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00004874 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00004875 (!IsAllConstants || Idx == 0)) {
4876 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004877 // Handle SSE only.
4878 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
4879 EVT VecVT = MVT::v4i32;
4880 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00004881
Chris Lattner62098042008-03-09 01:05:04 +00004882 // Truncate the value (which may itself be a constant) to i32, and
4883 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00004884 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00004885 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00004886 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4887 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00004888
Chris Lattner62098042008-03-09 01:05:04 +00004889 // Now we have our 32-bit value zero extended in the low element of
4890 // a vector. If Idx != 0, swizzle it into place.
4891 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004892 SmallVector<int, 4> Mask;
4893 Mask.push_back(Idx);
4894 for (unsigned i = 1; i != VecElts; ++i)
4895 Mask.push_back(i);
4896 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00004897 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00004898 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004899 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004900 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00004901 }
4902 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004903
Chris Lattner19f79692008-03-08 22:59:52 +00004904 // If we have a constant or non-constant insertion into the low element of
4905 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
4906 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00004907 // depending on what the source datatype is.
4908 if (Idx == 0) {
4909 if (NumZero == 0) {
4910 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00004911 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
4912 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00004913 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
4914 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
4915 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
4916 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00004917 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
4918 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00004919 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
4920 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00004921 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
4922 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4923 Subtarget->hasSSE2(), DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004924 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00004925 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004926 }
Evan Chengf26ffe92008-05-29 08:22:04 +00004927
4928 // Is it a vector logical left shift?
4929 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00004930 X86::isZeroNode(Op.getOperand(0)) &&
4931 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004932 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00004933 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004934 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004935 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00004936 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004937 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004938
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004939 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00004940 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004941
Chris Lattner19f79692008-03-08 22:59:52 +00004942 // Otherwise, if this is a vector with i32 or f32 elements, and the element
4943 // is a non-constant being inserted into an element other than the low one,
4944 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
4945 // movd/movss) to move this into the low element, then shuffle it into
4946 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004947 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00004948 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00004949
Evan Cheng0db9fe62006-04-25 20:13:52 +00004950 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00004951 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
4952 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00004953 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004954 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00004955 MaskVec.push_back(i == Idx ? 0 : 1);
4956 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004957 }
4958 }
4959
Chris Lattner67f453a2008-03-09 05:42:06 +00004960 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00004961 if (Values.size() == 1) {
4962 if (EVTBits == 32) {
4963 // Instead of a shuffle like this:
4964 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
4965 // Check if it's possible to issue this instead.
4966 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
4967 unsigned Idx = CountTrailingZeros_32(NonZeros);
4968 SDValue Item = Op.getOperand(Idx);
4969 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
4970 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
4971 }
Dan Gohman475871a2008-07-27 21:46:04 +00004972 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004973 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004974
Dan Gohmana3941172007-07-24 22:55:08 +00004975 // A vector full of immediates; various special cases are already
4976 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004977 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00004978 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00004979
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00004980 // For AVX-length vectors, build the individual 128-bit pieces and use
4981 // shuffles to put them in place.
4982 if (VT.getSizeInBits() == 256 && !ISD::isBuildVectorAllZeros(Op.getNode())) {
4983 SmallVector<SDValue, 32> V;
4984 for (unsigned i = 0; i < NumElems; ++i)
4985 V.push_back(Op.getOperand(i));
4986
4987 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
4988
4989 // Build both the lower and upper subvector.
4990 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
4991 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
4992 NumElems/2);
4993
4994 // Recreate the wider vector with the lower and upper part.
Bruno Cardoso Lopes15d03fb2011-07-28 01:26:53 +00004995 SDValue Vec = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), Lower,
4996 DAG.getConstant(0, MVT::i32), DAG, dl);
4997 return Insert128BitVector(Vec, Upper, DAG.getConstant(NumElems/2, MVT::i32),
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00004998 DAG, dl);
4999 }
5000
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005001 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005002 if (EVTBits == 64) {
5003 if (NumNonZero == 1) {
5004 // One half is zero or undef.
5005 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005006 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005007 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00005008 return getShuffleVectorZeroOrUndef(V2, Idx, true,
5009 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005010 }
Dan Gohman475871a2008-07-27 21:46:04 +00005011 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005012 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005013
5014 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005015 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005016 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00005017 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005018 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005019 }
5020
Bill Wendling826f36f2007-03-28 00:57:11 +00005021 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005022 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00005023 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005024 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005025 }
5026
5027 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00005028 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00005029 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005030 if (NumElems == 4 && NumZero > 0) {
5031 for (unsigned i = 0; i < 4; ++i) {
5032 bool isZero = !(NonZeros & (1 << i));
5033 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00005034 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005035 else
Dale Johannesenace16102009-02-03 19:33:06 +00005036 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005037 }
5038
5039 for (unsigned i = 0; i < 2; ++i) {
5040 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5041 default: break;
5042 case 0:
5043 V[i] = V[i*2]; // Must be a zero vector.
5044 break;
5045 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005046 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005047 break;
5048 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005049 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005050 break;
5051 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005052 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005053 break;
5054 }
5055 }
5056
Nate Begeman9008ca62009-04-27 18:41:29 +00005057 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005058 bool Reverse = (NonZeros & 0x3) == 2;
5059 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005060 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005061 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5062 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005063 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
5064 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005065 }
5066
Nate Begemanfdea31a2010-03-24 20:49:50 +00005067 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
5068 // Check for a build vector of consecutive loads.
5069 for (unsigned i = 0; i < NumElems; ++i)
5070 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005071
Nate Begemanfdea31a2010-03-24 20:49:50 +00005072 // Check for elements which are consecutive loads.
5073 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5074 if (LD.getNode())
5075 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005076
5077 // For SSE 4.1, use insertps to put the high elements into the low element.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005078 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005079 SDValue Result;
5080 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5081 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5082 else
5083 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005084
Chris Lattner24faf612010-08-28 17:59:08 +00005085 for (unsigned i = 1; i < NumElems; ++i) {
5086 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5087 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005088 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005089 }
5090 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005091 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005092
Chris Lattner6e80e442010-08-28 17:15:43 +00005093 // Otherwise, expand into a number of unpckl*, start by extending each of
5094 // our (non-undef) elements to the full vector width with the element in the
5095 // bottom slot of the vector (which generates no code for SSE).
5096 for (unsigned i = 0; i < NumElems; ++i) {
5097 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5098 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5099 else
5100 V[i] = DAG.getUNDEF(VT);
5101 }
5102
5103 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005104 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5105 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5106 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005107 unsigned EltStride = NumElems >> 1;
5108 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005109 for (unsigned i = 0; i < EltStride; ++i) {
5110 // If V[i+EltStride] is undef and this is the first round of mixing,
5111 // then it is safe to just drop this shuffle: V[i] is already in the
5112 // right place, the one element (since it's the first round) being
5113 // inserted as undef can be dropped. This isn't safe for successive
5114 // rounds because they will permute elements within both vectors.
5115 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5116 EltStride == NumElems/2)
5117 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005118
Chris Lattner6e80e442010-08-28 17:15:43 +00005119 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005120 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005121 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005122 }
5123 return V[0];
5124 }
Dan Gohman475871a2008-07-27 21:46:04 +00005125 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005126}
5127
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005128// LowerMMXCONCAT_VECTORS - We support concatenate two MMX registers and place
5129// them in a MMX register. This is better than doing a stack convert.
5130static SDValue LowerMMXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005131 DebugLoc dl = Op.getDebugLoc();
5132 EVT ResVT = Op.getValueType();
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005133
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005134 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
5135 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
5136 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005137 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005138 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
5139 InVec = Op.getOperand(1);
5140 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5141 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005142 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005143 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
5144 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
5145 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005146 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005147 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
5148 Mask[0] = 0; Mask[1] = 2;
5149 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
5150 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005151 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005152}
5153
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005154// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5155// to create 256-bit vectors from two other 128-bit ones.
5156static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5157 DebugLoc dl = Op.getDebugLoc();
5158 EVT ResVT = Op.getValueType();
5159
5160 assert(ResVT.getSizeInBits() == 256 && "Value type must be 256-bit wide");
5161
5162 SDValue V1 = Op.getOperand(0);
5163 SDValue V2 = Op.getOperand(1);
5164 unsigned NumElems = ResVT.getVectorNumElements();
5165
5166 SDValue V = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, ResVT), V1,
5167 DAG.getConstant(0, MVT::i32), DAG, dl);
5168 return Insert128BitVector(V, V2, DAG.getConstant(NumElems/2, MVT::i32),
5169 DAG, dl);
5170}
5171
5172SDValue
5173X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005174 EVT ResVT = Op.getValueType();
5175
5176 assert(Op.getNumOperands() == 2);
5177 assert((ResVT.getSizeInBits() == 128 || ResVT.getSizeInBits() == 256) &&
5178 "Unsupported CONCAT_VECTORS for value type");
5179
5180 // We support concatenate two MMX registers and place them in a MMX register.
5181 // This is better than doing a stack convert.
5182 if (ResVT.is128BitVector())
5183 return LowerMMXCONCAT_VECTORS(Op, DAG);
5184
5185 // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
5186 // from two other 128-bit ones.
5187 return LowerAVXCONCAT_VECTORS(Op, DAG);
5188}
5189
Nate Begemanb9a47b82009-02-23 08:49:38 +00005190// v8i16 shuffles - Prefer shuffles in the following order:
5191// 1. [all] pshuflw, pshufhw, optional move
5192// 2. [ssse3] 1 x pshufb
5193// 3. [ssse3] 2 x pshufb + 1 x por
5194// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005195SDValue
5196X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
5197 SelectionDAG &DAG) const {
5198 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00005199 SDValue V1 = SVOp->getOperand(0);
5200 SDValue V2 = SVOp->getOperand(1);
5201 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005202 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00005203
Nate Begemanb9a47b82009-02-23 08:49:38 +00005204 // Determine if more than 1 of the words in each of the low and high quadwords
5205 // of the result come from the same quadword of one of the two inputs. Undef
5206 // mask values count as coming from any quadword, for better codegen.
5207 SmallVector<unsigned, 4> LoQuad(4);
5208 SmallVector<unsigned, 4> HiQuad(4);
5209 BitVector InputQuads(4);
5210 for (unsigned i = 0; i < 8; ++i) {
5211 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00005212 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005213 MaskVals.push_back(EltIdx);
5214 if (EltIdx < 0) {
5215 ++Quad[0];
5216 ++Quad[1];
5217 ++Quad[2];
5218 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00005219 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005220 }
5221 ++Quad[EltIdx / 4];
5222 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00005223 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005224
Nate Begemanb9a47b82009-02-23 08:49:38 +00005225 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005226 unsigned MaxQuad = 1;
5227 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005228 if (LoQuad[i] > MaxQuad) {
5229 BestLoQuad = i;
5230 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005231 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005232 }
5233
Nate Begemanb9a47b82009-02-23 08:49:38 +00005234 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005235 MaxQuad = 1;
5236 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005237 if (HiQuad[i] > MaxQuad) {
5238 BestHiQuad = i;
5239 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005240 }
5241 }
5242
Nate Begemanb9a47b82009-02-23 08:49:38 +00005243 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00005244 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00005245 // single pshufb instruction is necessary. If There are more than 2 input
5246 // quads, disable the next transformation since it does not help SSSE3.
5247 bool V1Used = InputQuads[0] || InputQuads[1];
5248 bool V2Used = InputQuads[2] || InputQuads[3];
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005249 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005250 if (InputQuads.count() == 2 && V1Used && V2Used) {
5251 BestLoQuad = InputQuads.find_first();
5252 BestHiQuad = InputQuads.find_next(BestLoQuad);
5253 }
5254 if (InputQuads.count() > 2) {
5255 BestLoQuad = -1;
5256 BestHiQuad = -1;
5257 }
5258 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005259
Nate Begemanb9a47b82009-02-23 08:49:38 +00005260 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5261 // the shuffle mask. If a quad is scored as -1, that means that it contains
5262 // words from all 4 input quadwords.
5263 SDValue NewV;
5264 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005265 SmallVector<int, 8> MaskV;
5266 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
5267 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00005268 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005269 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
5270 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
5271 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005272
Nate Begemanb9a47b82009-02-23 08:49:38 +00005273 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5274 // source words for the shuffle, to aid later transformations.
5275 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00005276 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00005277 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005278 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00005279 if (idx != (int)i)
5280 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005281 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00005282 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005283 AllWordsInNewV = false;
5284 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00005285 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005286
Nate Begemanb9a47b82009-02-23 08:49:38 +00005287 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5288 if (AllWordsInNewV) {
5289 for (int i = 0; i != 8; ++i) {
5290 int idx = MaskVals[i];
5291 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005292 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005293 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005294 if ((idx != i) && idx < 4)
5295 pshufhw = false;
5296 if ((idx != i) && idx > 3)
5297 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00005298 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005299 V1 = NewV;
5300 V2Used = false;
5301 BestLoQuad = 0;
5302 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005303 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005304
Nate Begemanb9a47b82009-02-23 08:49:38 +00005305 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5306 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005307 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005308 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5309 unsigned TargetMask = 0;
5310 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005311 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005312 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
5313 X86::getShufflePSHUFLWImmediate(NewV.getNode());
5314 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005315 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005316 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005317 }
Eric Christopherfd179292009-08-27 18:07:15 +00005318
Nate Begemanb9a47b82009-02-23 08:49:38 +00005319 // If we have SSSE3, and all words of the result are from 1 input vector,
5320 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5321 // is present, fall back to case 4.
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005322 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005323 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005324
Nate Begemanb9a47b82009-02-23 08:49:38 +00005325 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005326 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005327 // mask, and elements that come from V1 in the V2 mask, so that the two
5328 // results can be OR'd together.
5329 bool TwoInputs = V1Used && V2Used;
5330 for (unsigned i = 0; i != 8; ++i) {
5331 int EltIdx = MaskVals[i] * 2;
5332 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005333 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5334 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005335 continue;
5336 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005337 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
5338 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005339 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005340 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005341 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005342 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005343 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005344 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005345 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005346
Nate Begemanb9a47b82009-02-23 08:49:38 +00005347 // Calculate the shuffle mask for the second input, shuffle it, and
5348 // OR it with the first shuffled input.
5349 pshufbMask.clear();
5350 for (unsigned i = 0; i != 8; ++i) {
5351 int EltIdx = MaskVals[i] * 2;
5352 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005353 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5354 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005355 continue;
5356 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005357 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
5358 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005359 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005360 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005361 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005362 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005363 MVT::v16i8, &pshufbMask[0], 16));
5364 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005365 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005366 }
5367
5368 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5369 // and update MaskVals with new element order.
5370 BitVector InOrder(8);
5371 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005372 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005373 for (int i = 0; i != 4; ++i) {
5374 int idx = MaskVals[i];
5375 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005376 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005377 InOrder.set(i);
5378 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005379 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005380 InOrder.set(i);
5381 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005382 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005383 }
5384 }
5385 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005386 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00005387 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005388 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005389
5390 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
5391 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
5392 NewV.getOperand(0),
5393 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
5394 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005395 }
Eric Christopherfd179292009-08-27 18:07:15 +00005396
Nate Begemanb9a47b82009-02-23 08:49:38 +00005397 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5398 // and update MaskVals with the new element order.
5399 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005400 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005401 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005402 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005403 for (unsigned i = 4; i != 8; ++i) {
5404 int idx = MaskVals[i];
5405 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005406 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005407 InOrder.set(i);
5408 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005409 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005410 InOrder.set(i);
5411 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005412 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005413 }
5414 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005415 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005416 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005417
5418 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
5419 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
5420 NewV.getOperand(0),
5421 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
5422 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005423 }
Eric Christopherfd179292009-08-27 18:07:15 +00005424
Nate Begemanb9a47b82009-02-23 08:49:38 +00005425 // In case BestHi & BestLo were both -1, which means each quadword has a word
5426 // from each of the four input quadwords, calculate the InOrder bitvector now
5427 // before falling through to the insert/extract cleanup.
5428 if (BestLoQuad == -1 && BestHiQuad == -1) {
5429 NewV = V1;
5430 for (int i = 0; i != 8; ++i)
5431 if (MaskVals[i] < 0 || MaskVals[i] == i)
5432 InOrder.set(i);
5433 }
Eric Christopherfd179292009-08-27 18:07:15 +00005434
Nate Begemanb9a47b82009-02-23 08:49:38 +00005435 // The other elements are put in the right place using pextrw and pinsrw.
5436 for (unsigned i = 0; i != 8; ++i) {
5437 if (InOrder[i])
5438 continue;
5439 int EltIdx = MaskVals[i];
5440 if (EltIdx < 0)
5441 continue;
5442 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00005443 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005444 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00005445 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005446 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005447 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005448 DAG.getIntPtrConstant(i));
5449 }
5450 return NewV;
5451}
5452
5453// v16i8 shuffles - Prefer shuffles in the following order:
5454// 1. [ssse3] 1 x pshufb
5455// 2. [ssse3] 2 x pshufb + 1 x por
5456// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5457static
Nate Begeman9008ca62009-04-27 18:41:29 +00005458SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005459 SelectionDAG &DAG,
5460 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005461 SDValue V1 = SVOp->getOperand(0);
5462 SDValue V2 = SVOp->getOperand(1);
5463 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005464 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00005465 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00005466
Nate Begemanb9a47b82009-02-23 08:49:38 +00005467 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005468 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005469 // present, fall back to case 3.
5470 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
5471 bool V1Only = true;
5472 bool V2Only = true;
5473 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005474 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00005475 if (EltIdx < 0)
5476 continue;
5477 if (EltIdx < 16)
5478 V2Only = false;
5479 else
5480 V1Only = false;
5481 }
Eric Christopherfd179292009-08-27 18:07:15 +00005482
Nate Begemanb9a47b82009-02-23 08:49:38 +00005483 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
5484 if (TLI.getSubtarget()->hasSSSE3()) {
5485 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005486
Nate Begemanb9a47b82009-02-23 08:49:38 +00005487 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005488 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005489 //
5490 // Otherwise, we have elements from both input vectors, and must zero out
5491 // elements that come from V2 in the first mask, and V1 in the second mask
5492 // so that we can OR them together.
5493 bool TwoInputs = !(V1Only || V2Only);
5494 for (unsigned i = 0; i != 16; ++i) {
5495 int EltIdx = MaskVals[i];
5496 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005497 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005498 continue;
5499 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005500 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005501 }
5502 // If all the elements are from V2, assign it to V1 and return after
5503 // building the first pshufb.
5504 if (V2Only)
5505 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00005506 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005507 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005508 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005509 if (!TwoInputs)
5510 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005511
Nate Begemanb9a47b82009-02-23 08:49:38 +00005512 // Calculate the shuffle mask for the second input, shuffle it, and
5513 // OR it with the first shuffled input.
5514 pshufbMask.clear();
5515 for (unsigned i = 0; i != 16; ++i) {
5516 int EltIdx = MaskVals[i];
5517 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005518 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005519 continue;
5520 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005521 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005522 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005523 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005524 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005525 MVT::v16i8, &pshufbMask[0], 16));
5526 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005527 }
Eric Christopherfd179292009-08-27 18:07:15 +00005528
Nate Begemanb9a47b82009-02-23 08:49:38 +00005529 // No SSSE3 - Calculate in place words and then fix all out of place words
5530 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5531 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005532 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5533 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005534 SDValue NewV = V2Only ? V2 : V1;
5535 for (int i = 0; i != 8; ++i) {
5536 int Elt0 = MaskVals[i*2];
5537 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005538
Nate Begemanb9a47b82009-02-23 08:49:38 +00005539 // This word of the result is all undef, skip it.
5540 if (Elt0 < 0 && Elt1 < 0)
5541 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005542
Nate Begemanb9a47b82009-02-23 08:49:38 +00005543 // This word of the result is already in the correct place, skip it.
5544 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
5545 continue;
5546 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
5547 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005548
Nate Begemanb9a47b82009-02-23 08:49:38 +00005549 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5550 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5551 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005552
5553 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5554 // using a single extract together, load it and store it.
5555 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005556 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005557 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005558 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005559 DAG.getIntPtrConstant(i));
5560 continue;
5561 }
5562
Nate Begemanb9a47b82009-02-23 08:49:38 +00005563 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00005564 // source byte is not also odd, shift the extracted word left 8 bits
5565 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005566 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005567 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005568 DAG.getIntPtrConstant(Elt1 / 2));
5569 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005570 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00005571 DAG.getConstant(8,
5572 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005573 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005574 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
5575 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005576 }
5577 // If Elt0 is defined, extract it from the appropriate source. If the
5578 // source byte is not also even, shift the extracted word right 8 bits. If
5579 // Elt1 was also defined, OR the extracted values together before
5580 // inserting them in the result.
5581 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005582 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005583 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
5584 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005585 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00005586 DAG.getConstant(8,
5587 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005588 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005589 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
5590 DAG.getConstant(0x00FF, MVT::i16));
5591 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00005592 : InsElt0;
5593 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005594 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005595 DAG.getIntPtrConstant(i));
5596 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005597 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005598}
5599
Evan Cheng7a831ce2007-12-15 03:00:47 +00005600/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005601/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00005602/// done when every pair / quad of shuffle mask elements point to elements in
5603/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005604/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00005605static
Nate Begeman9008ca62009-04-27 18:41:29 +00005606SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005607 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00005608 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00005609 SDValue V1 = SVOp->getOperand(0);
5610 SDValue V2 = SVOp->getOperand(1);
5611 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00005612 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005613 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00005614 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005615 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005616 case MVT::v4f32: NewVT = MVT::v2f64; break;
5617 case MVT::v4i32: NewVT = MVT::v2i64; break;
5618 case MVT::v8i16: NewVT = MVT::v4i32; break;
5619 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00005620 }
5621
Nate Begeman9008ca62009-04-27 18:41:29 +00005622 int Scale = NumElems / NewWidth;
5623 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00005624 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005625 int StartIdx = -1;
5626 for (int j = 0; j < Scale; ++j) {
5627 int EltIdx = SVOp->getMaskElt(i+j);
5628 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005629 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00005630 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00005631 StartIdx = EltIdx - (EltIdx % Scale);
5632 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00005633 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005634 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005635 if (StartIdx == -1)
5636 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00005637 else
Nate Begeman9008ca62009-04-27 18:41:29 +00005638 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005639 }
5640
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005641 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
5642 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00005643 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005644}
5645
Evan Chengd880b972008-05-09 21:53:03 +00005646/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005647///
Owen Andersone50ed302009-08-10 22:56:29 +00005648static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00005649 SDValue SrcOp, SelectionDAG &DAG,
5650 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005651 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005652 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00005653 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00005654 LD = dyn_cast<LoadSDNode>(SrcOp);
5655 if (!LD) {
5656 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
5657 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00005658 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00005659 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00005660 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005661 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00005662 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005663 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00005664 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005665 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005666 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
5667 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
5668 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00005669 SrcOp.getOperand(0)
5670 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005671 }
5672 }
5673 }
5674
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005675 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005676 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005677 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00005678 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005679}
5680
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00005681/// areShuffleHalvesWithinDisjointLanes - Check whether each half of a vector
5682/// shuffle node referes to only one lane in the sources.
5683static bool areShuffleHalvesWithinDisjointLanes(ShuffleVectorSDNode *SVOp) {
5684 EVT VT = SVOp->getValueType(0);
5685 int NumElems = VT.getVectorNumElements();
5686 int HalfSize = NumElems/2;
5687 SmallVector<int, 16> M;
5688 SVOp->getMask(M);
5689 bool MatchA = false, MatchB = false;
5690
5691 for (int l = 0; l < NumElems*2; l += HalfSize) {
5692 if (isUndefOrInRange(M, 0, HalfSize, l, l+HalfSize)) {
5693 MatchA = true;
5694 break;
5695 }
5696 }
5697
5698 for (int l = 0; l < NumElems*2; l += HalfSize) {
5699 if (isUndefOrInRange(M, HalfSize, HalfSize, l, l+HalfSize)) {
5700 MatchB = true;
5701 break;
5702 }
5703 }
5704
5705 return MatchA && MatchB;
5706}
5707
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00005708/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
5709/// which could not be matched by any known target speficic shuffle
5710static SDValue
5711LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00005712 if (areShuffleHalvesWithinDisjointLanes(SVOp)) {
5713 // If each half of a vector shuffle node referes to only one lane in the
5714 // source vectors, extract each used 128-bit lane and shuffle them using
5715 // 128-bit shuffles. Then, concatenate the results. Otherwise leave
5716 // the work to the legalizer.
5717 DebugLoc dl = SVOp->getDebugLoc();
5718 EVT VT = SVOp->getValueType(0);
5719 int NumElems = VT.getVectorNumElements();
5720 int HalfSize = NumElems/2;
5721
5722 // Extract the reference for each half
5723 int FstVecExtractIdx = 0, SndVecExtractIdx = 0;
5724 int FstVecOpNum = 0, SndVecOpNum = 0;
5725 for (int i = 0; i < HalfSize; ++i) {
5726 int Elt = SVOp->getMaskElt(i);
5727 if (SVOp->getMaskElt(i) < 0)
5728 continue;
5729 FstVecOpNum = Elt/NumElems;
5730 FstVecExtractIdx = Elt % NumElems < HalfSize ? 0 : HalfSize;
5731 break;
5732 }
5733 for (int i = HalfSize; i < NumElems; ++i) {
5734 int Elt = SVOp->getMaskElt(i);
5735 if (SVOp->getMaskElt(i) < 0)
5736 continue;
5737 SndVecOpNum = Elt/NumElems;
5738 SndVecExtractIdx = Elt % NumElems < HalfSize ? 0 : HalfSize;
5739 break;
5740 }
5741
5742 // Extract the subvectors
5743 SDValue V1 = Extract128BitVector(SVOp->getOperand(FstVecOpNum),
5744 DAG.getConstant(FstVecExtractIdx, MVT::i32), DAG, dl);
5745 SDValue V2 = Extract128BitVector(SVOp->getOperand(SndVecOpNum),
5746 DAG.getConstant(SndVecExtractIdx, MVT::i32), DAG, dl);
5747
5748 // Generate 128-bit shuffles
5749 SmallVector<int, 16> MaskV1, MaskV2;
5750 for (int i = 0; i < HalfSize; ++i) {
5751 int Elt = SVOp->getMaskElt(i);
5752 MaskV1.push_back(Elt < 0 ? Elt : Elt % HalfSize);
5753 }
5754 for (int i = HalfSize; i < NumElems; ++i) {
5755 int Elt = SVOp->getMaskElt(i);
5756 MaskV2.push_back(Elt < 0 ? Elt : Elt % HalfSize);
5757 }
5758
5759 EVT NVT = V1.getValueType();
5760 V1 = DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &MaskV1[0]);
5761 V2 = DAG.getVectorShuffle(NVT, dl, V2, DAG.getUNDEF(NVT), &MaskV2[0]);
5762
5763 // Concatenate the result back
5764 SDValue V = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), V1,
5765 DAG.getConstant(0, MVT::i32), DAG, dl);
5766 return Insert128BitVector(V, V2, DAG.getConstant(NumElems/2, MVT::i32),
5767 DAG, dl);
5768 }
5769
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00005770 return SDValue();
5771}
5772
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00005773/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
5774/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00005775static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00005776LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005777 SDValue V1 = SVOp->getOperand(0);
5778 SDValue V2 = SVOp->getOperand(1);
5779 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005780 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00005781
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00005782 assert(VT.getSizeInBits() == 128 && "Unsupported vector size");
5783
Evan Chengace3c172008-07-22 21:13:36 +00005784 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00005785 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00005786 SmallVector<int, 8> Mask1(4U, -1);
5787 SmallVector<int, 8> PermMask;
5788 SVOp->getMask(PermMask);
5789
Evan Chengace3c172008-07-22 21:13:36 +00005790 unsigned NumHi = 0;
5791 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00005792 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005793 int Idx = PermMask[i];
5794 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005795 Locs[i] = std::make_pair(-1, -1);
5796 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005797 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
5798 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005799 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00005800 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005801 NumLo++;
5802 } else {
5803 Locs[i] = std::make_pair(1, NumHi);
5804 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005805 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005806 NumHi++;
5807 }
5808 }
5809 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005810
Evan Chengace3c172008-07-22 21:13:36 +00005811 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005812 // If no more than two elements come from either vector. This can be
5813 // implemented with two shuffles. First shuffle gather the elements.
5814 // The second shuffle, which takes the first shuffle as both of its
5815 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00005816 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005817
Nate Begeman9008ca62009-04-27 18:41:29 +00005818 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00005819
Evan Chengace3c172008-07-22 21:13:36 +00005820 for (unsigned i = 0; i != 4; ++i) {
5821 if (Locs[i].first == -1)
5822 continue;
5823 else {
5824 unsigned Idx = (i < 2) ? 0 : 4;
5825 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005826 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005827 }
5828 }
5829
Nate Begeman9008ca62009-04-27 18:41:29 +00005830 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005831 } else if (NumLo == 3 || NumHi == 3) {
5832 // Otherwise, we must have three elements from one vector, call it X, and
5833 // one element from the other, call it Y. First, use a shufps to build an
5834 // intermediate vector with the one element from Y and the element from X
5835 // that will be in the same half in the final destination (the indexes don't
5836 // matter). Then, use a shufps to build the final vector, taking the half
5837 // containing the element from Y from the intermediate, and the other half
5838 // from X.
5839 if (NumHi == 3) {
5840 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00005841 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005842 std::swap(V1, V2);
5843 }
5844
5845 // Find the element from V2.
5846 unsigned HiIndex;
5847 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005848 int Val = PermMask[HiIndex];
5849 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005850 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005851 if (Val >= 4)
5852 break;
5853 }
5854
Nate Begeman9008ca62009-04-27 18:41:29 +00005855 Mask1[0] = PermMask[HiIndex];
5856 Mask1[1] = -1;
5857 Mask1[2] = PermMask[HiIndex^1];
5858 Mask1[3] = -1;
5859 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005860
5861 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005862 Mask1[0] = PermMask[0];
5863 Mask1[1] = PermMask[1];
5864 Mask1[2] = HiIndex & 1 ? 6 : 4;
5865 Mask1[3] = HiIndex & 1 ? 4 : 6;
5866 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005867 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005868 Mask1[0] = HiIndex & 1 ? 2 : 0;
5869 Mask1[1] = HiIndex & 1 ? 0 : 2;
5870 Mask1[2] = PermMask[2];
5871 Mask1[3] = PermMask[3];
5872 if (Mask1[2] >= 0)
5873 Mask1[2] += 4;
5874 if (Mask1[3] >= 0)
5875 Mask1[3] += 4;
5876 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005877 }
Evan Chengace3c172008-07-22 21:13:36 +00005878 }
5879
5880 // Break it into (shuffle shuffle_hi, shuffle_lo).
5881 Locs.clear();
David Greenec4db4e52011-02-28 19:06:56 +00005882 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00005883 SmallVector<int,8> LoMask(4U, -1);
5884 SmallVector<int,8> HiMask(4U, -1);
5885
5886 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00005887 unsigned MaskIdx = 0;
5888 unsigned LoIdx = 0;
5889 unsigned HiIdx = 2;
5890 for (unsigned i = 0; i != 4; ++i) {
5891 if (i == 2) {
5892 MaskPtr = &HiMask;
5893 MaskIdx = 1;
5894 LoIdx = 0;
5895 HiIdx = 2;
5896 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005897 int Idx = PermMask[i];
5898 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005899 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00005900 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005901 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005902 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005903 LoIdx++;
5904 } else {
5905 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005906 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005907 HiIdx++;
5908 }
5909 }
5910
Nate Begeman9008ca62009-04-27 18:41:29 +00005911 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
5912 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
5913 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00005914 for (unsigned i = 0; i != 4; ++i) {
5915 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005916 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00005917 } else {
5918 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005919 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00005920 }
5921 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005922 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00005923}
5924
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005925static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005926 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005927 V = V.getOperand(0);
5928 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5929 V = V.getOperand(0);
5930 if (MayFoldLoad(V))
5931 return true;
5932 return false;
5933}
5934
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005935// FIXME: the version above should always be used. Since there's
5936// a bug where several vector shuffles can't be folded because the
5937// DAG is not updated during lowering and a node claims to have two
5938// uses while it only has one, use this version, and let isel match
5939// another instruction if the load really happens to have more than
5940// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00005941// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005942static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005943 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005944 V = V.getOperand(0);
5945 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5946 V = V.getOperand(0);
5947 if (ISD::isNormalLoad(V.getNode()))
5948 return true;
5949 return false;
5950}
5951
5952/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
5953/// a vector extract, and if both can be later optimized into a single load.
5954/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
5955/// here because otherwise a target specific shuffle node is going to be
5956/// emitted for this shuffle, and the optimization not done.
5957/// FIXME: This is probably not the best approach, but fix the problem
5958/// until the right path is decided.
5959static
5960bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
5961 const TargetLowering &TLI) {
5962 EVT VT = V.getValueType();
5963 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
5964
5965 // Be sure that the vector shuffle is present in a pattern like this:
5966 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
5967 if (!V.hasOneUse())
5968 return false;
5969
5970 SDNode *N = *V.getNode()->use_begin();
5971 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
5972 return false;
5973
5974 SDValue EltNo = N->getOperand(1);
5975 if (!isa<ConstantSDNode>(EltNo))
5976 return false;
5977
5978 // If the bit convert changed the number of elements, it is unsafe
5979 // to examine the mask.
5980 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005981 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005982 EVT SrcVT = V.getOperand(0).getValueType();
5983 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
5984 return false;
5985 V = V.getOperand(0);
5986 HasShuffleIntoBitcast = true;
5987 }
5988
5989 // Select the input vector, guarding against out of range extract vector.
5990 unsigned NumElems = VT.getVectorNumElements();
5991 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
5992 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
5993 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
5994
5995 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005996 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005997 V = V.getOperand(0);
5998
5999 if (ISD::isNormalLoad(V.getNode())) {
6000 // Is the original load suitable?
6001 LoadSDNode *LN0 = cast<LoadSDNode>(V);
6002
6003 // FIXME: avoid the multi-use bug that is preventing lots of
6004 // of foldings to be detected, this is still wrong of course, but
6005 // give the temporary desired behavior, and if it happens that
6006 // the load has real more uses, during isel it will not fold, and
6007 // will generate poor code.
6008 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
6009 return false;
6010
6011 if (!HasShuffleIntoBitcast)
6012 return true;
6013
6014 // If there's a bitcast before the shuffle, check if the load type and
6015 // alignment is valid.
6016 unsigned Align = LN0->getAlignment();
6017 unsigned NewAlign =
6018 TLI.getTargetData()->getABITypeAlignment(
6019 VT.getTypeForEVT(*DAG.getContext()));
6020
6021 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
6022 return false;
6023 }
6024
6025 return true;
6026}
6027
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006028static
Evan Cheng835580f2010-10-07 20:50:20 +00006029SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
6030 EVT VT = Op.getValueType();
6031
6032 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006033 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6034 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006035 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6036 V1, DAG));
6037}
6038
6039static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006040SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
6041 bool HasSSE2) {
6042 SDValue V1 = Op.getOperand(0);
6043 SDValue V2 = Op.getOperand(1);
6044 EVT VT = Op.getValueType();
6045
6046 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6047
6048 if (HasSSE2 && VT == MVT::v2f64)
6049 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6050
6051 // v4f32 or v4i32
6052 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V2, DAG);
6053}
6054
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006055static
6056SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
6057 SDValue V1 = Op.getOperand(0);
6058 SDValue V2 = Op.getOperand(1);
6059 EVT VT = Op.getValueType();
6060
6061 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6062 "unsupported shuffle type");
6063
6064 if (V2.getOpcode() == ISD::UNDEF)
6065 V2 = V1;
6066
6067 // v4i32 or v4f32
6068 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6069}
6070
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006071static
6072SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
6073 SDValue V1 = Op.getOperand(0);
6074 SDValue V2 = Op.getOperand(1);
6075 EVT VT = Op.getValueType();
6076 unsigned NumElems = VT.getVectorNumElements();
6077
6078 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6079 // operand of these instructions is only memory, so check if there's a
6080 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6081 // same masks.
6082 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006083
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006084 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006085 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006086 CanFoldLoad = true;
6087
6088 // When V1 is a load, it can be folded later into a store in isel, example:
6089 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6090 // turns into:
6091 // (MOVLPSmr addr:$src1, VR128:$src2)
6092 // So, recognize this potential and also use MOVLPS or MOVLPD
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006093 if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006094 CanFoldLoad = true;
6095
Eric Christopher893a8822011-02-20 05:04:42 +00006096 // Both of them can't be memory operations though.
6097 if (MayFoldVectorLoad(V1) && MayFoldVectorLoad(V2))
6098 CanFoldLoad = false;
Owen Anderson95771af2011-02-25 21:41:48 +00006099
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006100 if (CanFoldLoad) {
6101 if (HasSSE2 && NumElems == 2)
6102 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6103
6104 if (NumElems == 4)
6105 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
6106 }
6107
6108 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6109 // movl and movlp will both match v2i64, but v2i64 is never matched by
6110 // movl earlier because we make it strict to avoid messing with the movlp load
6111 // folding logic (see the code above getMOVLP call). Match it here then,
6112 // this is horrible, but will stay like this until we move all shuffle
6113 // matching to x86 specific nodes. Note that for the 1st condition all
6114 // types are matched with movsd.
6115 if ((HasSSE2 && NumElems == 2) || !X86::isMOVLMask(SVOp))
6116 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6117 else if (HasSSE2)
6118 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6119
6120
6121 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6122
6123 // Invert the operand order and use SHUFPS to match it.
6124 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V2, V1,
6125 X86::getShuffleSHUFImmediate(SVOp), DAG);
6126}
6127
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006128static inline unsigned getUNPCKLOpcode(EVT VT) {
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006129 switch(VT.getSimpleVT().SimpleTy) {
6130 case MVT::v4i32: return X86ISD::PUNPCKLDQ;
6131 case MVT::v2i64: return X86ISD::PUNPCKLQDQ;
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00006132 case MVT::v4f32: return X86ISD::UNPCKLPS;
6133 case MVT::v2f64: return X86ISD::UNPCKLPD;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006134 case MVT::v8i32: // Use fp unit for int unpack.
David Greenec4db4e52011-02-28 19:06:56 +00006135 case MVT::v8f32: return X86ISD::VUNPCKLPSY;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006136 case MVT::v4i64: // Use fp unit for int unpack.
David Greenec4db4e52011-02-28 19:06:56 +00006137 case MVT::v4f64: return X86ISD::VUNPCKLPDY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006138 case MVT::v16i8: return X86ISD::PUNPCKLBW;
6139 case MVT::v8i16: return X86ISD::PUNPCKLWD;
6140 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00006141 llvm_unreachable("Unknown type for unpckl");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006142 }
6143 return 0;
6144}
6145
6146static inline unsigned getUNPCKHOpcode(EVT VT) {
6147 switch(VT.getSimpleVT().SimpleTy) {
6148 case MVT::v4i32: return X86ISD::PUNPCKHDQ;
6149 case MVT::v2i64: return X86ISD::PUNPCKHQDQ;
6150 case MVT::v4f32: return X86ISD::UNPCKHPS;
6151 case MVT::v2f64: return X86ISD::UNPCKHPD;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006152 case MVT::v8i32: // Use fp unit for int unpack.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00006153 case MVT::v8f32: return X86ISD::VUNPCKHPSY;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006154 case MVT::v4i64: // Use fp unit for int unpack.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00006155 case MVT::v4f64: return X86ISD::VUNPCKHPDY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006156 case MVT::v16i8: return X86ISD::PUNPCKHBW;
6157 case MVT::v8i16: return X86ISD::PUNPCKHWD;
6158 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00006159 llvm_unreachable("Unknown type for unpckh");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006160 }
6161 return 0;
6162}
6163
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006164static inline unsigned getVPERMILOpcode(EVT VT) {
6165 switch(VT.getSimpleVT().SimpleTy) {
6166 case MVT::v4i32:
6167 case MVT::v4f32: return X86ISD::VPERMILPS;
6168 case MVT::v2i64:
6169 case MVT::v2f64: return X86ISD::VPERMILPD;
6170 case MVT::v8i32:
6171 case MVT::v8f32: return X86ISD::VPERMILPSY;
6172 case MVT::v4i64:
6173 case MVT::v4f64: return X86ISD::VPERMILPDY;
6174 default:
6175 llvm_unreachable("Unknown type for vpermil");
6176 }
6177 return 0;
6178}
6179
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006180/// isVectorBroadcast - Check if the node chain is suitable to be xformed to
6181/// a vbroadcast node. The nodes are suitable whenever we can fold a load coming
6182/// from a 32 or 64 bit scalar. Update Op to the desired load to be folded.
6183static bool isVectorBroadcast(SDValue &Op) {
6184 EVT VT = Op.getValueType();
6185 bool Is256 = VT.getSizeInBits() == 256;
6186
6187 assert((VT.getSizeInBits() == 128 || Is256) &&
6188 "Unsupported type for vbroadcast node");
6189
6190 SDValue V = Op;
6191 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
6192 V = V.getOperand(0);
6193
6194 if (Is256 && !(V.hasOneUse() &&
6195 V.getOpcode() == ISD::INSERT_SUBVECTOR &&
6196 V.getOperand(0).getOpcode() == ISD::UNDEF))
6197 return false;
6198
6199 if (Is256)
6200 V = V.getOperand(1);
6201 if (V.hasOneUse() && V.getOpcode() != ISD::SCALAR_TO_VECTOR)
6202 return false;
6203
6204 // Check the source scalar_to_vector type. 256-bit broadcasts are
6205 // supported for 32/64-bit sizes, while 128-bit ones are only supported
6206 // for 32-bit scalars.
6207 unsigned ScalarSize = V.getOperand(0).getValueType().getSizeInBits();
6208 if (ScalarSize != 32 && ScalarSize != 64)
6209 return false;
6210 if (!Is256 && ScalarSize == 64)
6211 return false;
6212
6213 V = V.getOperand(0);
6214 if (!MayFoldLoad(V))
6215 return false;
6216
6217 // Return the load node
6218 Op = V;
6219 return true;
6220}
6221
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006222static
6223SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006224 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006225 const X86Subtarget *Subtarget) {
6226 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6227 EVT VT = Op.getValueType();
6228 DebugLoc dl = Op.getDebugLoc();
6229 SDValue V1 = Op.getOperand(0);
6230 SDValue V2 = Op.getOperand(1);
6231
6232 if (isZeroShuffle(SVOp))
6233 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
6234
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006235 // Handle splat operations
6236 if (SVOp->isSplat()) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006237 unsigned NumElem = VT.getVectorNumElements();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006238 int Size = VT.getSizeInBits();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006239 // Special case, this is the only place now where it's allowed to return
6240 // a vector_shuffle operation without using a target specific node, because
6241 // *hopefully* it will be optimized away by the dag combiner. FIXME: should
6242 // this be moved to DAGCombine instead?
6243 if (NumElem <= 4 && CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006244 return Op;
6245
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006246 // Use vbroadcast whenever the splat comes from a foldable load
6247 if (Subtarget->hasAVX() && isVectorBroadcast(V1))
6248 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, V1);
6249
Bruno Cardoso Lopes6a32adc2011-07-25 23:05:25 +00006250 // Handle splats by matching through known shuffle masks
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006251 if ((Size == 128 && NumElem <= 4) ||
6252 (Size == 256 && NumElem < 8))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006253 return SDValue();
6254
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00006255 // All remaning splats are promoted to target supported vector shuffles.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006256 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006257 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006258
6259 // If the shuffle can be profitably rewritten as a narrower shuffle, then
6260 // do it!
6261 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
6262 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6263 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006264 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006265 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
6266 // FIXME: Figure out a cleaner way to do this.
6267 // Try to make use of movq to zero out the top part.
6268 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
6269 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6270 if (NewOp.getNode()) {
6271 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
6272 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
6273 DAG, Subtarget, dl);
6274 }
6275 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
6276 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6277 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
6278 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
6279 DAG, Subtarget, dl);
6280 }
6281 }
6282 return SDValue();
6283}
6284
Dan Gohman475871a2008-07-27 21:46:04 +00006285SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006286X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00006287 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00006288 SDValue V1 = Op.getOperand(0);
6289 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006290 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006291 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006292 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006293 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006294 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
6295 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00006296 bool V1IsSplat = false;
6297 bool V2IsSplat = false;
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006298 bool HasSSE2 = Subtarget->hasSSE2() || Subtarget->hasAVX();
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006299 bool HasSSE3 = Subtarget->hasSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006300 bool HasSSSE3 = Subtarget->hasSSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006301 MachineFunction &MF = DAG.getMachineFunction();
6302 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006303
Dale Johannesen0488fb62010-09-30 23:57:10 +00006304 // Shuffle operations on MMX not supported.
6305 if (isMMX)
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00006306 return Op;
6307
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006308 // Vector shuffle lowering takes 3 steps:
6309 //
6310 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
6311 // narrowing and commutation of operands should be handled.
6312 // 2) Matching of shuffles with known shuffle masks to x86 target specific
6313 // shuffle nodes.
6314 // 3) Rewriting of unmatched masks into new generic shuffle operations,
6315 // so the shuffle can be broken into other shuffles and the legalizer can
6316 // try the lowering again.
6317 //
6318 // The general ideia is that no vector_shuffle operation should be left to
6319 // be matched during isel, all of them must be converted to a target specific
6320 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00006321
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006322 // Normalize the input vectors. Here splats, zeroed vectors, profitable
6323 // narrowing and commutation of operands should be handled. The actual code
6324 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006325 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006326 if (NewOp.getNode())
6327 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00006328
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006329 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
6330 // unpckh_undef). Only use pshufd if speed is more important than size.
6331 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006332 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006333 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
Rafael Espindola23e31012011-07-22 18:56:05 +00006334 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00006335
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006336 if (X86::isMOVDDUPMask(SVOp) && HasSSE3 && V2IsUndef &&
Dale Johannesen0488fb62010-09-30 23:57:10 +00006337 RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00006338 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006339
Dale Johannesen0488fb62010-09-30 23:57:10 +00006340 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006341 return getMOVHighToLow(Op, dl, DAG);
6342
6343 // Use to match splats
6344 if (HasSSE2 && X86::isUNPCKHMask(SVOp) && V2IsUndef &&
6345 (VT == MVT::v2f64 || VT == MVT::v2i64))
6346 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
6347
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006348 if (X86::isPSHUFDMask(SVOp)) {
6349 // The actual implementation will match the mask in the if above and then
6350 // during isel it can match several different instructions, not only pshufd
6351 // as its name says, sad but true, emulate the behavior for now...
6352 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
6353 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
6354
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006355 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
6356
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006357 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006358 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
6359
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006360 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006361 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V1,
6362 TargetMask, DAG);
6363
6364 if (VT == MVT::v4f32)
6365 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V1,
6366 TargetMask, DAG);
6367 }
Eric Christopherfd179292009-08-27 18:07:15 +00006368
Evan Chengf26ffe92008-05-29 08:22:04 +00006369 // Check if this can be converted into a logical shift.
6370 bool isLeft = false;
6371 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00006372 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00006373 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00006374 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00006375 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006376 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00006377 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006378 EVT EltVT = VT.getVectorElementType();
6379 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006380 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006381 }
Eric Christopherfd179292009-08-27 18:07:15 +00006382
Nate Begeman9008ca62009-04-27 18:41:29 +00006383 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006384 if (V1IsUndef)
6385 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00006386 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00006387 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00006388 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006389 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006390 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6391
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006392 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006393 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6394 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00006395 }
Eric Christopherfd179292009-08-27 18:07:15 +00006396
Nate Begeman9008ca62009-04-27 18:41:29 +00006397 // FIXME: fold these into legal mask.
Dale Johannesen0488fb62010-09-30 23:57:10 +00006398 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp))
6399 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006400
Dale Johannesen0488fb62010-09-30 23:57:10 +00006401 if (X86::isMOVHLPSMask(SVOp))
6402 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006403
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00006404 if (X86::isMOVSHDUPMask(SVOp, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006405 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006406
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00006407 if (X86::isMOVSLDUPMask(SVOp, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006408 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00006409
Dale Johannesen0488fb62010-09-30 23:57:10 +00006410 if (X86::isMOVLPMask(SVOp))
6411 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006412
Nate Begeman9008ca62009-04-27 18:41:29 +00006413 if (ShouldXformToMOVHLPS(SVOp) ||
6414 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
6415 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006416
Evan Chengf26ffe92008-05-29 08:22:04 +00006417 if (isShift) {
6418 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006419 EVT EltVT = VT.getVectorElementType();
6420 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006421 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006422 }
Eric Christopherfd179292009-08-27 18:07:15 +00006423
Evan Cheng9eca5e82006-10-25 21:49:50 +00006424 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00006425 // FIXME: This should also accept a bitcast of a splat? Be careful, not
6426 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00006427 V1IsSplat = isSplatVector(V1.getNode());
6428 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006429
Chris Lattner8a594482007-11-25 00:24:49 +00006430 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00006431 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006432 Op = CommuteVectorShuffle(SVOp, DAG);
6433 SVOp = cast<ShuffleVectorSDNode>(Op);
6434 V1 = SVOp->getOperand(0);
6435 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00006436 std::swap(V1IsSplat, V2IsSplat);
6437 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006438 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00006439 }
6440
Nate Begeman9008ca62009-04-27 18:41:29 +00006441 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
6442 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00006443 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00006444 return V1;
6445 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6446 // the instruction selector will not match, so get a canonical MOVL with
6447 // swapped operands to undo the commute.
6448 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006449 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006450
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006451 if (X86::isUNPCKLMask(SVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006452 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006453
6454 if (X86::isUNPCKHMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006455 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006456
Evan Cheng9bbbb982006-10-25 20:48:19 +00006457 if (V2IsSplat) {
6458 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006459 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00006460 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00006461 SDValue NewMask = NormalizeMask(SVOp, DAG);
6462 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
6463 if (NSVOp != SVOp) {
6464 if (X86::isUNPCKLMask(NSVOp, true)) {
6465 return NewMask;
6466 } else if (X86::isUNPCKHMask(NSVOp, true)) {
6467 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006468 }
6469 }
6470 }
6471
Evan Cheng9eca5e82006-10-25 21:49:50 +00006472 if (Commuted) {
6473 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006474 // FIXME: this seems wrong.
6475 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
6476 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006477
6478 if (X86::isUNPCKLMask(NewSVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006479 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V2, V1, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006480
6481 if (X86::isUNPCKHMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006482 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V2, V1, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006483 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006484
Nate Begeman9008ca62009-04-27 18:41:29 +00006485 // Normalize the node to match x86 shuffle ops if needed
Dale Johannesen0488fb62010-09-30 23:57:10 +00006486 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
Nate Begeman9008ca62009-04-27 18:41:29 +00006487 return CommuteVectorShuffle(SVOp, DAG);
6488
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006489 // The checks below are all present in isShuffleMaskLegal, but they are
6490 // inlined here right now to enable us to directly emit target specific
6491 // nodes, and remove one by one until they don't return Op anymore.
6492 SmallVector<int, 16> M;
6493 SVOp->getMask(M);
6494
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006495 if (isPALIGNRMask(M, VT, HasSSSE3))
6496 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
6497 X86::getShufflePALIGNRImmediate(SVOp),
6498 DAG);
6499
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006500 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6501 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00006502 if (VT == MVT::v2f64)
6503 return getTargetShuffleNode(X86ISD::UNPCKLPD, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006504 if (VT == MVT::v2i64)
6505 return getTargetShuffleNode(X86ISD::PUNPCKLQDQ, dl, VT, V1, V1, DAG);
6506 }
6507
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006508 if (isPSHUFHWMask(M, VT))
6509 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
6510 X86::getShufflePSHUFHWImmediate(SVOp),
6511 DAG);
6512
6513 if (isPSHUFLWMask(M, VT))
6514 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
6515 X86::getShufflePSHUFLWImmediate(SVOp),
6516 DAG);
6517
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00006518 if (isSHUFPMask(M, VT)) {
6519 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
6520 if (VT == MVT::v4f32 || VT == MVT::v4i32)
6521 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V2,
6522 TargetMask, DAG);
6523 if (VT == MVT::v2f64 || VT == MVT::v2i64)
6524 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V2,
6525 TargetMask, DAG);
6526 }
6527
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006528 if (X86::isUNPCKL_v_undef_Mask(SVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006529 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006530 if (X86::isUNPCKH_v_undef_Mask(SVOp))
Rafael Espindola23e31012011-07-22 18:56:05 +00006531 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006532
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006533 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006534 // Generate target specific nodes for 128 or 256-bit shuffles only
6535 // supported in the AVX instruction set.
6536 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006537
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006538 // Handle VPERMILPS* permutations
6539 if (isVPERMILPSMask(M, VT, Subtarget))
6540 return getTargetShuffleNode(getVPERMILOpcode(VT), dl, VT, V1,
6541 getShuffleVPERMILPSImmediate(SVOp), DAG);
6542
6543 // Handle VPERMILPD* permutations
6544 if (isVPERMILPDMask(M, VT, Subtarget))
6545 return getTargetShuffleNode(getVPERMILOpcode(VT), dl, VT, V1,
6546 getShuffleVPERMILPDImmediate(SVOp), DAG);
6547
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00006548 // Handle VPERM2F128 permutations
6549 if (isVPERM2F128Mask(M, VT, Subtarget))
6550 return getTargetShuffleNode(X86ISD::VPERM2F128, dl, VT, V1, V2,
6551 getShuffleVPERM2F128Immediate(SVOp), DAG);
6552
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006553 //===--------------------------------------------------------------------===//
6554 // Since no target specific shuffle was selected for this generic one,
6555 // lower it into other known shuffles. FIXME: this isn't true yet, but
6556 // this is the plan.
6557 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00006558
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006559 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
6560 if (VT == MVT::v8i16) {
6561 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
6562 if (NewOp.getNode())
6563 return NewOp;
6564 }
6565
6566 if (VT == MVT::v16i8) {
6567 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
6568 if (NewOp.getNode())
6569 return NewOp;
6570 }
6571
6572 // Handle all 128-bit wide vectors with 4 elements, and match them with
6573 // several different shuffle types.
6574 if (NumElems == 4 && VT.getSizeInBits() == 128)
6575 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
6576
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006577 // Handle general 256-bit shuffles
6578 if (VT.is256BitVector())
6579 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
6580
Dan Gohman475871a2008-07-27 21:46:04 +00006581 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006582}
6583
Dan Gohman475871a2008-07-27 21:46:04 +00006584SDValue
6585X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00006586 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006587 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006588 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006589
6590 if (Op.getOperand(0).getValueType().getSizeInBits() != 128)
6591 return SDValue();
6592
Duncan Sands83ec4b62008-06-06 12:08:01 +00006593 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006594 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006595 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006596 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006597 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006598 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006599 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00006600 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
6601 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
6602 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006603 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6604 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006605 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006606 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00006607 Op.getOperand(0)),
6608 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006609 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006610 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006611 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006612 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006613 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00006614 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00006615 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
6616 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006617 // result has a single use which is a store or a bitcast to i32. And in
6618 // the case of a store, it's not worth it if the index is a constant 0,
6619 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00006620 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00006621 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00006622 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006623 if ((User->getOpcode() != ISD::STORE ||
6624 (isa<ConstantSDNode>(Op.getOperand(1)) &&
6625 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006626 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00006627 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00006628 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00006629 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006630 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00006631 Op.getOperand(0)),
6632 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006633 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Owen Anderson825b72b2009-08-11 20:47:22 +00006634 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00006635 // ExtractPS works with constant index.
6636 if (isa<ConstantSDNode>(Op.getOperand(1)))
6637 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006638 }
Dan Gohman475871a2008-07-27 21:46:04 +00006639 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006640}
6641
6642
Dan Gohman475871a2008-07-27 21:46:04 +00006643SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006644X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
6645 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006646 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00006647 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006648
David Greene74a579d2011-02-10 16:57:36 +00006649 SDValue Vec = Op.getOperand(0);
6650 EVT VecVT = Vec.getValueType();
6651
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006652 // If this is a 256-bit vector result, first extract the 128-bit vector and
6653 // then extract the element from the 128-bit vector.
6654 if (VecVT.getSizeInBits() == 256) {
David Greene74a579d2011-02-10 16:57:36 +00006655 DebugLoc dl = Op.getNode()->getDebugLoc();
6656 unsigned NumElems = VecVT.getVectorNumElements();
6657 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00006658 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
6659
6660 // Get the 128-bit vector.
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006661 bool Upper = IdxVal >= NumElems/2;
6662 Vec = Extract128BitVector(Vec,
6663 DAG.getConstant(Upper ? NumElems/2 : 0, MVT::i32), DAG, dl);
David Greene74a579d2011-02-10 16:57:36 +00006664
David Greene74a579d2011-02-10 16:57:36 +00006665 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006666 Upper ? DAG.getConstant(IdxVal-NumElems/2, MVT::i32) : Idx);
David Greene74a579d2011-02-10 16:57:36 +00006667 }
6668
6669 assert(Vec.getValueSizeInBits() <= 128 && "Unexpected vector length");
6670
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006671 if (Subtarget->hasSSE41() || Subtarget->hasAVX()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006672 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006673 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00006674 return Res;
6675 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00006676
Owen Andersone50ed302009-08-10 22:56:29 +00006677 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006678 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006679 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006680 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00006681 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006682 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006683 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006684 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6685 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006686 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006687 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00006688 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006689 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00006690 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00006691 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006692 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00006693 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006694 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006695 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006696 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006697 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006698 if (Idx == 0)
6699 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00006700
Evan Cheng0db9fe62006-04-25 20:13:52 +00006701 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00006702 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006703 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006704 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006705 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006706 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006707 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00006708 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006709 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
6710 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
6711 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006712 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006713 if (Idx == 0)
6714 return Op;
6715
6716 // UNPCKHPD the element to the lowest double word, then movsd.
6717 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
6718 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00006719 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006720 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006721 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006722 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006723 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006724 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006725 }
6726
Dan Gohman475871a2008-07-27 21:46:04 +00006727 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006728}
6729
Dan Gohman475871a2008-07-27 21:46:04 +00006730SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006731X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
6732 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006733 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006734 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006735 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006736
Dan Gohman475871a2008-07-27 21:46:04 +00006737 SDValue N0 = Op.getOperand(0);
6738 SDValue N1 = Op.getOperand(1);
6739 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00006740
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006741 if (VT.getSizeInBits() == 256)
6742 return SDValue();
6743
Dan Gohman8a55ce42009-09-23 21:02:20 +00006744 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00006745 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006746 unsigned Opc;
6747 if (VT == MVT::v8i16)
6748 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006749 else if (VT == MVT::v16i8)
6750 Opc = X86ISD::PINSRB;
6751 else
6752 Opc = X86ISD::PINSRB;
6753
Nate Begeman14d12ca2008-02-11 04:19:36 +00006754 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
6755 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006756 if (N1.getValueType() != MVT::i32)
6757 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6758 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006759 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00006760 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006761 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006762 // Bits [7:6] of the constant are the source select. This will always be
6763 // zero here. The DAG Combiner may combine an extract_elt index into these
6764 // bits. For example (insert (extract, 3), 2) could be matched by putting
6765 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00006766 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00006767 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00006768 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00006769 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006770 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00006771 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00006772 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00006773 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006774 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00006775 // PINSR* works with constant index.
6776 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006777 }
Dan Gohman475871a2008-07-27 21:46:04 +00006778 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006779}
6780
Dan Gohman475871a2008-07-27 21:46:04 +00006781SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006782X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006783 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006784 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006785
David Greene6b381262011-02-09 15:32:06 +00006786 DebugLoc dl = Op.getDebugLoc();
6787 SDValue N0 = Op.getOperand(0);
6788 SDValue N1 = Op.getOperand(1);
6789 SDValue N2 = Op.getOperand(2);
6790
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006791 // If this is a 256-bit vector result, first extract the 128-bit vector,
6792 // insert the element into the extracted half and then place it back.
6793 if (VT.getSizeInBits() == 256) {
David Greene6b381262011-02-09 15:32:06 +00006794 if (!isa<ConstantSDNode>(N2))
6795 return SDValue();
6796
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006797 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00006798 unsigned NumElems = VT.getVectorNumElements();
6799 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006800 bool Upper = IdxVal >= NumElems/2;
6801 SDValue Ins128Idx = DAG.getConstant(Upper ? NumElems/2 : 0, MVT::i32);
6802 SDValue V = Extract128BitVector(N0, Ins128Idx, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00006803
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006804 // Insert the element into the desired half.
6805 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V,
6806 N1, Upper ? DAG.getConstant(IdxVal-NumElems/2, MVT::i32) : N2);
David Greene6b381262011-02-09 15:32:06 +00006807
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006808 // Insert the changed part back to the 256-bit vector
6809 return Insert128BitVector(N0, V, Ins128Idx, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00006810 }
6811
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006812 if (Subtarget->hasSSE41() || Subtarget->hasAVX())
Nate Begeman14d12ca2008-02-11 04:19:36 +00006813 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
6814
Dan Gohman8a55ce42009-09-23 21:02:20 +00006815 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00006816 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00006817
Dan Gohman8a55ce42009-09-23 21:02:20 +00006818 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00006819 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
6820 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006821 if (N1.getValueType() != MVT::i32)
6822 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6823 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006824 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00006825 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006826 }
Dan Gohman475871a2008-07-27 21:46:04 +00006827 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006828}
6829
Dan Gohman475871a2008-07-27 21:46:04 +00006830SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006831X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00006832 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006833 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00006834 EVT OpVT = Op.getValueType();
6835
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00006836 // If this is a 256-bit vector result, first insert into a 128-bit
6837 // vector and then insert into the 256-bit vector.
6838 if (OpVT.getSizeInBits() > 128) {
6839 // Insert into a 128-bit vector.
6840 EVT VT128 = EVT::getVectorVT(*Context,
6841 OpVT.getVectorElementType(),
6842 OpVT.getVectorNumElements() / 2);
6843
6844 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
6845
6846 // Insert the 128-bit vector.
6847 return Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, OpVT), Op,
6848 DAG.getConstant(0, MVT::i32),
6849 DAG, dl);
6850 }
6851
Chris Lattnerf172ecd2010-07-04 23:07:25 +00006852 if (Op.getValueType() == MVT::v1i64 &&
6853 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00006854 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00006855
Owen Anderson825b72b2009-08-11 20:47:22 +00006856 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00006857 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
6858 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006859 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00006860 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006861}
6862
David Greene91585092011-01-26 15:38:49 +00006863// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
6864// a simple subregister reference or explicit instructions to grab
6865// upper bits of a vector.
6866SDValue
6867X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
6868 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00006869 DebugLoc dl = Op.getNode()->getDebugLoc();
6870 SDValue Vec = Op.getNode()->getOperand(0);
6871 SDValue Idx = Op.getNode()->getOperand(1);
6872
6873 if (Op.getNode()->getValueType(0).getSizeInBits() == 128
6874 && Vec.getNode()->getValueType(0).getSizeInBits() == 256) {
6875 return Extract128BitVector(Vec, Idx, DAG, dl);
6876 }
David Greene91585092011-01-26 15:38:49 +00006877 }
6878 return SDValue();
6879}
6880
David Greenecfe33c42011-01-26 19:13:22 +00006881// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
6882// simple superregister reference or explicit instructions to insert
6883// the upper bits of a vector.
6884SDValue
6885X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
6886 if (Subtarget->hasAVX()) {
6887 DebugLoc dl = Op.getNode()->getDebugLoc();
6888 SDValue Vec = Op.getNode()->getOperand(0);
6889 SDValue SubVec = Op.getNode()->getOperand(1);
6890 SDValue Idx = Op.getNode()->getOperand(2);
6891
6892 if (Op.getNode()->getValueType(0).getSizeInBits() == 256
6893 && SubVec.getNode()->getValueType(0).getSizeInBits() == 128) {
David Greenea5f26012011-02-07 19:36:54 +00006894 return Insert128BitVector(Vec, SubVec, Idx, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00006895 }
6896 }
6897 return SDValue();
6898}
6899
Bill Wendling056292f2008-09-16 21:48:12 +00006900// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
6901// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
6902// one of the above mentioned nodes. It has to be wrapped because otherwise
6903// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
6904// be used to form addressing mode. These wrapped nodes will be selected
6905// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00006906SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006907X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006908 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00006909
Chris Lattner41621a22009-06-26 19:22:52 +00006910 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6911 // global base reg.
6912 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006913 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006914 CodeModel::Model M = getTargetMachine().getCodeModel();
6915
Chris Lattner4f066492009-07-11 20:29:19 +00006916 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006917 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006918 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006919 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006920 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006921 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006922 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006923
Evan Cheng1606e8e2009-03-13 07:51:59 +00006924 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00006925 CP->getAlignment(),
6926 CP->getOffset(), OpFlag);
6927 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00006928 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006929 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00006930 if (OpFlag) {
6931 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00006932 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006933 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006934 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006935 }
6936
6937 return Result;
6938}
6939
Dan Gohmand858e902010-04-17 15:26:15 +00006940SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00006941 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00006942
Chris Lattner18c59872009-06-27 04:16:01 +00006943 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6944 // global base reg.
6945 unsigned char OpFlag = 0;
6946 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006947 CodeModel::Model M = getTargetMachine().getCodeModel();
6948
Chris Lattner4f066492009-07-11 20:29:19 +00006949 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006950 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006951 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006952 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006953 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006954 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006955 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006956
Chris Lattner18c59872009-06-27 04:16:01 +00006957 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
6958 OpFlag);
6959 DebugLoc DL = JT->getDebugLoc();
6960 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006961
Chris Lattner18c59872009-06-27 04:16:01 +00006962 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00006963 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00006964 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6965 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006966 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00006967 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006968
Chris Lattner18c59872009-06-27 04:16:01 +00006969 return Result;
6970}
6971
6972SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006973X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00006974 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00006975
Chris Lattner18c59872009-06-27 04:16:01 +00006976 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6977 // global base reg.
6978 unsigned char OpFlag = 0;
6979 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006980 CodeModel::Model M = getTargetMachine().getCodeModel();
6981
Chris Lattner4f066492009-07-11 20:29:19 +00006982 if (Subtarget->isPICStyleRIPRel() &&
Eli Friedman586272d2011-08-11 01:48:05 +00006983 (M == CodeModel::Small || M == CodeModel::Kernel)) {
6984 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
6985 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00006986 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00006987 } else if (Subtarget->isPICStyleGOT()) {
6988 OpFlag = X86II::MO_GOT;
6989 } else if (Subtarget->isPICStyleStubPIC()) {
6990 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
6991 } else if (Subtarget->isPICStyleStubNoDynamic()) {
6992 OpFlag = X86II::MO_DARWIN_NONLAZY;
6993 }
Eric Christopherfd179292009-08-27 18:07:15 +00006994
Chris Lattner18c59872009-06-27 04:16:01 +00006995 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00006996
Chris Lattner18c59872009-06-27 04:16:01 +00006997 DebugLoc DL = Op.getDebugLoc();
6998 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006999
7000
Chris Lattner18c59872009-06-27 04:16:01 +00007001 // With PIC, the address is actually $g + Offset.
7002 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007003 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007004 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7005 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007006 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007007 Result);
7008 }
Eric Christopherfd179292009-08-27 18:07:15 +00007009
Eli Friedman586272d2011-08-11 01:48:05 +00007010 // For symbols that require a load from a stub to get the address, emit the
7011 // load.
7012 if (isGlobalStubReference(OpFlag))
7013 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
7014 MachinePointerInfo::getGOT(), false, false, 0);
7015
Chris Lattner18c59872009-06-27 04:16:01 +00007016 return Result;
7017}
7018
Dan Gohman475871a2008-07-27 21:46:04 +00007019SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007020X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007021 // Create the TargetBlockAddressAddress node.
7022 unsigned char OpFlags =
7023 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007024 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007025 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00007026 DebugLoc dl = Op.getDebugLoc();
7027 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
7028 /*isTarget=*/true, OpFlags);
7029
Dan Gohmanf705adb2009-10-30 01:28:02 +00007030 if (Subtarget->isPICStyleRIPRel() &&
7031 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007032 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7033 else
7034 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007035
Dan Gohman29cbade2009-11-20 23:18:13 +00007036 // With PIC, the address is actually $g + Offset.
7037 if (isGlobalRelativeToPICBase(OpFlags)) {
7038 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7039 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7040 Result);
7041 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007042
7043 return Result;
7044}
7045
7046SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00007047X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00007048 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00007049 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007050 // Create the TargetGlobalAddress node, folding in the constant
7051 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007052 unsigned char OpFlags =
7053 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007054 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007055 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007056 if (OpFlags == X86II::MO_NO_FLAG &&
7057 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007058 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007059 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007060 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007061 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007062 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007063 }
Eric Christopherfd179292009-08-27 18:07:15 +00007064
Chris Lattner4f066492009-07-11 20:29:19 +00007065 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007066 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007067 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7068 else
7069 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007070
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007071 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007072 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007073 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7074 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007075 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007076 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007077
Chris Lattner36c25012009-07-10 07:34:39 +00007078 // For globals that require a load from a stub to get the address, emit the
7079 // load.
7080 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007081 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00007082 MachinePointerInfo::getGOT(), false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007083
Dan Gohman6520e202008-10-18 02:06:02 +00007084 // If there was a non-zero offset that we didn't fold, create an explicit
7085 // addition for it.
7086 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007087 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007088 DAG.getConstant(Offset, getPointerTy()));
7089
Evan Cheng0db9fe62006-04-25 20:13:52 +00007090 return Result;
7091}
7092
Evan Chengda43bcf2008-09-24 00:05:32 +00007093SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007094X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007095 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007096 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007097 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007098}
7099
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007100static SDValue
7101GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007102 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00007103 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007104 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007105 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007106 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007107 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007108 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007109 GA->getOffset(),
7110 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007111 if (InFlag) {
7112 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00007113 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007114 } else {
7115 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00007116 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007117 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007118
7119 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007120 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007121
Rafael Espindola15f1b662009-04-24 12:59:40 +00007122 SDValue Flag = Chain.getValue(1);
7123 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007124}
7125
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007126// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007127static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007128LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007129 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007130 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00007131 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
7132 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007133 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007134 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007135 InFlag = Chain.getValue(1);
7136
Chris Lattnerb903bed2009-06-26 21:20:29 +00007137 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007138}
7139
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007140// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007141static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007142LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007143 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007144 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7145 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007146}
7147
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007148// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
7149// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007150static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007151 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00007152 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007153 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00007154
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007155 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
7156 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
7157 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00007158
Michael J. Spencerec38de22010-10-10 22:04:20 +00007159 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007160 DAG.getIntPtrConstant(0),
7161 MachinePointerInfo(Ptr), false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00007162
Chris Lattnerb903bed2009-06-26 21:20:29 +00007163 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007164 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
7165 // initialexec.
7166 unsigned WrapperKind = X86ISD::Wrapper;
7167 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007168 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00007169 } else if (is64Bit) {
7170 assert(model == TLSModel::InitialExec);
7171 OperandFlags = X86II::MO_GOTTPOFF;
7172 WrapperKind = X86ISD::WrapperRIP;
7173 } else {
7174 assert(model == TLSModel::InitialExec);
7175 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00007176 }
Eric Christopherfd179292009-08-27 18:07:15 +00007177
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007178 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
7179 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00007180 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00007181 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007182 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007183 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007184
Rafael Espindola9a580232009-02-27 13:37:18 +00007185 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007186 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00007187 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007188
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007189 // The address of the thread local variable is the add of the thread
7190 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00007191 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007192}
7193
Dan Gohman475871a2008-07-27 21:46:04 +00007194SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007195X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00007196
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007197 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00007198 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00007199
Eric Christopher30ef0e52010-06-03 04:07:48 +00007200 if (Subtarget->isTargetELF()) {
7201 // TODO: implement the "local dynamic" model
7202 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00007203
Eric Christopher30ef0e52010-06-03 04:07:48 +00007204 // If GV is an alias then use the aliasee for determining
7205 // thread-localness.
7206 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
7207 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007208
7209 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00007210 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00007211
Eric Christopher30ef0e52010-06-03 04:07:48 +00007212 switch (model) {
7213 case TLSModel::GeneralDynamic:
7214 case TLSModel::LocalDynamic: // not implemented
7215 if (Subtarget->is64Bit())
7216 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
7217 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00007218
Eric Christopher30ef0e52010-06-03 04:07:48 +00007219 case TLSModel::InitialExec:
7220 case TLSModel::LocalExec:
7221 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
7222 Subtarget->is64Bit());
7223 }
7224 } else if (Subtarget->isTargetDarwin()) {
7225 // Darwin only has one model of TLS. Lower to that.
7226 unsigned char OpFlag = 0;
7227 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7228 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007229
Eric Christopher30ef0e52010-06-03 04:07:48 +00007230 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7231 // global base reg.
7232 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7233 !Subtarget->is64Bit();
7234 if (PIC32)
7235 OpFlag = X86II::MO_TLVP_PIC_BASE;
7236 else
7237 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007238 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007239 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00007240 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007241 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007242 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007243
Eric Christopher30ef0e52010-06-03 04:07:48 +00007244 // With PIC32, the address is actually $g + Offset.
7245 if (PIC32)
7246 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7247 DAG.getNode(X86ISD::GlobalBaseReg,
7248 DebugLoc(), getPointerTy()),
7249 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007250
Eric Christopher30ef0e52010-06-03 04:07:48 +00007251 // Lowering the machine isd will make sure everything is in the right
7252 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007253 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007254 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00007255 SDValue Args[] = { Chain, Offset };
7256 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007257
Eric Christopher30ef0e52010-06-03 04:07:48 +00007258 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7259 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7260 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007261
Eric Christopher30ef0e52010-06-03 04:07:48 +00007262 // And our return value (tls address) is in the standard call return value
7263 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007264 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
7265 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007266 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007267
Eric Christopher30ef0e52010-06-03 04:07:48 +00007268 assert(false &&
7269 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00007270
Torok Edwinc23197a2009-07-14 16:55:14 +00007271 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00007272 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007273}
7274
Evan Cheng0db9fe62006-04-25 20:13:52 +00007275
Nadav Rotem43012222011-05-11 08:12:09 +00007276/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00007277/// take a 2 x i32 value to shift plus a shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +00007278SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00007279 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00007280 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007281 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007282 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007283 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00007284 SDValue ShOpLo = Op.getOperand(0);
7285 SDValue ShOpHi = Op.getOperand(1);
7286 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00007287 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00007288 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00007289 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00007290
Dan Gohman475871a2008-07-27 21:46:04 +00007291 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007292 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007293 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
7294 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007295 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007296 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
7297 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007298 }
Evan Chenge3413162006-01-09 18:33:28 +00007299
Owen Anderson825b72b2009-08-11 20:47:22 +00007300 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
7301 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00007302 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00007303 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00007304
Dan Gohman475871a2008-07-27 21:46:04 +00007305 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00007306 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00007307 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
7308 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00007309
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007310 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007311 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7312 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007313 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007314 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7315 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007316 }
7317
Dan Gohman475871a2008-07-27 21:46:04 +00007318 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00007319 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007320}
Evan Chenga3195e82006-01-12 22:54:21 +00007321
Dan Gohmand858e902010-04-17 15:26:15 +00007322SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
7323 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007324 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00007325
Dale Johannesen0488fb62010-09-30 23:57:10 +00007326 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007327 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007328
Owen Anderson825b72b2009-08-11 20:47:22 +00007329 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00007330 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007331
Eli Friedman36df4992009-05-27 00:47:34 +00007332 // These are really Legal; return the operand so the caller accepts it as
7333 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007334 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00007335 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00007336 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00007337 Subtarget->is64Bit()) {
7338 return Op;
7339 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007340
Bruno Cardoso Lopesa511b8e2011-08-09 17:39:01 +00007341 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007342 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007343 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00007344 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007345 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00007346 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00007347 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007348 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007349 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007350 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
7351}
Evan Cheng0db9fe62006-04-25 20:13:52 +00007352
Owen Andersone50ed302009-08-10 22:56:29 +00007353SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007354 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00007355 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007356 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00007357 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00007358 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00007359 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007360 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007361 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00007362 else
Owen Anderson825b72b2009-08-11 20:47:22 +00007363 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007364
Chris Lattner492a43e2010-09-22 01:28:21 +00007365 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007366
Stuart Hastings84be9582011-06-02 15:57:11 +00007367 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
7368 MachineMemOperand *MMO;
7369 if (FI) {
7370 int SSFI = FI->getIndex();
7371 MMO =
7372 DAG.getMachineFunction()
7373 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7374 MachineMemOperand::MOLoad, ByteSize, ByteSize);
7375 } else {
7376 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
7377 StackSlot = StackSlot.getOperand(1);
7378 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007379 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007380 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
7381 X86ISD::FILD, DL,
7382 Tys, Ops, array_lengthof(Ops),
7383 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007384
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007385 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007386 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00007387 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007388
7389 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
7390 // shouldn't be necessary except that RFP cannot be live across
7391 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007392 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007393 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
7394 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007395 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00007396 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007397 SDValue Ops[] = {
7398 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
7399 };
Chris Lattner492a43e2010-09-22 01:28:21 +00007400 MachineMemOperand *MMO =
7401 DAG.getMachineFunction()
7402 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007403 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007404
Chris Lattner492a43e2010-09-22 01:28:21 +00007405 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
7406 Ops, array_lengthof(Ops),
7407 Op.getValueType(), MMO);
7408 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007409 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007410 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007411 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007412
Evan Cheng0db9fe62006-04-25 20:13:52 +00007413 return Result;
7414}
7415
Bill Wendling8b8a6362009-01-17 03:56:04 +00007416// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007417SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
7418 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00007419 // This algorithm is not obvious. Here it is in C code, more or less:
7420 /*
7421 double uint64_to_double( uint32_t hi, uint32_t lo ) {
7422 static const __m128i exp = { 0x4330000045300000ULL, 0 };
7423 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00007424
Bill Wendling8b8a6362009-01-17 03:56:04 +00007425 // Copy ints to xmm registers.
7426 __m128i xh = _mm_cvtsi32_si128( hi );
7427 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00007428
Bill Wendling8b8a6362009-01-17 03:56:04 +00007429 // Combine into low half of a single xmm register.
7430 __m128i x = _mm_unpacklo_epi32( xh, xl );
7431 __m128d d;
7432 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00007433
Bill Wendling8b8a6362009-01-17 03:56:04 +00007434 // Merge in appropriate exponents to give the integer bits the right
7435 // magnitude.
7436 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00007437
Bill Wendling8b8a6362009-01-17 03:56:04 +00007438 // Subtract away the biases to deal with the IEEE-754 double precision
7439 // implicit 1.
7440 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00007441
Bill Wendling8b8a6362009-01-17 03:56:04 +00007442 // All conversions up to here are exact. The correctly rounded result is
7443 // calculated using the current rounding mode using the following
7444 // horizontal add.
7445 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
7446 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
7447 // store doesn't really need to be here (except
7448 // maybe to zero the other double)
7449 return sd;
7450 }
7451 */
Dale Johannesen040225f2008-10-21 23:07:49 +00007452
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007453 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00007454 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00007455
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007456 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00007457 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00007458 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
7459 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
7460 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
7461 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00007462 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007463 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007464
Bill Wendling8b8a6362009-01-17 03:56:04 +00007465 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00007466 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007467 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00007468 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007469 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00007470 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007471 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007472
Owen Anderson825b72b2009-08-11 20:47:22 +00007473 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7474 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00007475 Op.getOperand(0),
7476 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007477 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7478 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00007479 Op.getOperand(0),
7480 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007481 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
7482 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00007483 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007484 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00007485 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007486 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
Owen Anderson825b72b2009-08-11 20:47:22 +00007487 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00007488 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007489 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00007490 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007491
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007492 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00007493 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00007494 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
7495 DAG.getUNDEF(MVT::v2f64), ShufMask);
7496 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
7497 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007498 DAG.getIntPtrConstant(0));
7499}
7500
Bill Wendling8b8a6362009-01-17 03:56:04 +00007501// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007502SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
7503 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007504 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007505 // FP constant to bias correct the final result.
7506 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00007507 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007508
7509 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00007510 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00007511 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007512
Owen Anderson825b72b2009-08-11 20:47:22 +00007513 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007514 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007515 DAG.getIntPtrConstant(0));
7516
7517 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007518 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007519 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007520 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007521 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007522 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007523 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007524 MVT::v2f64, Bias)));
7525 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007526 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007527 DAG.getIntPtrConstant(0));
7528
7529 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007530 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007531
7532 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00007533 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00007534
Owen Anderson825b72b2009-08-11 20:47:22 +00007535 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007536 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00007537 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007538 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007539 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00007540 }
7541
7542 // Handle final rounding.
7543 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00007544}
7545
Dan Gohmand858e902010-04-17 15:26:15 +00007546SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
7547 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00007548 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007549 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007550
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007551 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00007552 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
7553 // the optimization here.
7554 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00007555 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00007556
Owen Andersone50ed302009-08-10 22:56:29 +00007557 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007558 EVT DstVT = Op.getValueType();
7559 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007560 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007561 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007562 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00007563
7564 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00007565 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007566 if (SrcVT == MVT::i32) {
7567 SDValue WordOff = DAG.getConstant(4, getPointerTy());
7568 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
7569 getPointerTy(), StackSlot, WordOff);
7570 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007571 StackSlot, MachinePointerInfo(),
7572 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007573 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007574 OffsetSlot, MachinePointerInfo(),
7575 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007576 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
7577 return Fild;
7578 }
7579
7580 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
7581 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007582 StackSlot, MachinePointerInfo(),
7583 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007584 // For i64 source, we need to add the appropriate power of 2 if the input
7585 // was negative. This is the same as the optimization in
7586 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
7587 // we must be careful to do the computation in x87 extended precision, not
7588 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00007589 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
7590 MachineMemOperand *MMO =
7591 DAG.getMachineFunction()
7592 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7593 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007594
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007595 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
7596 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007597 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
7598 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007599
7600 APInt FF(32, 0x5F800000ULL);
7601
7602 // Check whether the sign bit is set.
7603 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
7604 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
7605 ISD::SETLT);
7606
7607 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
7608 SDValue FudgePtr = DAG.getConstantPool(
7609 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
7610 getPointerTy());
7611
7612 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
7613 SDValue Zero = DAG.getIntPtrConstant(0);
7614 SDValue Four = DAG.getIntPtrConstant(4);
7615 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
7616 Zero, Four);
7617 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
7618
7619 // Load the value out, extending it from f32 to f80.
7620 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00007621 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00007622 FudgePtr, MachinePointerInfo::getConstantPool(),
7623 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007624 // Extend everything to 80 bits to force it to be done on x87.
7625 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
7626 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007627}
7628
Dan Gohman475871a2008-07-27 21:46:04 +00007629std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00007630FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00007631 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00007632
Owen Andersone50ed302009-08-10 22:56:29 +00007633 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00007634
7635 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007636 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
7637 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00007638 }
7639
Owen Anderson825b72b2009-08-11 20:47:22 +00007640 assert(DstTy.getSimpleVT() <= MVT::i64 &&
7641 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00007642 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00007643
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007644 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007645 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00007646 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007647 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00007648 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00007649 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00007650 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007651 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007652
Evan Cheng87c89352007-10-15 20:11:21 +00007653 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
7654 // stack slot.
7655 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00007656 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00007657 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007658 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00007659
Michael J. Spencerec38de22010-10-10 22:04:20 +00007660
7661
Evan Cheng0db9fe62006-04-25 20:13:52 +00007662 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00007663 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007664 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007665 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
7666 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
7667 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007668 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007669
Dan Gohman475871a2008-07-27 21:46:04 +00007670 SDValue Chain = DAG.getEntryNode();
7671 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00007672 EVT TheVT = Op.getOperand(0).getValueType();
7673 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007674 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00007675 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007676 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007677 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007678 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00007679 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00007680 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00007681 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00007682
Chris Lattner492a43e2010-09-22 01:28:21 +00007683 MachineMemOperand *MMO =
7684 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7685 MachineMemOperand::MOLoad, MemSize, MemSize);
7686 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
7687 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007688 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00007689 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007690 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
7691 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007692
Chris Lattner07290932010-09-22 01:05:16 +00007693 MachineMemOperand *MMO =
7694 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7695 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007696
Evan Cheng0db9fe62006-04-25 20:13:52 +00007697 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00007698 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00007699 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
7700 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00007701
Chris Lattner27a6c732007-11-24 07:07:01 +00007702 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007703}
7704
Dan Gohmand858e902010-04-17 15:26:15 +00007705SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
7706 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00007707 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007708 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007709
Eli Friedman948e95a2009-05-23 09:59:16 +00007710 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00007711 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00007712 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
7713 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00007714
Chris Lattner27a6c732007-11-24 07:07:01 +00007715 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007716 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007717 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00007718}
7719
Dan Gohmand858e902010-04-17 15:26:15 +00007720SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
7721 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00007722 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
7723 SDValue FIST = Vals.first, StackSlot = Vals.second;
7724 assert(FIST.getNode() && "Unexpected failure");
7725
7726 // Load the result.
7727 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007728 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007729}
7730
Dan Gohmand858e902010-04-17 15:26:15 +00007731SDValue X86TargetLowering::LowerFABS(SDValue Op,
7732 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007733 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007734 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007735 EVT VT = Op.getValueType();
7736 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007737 if (VT.isVector())
7738 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007739 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007740 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007741 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00007742 CV.push_back(C);
7743 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007744 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007745 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00007746 CV.push_back(C);
7747 CV.push_back(C);
7748 CV.push_back(C);
7749 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007750 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007751 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007752 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007753 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007754 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007755 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007756 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007757}
7758
Dan Gohmand858e902010-04-17 15:26:15 +00007759SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007760 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007761 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007762 EVT VT = Op.getValueType();
7763 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00007764 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00007765 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007766 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007767 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007768 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00007769 CV.push_back(C);
7770 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007771 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007772 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00007773 CV.push_back(C);
7774 CV.push_back(C);
7775 CV.push_back(C);
7776 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007777 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007778 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007779 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007780 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007781 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007782 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007783 if (VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007784 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007785 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007786 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007787 Op.getOperand(0)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007788 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00007789 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007790 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00007791 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00007792}
7793
Dan Gohmand858e902010-04-17 15:26:15 +00007794SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007795 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00007796 SDValue Op0 = Op.getOperand(0);
7797 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007798 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007799 EVT VT = Op.getValueType();
7800 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00007801
7802 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007803 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007804 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00007805 SrcVT = VT;
7806 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007807 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007808 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007809 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007810 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007811 }
7812
7813 // At this point the operands and the result should have the same
7814 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00007815
Evan Cheng68c47cb2007-01-05 07:55:56 +00007816 // First get the sign bit of second operand.
7817 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007818 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007819 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
7820 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007821 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007822 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
7823 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7824 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7825 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007826 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007827 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007828 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007829 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007830 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007831 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007832 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007833
7834 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007835 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007836 // Op0 is MVT::f32, Op1 is MVT::f64.
7837 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
7838 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
7839 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007840 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00007841 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00007842 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007843 }
7844
Evan Cheng73d6cf12007-01-05 21:37:56 +00007845 // Clear first operand sign bit.
7846 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00007847 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007848 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
7849 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007850 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007851 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
7852 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7853 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7854 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007855 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007856 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007857 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007858 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007859 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007860 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007861 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00007862
7863 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00007864 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007865}
7866
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00007867SDValue X86TargetLowering::LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) const {
7868 SDValue N0 = Op.getOperand(0);
7869 DebugLoc dl = Op.getDebugLoc();
7870 EVT VT = Op.getValueType();
7871
7872 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
7873 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
7874 DAG.getConstant(1, VT));
7875 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
7876}
7877
Dan Gohman076aee32009-03-04 19:44:21 +00007878/// Emit nodes that will be selected as "test Op0,Op0", or something
7879/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00007880SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00007881 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00007882 DebugLoc dl = Op.getDebugLoc();
7883
Dan Gohman31125812009-03-07 01:58:32 +00007884 // CF and OF aren't always set the way we want. Determine which
7885 // of these we need.
7886 bool NeedCF = false;
7887 bool NeedOF = false;
7888 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007889 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00007890 case X86::COND_A: case X86::COND_AE:
7891 case X86::COND_B: case X86::COND_BE:
7892 NeedCF = true;
7893 break;
7894 case X86::COND_G: case X86::COND_GE:
7895 case X86::COND_L: case X86::COND_LE:
7896 case X86::COND_O: case X86::COND_NO:
7897 NeedOF = true;
7898 break;
Dan Gohman31125812009-03-07 01:58:32 +00007899 }
7900
Dan Gohman076aee32009-03-04 19:44:21 +00007901 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00007902 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
7903 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007904 if (Op.getResNo() != 0 || NeedOF || NeedCF)
7905 // Emit a CMP with 0, which is the TEST pattern.
7906 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
7907 DAG.getConstant(0, Op.getValueType()));
7908
7909 unsigned Opcode = 0;
7910 unsigned NumOperands = 0;
7911 switch (Op.getNode()->getOpcode()) {
7912 case ISD::ADD:
7913 // Due to an isel shortcoming, be conservative if this add is likely to be
7914 // selected as part of a load-modify-store instruction. When the root node
7915 // in a match is a store, isel doesn't know how to remap non-chain non-flag
7916 // uses of other nodes in the match, such as the ADD in this case. This
7917 // leads to the ADD being left around and reselected, with the result being
7918 // two adds in the output. Alas, even if none our users are stores, that
7919 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
7920 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
7921 // climbing the DAG back to the root, and it doesn't seem to be worth the
7922 // effort.
7923 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Dan Gohman076aee32009-03-04 19:44:21 +00007924 UE = Op.getNode()->use_end(); UI != UE; ++UI)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007925 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
7926 goto default_case;
7927
7928 if (ConstantSDNode *C =
7929 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
7930 // An add of one will be selected as an INC.
7931 if (C->getAPIntValue() == 1) {
7932 Opcode = X86ISD::INC;
7933 NumOperands = 1;
7934 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00007935 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007936
7937 // An add of negative one (subtract of one) will be selected as a DEC.
7938 if (C->getAPIntValue().isAllOnesValue()) {
7939 Opcode = X86ISD::DEC;
7940 NumOperands = 1;
7941 break;
7942 }
Dan Gohman076aee32009-03-04 19:44:21 +00007943 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007944
7945 // Otherwise use a regular EFLAGS-setting add.
7946 Opcode = X86ISD::ADD;
7947 NumOperands = 2;
7948 break;
7949 case ISD::AND: {
7950 // If the primary and result isn't used, don't bother using X86ISD::AND,
7951 // because a TEST instruction will be better.
7952 bool NonFlagUse = false;
7953 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
7954 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
7955 SDNode *User = *UI;
7956 unsigned UOpNo = UI.getOperandNo();
7957 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
7958 // Look pass truncate.
7959 UOpNo = User->use_begin().getOperandNo();
7960 User = *User->use_begin();
7961 }
7962
7963 if (User->getOpcode() != ISD::BRCOND &&
7964 User->getOpcode() != ISD::SETCC &&
7965 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
7966 NonFlagUse = true;
7967 break;
7968 }
Dan Gohman076aee32009-03-04 19:44:21 +00007969 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007970
7971 if (!NonFlagUse)
7972 break;
7973 }
7974 // FALL THROUGH
7975 case ISD::SUB:
7976 case ISD::OR:
7977 case ISD::XOR:
7978 // Due to the ISEL shortcoming noted above, be conservative if this op is
7979 // likely to be selected as part of a load-modify-store instruction.
7980 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
7981 UE = Op.getNode()->use_end(); UI != UE; ++UI)
7982 if (UI->getOpcode() == ISD::STORE)
7983 goto default_case;
7984
7985 // Otherwise use a regular EFLAGS-setting instruction.
7986 switch (Op.getNode()->getOpcode()) {
7987 default: llvm_unreachable("unexpected operator!");
7988 case ISD::SUB: Opcode = X86ISD::SUB; break;
7989 case ISD::OR: Opcode = X86ISD::OR; break;
7990 case ISD::XOR: Opcode = X86ISD::XOR; break;
7991 case ISD::AND: Opcode = X86ISD::AND; break;
7992 }
7993
7994 NumOperands = 2;
7995 break;
7996 case X86ISD::ADD:
7997 case X86ISD::SUB:
7998 case X86ISD::INC:
7999 case X86ISD::DEC:
8000 case X86ISD::OR:
8001 case X86ISD::XOR:
8002 case X86ISD::AND:
8003 return SDValue(Op.getNode(), 1);
8004 default:
8005 default_case:
8006 break;
Dan Gohman076aee32009-03-04 19:44:21 +00008007 }
8008
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008009 if (Opcode == 0)
8010 // Emit a CMP with 0, which is the TEST pattern.
8011 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8012 DAG.getConstant(0, Op.getValueType()));
8013
8014 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
8015 SmallVector<SDValue, 4> Ops;
8016 for (unsigned i = 0; i != NumOperands; ++i)
8017 Ops.push_back(Op.getOperand(i));
8018
8019 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
8020 DAG.ReplaceAllUsesWith(Op, New);
8021 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00008022}
8023
8024/// Emit nodes that will be selected as "cmp Op0,Op1", or something
8025/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008026SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008027 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008028 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
8029 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00008030 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00008031
8032 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00008033 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00008034}
8035
Evan Chengd40d03e2010-01-06 19:38:29 +00008036/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
8037/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00008038SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
8039 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008040 SDValue Op0 = And.getOperand(0);
8041 SDValue Op1 = And.getOperand(1);
8042 if (Op0.getOpcode() == ISD::TRUNCATE)
8043 Op0 = Op0.getOperand(0);
8044 if (Op1.getOpcode() == ISD::TRUNCATE)
8045 Op1 = Op1.getOperand(0);
8046
Evan Chengd40d03e2010-01-06 19:38:29 +00008047 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008048 if (Op1.getOpcode() == ISD::SHL)
8049 std::swap(Op0, Op1);
8050 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008051 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
8052 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008053 // If we looked past a truncate, check that it's only truncating away
8054 // known zeros.
8055 unsigned BitWidth = Op0.getValueSizeInBits();
8056 unsigned AndBitWidth = And.getValueSizeInBits();
8057 if (BitWidth > AndBitWidth) {
8058 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
8059 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
8060 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
8061 return SDValue();
8062 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008063 LHS = Op1;
8064 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00008065 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008066 } else if (Op1.getOpcode() == ISD::Constant) {
8067 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
8068 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00008069 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
8070 LHS = AndLHS.getOperand(0);
8071 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008072 }
Evan Chengd40d03e2010-01-06 19:38:29 +00008073 }
Evan Cheng0488db92007-09-25 01:57:46 +00008074
Evan Chengd40d03e2010-01-06 19:38:29 +00008075 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00008076 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00008077 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00008078 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00008079 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00008080 // Also promote i16 to i32 for performance / code size reason.
8081 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00008082 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00008083 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00008084
Evan Chengd40d03e2010-01-06 19:38:29 +00008085 // If the operand types disagree, extend the shift amount to match. Since
8086 // BT ignores high bits (like shifts) we can use anyextend.
8087 if (LHS.getValueType() != RHS.getValueType())
8088 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008089
Evan Chengd40d03e2010-01-06 19:38:29 +00008090 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
8091 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
8092 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8093 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00008094 }
8095
Evan Cheng54de3ea2010-01-05 06:52:31 +00008096 return SDValue();
8097}
8098
Dan Gohmand858e902010-04-17 15:26:15 +00008099SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng54de3ea2010-01-05 06:52:31 +00008100 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
8101 SDValue Op0 = Op.getOperand(0);
8102 SDValue Op1 = Op.getOperand(1);
8103 DebugLoc dl = Op.getDebugLoc();
8104 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
8105
8106 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00008107 // Lower (X & (1 << N)) == 0 to BT(X, N).
8108 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
8109 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00008110 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008111 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00008112 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008113 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
8114 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
8115 if (NewSetCC.getNode())
8116 return NewSetCC;
8117 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00008118
Chris Lattner481eebc2010-12-19 21:23:48 +00008119 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
8120 // these.
8121 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00008122 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00008123 cast<ConstantSDNode>(Op1)->isNullValue()) &&
8124 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008125
Chris Lattner481eebc2010-12-19 21:23:48 +00008126 // If the input is a setcc, then reuse the input setcc or use a new one with
8127 // the inverted condition.
8128 if (Op0.getOpcode() == X86ISD::SETCC) {
8129 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
8130 bool Invert = (CC == ISD::SETNE) ^
8131 cast<ConstantSDNode>(Op1)->isNullValue();
8132 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008133
Evan Cheng2c755ba2010-02-27 07:36:59 +00008134 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00008135 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8136 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
8137 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008138 }
8139
Evan Chenge5b51ac2010-04-17 06:13:15 +00008140 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00008141 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008142 if (X86CC == X86::COND_INVALID)
8143 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008144
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008145 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00008146 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008147 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00008148}
8149
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008150// Lower256IntVETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
8151// ones, and then concatenate the result back.
8152static SDValue Lower256IntVETCC(SDValue Op, SelectionDAG &DAG) {
8153 EVT VT = Op.getValueType();
8154
8155 assert(VT.getSizeInBits() == 256 && Op.getOpcode() == ISD::VSETCC &&
8156 "Unsupported value type for operation");
8157
8158 int NumElems = VT.getVectorNumElements();
8159 DebugLoc dl = Op.getDebugLoc();
8160 SDValue CC = Op.getOperand(2);
8161 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
8162 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
8163
8164 // Extract the LHS vectors
8165 SDValue LHS = Op.getOperand(0);
8166 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
8167 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
8168
8169 // Extract the RHS vectors
8170 SDValue RHS = Op.getOperand(1);
8171 SDValue RHS1 = Extract128BitVector(RHS, Idx0, DAG, dl);
8172 SDValue RHS2 = Extract128BitVector(RHS, Idx1, DAG, dl);
8173
8174 // Issue the operation on the smaller types and concatenate the result back
8175 MVT EltVT = VT.getVectorElementType().getSimpleVT();
8176 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
8177 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
8178 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
8179 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
8180}
8181
8182
Dan Gohmand858e902010-04-17 15:26:15 +00008183SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008184 SDValue Cond;
8185 SDValue Op0 = Op.getOperand(0);
8186 SDValue Op1 = Op.getOperand(1);
8187 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00008188 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00008189 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
8190 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008191 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00008192
8193 if (isFP) {
8194 unsigned SSECC = 8;
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008195 EVT EltVT = Op0.getValueType().getVectorElementType();
8196 assert(EltVT == MVT::f32 || EltVT == MVT::f64);
8197
8198 unsigned Opc = EltVT == MVT::f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00008199 bool Swap = false;
8200
8201 switch (SetCCOpcode) {
8202 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008203 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00008204 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00008205 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00008206 case ISD::SETGT: Swap = true; // Fallthrough
8207 case ISD::SETLT:
8208 case ISD::SETOLT: SSECC = 1; break;
8209 case ISD::SETOGE:
8210 case ISD::SETGE: Swap = true; // Fallthrough
8211 case ISD::SETLE:
8212 case ISD::SETOLE: SSECC = 2; break;
8213 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008214 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00008215 case ISD::SETNE: SSECC = 4; break;
8216 case ISD::SETULE: Swap = true;
8217 case ISD::SETUGE: SSECC = 5; break;
8218 case ISD::SETULT: Swap = true;
8219 case ISD::SETUGT: SSECC = 6; break;
8220 case ISD::SETO: SSECC = 7; break;
8221 }
8222 if (Swap)
8223 std::swap(Op0, Op1);
8224
Nate Begemanfb8ead02008-07-25 19:05:58 +00008225 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00008226 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00008227 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00008228 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00008229 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
8230 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00008231 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00008232 }
8233 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00008234 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00008235 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
8236 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00008237 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00008238 }
Torok Edwinc23197a2009-07-14 16:55:14 +00008239 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00008240 }
8241 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00008242 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00008243 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008244
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008245 // Break 256-bit integer vector compare into smaller ones.
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008246 if (!isFP && VT.getSizeInBits() == 256)
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008247 return Lower256IntVETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008248
Nate Begeman30a0de92008-07-17 16:51:19 +00008249 // We are handling one of the integer comparisons here. Since SSE only has
8250 // GT and EQ comparisons for integer, swapping operands and multiple
8251 // operations may be required for some comparisons.
8252 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
8253 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00008254
Owen Anderson825b72b2009-08-11 20:47:22 +00008255 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00008256 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00008257 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00008258 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00008259 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
8260 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008261 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008262
Nate Begeman30a0de92008-07-17 16:51:19 +00008263 switch (SetCCOpcode) {
8264 default: break;
8265 case ISD::SETNE: Invert = true;
8266 case ISD::SETEQ: Opc = EQOpc; break;
8267 case ISD::SETLT: Swap = true;
8268 case ISD::SETGT: Opc = GTOpc; break;
8269 case ISD::SETGE: Swap = true;
8270 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
8271 case ISD::SETULT: Swap = true;
8272 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
8273 case ISD::SETUGE: Swap = true;
8274 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
8275 }
8276 if (Swap)
8277 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008278
Nate Begeman30a0de92008-07-17 16:51:19 +00008279 // Since SSE has no unsigned integer comparisons, we need to flip the sign
8280 // bits of the inputs before performing those operations.
8281 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00008282 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00008283 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
8284 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00008285 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00008286 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
8287 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00008288 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
8289 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00008290 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008291
Dale Johannesenace16102009-02-03 19:33:06 +00008292 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00008293
8294 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00008295 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00008296 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00008297
Nate Begeman30a0de92008-07-17 16:51:19 +00008298 return Result;
8299}
Evan Cheng0488db92007-09-25 01:57:46 +00008300
Evan Cheng370e5342008-12-03 08:38:43 +00008301// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00008302static bool isX86LogicalCmp(SDValue Op) {
8303 unsigned Opc = Op.getNode()->getOpcode();
8304 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
8305 return true;
8306 if (Op.getResNo() == 1 &&
8307 (Opc == X86ISD::ADD ||
8308 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00008309 Opc == X86ISD::ADC ||
8310 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00008311 Opc == X86ISD::SMUL ||
8312 Opc == X86ISD::UMUL ||
8313 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00008314 Opc == X86ISD::DEC ||
8315 Opc == X86ISD::OR ||
8316 Opc == X86ISD::XOR ||
8317 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00008318 return true;
8319
Chris Lattner9637d5b2010-12-05 07:49:54 +00008320 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
8321 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008322
Dan Gohman076aee32009-03-04 19:44:21 +00008323 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00008324}
8325
Chris Lattnera2b56002010-12-05 01:23:24 +00008326static bool isZero(SDValue V) {
8327 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8328 return C && C->isNullValue();
8329}
8330
Chris Lattner96908b12010-12-05 02:00:51 +00008331static bool isAllOnes(SDValue V) {
8332 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8333 return C && C->isAllOnesValue();
8334}
8335
Dan Gohmand858e902010-04-17 15:26:15 +00008336SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008337 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008338 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00008339 SDValue Op1 = Op.getOperand(1);
8340 SDValue Op2 = Op.getOperand(2);
8341 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008342 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00008343
Dan Gohman1a492952009-10-20 16:22:37 +00008344 if (Cond.getOpcode() == ISD::SETCC) {
8345 SDValue NewCond = LowerSETCC(Cond, DAG);
8346 if (NewCond.getNode())
8347 Cond = NewCond;
8348 }
Evan Cheng734503b2006-09-11 02:19:56 +00008349
Chris Lattnera2b56002010-12-05 01:23:24 +00008350 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008351 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00008352 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008353 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008354 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00008355 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
8356 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008357 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008358
Chris Lattnera2b56002010-12-05 01:23:24 +00008359 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008360
8361 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00008362 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
8363 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00008364
8365 SDValue CmpOp0 = Cmp.getOperand(0);
8366 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
8367 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008368
Chris Lattner96908b12010-12-05 02:00:51 +00008369 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00008370 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8371 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008372
Chris Lattner96908b12010-12-05 02:00:51 +00008373 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
8374 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008375
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008376 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00008377 if (N2C == 0 || !N2C->isNullValue())
8378 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
8379 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008380 }
8381 }
8382
Chris Lattnera2b56002010-12-05 01:23:24 +00008383 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00008384 if (Cond.getOpcode() == ISD::AND &&
8385 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8386 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008387 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008388 Cond = Cond.getOperand(0);
8389 }
8390
Evan Cheng3f41d662007-10-08 22:16:29 +00008391 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8392 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00008393 if (Cond.getOpcode() == X86ISD::SETCC ||
8394 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008395 CC = Cond.getOperand(0);
8396
Dan Gohman475871a2008-07-27 21:46:04 +00008397 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008398 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00008399 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00008400
Evan Cheng3f41d662007-10-08 22:16:29 +00008401 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008402 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00008403 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00008404 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00008405
Chris Lattnerd1980a52009-03-12 06:52:53 +00008406 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
8407 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00008408 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008409 addTest = false;
8410 }
8411 }
8412
8413 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008414 // Look pass the truncate.
8415 if (Cond.getOpcode() == ISD::TRUNCATE)
8416 Cond = Cond.getOperand(0);
8417
8418 // We know the result of AND is compared against zero. Try to match
8419 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00008420 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00008421 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00008422 if (NewSetCC.getNode()) {
8423 CC = NewSetCC.getOperand(0);
8424 Cond = NewSetCC.getOperand(1);
8425 addTest = false;
8426 }
8427 }
8428 }
8429
8430 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008431 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00008432 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00008433 }
8434
Benjamin Kramere915ff32010-12-22 23:09:28 +00008435 // a < b ? -1 : 0 -> RES = ~setcc_carry
8436 // a < b ? 0 : -1 -> RES = setcc_carry
8437 // a >= b ? -1 : 0 -> RES = setcc_carry
8438 // a >= b ? 0 : -1 -> RES = ~setcc_carry
8439 if (Cond.getOpcode() == X86ISD::CMP) {
8440 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
8441
8442 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
8443 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
8444 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8445 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
8446 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
8447 return DAG.getNOT(DL, Res, Res.getValueType());
8448 return Res;
8449 }
8450 }
8451
Evan Cheng0488db92007-09-25 01:57:46 +00008452 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
8453 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008454 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008455 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00008456 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00008457}
8458
Evan Cheng370e5342008-12-03 08:38:43 +00008459// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
8460// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
8461// from the AND / OR.
8462static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
8463 Opc = Op.getOpcode();
8464 if (Opc != ISD::OR && Opc != ISD::AND)
8465 return false;
8466 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8467 Op.getOperand(0).hasOneUse() &&
8468 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
8469 Op.getOperand(1).hasOneUse());
8470}
8471
Evan Cheng961d6d42009-02-02 08:19:07 +00008472// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
8473// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00008474static bool isXor1OfSetCC(SDValue Op) {
8475 if (Op.getOpcode() != ISD::XOR)
8476 return false;
8477 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
8478 if (N1C && N1C->getAPIntValue() == 1) {
8479 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8480 Op.getOperand(0).hasOneUse();
8481 }
8482 return false;
8483}
8484
Dan Gohmand858e902010-04-17 15:26:15 +00008485SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008486 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008487 SDValue Chain = Op.getOperand(0);
8488 SDValue Cond = Op.getOperand(1);
8489 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008490 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008491 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00008492
Dan Gohman1a492952009-10-20 16:22:37 +00008493 if (Cond.getOpcode() == ISD::SETCC) {
8494 SDValue NewCond = LowerSETCC(Cond, DAG);
8495 if (NewCond.getNode())
8496 Cond = NewCond;
8497 }
Chris Lattnere55484e2008-12-25 05:34:37 +00008498#if 0
8499 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00008500 else if (Cond.getOpcode() == X86ISD::ADD ||
8501 Cond.getOpcode() == X86ISD::SUB ||
8502 Cond.getOpcode() == X86ISD::SMUL ||
8503 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00008504 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00008505#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00008506
Evan Chengad9c0a32009-12-15 00:53:42 +00008507 // Look pass (and (setcc_carry (cmp ...)), 1).
8508 if (Cond.getOpcode() == ISD::AND &&
8509 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8510 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008511 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008512 Cond = Cond.getOperand(0);
8513 }
8514
Evan Cheng3f41d662007-10-08 22:16:29 +00008515 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8516 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00008517 if (Cond.getOpcode() == X86ISD::SETCC ||
8518 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008519 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008520
Dan Gohman475871a2008-07-27 21:46:04 +00008521 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008522 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00008523 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00008524 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00008525 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008526 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00008527 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00008528 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00008529 default: break;
8530 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00008531 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00008532 // These can only come from an arithmetic instruction with overflow,
8533 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00008534 Cond = Cond.getNode()->getOperand(1);
8535 addTest = false;
8536 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00008537 }
Evan Cheng0488db92007-09-25 01:57:46 +00008538 }
Evan Cheng370e5342008-12-03 08:38:43 +00008539 } else {
8540 unsigned CondOpc;
8541 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
8542 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00008543 if (CondOpc == ISD::OR) {
8544 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
8545 // two branches instead of an explicit OR instruction with a
8546 // separate test.
8547 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00008548 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00008549 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008550 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00008551 Chain, Dest, CC, Cmp);
8552 CC = Cond.getOperand(1).getOperand(0);
8553 Cond = Cmp;
8554 addTest = false;
8555 }
8556 } else { // ISD::AND
8557 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
8558 // two branches instead of an explicit AND instruction with a
8559 // separate test. However, we only do this if this block doesn't
8560 // have a fall-through edge, because this requires an explicit
8561 // jmp when the condition is false.
8562 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00008563 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00008564 Op.getNode()->hasOneUse()) {
8565 X86::CondCode CCode =
8566 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
8567 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008568 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00008569 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00008570 // Look for an unconditional branch following this conditional branch.
8571 // We need this because we need to reverse the successors in order
8572 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00008573 if (User->getOpcode() == ISD::BR) {
8574 SDValue FalseBB = User->getOperand(1);
8575 SDNode *NewBR =
8576 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00008577 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00008578 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00008579 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00008580
Dale Johannesene4d209d2009-02-03 20:21:25 +00008581 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00008582 Chain, Dest, CC, Cmp);
8583 X86::CondCode CCode =
8584 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
8585 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008586 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00008587 Cond = Cmp;
8588 addTest = false;
8589 }
8590 }
Dan Gohman279c22e2008-10-21 03:29:32 +00008591 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00008592 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
8593 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
8594 // It should be transformed during dag combiner except when the condition
8595 // is set by a arithmetics with overflow node.
8596 X86::CondCode CCode =
8597 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
8598 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008599 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00008600 Cond = Cond.getOperand(0).getOperand(1);
8601 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00008602 }
Evan Cheng0488db92007-09-25 01:57:46 +00008603 }
8604
8605 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008606 // Look pass the truncate.
8607 if (Cond.getOpcode() == ISD::TRUNCATE)
8608 Cond = Cond.getOperand(0);
8609
8610 // We know the result of AND is compared against zero. Try to match
8611 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00008612 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008613 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
8614 if (NewSetCC.getNode()) {
8615 CC = NewSetCC.getOperand(0);
8616 Cond = NewSetCC.getOperand(1);
8617 addTest = false;
8618 }
8619 }
8620 }
8621
8622 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008623 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00008624 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00008625 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00008626 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00008627 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00008628}
8629
Anton Korobeynikove060b532007-04-17 19:34:00 +00008630
8631// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
8632// Calls to _alloca is needed to probe the stack when allocating more than 4k
8633// bytes in one go. Touching the stack at 4K increments is necessary to ensure
8634// that the guard pages used by the OS virtual memory manager are allocated in
8635// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00008636SDValue
8637X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008638 SelectionDAG &DAG) const {
Duncan Sands1e1ca0b2010-10-21 16:02:12 +00008639 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows()) &&
Michael J. Spencere9c253e2010-10-21 01:41:01 +00008640 "This should be used only on Windows targets");
NAKAMURA Takumia2e07622011-03-24 07:07:00 +00008641 assert(!Subtarget->isTargetEnvMacho());
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008642 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008643
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008644 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00008645 SDValue Chain = Op.getOperand(0);
8646 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008647 // FIXME: Ensure alignment here
8648
Dan Gohman475871a2008-07-27 21:46:04 +00008649 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008650
Owen Anderson825b72b2009-08-11 20:47:22 +00008651 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
NAKAMURA Takumia2e07622011-03-24 07:07:00 +00008652 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008653
NAKAMURA Takumia2e07622011-03-24 07:07:00 +00008654 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00008655 Flag = Chain.getValue(1);
8656
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008657 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00008658
Michael J. Spencere9c253e2010-10-21 01:41:01 +00008659 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008660 Flag = Chain.getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008661
Dale Johannesendd64c412009-02-04 00:33:20 +00008662 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008663
Dan Gohman475871a2008-07-27 21:46:04 +00008664 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008665 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008666}
8667
Dan Gohmand858e902010-04-17 15:26:15 +00008668SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00008669 MachineFunction &MF = DAG.getMachineFunction();
8670 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
8671
Dan Gohman69de1932008-02-06 22:27:42 +00008672 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00008673 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00008674
Anton Korobeynikove7beda12010-10-03 22:52:07 +00008675 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00008676 // vastart just stores the address of the VarArgsFrameIndex slot into the
8677 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00008678 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
8679 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00008680 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
8681 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008682 }
8683
8684 // __va_list_tag:
8685 // gp_offset (0 - 6 * 8)
8686 // fp_offset (48 - 48 + 8 * 16)
8687 // overflow_arg_area (point to parameters coming in memory).
8688 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00008689 SmallVector<SDValue, 8> MemOps;
8690 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00008691 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00008692 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00008693 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
8694 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008695 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008696 MemOps.push_back(Store);
8697
8698 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00008699 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008700 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008701 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00008702 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
8703 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008704 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008705 MemOps.push_back(Store);
8706
8707 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00008708 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008709 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00008710 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
8711 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00008712 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
8713 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00008714 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008715 MemOps.push_back(Store);
8716
8717 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00008718 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008719 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00008720 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
8721 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00008722 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
8723 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008724 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00008725 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00008726 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00008727}
8728
Dan Gohmand858e902010-04-17 15:26:15 +00008729SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00008730 assert(Subtarget->is64Bit() &&
8731 "LowerVAARG only handles 64-bit va_arg!");
8732 assert((Subtarget->isTargetLinux() ||
8733 Subtarget->isTargetDarwin()) &&
8734 "Unhandled target in LowerVAARG");
8735 assert(Op.getNode()->getNumOperands() == 4);
8736 SDValue Chain = Op.getOperand(0);
8737 SDValue SrcPtr = Op.getOperand(1);
8738 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
8739 unsigned Align = Op.getConstantOperandVal(3);
8740 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00008741
Dan Gohman320afb82010-10-12 18:00:49 +00008742 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008743 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Dan Gohman320afb82010-10-12 18:00:49 +00008744 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
8745 uint8_t ArgMode;
8746
8747 // Decide which area this value should be read from.
8748 // TODO: Implement the AMD64 ABI in its entirety. This simple
8749 // selection mechanism works only for the basic types.
8750 if (ArgVT == MVT::f80) {
8751 llvm_unreachable("va_arg for f80 not yet implemented");
8752 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
8753 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
8754 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
8755 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
8756 } else {
8757 llvm_unreachable("Unhandled argument type in LowerVAARG");
8758 }
8759
8760 if (ArgMode == 2) {
8761 // Sanity Check: Make sure using fp_offset makes sense.
Michael J. Spencer87b86652010-10-19 07:32:42 +00008762 assert(!UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00008763 !(DAG.getMachineFunction()
8764 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00008765 Subtarget->hasXMM());
Dan Gohman320afb82010-10-12 18:00:49 +00008766 }
8767
8768 // Insert VAARG_64 node into the DAG
8769 // VAARG_64 returns two values: Variable Argument Address, Chain
8770 SmallVector<SDValue, 11> InstOps;
8771 InstOps.push_back(Chain);
8772 InstOps.push_back(SrcPtr);
8773 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
8774 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
8775 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
8776 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
8777 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
8778 VTs, &InstOps[0], InstOps.size(),
8779 MVT::i64,
8780 MachinePointerInfo(SV),
8781 /*Align=*/0,
8782 /*Volatile=*/false,
8783 /*ReadMem=*/true,
8784 /*WriteMem=*/true);
8785 Chain = VAARG.getValue(1);
8786
8787 // Load the next argument and return it
8788 return DAG.getLoad(ArgVT, dl,
8789 Chain,
8790 VAARG,
8791 MachinePointerInfo(),
8792 false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00008793}
8794
Dan Gohmand858e902010-04-17 15:26:15 +00008795SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00008796 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00008797 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00008798 SDValue Chain = Op.getOperand(0);
8799 SDValue DstPtr = Op.getOperand(1);
8800 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00008801 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
8802 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00008803 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00008804
Chris Lattnere72f2022010-09-21 05:40:29 +00008805 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00008806 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00008807 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00008808 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00008809}
8810
Dan Gohman475871a2008-07-27 21:46:04 +00008811SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00008812X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008813 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008814 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008815 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00008816 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00008817 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00008818 case Intrinsic::x86_sse_comieq_ss:
8819 case Intrinsic::x86_sse_comilt_ss:
8820 case Intrinsic::x86_sse_comile_ss:
8821 case Intrinsic::x86_sse_comigt_ss:
8822 case Intrinsic::x86_sse_comige_ss:
8823 case Intrinsic::x86_sse_comineq_ss:
8824 case Intrinsic::x86_sse_ucomieq_ss:
8825 case Intrinsic::x86_sse_ucomilt_ss:
8826 case Intrinsic::x86_sse_ucomile_ss:
8827 case Intrinsic::x86_sse_ucomigt_ss:
8828 case Intrinsic::x86_sse_ucomige_ss:
8829 case Intrinsic::x86_sse_ucomineq_ss:
8830 case Intrinsic::x86_sse2_comieq_sd:
8831 case Intrinsic::x86_sse2_comilt_sd:
8832 case Intrinsic::x86_sse2_comile_sd:
8833 case Intrinsic::x86_sse2_comigt_sd:
8834 case Intrinsic::x86_sse2_comige_sd:
8835 case Intrinsic::x86_sse2_comineq_sd:
8836 case Intrinsic::x86_sse2_ucomieq_sd:
8837 case Intrinsic::x86_sse2_ucomilt_sd:
8838 case Intrinsic::x86_sse2_ucomile_sd:
8839 case Intrinsic::x86_sse2_ucomigt_sd:
8840 case Intrinsic::x86_sse2_ucomige_sd:
8841 case Intrinsic::x86_sse2_ucomineq_sd: {
8842 unsigned Opc = 0;
8843 ISD::CondCode CC = ISD::SETCC_INVALID;
8844 switch (IntNo) {
8845 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008846 case Intrinsic::x86_sse_comieq_ss:
8847 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008848 Opc = X86ISD::COMI;
8849 CC = ISD::SETEQ;
8850 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00008851 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008852 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008853 Opc = X86ISD::COMI;
8854 CC = ISD::SETLT;
8855 break;
8856 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008857 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008858 Opc = X86ISD::COMI;
8859 CC = ISD::SETLE;
8860 break;
8861 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008862 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008863 Opc = X86ISD::COMI;
8864 CC = ISD::SETGT;
8865 break;
8866 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008867 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008868 Opc = X86ISD::COMI;
8869 CC = ISD::SETGE;
8870 break;
8871 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008872 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008873 Opc = X86ISD::COMI;
8874 CC = ISD::SETNE;
8875 break;
8876 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008877 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008878 Opc = X86ISD::UCOMI;
8879 CC = ISD::SETEQ;
8880 break;
8881 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008882 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008883 Opc = X86ISD::UCOMI;
8884 CC = ISD::SETLT;
8885 break;
8886 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008887 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008888 Opc = X86ISD::UCOMI;
8889 CC = ISD::SETLE;
8890 break;
8891 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008892 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008893 Opc = X86ISD::UCOMI;
8894 CC = ISD::SETGT;
8895 break;
8896 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008897 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008898 Opc = X86ISD::UCOMI;
8899 CC = ISD::SETGE;
8900 break;
8901 case Intrinsic::x86_sse_ucomineq_ss:
8902 case Intrinsic::x86_sse2_ucomineq_sd:
8903 Opc = X86ISD::UCOMI;
8904 CC = ISD::SETNE;
8905 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00008906 }
Evan Cheng734503b2006-09-11 02:19:56 +00008907
Dan Gohman475871a2008-07-27 21:46:04 +00008908 SDValue LHS = Op.getOperand(1);
8909 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00008910 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008911 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00008912 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
8913 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8914 DAG.getConstant(X86CC, MVT::i8), Cond);
8915 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00008916 }
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008917 // ptest and testp intrinsics. The intrinsic these come from are designed to
8918 // return an integer value, not just an instruction so lower it to the ptest
8919 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00008920 case Intrinsic::x86_sse41_ptestz:
8921 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008922 case Intrinsic::x86_sse41_ptestnzc:
8923 case Intrinsic::x86_avx_ptestz_256:
8924 case Intrinsic::x86_avx_ptestc_256:
8925 case Intrinsic::x86_avx_ptestnzc_256:
8926 case Intrinsic::x86_avx_vtestz_ps:
8927 case Intrinsic::x86_avx_vtestc_ps:
8928 case Intrinsic::x86_avx_vtestnzc_ps:
8929 case Intrinsic::x86_avx_vtestz_pd:
8930 case Intrinsic::x86_avx_vtestc_pd:
8931 case Intrinsic::x86_avx_vtestnzc_pd:
8932 case Intrinsic::x86_avx_vtestz_ps_256:
8933 case Intrinsic::x86_avx_vtestc_ps_256:
8934 case Intrinsic::x86_avx_vtestnzc_ps_256:
8935 case Intrinsic::x86_avx_vtestz_pd_256:
8936 case Intrinsic::x86_avx_vtestc_pd_256:
8937 case Intrinsic::x86_avx_vtestnzc_pd_256: {
8938 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00008939 unsigned X86CC = 0;
8940 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00008941 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008942 case Intrinsic::x86_avx_vtestz_ps:
8943 case Intrinsic::x86_avx_vtestz_pd:
8944 case Intrinsic::x86_avx_vtestz_ps_256:
8945 case Intrinsic::x86_avx_vtestz_pd_256:
8946 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00008947 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008948 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008949 // ZF = 1
8950 X86CC = X86::COND_E;
8951 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008952 case Intrinsic::x86_avx_vtestc_ps:
8953 case Intrinsic::x86_avx_vtestc_pd:
8954 case Intrinsic::x86_avx_vtestc_ps_256:
8955 case Intrinsic::x86_avx_vtestc_pd_256:
8956 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00008957 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008958 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008959 // CF = 1
8960 X86CC = X86::COND_B;
8961 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008962 case Intrinsic::x86_avx_vtestnzc_ps:
8963 case Intrinsic::x86_avx_vtestnzc_pd:
8964 case Intrinsic::x86_avx_vtestnzc_ps_256:
8965 case Intrinsic::x86_avx_vtestnzc_pd_256:
8966 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00008967 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008968 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008969 // ZF and CF = 0
8970 X86CC = X86::COND_A;
8971 break;
8972 }
Eric Christopherfd179292009-08-27 18:07:15 +00008973
Eric Christopher71c67532009-07-29 00:28:05 +00008974 SDValue LHS = Op.getOperand(1);
8975 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008976 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
8977 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00008978 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
8979 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
8980 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00008981 }
Evan Cheng5759f972008-05-04 09:15:50 +00008982
8983 // Fix vector shift instructions where the last operand is a non-immediate
8984 // i32 value.
8985 case Intrinsic::x86_sse2_pslli_w:
8986 case Intrinsic::x86_sse2_pslli_d:
8987 case Intrinsic::x86_sse2_pslli_q:
8988 case Intrinsic::x86_sse2_psrli_w:
8989 case Intrinsic::x86_sse2_psrli_d:
8990 case Intrinsic::x86_sse2_psrli_q:
8991 case Intrinsic::x86_sse2_psrai_w:
8992 case Intrinsic::x86_sse2_psrai_d:
8993 case Intrinsic::x86_mmx_pslli_w:
8994 case Intrinsic::x86_mmx_pslli_d:
8995 case Intrinsic::x86_mmx_pslli_q:
8996 case Intrinsic::x86_mmx_psrli_w:
8997 case Intrinsic::x86_mmx_psrli_d:
8998 case Intrinsic::x86_mmx_psrli_q:
8999 case Intrinsic::x86_mmx_psrai_w:
9000 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00009001 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00009002 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00009003 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00009004
9005 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00009006 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00009007 switch (IntNo) {
9008 case Intrinsic::x86_sse2_pslli_w:
9009 NewIntNo = Intrinsic::x86_sse2_psll_w;
9010 break;
9011 case Intrinsic::x86_sse2_pslli_d:
9012 NewIntNo = Intrinsic::x86_sse2_psll_d;
9013 break;
9014 case Intrinsic::x86_sse2_pslli_q:
9015 NewIntNo = Intrinsic::x86_sse2_psll_q;
9016 break;
9017 case Intrinsic::x86_sse2_psrli_w:
9018 NewIntNo = Intrinsic::x86_sse2_psrl_w;
9019 break;
9020 case Intrinsic::x86_sse2_psrli_d:
9021 NewIntNo = Intrinsic::x86_sse2_psrl_d;
9022 break;
9023 case Intrinsic::x86_sse2_psrli_q:
9024 NewIntNo = Intrinsic::x86_sse2_psrl_q;
9025 break;
9026 case Intrinsic::x86_sse2_psrai_w:
9027 NewIntNo = Intrinsic::x86_sse2_psra_w;
9028 break;
9029 case Intrinsic::x86_sse2_psrai_d:
9030 NewIntNo = Intrinsic::x86_sse2_psra_d;
9031 break;
9032 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00009033 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00009034 switch (IntNo) {
9035 case Intrinsic::x86_mmx_pslli_w:
9036 NewIntNo = Intrinsic::x86_mmx_psll_w;
9037 break;
9038 case Intrinsic::x86_mmx_pslli_d:
9039 NewIntNo = Intrinsic::x86_mmx_psll_d;
9040 break;
9041 case Intrinsic::x86_mmx_pslli_q:
9042 NewIntNo = Intrinsic::x86_mmx_psll_q;
9043 break;
9044 case Intrinsic::x86_mmx_psrli_w:
9045 NewIntNo = Intrinsic::x86_mmx_psrl_w;
9046 break;
9047 case Intrinsic::x86_mmx_psrli_d:
9048 NewIntNo = Intrinsic::x86_mmx_psrl_d;
9049 break;
9050 case Intrinsic::x86_mmx_psrli_q:
9051 NewIntNo = Intrinsic::x86_mmx_psrl_q;
9052 break;
9053 case Intrinsic::x86_mmx_psrai_w:
9054 NewIntNo = Intrinsic::x86_mmx_psra_w;
9055 break;
9056 case Intrinsic::x86_mmx_psrai_d:
9057 NewIntNo = Intrinsic::x86_mmx_psra_d;
9058 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00009059 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00009060 }
9061 break;
9062 }
9063 }
Mon P Wangefa42202009-09-03 19:56:25 +00009064
9065 // The vector shift intrinsics with scalars uses 32b shift amounts but
9066 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
9067 // to be zero.
9068 SDValue ShOps[4];
9069 ShOps[0] = ShAmt;
9070 ShOps[1] = DAG.getConstant(0, MVT::i32);
9071 if (ShAmtVT == MVT::v4i32) {
9072 ShOps[2] = DAG.getUNDEF(MVT::i32);
9073 ShOps[3] = DAG.getUNDEF(MVT::i32);
9074 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
9075 } else {
9076 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00009077// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00009078 }
9079
Owen Andersone50ed302009-08-10 22:56:29 +00009080 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009081 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009082 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009083 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00009084 Op.getOperand(1), ShAmt);
9085 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00009086 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00009087}
Evan Cheng72261582005-12-20 06:22:03 +00009088
Dan Gohmand858e902010-04-17 15:26:15 +00009089SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
9090 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00009091 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9092 MFI->setReturnAddressIsTaken(true);
9093
Bill Wendling64e87322009-01-16 19:25:27 +00009094 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009095 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00009096
9097 if (Depth > 0) {
9098 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
9099 SDValue Offset =
9100 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00009101 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009102 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00009103 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009104 FrameAddr, Offset),
Chris Lattner51abfe42010-09-21 06:02:19 +00009105 MachinePointerInfo(), false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00009106 }
9107
9108 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00009109 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00009110 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Chris Lattner51abfe42010-09-21 06:02:19 +00009111 RetAddrFI, MachinePointerInfo(), false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00009112}
9113
Dan Gohmand858e902010-04-17 15:26:15 +00009114SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00009115 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9116 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00009117
Owen Andersone50ed302009-08-10 22:56:29 +00009118 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009119 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00009120 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
9121 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00009122 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00009123 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00009124 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
9125 MachinePointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +00009126 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00009127 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00009128}
9129
Dan Gohman475871a2008-07-27 21:46:04 +00009130SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009131 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00009132 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009133}
9134
Dan Gohmand858e902010-04-17 15:26:15 +00009135SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009136 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00009137 SDValue Chain = Op.getOperand(0);
9138 SDValue Offset = Op.getOperand(1);
9139 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009140 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009141
Dan Gohmand8816272010-08-11 18:14:00 +00009142 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
9143 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
9144 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009145 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009146
Dan Gohmand8816272010-08-11 18:14:00 +00009147 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
9148 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009149 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009150 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
9151 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00009152 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009153 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009154
Dale Johannesene4d209d2009-02-03 20:21:25 +00009155 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00009156 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009157 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009158}
9159
Dan Gohman475871a2008-07-27 21:46:04 +00009160SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009161 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00009162 SDValue Root = Op.getOperand(0);
9163 SDValue Trmp = Op.getOperand(1); // trampoline
9164 SDValue FPtr = Op.getOperand(2); // nested function
9165 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009166 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009167
Dan Gohman69de1932008-02-06 22:27:42 +00009168 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009169
9170 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00009171 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00009172
9173 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00009174 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
9175 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00009176
Evan Cheng0e6a0522011-07-18 20:57:22 +00009177 const unsigned char N86R10 = X86_MC::getX86RegNum(X86::R10);
9178 const unsigned char N86R11 = X86_MC::getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00009179
9180 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
9181
9182 // Load the pointer to the nested function into R11.
9183 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00009184 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00009185 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009186 Addr, MachinePointerInfo(TrmpAddr),
9187 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009188
Owen Anderson825b72b2009-08-11 20:47:22 +00009189 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9190 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009191 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
9192 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00009193 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00009194
9195 // Load the 'nest' parameter value into R10.
9196 // R10 is specified in X86CallingConv.td
9197 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00009198 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9199 DAG.getConstant(10, MVT::i64));
9200 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009201 Addr, MachinePointerInfo(TrmpAddr, 10),
9202 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009203
Owen Anderson825b72b2009-08-11 20:47:22 +00009204 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9205 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009206 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
9207 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00009208 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00009209
9210 // Jump to the nested function.
9211 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00009212 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9213 DAG.getConstant(20, MVT::i64));
9214 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009215 Addr, MachinePointerInfo(TrmpAddr, 20),
9216 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009217
9218 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00009219 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9220 DAG.getConstant(22, MVT::i64));
9221 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00009222 MachinePointerInfo(TrmpAddr, 22),
9223 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009224
Dan Gohman475871a2008-07-27 21:46:04 +00009225 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00009226 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00009227 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009228 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00009229 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00009230 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00009231 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00009232 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009233
9234 switch (CC) {
9235 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00009236 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00009237 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00009238 case CallingConv::X86_StdCall: {
9239 // Pass 'nest' parameter in ECX.
9240 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00009241 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009242
9243 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009244 FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00009245 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009246
Chris Lattner58d74912008-03-12 17:45:29 +00009247 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00009248 unsigned InRegCount = 0;
9249 unsigned Idx = 1;
9250
9251 for (FunctionType::param_iterator I = FTy->param_begin(),
9252 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00009253 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00009254 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00009255 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009256
9257 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00009258 report_fatal_error("Nest register in use - reduce number of inreg"
9259 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00009260 }
9261 }
9262 break;
9263 }
9264 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00009265 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00009266 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00009267 // Pass 'nest' parameter in EAX.
9268 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00009269 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009270 break;
9271 }
9272
Dan Gohman475871a2008-07-27 21:46:04 +00009273 SDValue OutChains[4];
9274 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009275
Owen Anderson825b72b2009-08-11 20:47:22 +00009276 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9277 DAG.getConstant(10, MVT::i32));
9278 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009279
Chris Lattnera62fe662010-02-05 19:20:30 +00009280 // This is storing the opcode for MOV32ri.
9281 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Evan Cheng0e6a0522011-07-18 20:57:22 +00009282 const unsigned char N86Reg = X86_MC::getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009283 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00009284 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009285 Trmp, MachinePointerInfo(TrmpAddr),
9286 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009287
Owen Anderson825b72b2009-08-11 20:47:22 +00009288 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9289 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009290 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
9291 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00009292 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009293
Chris Lattnera62fe662010-02-05 19:20:30 +00009294 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00009295 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9296 DAG.getConstant(5, MVT::i32));
9297 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00009298 MachinePointerInfo(TrmpAddr, 5),
9299 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009300
Owen Anderson825b72b2009-08-11 20:47:22 +00009301 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9302 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009303 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
9304 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00009305 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009306
Dan Gohman475871a2008-07-27 21:46:04 +00009307 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00009308 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00009309 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009310 }
9311}
9312
Dan Gohmand858e902010-04-17 15:26:15 +00009313SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
9314 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009315 /*
9316 The rounding mode is in bits 11:10 of FPSR, and has the following
9317 settings:
9318 00 Round to nearest
9319 01 Round to -inf
9320 10 Round to +inf
9321 11 Round to 0
9322
9323 FLT_ROUNDS, on the other hand, expects the following:
9324 -1 Undefined
9325 0 Round to 0
9326 1 Round to nearest
9327 2 Round to +inf
9328 3 Round to -inf
9329
9330 To perform the conversion, we do:
9331 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
9332 */
9333
9334 MachineFunction &MF = DAG.getMachineFunction();
9335 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00009336 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009337 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00009338 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00009339 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009340
9341 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00009342 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00009343 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009344
Michael J. Spencerec38de22010-10-10 22:04:20 +00009345
Chris Lattner2156b792010-09-22 01:11:26 +00009346 MachineMemOperand *MMO =
9347 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
9348 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009349
Chris Lattner2156b792010-09-22 01:11:26 +00009350 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
9351 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
9352 DAG.getVTList(MVT::Other),
9353 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009354
9355 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00009356 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Chris Lattner51abfe42010-09-21 06:02:19 +00009357 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009358
9359 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00009360 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00009361 DAG.getNode(ISD::SRL, DL, MVT::i16,
9362 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00009363 CWD, DAG.getConstant(0x800, MVT::i16)),
9364 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00009365 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00009366 DAG.getNode(ISD::SRL, DL, MVT::i16,
9367 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00009368 CWD, DAG.getConstant(0x400, MVT::i16)),
9369 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009370
Dan Gohman475871a2008-07-27 21:46:04 +00009371 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00009372 DAG.getNode(ISD::AND, DL, MVT::i16,
9373 DAG.getNode(ISD::ADD, DL, MVT::i16,
9374 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00009375 DAG.getConstant(1, MVT::i16)),
9376 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009377
9378
Duncan Sands83ec4b62008-06-06 12:08:01 +00009379 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00009380 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009381}
9382
Dan Gohmand858e902010-04-17 15:26:15 +00009383SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009384 EVT VT = Op.getValueType();
9385 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009386 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009387 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00009388
9389 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009390 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00009391 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00009392 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00009393 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009394 }
Evan Cheng18efe262007-12-14 02:13:44 +00009395
Evan Cheng152804e2007-12-14 08:30:15 +00009396 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00009397 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009398 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00009399
9400 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00009401 SDValue Ops[] = {
9402 Op,
9403 DAG.getConstant(NumBits+NumBits-1, OpVT),
9404 DAG.getConstant(X86::COND_E, MVT::i8),
9405 Op.getValue(1)
9406 };
9407 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00009408
9409 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00009410 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00009411
Owen Anderson825b72b2009-08-11 20:47:22 +00009412 if (VT == MVT::i8)
9413 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009414 return Op;
9415}
9416
Dan Gohmand858e902010-04-17 15:26:15 +00009417SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009418 EVT VT = Op.getValueType();
9419 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009420 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009421 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00009422
9423 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009424 if (VT == MVT::i8) {
9425 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00009426 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009427 }
Evan Cheng152804e2007-12-14 08:30:15 +00009428
9429 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00009430 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009431 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00009432
9433 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00009434 SDValue Ops[] = {
9435 Op,
9436 DAG.getConstant(NumBits, OpVT),
9437 DAG.getConstant(X86::COND_E, MVT::i8),
9438 Op.getValue(1)
9439 };
9440 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00009441
Owen Anderson825b72b2009-08-11 20:47:22 +00009442 if (VT == MVT::i8)
9443 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009444 return Op;
9445}
9446
Craig Topper13894fa2011-08-24 06:14:18 +00009447// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
9448// ones, and then concatenate the result back.
9449static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00009450 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +00009451
9452 assert(VT.getSizeInBits() == 256 && VT.isInteger() &&
9453 "Unsupported value type for operation");
9454
9455 int NumElems = VT.getVectorNumElements();
9456 DebugLoc dl = Op.getDebugLoc();
9457 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
9458 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
9459
9460 // Extract the LHS vectors
9461 SDValue LHS = Op.getOperand(0);
9462 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
9463 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
9464
9465 // Extract the RHS vectors
9466 SDValue RHS = Op.getOperand(1);
9467 SDValue RHS1 = Extract128BitVector(RHS, Idx0, DAG, dl);
9468 SDValue RHS2 = Extract128BitVector(RHS, Idx1, DAG, dl);
9469
9470 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9471 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
9472
9473 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
9474 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
9475 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
9476}
9477
9478SDValue X86TargetLowering::LowerADD(SDValue Op, SelectionDAG &DAG) const {
9479 assert(Op.getValueType().getSizeInBits() == 256 &&
9480 Op.getValueType().isInteger() &&
9481 "Only handle AVX 256-bit vector integer operation");
9482 return Lower256IntArith(Op, DAG);
9483}
9484
9485SDValue X86TargetLowering::LowerSUB(SDValue Op, SelectionDAG &DAG) const {
9486 assert(Op.getValueType().getSizeInBits() == 256 &&
9487 Op.getValueType().isInteger() &&
9488 "Only handle AVX 256-bit vector integer operation");
9489 return Lower256IntArith(Op, DAG);
9490}
9491
9492SDValue X86TargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
9493 EVT VT = Op.getValueType();
9494
9495 // Decompose 256-bit ops into smaller 128-bit ops.
9496 if (VT.getSizeInBits() == 256)
9497 return Lower256IntArith(Op, DAG);
9498
Owen Anderson825b72b2009-08-11 20:47:22 +00009499 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009500 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00009501
Mon P Wangaf9b9522008-12-18 21:42:19 +00009502 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
9503 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
9504 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
9505 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
9506 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
9507 //
9508 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
9509 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
9510 // return AloBlo + AloBhi + AhiBlo;
9511
9512 SDValue A = Op.getOperand(0);
9513 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009514
Dale Johannesene4d209d2009-02-03 20:21:25 +00009515 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009516 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
9517 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009518 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009519 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
9520 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009521 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009522 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00009523 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009524 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009525 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00009526 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009527 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009528 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00009529 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009530 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009531 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
9532 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009533 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009534 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
9535 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009536 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
9537 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00009538 return Res;
9539}
9540
Nadav Rotem43012222011-05-11 08:12:09 +00009541SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
9542
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009543 EVT VT = Op.getValueType();
9544 DebugLoc dl = Op.getDebugLoc();
9545 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +00009546 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009547 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009548
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00009549 if (!(Subtarget->hasSSE2() || Subtarget->hasAVX()))
9550 return SDValue();
9551
9552 // Decompose 256-bit shifts into smaller 128-bit shifts.
9553 if (VT.getSizeInBits() == 256) {
9554 int NumElems = VT.getVectorNumElements();
9555 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9556 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
9557
9558 // Extract the two vectors
9559 SDValue V1 = Extract128BitVector(R, DAG.getConstant(0, MVT::i32), DAG, dl);
9560 SDValue V2 = Extract128BitVector(R, DAG.getConstant(NumElems/2, MVT::i32),
9561 DAG, dl);
9562
9563 // Recreate the shift amount vectors
Bruno Cardoso Lopes0dd80b02011-08-17 22:12:20 +00009564 SDValue Amt1, Amt2;
9565 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
9566 // Constant shift amount
9567 SmallVector<SDValue, 4> Amt1Csts;
9568 SmallVector<SDValue, 4> Amt2Csts;
9569 for (int i = 0; i < NumElems/2; ++i)
9570 Amt1Csts.push_back(Amt->getOperand(i));
9571 for (int i = NumElems/2; i < NumElems; ++i)
9572 Amt2Csts.push_back(Amt->getOperand(i));
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00009573
Bruno Cardoso Lopes0dd80b02011-08-17 22:12:20 +00009574 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
9575 &Amt1Csts[0], NumElems/2);
9576 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
9577 &Amt2Csts[0], NumElems/2);
9578 } else {
9579 // Variable shift amount
9580 Amt1 = Extract128BitVector(Amt, DAG.getConstant(0, MVT::i32), DAG, dl);
9581 Amt2 = Extract128BitVector(Amt, DAG.getConstant(NumElems/2, MVT::i32),
9582 DAG, dl);
9583 }
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00009584
9585 // Issue new vector shifts for the smaller types
9586 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
9587 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
9588
9589 // Concatenate the result back
9590 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
9591 }
Nate Begeman51409212010-07-28 00:21:48 +00009592
Nadav Rotem43012222011-05-11 08:12:09 +00009593 // Optimize shl/srl/sra with constant shift amount.
9594 if (isSplatVector(Amt.getNode())) {
9595 SDValue SclrAmt = Amt->getOperand(0);
9596 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
9597 uint64_t ShiftAmt = C->getZExtValue();
9598
9599 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SHL)
9600 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9601 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
9602 R, DAG.getConstant(ShiftAmt, MVT::i32));
9603
9604 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SHL)
9605 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9606 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
9607 R, DAG.getConstant(ShiftAmt, MVT::i32));
9608
9609 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SHL)
9610 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9611 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
9612 R, DAG.getConstant(ShiftAmt, MVT::i32));
9613
9614 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SRL)
9615 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9616 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
9617 R, DAG.getConstant(ShiftAmt, MVT::i32));
9618
9619 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRL)
9620 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9621 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
9622 R, DAG.getConstant(ShiftAmt, MVT::i32));
9623
9624 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRL)
9625 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9626 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
9627 R, DAG.getConstant(ShiftAmt, MVT::i32));
9628
9629 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRA)
9630 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9631 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
9632 R, DAG.getConstant(ShiftAmt, MVT::i32));
9633
9634 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRA)
9635 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9636 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
9637 R, DAG.getConstant(ShiftAmt, MVT::i32));
9638 }
9639 }
9640
9641 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +00009642 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +00009643 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9644 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
9645 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
9646
9647 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009648
Nate Begeman51409212010-07-28 00:21:48 +00009649 std::vector<Constant*> CV(4, CI);
9650 Constant *C = ConstantVector::get(CV);
9651 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
9652 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00009653 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00009654 false, false, 16);
9655
9656 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009657 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +00009658 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
9659 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
9660 }
Nadav Rotem43012222011-05-11 08:12:09 +00009661 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +00009662 // a = a << 5;
9663 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9664 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
9665 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
9666
9667 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
9668 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
9669
9670 std::vector<Constant*> CVM1(16, CM1);
9671 std::vector<Constant*> CVM2(16, CM2);
9672 Constant *C = ConstantVector::get(CVM1);
9673 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
9674 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00009675 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00009676 false, false, 16);
9677
9678 // r = pblendv(r, psllw(r & (char16)15, 4), a);
9679 M = DAG.getNode(ISD::AND, dl, VT, R, M);
9680 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9681 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
9682 DAG.getConstant(4, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00009683 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00009684 // a += a
9685 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009686
Nate Begeman51409212010-07-28 00:21:48 +00009687 C = ConstantVector::get(CVM2);
9688 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
9689 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00009690 MachinePointerInfo::getConstantPool(),
Chris Lattner51abfe42010-09-21 06:02:19 +00009691 false, false, 16);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009692
Nate Begeman51409212010-07-28 00:21:48 +00009693 // r = pblendv(r, psllw(r & (char16)63, 2), a);
9694 M = DAG.getNode(ISD::AND, dl, VT, R, M);
9695 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9696 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
9697 DAG.getConstant(2, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00009698 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00009699 // a += a
9700 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009701
Nate Begeman51409212010-07-28 00:21:48 +00009702 // return pblendv(r, r+r, a);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009703 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT,
Nate Begeman51409212010-07-28 00:21:48 +00009704 R, DAG.getNode(ISD::ADD, dl, VT, R, R), Op);
9705 return R;
9706 }
9707 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009708}
Mon P Wangaf9b9522008-12-18 21:42:19 +00009709
Dan Gohmand858e902010-04-17 15:26:15 +00009710SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +00009711 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
9712 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00009713 // looks for this combo and may remove the "setcc" instruction if the "setcc"
9714 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00009715 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00009716 SDValue LHS = N->getOperand(0);
9717 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00009718 unsigned BaseOp = 0;
9719 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009720 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00009721 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00009722 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00009723 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00009724 // A subtract of one will be selected as a INC. Note that INC doesn't
9725 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +00009726 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
9727 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +00009728 BaseOp = X86ISD::INC;
9729 Cond = X86::COND_O;
9730 break;
9731 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009732 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00009733 Cond = X86::COND_O;
9734 break;
9735 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009736 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00009737 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00009738 break;
9739 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00009740 // A subtract of one will be selected as a DEC. Note that DEC doesn't
9741 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +00009742 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
9743 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +00009744 BaseOp = X86ISD::DEC;
9745 Cond = X86::COND_O;
9746 break;
9747 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009748 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00009749 Cond = X86::COND_O;
9750 break;
9751 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009752 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00009753 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00009754 break;
9755 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00009756 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00009757 Cond = X86::COND_O;
9758 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009759 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
9760 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
9761 MVT::i32);
9762 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009763
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009764 SDValue SetCC =
9765 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9766 DAG.getConstant(X86::COND_O, MVT::i32),
9767 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009768
Dan Gohman6e5fda22011-07-22 18:45:15 +00009769 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009770 }
Bill Wendling74c37652008-12-09 22:08:41 +00009771 }
Bill Wendling3fafd932008-11-26 22:37:40 +00009772
Bill Wendling61edeb52008-12-02 01:06:39 +00009773 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00009774 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009775 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00009776
Bill Wendling61edeb52008-12-02 01:06:39 +00009777 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009778 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
9779 DAG.getConstant(Cond, MVT::i32),
9780 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00009781
Dan Gohman6e5fda22011-07-22 18:45:15 +00009782 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +00009783}
9784
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00009785SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op, SelectionDAG &DAG) const{
9786 DebugLoc dl = Op.getDebugLoc();
9787 SDNode* Node = Op.getNode();
9788 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
9789 EVT VT = Node->getValueType(0);
9790
9791 if (Subtarget->hasSSE2() && VT.isVector()) {
9792 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
9793 ExtraVT.getScalarType().getSizeInBits();
9794 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
9795
9796 unsigned SHLIntrinsicsID = 0;
9797 unsigned SRAIntrinsicsID = 0;
9798 switch (VT.getSimpleVT().SimpleTy) {
9799 default:
9800 return SDValue();
9801 case MVT::v2i64: {
9802 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_q;
9803 SRAIntrinsicsID = 0;
9804 break;
9805 }
9806 case MVT::v4i32: {
9807 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_d;
9808 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_d;
9809 break;
9810 }
9811 case MVT::v8i16: {
9812 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_w;
9813 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_w;
9814 break;
9815 }
9816 }
9817
9818 SDValue Tmp1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9819 DAG.getConstant(SHLIntrinsicsID, MVT::i32),
9820 Node->getOperand(0), ShAmt);
9821
9822 // In case of 1 bit sext, no need to shr
9823 if (ExtraVT.getScalarType().getSizeInBits() == 1) return Tmp1;
9824
9825 if (SRAIntrinsicsID) {
9826 Tmp1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9827 DAG.getConstant(SRAIntrinsicsID, MVT::i32),
9828 Tmp1, ShAmt);
9829 }
9830 return Tmp1;
9831 }
9832
9833 return SDValue();
9834}
9835
9836
Eric Christopher9a9d2752010-07-22 02:48:34 +00009837SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
9838 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00009839
Eric Christopher77ed1352011-07-08 00:04:56 +00009840 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
9841 // There isn't any reason to disable it if the target processor supports it.
9842 if (!Subtarget->hasSSE2() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +00009843 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +00009844 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +00009845 SDValue Ops[] = {
9846 DAG.getRegister(X86::ESP, MVT::i32), // Base
9847 DAG.getTargetConstant(1, MVT::i8), // Scale
9848 DAG.getRegister(0, MVT::i32), // Index
9849 DAG.getTargetConstant(0, MVT::i32), // Disp
9850 DAG.getRegister(0, MVT::i32), // Segment.
9851 Zero,
9852 Chain
9853 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00009854 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +00009855 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
9856 array_lengthof(Ops));
9857 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +00009858 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00009859
Eric Christopher9a9d2752010-07-22 02:48:34 +00009860 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +00009861 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +00009862 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009863
Chris Lattner132929a2010-08-14 17:26:09 +00009864 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
9865 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
9866 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
9867 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +00009868
Chris Lattner132929a2010-08-14 17:26:09 +00009869 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
9870 if (!Op1 && !Op2 && !Op3 && Op4)
9871 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009872
Chris Lattner132929a2010-08-14 17:26:09 +00009873 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
9874 if (Op1 && !Op2 && !Op3 && !Op4)
9875 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009876
9877 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +00009878 // (MFENCE)>;
9879 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +00009880}
9881
Eli Friedman14648462011-07-27 22:21:52 +00009882SDValue X86TargetLowering::LowerATOMIC_FENCE(SDValue Op,
9883 SelectionDAG &DAG) const {
9884 DebugLoc dl = Op.getDebugLoc();
9885 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
9886 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
9887 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
9888 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
9889
9890 // The only fence that needs an instruction is a sequentially-consistent
9891 // cross-thread fence.
9892 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
9893 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
9894 // no-sse2). There isn't any reason to disable it if the target processor
9895 // supports it.
9896 if (Subtarget->hasSSE2() || Subtarget->is64Bit())
9897 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
9898
9899 SDValue Chain = Op.getOperand(0);
9900 SDValue Zero = DAG.getConstant(0, MVT::i32);
9901 SDValue Ops[] = {
9902 DAG.getRegister(X86::ESP, MVT::i32), // Base
9903 DAG.getTargetConstant(1, MVT::i8), // Scale
9904 DAG.getRegister(0, MVT::i32), // Index
9905 DAG.getTargetConstant(0, MVT::i32), // Disp
9906 DAG.getRegister(0, MVT::i32), // Segment.
9907 Zero,
9908 Chain
9909 };
9910 SDNode *Res =
9911 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
9912 array_lengthof(Ops));
9913 return SDValue(Res, 0);
9914 }
9915
9916 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
9917 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
9918}
9919
9920
Dan Gohmand858e902010-04-17 15:26:15 +00009921SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009922 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +00009923 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00009924 unsigned Reg = 0;
9925 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00009926 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00009927 default:
9928 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00009929 case MVT::i8: Reg = X86::AL; size = 1; break;
9930 case MVT::i16: Reg = X86::AX; size = 2; break;
9931 case MVT::i32: Reg = X86::EAX; size = 4; break;
9932 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00009933 assert(Subtarget->is64Bit() && "Node not type legal!");
9934 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00009935 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00009936 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +00009937 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00009938 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00009939 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00009940 Op.getOperand(1),
9941 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00009942 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00009943 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009944 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +00009945 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
9946 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
9947 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +00009948 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +00009949 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00009950 return cpOut;
9951}
9952
Duncan Sands1607f052008-12-01 11:39:25 +00009953SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009954 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +00009955 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009956 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00009957 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009958 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00009959 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009960 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
9961 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00009962 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00009963 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
9964 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00009965 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00009966 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00009967 rdx.getValue(1)
9968 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00009969 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009970}
9971
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009972SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +00009973 SelectionDAG &DAG) const {
9974 EVT SrcVT = Op.getOperand(0).getValueType();
9975 EVT DstVT = Op.getValueType();
Chris Lattner2a786eb2010-12-19 20:19:20 +00009976 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
9977 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +00009978 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +00009979 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009980 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +00009981 // i64 <=> MMX conversions are Legal.
9982 if (SrcVT==MVT::i64 && DstVT.isVector())
9983 return Op;
9984 if (DstVT==MVT::i64 && SrcVT.isVector())
9985 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +00009986 // MMX <=> MMX conversions are Legal.
9987 if (SrcVT.isVector() && DstVT.isVector())
9988 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +00009989 // All other conversions need to be expanded.
9990 return SDValue();
9991}
Chris Lattner5b856542010-12-20 00:59:46 +00009992
Dan Gohmand858e902010-04-17 15:26:15 +00009993SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +00009994 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00009995 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00009996 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009997 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00009998 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009999 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010000 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000010001 Node->getOperand(0),
10002 Node->getOperand(1), negOp,
10003 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000010004 cast<AtomicSDNode>(Node)->getAlignment(),
10005 cast<AtomicSDNode>(Node)->getOrdering(),
10006 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000010007}
10008
Eli Friedman327236c2011-08-24 20:50:09 +000010009static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
10010 SDNode *Node = Op.getNode();
10011 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010012 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000010013
10014 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010015 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
10016 // FIXME: On 32-bit, store -> fist or movq would be more efficient
10017 // (The only way to get a 16-byte store is cmpxchg16b)
10018 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
10019 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
10020 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000010021 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
10022 cast<AtomicSDNode>(Node)->getMemoryVT(),
10023 Node->getOperand(0),
10024 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010025 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000010026 cast<AtomicSDNode>(Node)->getOrdering(),
10027 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000010028 return Swap.getValue(1);
10029 }
10030 // Other atomic stores have a simple pattern.
10031 return Op;
10032}
10033
Chris Lattner5b856542010-12-20 00:59:46 +000010034static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
10035 EVT VT = Op.getNode()->getValueType(0);
10036
10037 // Let legalize expand this if it isn't a legal type yet.
10038 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
10039 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010040
Chris Lattner5b856542010-12-20 00:59:46 +000010041 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010042
Chris Lattner5b856542010-12-20 00:59:46 +000010043 unsigned Opc;
10044 bool ExtraOp = false;
10045 switch (Op.getOpcode()) {
10046 default: assert(0 && "Invalid code");
10047 case ISD::ADDC: Opc = X86ISD::ADD; break;
10048 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
10049 case ISD::SUBC: Opc = X86ISD::SUB; break;
10050 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
10051 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010052
Chris Lattner5b856542010-12-20 00:59:46 +000010053 if (!ExtraOp)
10054 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
10055 Op.getOperand(1));
10056 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
10057 Op.getOperand(1), Op.getOperand(2));
10058}
10059
Evan Cheng0db9fe62006-04-25 20:13:52 +000010060/// LowerOperation - Provide custom lowering hooks for some operations.
10061///
Dan Gohmand858e902010-04-17 15:26:15 +000010062SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000010063 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010064 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010065 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Eric Christopher9a9d2752010-07-22 02:48:34 +000010066 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Eli Friedman14648462011-07-27 22:21:52 +000010067 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010068 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
10069 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000010070 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010071 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000010072 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010073 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
10074 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
10075 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +000010076 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +000010077 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010078 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
10079 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
10080 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000010081 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000010082 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000010083 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010084 case ISD::SHL_PARTS:
10085 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000010086 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010087 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000010088 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010089 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000010090 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010091 case ISD::FABS: return LowerFABS(Op, DAG);
10092 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000010093 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000010094 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000010095 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +000010096 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000010097 case ISD::SELECT: return LowerSELECT(Op, DAG);
10098 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010099 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010100 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000010101 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +000010102 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010103 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000010104 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
10105 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010106 case ISD::FRAME_TO_ARGS_OFFSET:
10107 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010108 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010109 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010110 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000010111 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000010112 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
10113 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000010114 case ISD::MUL: return LowerMUL(Op, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000010115 case ISD::SRA:
10116 case ISD::SRL:
10117 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000010118 case ISD::SADDO:
10119 case ISD::UADDO:
10120 case ISD::SSUBO:
10121 case ISD::USUBO:
10122 case ISD::SMULO:
10123 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +000010124 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010125 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000010126 case ISD::ADDC:
10127 case ISD::ADDE:
10128 case ISD::SUBC:
10129 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000010130 case ISD::ADD: return LowerADD(Op, DAG);
10131 case ISD::SUB: return LowerSUB(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010132 }
Chris Lattner27a6c732007-11-24 07:07:01 +000010133}
10134
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010135static void ReplaceATOMIC_LOAD(SDNode *Node,
10136 SmallVectorImpl<SDValue> &Results,
10137 SelectionDAG &DAG) {
10138 DebugLoc dl = Node->getDebugLoc();
10139 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
10140
10141 // Convert wide load -> cmpxchg8b/cmpxchg16b
10142 // FIXME: On 32-bit, load -> fild or movq would be more efficient
10143 // (The only way to get a 16-byte load is cmpxchg16b)
10144 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
10145 SDValue Zero = DAG.getConstant(0, cast<AtomicSDNode>(Node)->getMemoryVT());
10146 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl,
10147 cast<AtomicSDNode>(Node)->getMemoryVT(),
10148 Node->getOperand(0),
10149 Node->getOperand(1), Zero, Zero,
10150 cast<AtomicSDNode>(Node)->getMemOperand(),
10151 cast<AtomicSDNode>(Node)->getOrdering(),
10152 cast<AtomicSDNode>(Node)->getSynchScope());
10153 Results.push_back(Swap.getValue(0));
10154 Results.push_back(Swap.getValue(1));
10155}
10156
Duncan Sands1607f052008-12-01 11:39:25 +000010157void X86TargetLowering::
10158ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000010159 SelectionDAG &DAG, unsigned NewOp) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010160 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010161 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +000010162 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000010163
10164 SDValue Chain = Node->getOperand(0);
10165 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010166 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010167 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000010168 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010169 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000010170 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000010171 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000010172 SDValue Result =
10173 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
10174 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000010175 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000010176 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010177 Results.push_back(Result.getValue(2));
10178}
10179
Duncan Sands126d9072008-07-04 11:47:58 +000010180/// ReplaceNodeResults - Replace a node with an illegal result type
10181/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000010182void X86TargetLowering::ReplaceNodeResults(SDNode *N,
10183 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000010184 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010185 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +000010186 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000010187 default:
Duncan Sands1607f052008-12-01 11:39:25 +000010188 assert(false && "Do not know how to custom type legalize this operation!");
10189 return;
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010190 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000010191 case ISD::ADDC:
10192 case ISD::ADDE:
10193 case ISD::SUBC:
10194 case ISD::SUBE:
10195 // We don't want to expand or promote these.
10196 return;
Duncan Sands1607f052008-12-01 11:39:25 +000010197 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +000010198 std::pair<SDValue,SDValue> Vals =
10199 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +000010200 SDValue FIST = Vals.first, StackSlot = Vals.second;
10201 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000010202 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000010203 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +000010204 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
10205 MachinePointerInfo(), false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +000010206 }
10207 return;
10208 }
10209 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010210 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000010211 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010212 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010213 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000010214 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000010215 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010216 eax.getValue(2));
10217 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
10218 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +000010219 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010220 Results.push_back(edx.getValue(1));
10221 return;
10222 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010223 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000010224 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010225 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +000010226 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +000010227 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
10228 DAG.getConstant(0, MVT::i32));
10229 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
10230 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +000010231 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
10232 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +000010233 cpInL.getValue(1));
10234 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +000010235 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
10236 DAG.getConstant(0, MVT::i32));
10237 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
10238 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +000010239 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +000010240 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +000010241 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +000010242 swapInL.getValue(1));
10243 SDValue Ops[] = { swapInH.getValue(0),
10244 N->getOperand(1),
10245 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010246 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000010247 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
10248 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG8_DAG, dl, Tys,
10249 Ops, 3, T, MMO);
Dale Johannesendd64c412009-02-04 00:33:20 +000010250 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +000010251 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +000010252 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +000010253 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000010254 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +000010255 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010256 Results.push_back(cpOutH.getValue(1));
10257 return;
10258 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010259 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +000010260 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
10261 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010262 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +000010263 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
10264 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010265 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +000010266 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
10267 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010268 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +000010269 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
10270 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010271 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +000010272 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
10273 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010274 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +000010275 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
10276 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010277 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +000010278 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
10279 return;
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010280 case ISD::ATOMIC_LOAD:
10281 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000010282 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000010283}
10284
Evan Cheng72261582005-12-20 06:22:03 +000010285const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
10286 switch (Opcode) {
10287 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000010288 case X86ISD::BSF: return "X86ISD::BSF";
10289 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000010290 case X86ISD::SHLD: return "X86ISD::SHLD";
10291 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000010292 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000010293 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000010294 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000010295 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000010296 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000010297 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000010298 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
10299 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
10300 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000010301 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000010302 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000010303 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000010304 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000010305 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000010306 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000010307 case X86ISD::COMI: return "X86ISD::COMI";
10308 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000010309 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000010310 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000010311 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
10312 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000010313 case X86ISD::CMOV: return "X86ISD::CMOV";
10314 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000010315 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000010316 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
10317 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000010318 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000010319 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000010320 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000010321 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000010322 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000010323 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
10324 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000010325 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000010326 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000010327 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Nate Begemanb65c1752010-12-17 22:55:37 +000010328 case X86ISD::PSIGNB: return "X86ISD::PSIGNB";
10329 case X86ISD::PSIGNW: return "X86ISD::PSIGNW";
10330 case X86ISD::PSIGND: return "X86ISD::PSIGND";
Nate Begeman672fb622010-12-20 22:04:24 +000010331 case X86ISD::PBLENDVB: return "X86ISD::PBLENDVB";
Evan Cheng8ca29322006-11-10 21:43:37 +000010332 case X86ISD::FMAX: return "X86ISD::FMAX";
10333 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +000010334 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
10335 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000010336 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000010337 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010338 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000010339 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010340 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +000010341 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
10342 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010343 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
10344 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
10345 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
10346 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
10347 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
10348 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000010349 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
10350 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +000010351 case X86ISD::VSHL: return "X86ISD::VSHL";
10352 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +000010353 case X86ISD::CMPPD: return "X86ISD::CMPPD";
10354 case X86ISD::CMPPS: return "X86ISD::CMPPS";
10355 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
10356 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
10357 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
10358 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
10359 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
10360 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
10361 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
10362 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010363 case X86ISD::ADD: return "X86ISD::ADD";
10364 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000010365 case X86ISD::ADC: return "X86ISD::ADC";
10366 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000010367 case X86ISD::SMUL: return "X86ISD::SMUL";
10368 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000010369 case X86ISD::INC: return "X86ISD::INC";
10370 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000010371 case X86ISD::OR: return "X86ISD::OR";
10372 case X86ISD::XOR: return "X86ISD::XOR";
10373 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +000010374 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000010375 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010376 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010377 case X86ISD::PALIGN: return "X86ISD::PALIGN";
10378 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
10379 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
10380 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
10381 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
10382 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
10383 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
10384 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
10385 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010386 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000010387 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010388 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000010389 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
10390 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010391 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
10392 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
10393 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
10394 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
10395 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
10396 case X86ISD::MOVSD: return "X86ISD::MOVSD";
10397 case X86ISD::MOVSS: return "X86ISD::MOVSS";
10398 case X86ISD::UNPCKLPS: return "X86ISD::UNPCKLPS";
10399 case X86ISD::UNPCKLPD: return "X86ISD::UNPCKLPD";
David Greenefbf05d32011-02-22 23:31:46 +000010400 case X86ISD::VUNPCKLPDY: return "X86ISD::VUNPCKLPDY";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010401 case X86ISD::UNPCKHPS: return "X86ISD::UNPCKHPS";
10402 case X86ISD::UNPCKHPD: return "X86ISD::UNPCKHPD";
10403 case X86ISD::PUNPCKLBW: return "X86ISD::PUNPCKLBW";
10404 case X86ISD::PUNPCKLWD: return "X86ISD::PUNPCKLWD";
10405 case X86ISD::PUNPCKLDQ: return "X86ISD::PUNPCKLDQ";
10406 case X86ISD::PUNPCKLQDQ: return "X86ISD::PUNPCKLQDQ";
10407 case X86ISD::PUNPCKHBW: return "X86ISD::PUNPCKHBW";
10408 case X86ISD::PUNPCKHWD: return "X86ISD::PUNPCKHWD";
10409 case X86ISD::PUNPCKHDQ: return "X86ISD::PUNPCKHDQ";
10410 case X86ISD::PUNPCKHQDQ: return "X86ISD::PUNPCKHQDQ";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000010411 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +000010412 case X86ISD::VPERMILPS: return "X86ISD::VPERMILPS";
10413 case X86ISD::VPERMILPSY: return "X86ISD::VPERMILPSY";
10414 case X86ISD::VPERMILPD: return "X86ISD::VPERMILPD";
10415 case X86ISD::VPERMILPDY: return "X86ISD::VPERMILPDY";
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +000010416 case X86ISD::VPERM2F128: return "X86ISD::VPERM2F128";
Dan Gohmand6708ea2009-08-15 01:38:56 +000010417 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000010418 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010419 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000010420 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Evan Cheng72261582005-12-20 06:22:03 +000010421 }
10422}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010423
Chris Lattnerc9addb72007-03-30 23:15:24 +000010424// isLegalAddressingMode - Return true if the addressing mode represented
10425// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000010426bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010427 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000010428 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010429 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000010430 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000010431
Chris Lattnerc9addb72007-03-30 23:15:24 +000010432 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010433 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000010434 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000010435
Chris Lattnerc9addb72007-03-30 23:15:24 +000010436 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000010437 unsigned GVFlags =
10438 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010439
Chris Lattnerdfed4132009-07-10 07:38:24 +000010440 // If a reference to this global requires an extra load, we can't fold it.
10441 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000010442 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010443
Chris Lattnerdfed4132009-07-10 07:38:24 +000010444 // If BaseGV requires a register for the PIC base, we cannot also have a
10445 // BaseReg specified.
10446 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000010447 return false;
Evan Cheng52787842007-08-01 23:46:47 +000010448
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010449 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000010450 if ((M != CodeModel::Small || R != Reloc::Static) &&
10451 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010452 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000010453 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010454
Chris Lattnerc9addb72007-03-30 23:15:24 +000010455 switch (AM.Scale) {
10456 case 0:
10457 case 1:
10458 case 2:
10459 case 4:
10460 case 8:
10461 // These scales always work.
10462 break;
10463 case 3:
10464 case 5:
10465 case 9:
10466 // These scales are formed with basereg+scalereg. Only accept if there is
10467 // no basereg yet.
10468 if (AM.HasBaseReg)
10469 return false;
10470 break;
10471 default: // Other stuff never works.
10472 return false;
10473 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010474
Chris Lattnerc9addb72007-03-30 23:15:24 +000010475 return true;
10476}
10477
10478
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010479bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000010480 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000010481 return false;
Evan Chenge127a732007-10-29 07:57:50 +000010482 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
10483 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000010484 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +000010485 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000010486 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +000010487}
10488
Owen Andersone50ed302009-08-10 22:56:29 +000010489bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000010490 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000010491 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000010492 unsigned NumBits1 = VT1.getSizeInBits();
10493 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000010494 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +000010495 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000010496 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000010497}
Evan Cheng2bd122c2007-10-26 01:56:11 +000010498
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010499bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000010500 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000010501 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000010502}
10503
Owen Andersone50ed302009-08-10 22:56:29 +000010504bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000010505 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000010506 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000010507}
10508
Owen Andersone50ed302009-08-10 22:56:29 +000010509bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000010510 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000010511 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000010512}
10513
Evan Cheng60c07e12006-07-05 22:17:51 +000010514/// isShuffleMaskLegal - Targets can use this to indicate that they only
10515/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
10516/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
10517/// are assumed to be legal.
10518bool
Eric Christopherfd179292009-08-27 18:07:15 +000010519X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000010520 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000010521 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000010522 if (VT.getSizeInBits() == 64)
Eric Christophercff6f852010-04-15 01:40:20 +000010523 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3());
Nate Begeman9008ca62009-04-27 18:41:29 +000010524
Nate Begemana09008b2009-10-19 02:17:23 +000010525 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000010526 return (VT.getVectorNumElements() == 2 ||
10527 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
10528 isMOVLMask(M, VT) ||
10529 isSHUFPMask(M, VT) ||
10530 isPSHUFDMask(M, VT) ||
10531 isPSHUFHWMask(M, VT) ||
10532 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +000010533 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000010534 isUNPCKLMask(M, VT) ||
10535 isUNPCKHMask(M, VT) ||
10536 isUNPCKL_v_undef_Mask(M, VT) ||
10537 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +000010538}
10539
Dan Gohman7d8143f2008-04-09 20:09:42 +000010540bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000010541X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000010542 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000010543 unsigned NumElts = VT.getVectorNumElements();
10544 // FIXME: This collection of masks seems suspect.
10545 if (NumElts == 2)
10546 return true;
10547 if (NumElts == 4 && VT.getSizeInBits() == 128) {
10548 return (isMOVLMask(Mask, VT) ||
10549 isCommutedMOVLMask(Mask, VT, true) ||
10550 isSHUFPMask(Mask, VT) ||
10551 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +000010552 }
10553 return false;
10554}
10555
10556//===----------------------------------------------------------------------===//
10557// X86 Scheduler Hooks
10558//===----------------------------------------------------------------------===//
10559
Mon P Wang63307c32008-05-05 19:05:59 +000010560// private utility function
10561MachineBasicBlock *
10562X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
10563 MachineBasicBlock *MBB,
10564 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010565 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010566 unsigned LoadOpc,
10567 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010568 unsigned notOpc,
10569 unsigned EAXreg,
10570 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000010571 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000010572 // For the atomic bitwise operator, we generate
10573 // thisMBB:
10574 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000010575 // ld t1 = [bitinstr.addr]
10576 // op t2 = t1, [bitinstr.val]
10577 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000010578 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
10579 // bz newMBB
10580 // fallthrough -->nextMBB
10581 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10582 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000010583 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000010584 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000010585
Mon P Wang63307c32008-05-05 19:05:59 +000010586 /// First build the CFG
10587 MachineFunction *F = MBB->getParent();
10588 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000010589 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
10590 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
10591 F->insert(MBBIter, newMBB);
10592 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010593
Dan Gohman14152b42010-07-06 20:24:04 +000010594 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
10595 nextMBB->splice(nextMBB->begin(), thisMBB,
10596 llvm::next(MachineBasicBlock::iterator(bInstr)),
10597 thisMBB->end());
10598 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010599
Mon P Wang63307c32008-05-05 19:05:59 +000010600 // Update thisMBB to fall through to newMBB
10601 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010602
Mon P Wang63307c32008-05-05 19:05:59 +000010603 // newMBB jumps to itself and fall through to nextMBB
10604 newMBB->addSuccessor(nextMBB);
10605 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010606
Mon P Wang63307c32008-05-05 19:05:59 +000010607 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010608 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000010609 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +000010610 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000010611 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010612 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000010613 int numArgs = bInstr->getNumOperands() - 1;
10614 for (int i=0; i < numArgs; ++i)
10615 argOpers[i] = &bInstr->getOperand(i+1);
10616
10617 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010618 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000010619 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000010620
Dale Johannesen140be2d2008-08-19 18:47:28 +000010621 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010622 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000010623 for (int i=0; i <= lastAddrIndx; ++i)
10624 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010625
Dale Johannesen140be2d2008-08-19 18:47:28 +000010626 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010627 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010628 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010629 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010630 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010631 tt = t1;
10632
Dale Johannesen140be2d2008-08-19 18:47:28 +000010633 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +000010634 assert((argOpers[valArgIndx]->isReg() ||
10635 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000010636 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +000010637 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000010638 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000010639 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000010640 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010641 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +000010642 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010643
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010644 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +000010645 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +000010646
Dale Johannesene4d209d2009-02-03 20:21:25 +000010647 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +000010648 for (int i=0; i <= lastAddrIndx; ++i)
10649 (*MIB).addOperand(*argOpers[i]);
10650 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +000010651 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000010652 (*MIB).setMemRefs(bInstr->memoperands_begin(),
10653 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +000010654
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010655 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +000010656 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +000010657
Mon P Wang63307c32008-05-05 19:05:59 +000010658 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000010659 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000010660
Dan Gohman14152b42010-07-06 20:24:04 +000010661 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000010662 return nextMBB;
10663}
10664
Dale Johannesen1b54c7f2008-10-03 19:41:08 +000010665// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +000010666MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010667X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
10668 MachineBasicBlock *MBB,
10669 unsigned regOpcL,
10670 unsigned regOpcH,
10671 unsigned immOpcL,
10672 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000010673 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010674 // For the atomic bitwise operator, we generate
10675 // thisMBB (instructions are in pairs, except cmpxchg8b)
10676 // ld t1,t2 = [bitinstr.addr]
10677 // newMBB:
10678 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
10679 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +000010680 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010681 // mov ECX, EBX <- t5, t6
10682 // mov EAX, EDX <- t1, t2
10683 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
10684 // mov t3, t4 <- EAX, EDX
10685 // bz newMBB
10686 // result in out1, out2
10687 // fallthrough -->nextMBB
10688
10689 const TargetRegisterClass *RC = X86::GR32RegisterClass;
10690 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010691 const unsigned NotOpc = X86::NOT32r;
10692 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10693 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
10694 MachineFunction::iterator MBBIter = MBB;
10695 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000010696
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010697 /// First build the CFG
10698 MachineFunction *F = MBB->getParent();
10699 MachineBasicBlock *thisMBB = MBB;
10700 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
10701 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
10702 F->insert(MBBIter, newMBB);
10703 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010704
Dan Gohman14152b42010-07-06 20:24:04 +000010705 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
10706 nextMBB->splice(nextMBB->begin(), thisMBB,
10707 llvm::next(MachineBasicBlock::iterator(bInstr)),
10708 thisMBB->end());
10709 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010710
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010711 // Update thisMBB to fall through to newMBB
10712 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010713
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010714 // newMBB jumps to itself and fall through to nextMBB
10715 newMBB->addSuccessor(nextMBB);
10716 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010717
Dale Johannesene4d209d2009-02-03 20:21:25 +000010718 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010719 // Insert instructions into newMBB based on incoming instruction
10720 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010721 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000010722 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010723 MachineOperand& dest1Oper = bInstr->getOperand(0);
10724 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010725 MachineOperand* argOpers[2 + X86::AddrNumOperands];
10726 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010727 argOpers[i] = &bInstr->getOperand(i+2);
10728
Dan Gohman71ea4e52010-05-14 21:01:44 +000010729 // We use some of the operands multiple times, so conservatively just
10730 // clear any kill flags that might be present.
10731 if (argOpers[i]->isReg() && argOpers[i]->isUse())
10732 argOpers[i]->setIsKill(false);
10733 }
10734
Evan Chengad5b52f2010-01-08 19:14:57 +000010735 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010736 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +000010737
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010738 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010739 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010740 for (int i=0; i <= lastAddrIndx; ++i)
10741 (*MIB).addOperand(*argOpers[i]);
10742 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010743 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +000010744 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +000010745 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010746 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +000010747 MachineOperand newOp3 = *(argOpers[3]);
10748 if (newOp3.isImm())
10749 newOp3.setImm(newOp3.getImm()+4);
10750 else
10751 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010752 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +000010753 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010754
10755 // t3/4 are defined later, at the bottom of the loop
10756 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
10757 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010758 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010759 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010760 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010761 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
10762
Evan Cheng306b4ca2010-01-08 23:41:50 +000010763 // The subsequent operations should be using the destination registers of
10764 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +000010765 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +000010766 t1 = F->getRegInfo().createVirtualRegister(RC);
10767 t2 = F->getRegInfo().createVirtualRegister(RC);
10768 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
10769 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010770 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +000010771 t1 = dest1Oper.getReg();
10772 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010773 }
10774
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000010775 int valArgIndx = lastAddrIndx + 1;
10776 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +000010777 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010778 "invalid operand");
10779 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
10780 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000010781 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000010782 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010783 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000010784 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +000010785 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000010786 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000010787 (*MIB).addOperand(*argOpers[valArgIndx]);
10788 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000010789 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000010790 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000010791 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000010792 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000010793 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010794 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000010795 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +000010796 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000010797 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000010798 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010799
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010800 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010801 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010802 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010803 MIB.addReg(t2);
10804
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010805 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010806 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010807 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010808 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +000010809
Dale Johannesene4d209d2009-02-03 20:21:25 +000010810 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010811 for (int i=0; i <= lastAddrIndx; ++i)
10812 (*MIB).addOperand(*argOpers[i]);
10813
10814 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000010815 (*MIB).setMemRefs(bInstr->memoperands_begin(),
10816 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010817
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010818 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010819 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010820 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010821 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000010822
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010823 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000010824 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010825
Dan Gohman14152b42010-07-06 20:24:04 +000010826 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010827 return nextMBB;
10828}
10829
10830// private utility function
10831MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +000010832X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
10833 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000010834 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000010835 // For the atomic min/max operator, we generate
10836 // thisMBB:
10837 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000010838 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +000010839 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +000010840 // cmp t1, t2
10841 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +000010842 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000010843 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
10844 // bz newMBB
10845 // fallthrough -->nextMBB
10846 //
10847 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10848 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000010849 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000010850 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000010851
Mon P Wang63307c32008-05-05 19:05:59 +000010852 /// First build the CFG
10853 MachineFunction *F = MBB->getParent();
10854 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000010855 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
10856 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
10857 F->insert(MBBIter, newMBB);
10858 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010859
Dan Gohman14152b42010-07-06 20:24:04 +000010860 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
10861 nextMBB->splice(nextMBB->begin(), thisMBB,
10862 llvm::next(MachineBasicBlock::iterator(mInstr)),
10863 thisMBB->end());
10864 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010865
Mon P Wang63307c32008-05-05 19:05:59 +000010866 // Update thisMBB to fall through to newMBB
10867 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010868
Mon P Wang63307c32008-05-05 19:05:59 +000010869 // newMBB jumps to newMBB and fall through to nextMBB
10870 newMBB->addSuccessor(nextMBB);
10871 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010872
Dale Johannesene4d209d2009-02-03 20:21:25 +000010873 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000010874 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010875 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000010876 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +000010877 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010878 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000010879 int numArgs = mInstr->getNumOperands() - 1;
10880 for (int i=0; i < numArgs; ++i)
10881 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +000010882
Mon P Wang63307c32008-05-05 19:05:59 +000010883 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010884 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000010885 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000010886
Mon P Wangab3e7472008-05-05 22:56:23 +000010887 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010888 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000010889 for (int i=0; i <= lastAddrIndx; ++i)
10890 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +000010891
Mon P Wang63307c32008-05-05 19:05:59 +000010892 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +000010893 assert((argOpers[valArgIndx]->isReg() ||
10894 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000010895 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +000010896
10897 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +000010898 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010899 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +000010900 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000010901 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000010902 (*MIB).addOperand(*argOpers[valArgIndx]);
10903
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010904 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +000010905 MIB.addReg(t1);
10906
Dale Johannesene4d209d2009-02-03 20:21:25 +000010907 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +000010908 MIB.addReg(t1);
10909 MIB.addReg(t2);
10910
10911 // Generate movc
10912 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010913 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +000010914 MIB.addReg(t2);
10915 MIB.addReg(t1);
10916
10917 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +000010918 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +000010919 for (int i=0; i <= lastAddrIndx; ++i)
10920 (*MIB).addOperand(*argOpers[i]);
10921 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +000010922 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000010923 (*MIB).setMemRefs(mInstr->memoperands_begin(),
10924 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +000010925
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010926 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +000010927 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +000010928
Mon P Wang63307c32008-05-05 19:05:59 +000010929 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000010930 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000010931
Dan Gohman14152b42010-07-06 20:24:04 +000010932 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000010933 return nextMBB;
10934}
10935
Eric Christopherf83a5de2009-08-27 18:08:16 +000010936// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010937// or XMM0_V32I8 in AVX all of this code can be replaced with that
10938// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +000010939MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +000010940X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +000010941 unsigned numArgs, bool memArg) const {
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010942 assert((Subtarget->hasSSE42() || Subtarget->hasAVX()) &&
10943 "Target must have SSE4.2 or AVX features enabled");
10944
Eric Christopherb120ab42009-08-18 22:50:32 +000010945 DebugLoc dl = MI->getDebugLoc();
10946 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +000010947 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010948 if (!Subtarget->hasAVX()) {
10949 if (memArg)
10950 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
10951 else
10952 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
10953 } else {
10954 if (memArg)
10955 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
10956 else
10957 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
10958 }
Eric Christopherb120ab42009-08-18 22:50:32 +000010959
Eric Christopher41c902f2010-11-30 08:20:21 +000010960 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +000010961 for (unsigned i = 0; i < numArgs; ++i) {
10962 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +000010963 if (!(Op.isReg() && Op.isImplicit()))
10964 MIB.addOperand(Op);
10965 }
Eric Christopher41c902f2010-11-30 08:20:21 +000010966 BuildMI(*BB, MI, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000010967 .addReg(X86::XMM0);
10968
Dan Gohman14152b42010-07-06 20:24:04 +000010969 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000010970 return BB;
10971}
10972
10973MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +000010974X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000010975 DebugLoc dl = MI->getDebugLoc();
10976 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010977
Eric Christopher228232b2010-11-30 07:20:12 +000010978 // Address into RAX/EAX, other two args into ECX, EDX.
10979 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
10980 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
10981 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
10982 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000010983 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010984
Eric Christopher228232b2010-11-30 07:20:12 +000010985 unsigned ValOps = X86::AddrNumOperands;
10986 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
10987 .addReg(MI->getOperand(ValOps).getReg());
10988 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
10989 .addReg(MI->getOperand(ValOps+1).getReg());
10990
10991 // The instruction doesn't actually take any operands though.
10992 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010993
Eric Christopher228232b2010-11-30 07:20:12 +000010994 MI->eraseFromParent(); // The pseudo is gone now.
10995 return BB;
10996}
10997
10998MachineBasicBlock *
10999X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000011000 DebugLoc dl = MI->getDebugLoc();
11001 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011002
Eric Christopher228232b2010-11-30 07:20:12 +000011003 // First arg in ECX, the second in EAX.
11004 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
11005 .addReg(MI->getOperand(0).getReg());
11006 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
11007 .addReg(MI->getOperand(1).getReg());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011008
Eric Christopher228232b2010-11-30 07:20:12 +000011009 // The instruction doesn't actually take any operands though.
11010 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011011
Eric Christopher228232b2010-11-30 07:20:12 +000011012 MI->eraseFromParent(); // The pseudo is gone now.
11013 return BB;
11014}
11015
11016MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000011017X86TargetLowering::EmitVAARG64WithCustomInserter(
11018 MachineInstr *MI,
11019 MachineBasicBlock *MBB) const {
11020 // Emit va_arg instruction on X86-64.
11021
11022 // Operands to this pseudo-instruction:
11023 // 0 ) Output : destination address (reg)
11024 // 1-5) Input : va_list address (addr, i64mem)
11025 // 6 ) ArgSize : Size (in bytes) of vararg type
11026 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
11027 // 8 ) Align : Alignment of type
11028 // 9 ) EFLAGS (implicit-def)
11029
11030 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
11031 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
11032
11033 unsigned DestReg = MI->getOperand(0).getReg();
11034 MachineOperand &Base = MI->getOperand(1);
11035 MachineOperand &Scale = MI->getOperand(2);
11036 MachineOperand &Index = MI->getOperand(3);
11037 MachineOperand &Disp = MI->getOperand(4);
11038 MachineOperand &Segment = MI->getOperand(5);
11039 unsigned ArgSize = MI->getOperand(6).getImm();
11040 unsigned ArgMode = MI->getOperand(7).getImm();
11041 unsigned Align = MI->getOperand(8).getImm();
11042
11043 // Memory Reference
11044 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
11045 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
11046 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
11047
11048 // Machine Information
11049 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11050 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
11051 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
11052 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
11053 DebugLoc DL = MI->getDebugLoc();
11054
11055 // struct va_list {
11056 // i32 gp_offset
11057 // i32 fp_offset
11058 // i64 overflow_area (address)
11059 // i64 reg_save_area (address)
11060 // }
11061 // sizeof(va_list) = 24
11062 // alignment(va_list) = 8
11063
11064 unsigned TotalNumIntRegs = 6;
11065 unsigned TotalNumXMMRegs = 8;
11066 bool UseGPOffset = (ArgMode == 1);
11067 bool UseFPOffset = (ArgMode == 2);
11068 unsigned MaxOffset = TotalNumIntRegs * 8 +
11069 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
11070
11071 /* Align ArgSize to a multiple of 8 */
11072 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
11073 bool NeedsAlign = (Align > 8);
11074
11075 MachineBasicBlock *thisMBB = MBB;
11076 MachineBasicBlock *overflowMBB;
11077 MachineBasicBlock *offsetMBB;
11078 MachineBasicBlock *endMBB;
11079
11080 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
11081 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
11082 unsigned OffsetReg = 0;
11083
11084 if (!UseGPOffset && !UseFPOffset) {
11085 // If we only pull from the overflow region, we don't create a branch.
11086 // We don't need to alter control flow.
11087 OffsetDestReg = 0; // unused
11088 OverflowDestReg = DestReg;
11089
11090 offsetMBB = NULL;
11091 overflowMBB = thisMBB;
11092 endMBB = thisMBB;
11093 } else {
11094 // First emit code to check if gp_offset (or fp_offset) is below the bound.
11095 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
11096 // If not, pull from overflow_area. (branch to overflowMBB)
11097 //
11098 // thisMBB
11099 // | .
11100 // | .
11101 // offsetMBB overflowMBB
11102 // | .
11103 // | .
11104 // endMBB
11105
11106 // Registers for the PHI in endMBB
11107 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
11108 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
11109
11110 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11111 MachineFunction *MF = MBB->getParent();
11112 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11113 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11114 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11115
11116 MachineFunction::iterator MBBIter = MBB;
11117 ++MBBIter;
11118
11119 // Insert the new basic blocks
11120 MF->insert(MBBIter, offsetMBB);
11121 MF->insert(MBBIter, overflowMBB);
11122 MF->insert(MBBIter, endMBB);
11123
11124 // Transfer the remainder of MBB and its successor edges to endMBB.
11125 endMBB->splice(endMBB->begin(), thisMBB,
11126 llvm::next(MachineBasicBlock::iterator(MI)),
11127 thisMBB->end());
11128 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
11129
11130 // Make offsetMBB and overflowMBB successors of thisMBB
11131 thisMBB->addSuccessor(offsetMBB);
11132 thisMBB->addSuccessor(overflowMBB);
11133
11134 // endMBB is a successor of both offsetMBB and overflowMBB
11135 offsetMBB->addSuccessor(endMBB);
11136 overflowMBB->addSuccessor(endMBB);
11137
11138 // Load the offset value into a register
11139 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
11140 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
11141 .addOperand(Base)
11142 .addOperand(Scale)
11143 .addOperand(Index)
11144 .addDisp(Disp, UseFPOffset ? 4 : 0)
11145 .addOperand(Segment)
11146 .setMemRefs(MMOBegin, MMOEnd);
11147
11148 // Check if there is enough room left to pull this argument.
11149 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
11150 .addReg(OffsetReg)
11151 .addImm(MaxOffset + 8 - ArgSizeA8);
11152
11153 // Branch to "overflowMBB" if offset >= max
11154 // Fall through to "offsetMBB" otherwise
11155 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
11156 .addMBB(overflowMBB);
11157 }
11158
11159 // In offsetMBB, emit code to use the reg_save_area.
11160 if (offsetMBB) {
11161 assert(OffsetReg != 0);
11162
11163 // Read the reg_save_area address.
11164 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
11165 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
11166 .addOperand(Base)
11167 .addOperand(Scale)
11168 .addOperand(Index)
11169 .addDisp(Disp, 16)
11170 .addOperand(Segment)
11171 .setMemRefs(MMOBegin, MMOEnd);
11172
11173 // Zero-extend the offset
11174 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
11175 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
11176 .addImm(0)
11177 .addReg(OffsetReg)
11178 .addImm(X86::sub_32bit);
11179
11180 // Add the offset to the reg_save_area to get the final address.
11181 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
11182 .addReg(OffsetReg64)
11183 .addReg(RegSaveReg);
11184
11185 // Compute the offset for the next argument
11186 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
11187 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
11188 .addReg(OffsetReg)
11189 .addImm(UseFPOffset ? 16 : 8);
11190
11191 // Store it back into the va_list.
11192 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
11193 .addOperand(Base)
11194 .addOperand(Scale)
11195 .addOperand(Index)
11196 .addDisp(Disp, UseFPOffset ? 4 : 0)
11197 .addOperand(Segment)
11198 .addReg(NextOffsetReg)
11199 .setMemRefs(MMOBegin, MMOEnd);
11200
11201 // Jump to endMBB
11202 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
11203 .addMBB(endMBB);
11204 }
11205
11206 //
11207 // Emit code to use overflow area
11208 //
11209
11210 // Load the overflow_area address into a register.
11211 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
11212 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
11213 .addOperand(Base)
11214 .addOperand(Scale)
11215 .addOperand(Index)
11216 .addDisp(Disp, 8)
11217 .addOperand(Segment)
11218 .setMemRefs(MMOBegin, MMOEnd);
11219
11220 // If we need to align it, do so. Otherwise, just copy the address
11221 // to OverflowDestReg.
11222 if (NeedsAlign) {
11223 // Align the overflow address
11224 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
11225 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
11226
11227 // aligned_addr = (addr + (align-1)) & ~(align-1)
11228 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
11229 .addReg(OverflowAddrReg)
11230 .addImm(Align-1);
11231
11232 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
11233 .addReg(TmpReg)
11234 .addImm(~(uint64_t)(Align-1));
11235 } else {
11236 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
11237 .addReg(OverflowAddrReg);
11238 }
11239
11240 // Compute the next overflow address after this argument.
11241 // (the overflow address should be kept 8-byte aligned)
11242 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
11243 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
11244 .addReg(OverflowDestReg)
11245 .addImm(ArgSizeA8);
11246
11247 // Store the new overflow address.
11248 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
11249 .addOperand(Base)
11250 .addOperand(Scale)
11251 .addOperand(Index)
11252 .addDisp(Disp, 8)
11253 .addOperand(Segment)
11254 .addReg(NextAddrReg)
11255 .setMemRefs(MMOBegin, MMOEnd);
11256
11257 // If we branched, emit the PHI to the front of endMBB.
11258 if (offsetMBB) {
11259 BuildMI(*endMBB, endMBB->begin(), DL,
11260 TII->get(X86::PHI), DestReg)
11261 .addReg(OffsetDestReg).addMBB(offsetMBB)
11262 .addReg(OverflowDestReg).addMBB(overflowMBB);
11263 }
11264
11265 // Erase the pseudo instruction
11266 MI->eraseFromParent();
11267
11268 return endMBB;
11269}
11270
11271MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000011272X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
11273 MachineInstr *MI,
11274 MachineBasicBlock *MBB) const {
11275 // Emit code to save XMM registers to the stack. The ABI says that the
11276 // number of registers to save is given in %al, so it's theoretically
11277 // possible to do an indirect jump trick to avoid saving all of them,
11278 // however this code takes a simpler approach and just executes all
11279 // of the stores if %al is non-zero. It's less code, and it's probably
11280 // easier on the hardware branch predictor, and stores aren't all that
11281 // expensive anyway.
11282
11283 // Create the new basic blocks. One block contains all the XMM stores,
11284 // and one block is the final destination regardless of whether any
11285 // stores were performed.
11286 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11287 MachineFunction *F = MBB->getParent();
11288 MachineFunction::iterator MBBIter = MBB;
11289 ++MBBIter;
11290 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
11291 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
11292 F->insert(MBBIter, XMMSaveMBB);
11293 F->insert(MBBIter, EndMBB);
11294
Dan Gohman14152b42010-07-06 20:24:04 +000011295 // Transfer the remainder of MBB and its successor edges to EndMBB.
11296 EndMBB->splice(EndMBB->begin(), MBB,
11297 llvm::next(MachineBasicBlock::iterator(MI)),
11298 MBB->end());
11299 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
11300
Dan Gohmand6708ea2009-08-15 01:38:56 +000011301 // The original block will now fall through to the XMM save block.
11302 MBB->addSuccessor(XMMSaveMBB);
11303 // The XMMSaveMBB will fall through to the end block.
11304 XMMSaveMBB->addSuccessor(EndMBB);
11305
11306 // Now add the instructions.
11307 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11308 DebugLoc DL = MI->getDebugLoc();
11309
11310 unsigned CountReg = MI->getOperand(0).getReg();
11311 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
11312 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
11313
11314 if (!Subtarget->isTargetWin64()) {
11315 // If %al is 0, branch around the XMM save block.
11316 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011317 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000011318 MBB->addSuccessor(EndMBB);
11319 }
11320
11321 // In the XMM save block, save all the XMM argument registers.
11322 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
11323 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000011324 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000011325 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000011326 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000011327 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000011328 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +000011329 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
11330 .addFrameIndex(RegSaveFrameIndex)
11331 .addImm(/*Scale=*/1)
11332 .addReg(/*IndexReg=*/0)
11333 .addImm(/*Disp=*/Offset)
11334 .addReg(/*Segment=*/0)
11335 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000011336 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000011337 }
11338
Dan Gohman14152b42010-07-06 20:24:04 +000011339 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000011340
11341 return EndMBB;
11342}
Mon P Wang63307c32008-05-05 19:05:59 +000011343
Evan Cheng60c07e12006-07-05 22:17:51 +000011344MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000011345X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000011346 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000011347 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11348 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000011349
Chris Lattner52600972009-09-02 05:57:00 +000011350 // To "insert" a SELECT_CC instruction, we actually have to insert the
11351 // diamond control-flow pattern. The incoming instruction knows the
11352 // destination vreg to set, the condition code register to branch on, the
11353 // true/false values to select between, and a branch opcode to use.
11354 const BasicBlock *LLVM_BB = BB->getBasicBlock();
11355 MachineFunction::iterator It = BB;
11356 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000011357
Chris Lattner52600972009-09-02 05:57:00 +000011358 // thisMBB:
11359 // ...
11360 // TrueVal = ...
11361 // cmpTY ccX, r1, r2
11362 // bCC copy1MBB
11363 // fallthrough --> copy0MBB
11364 MachineBasicBlock *thisMBB = BB;
11365 MachineFunction *F = BB->getParent();
11366 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
11367 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000011368 F->insert(It, copy0MBB);
11369 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000011370
Bill Wendling730c07e2010-06-25 20:48:10 +000011371 // If the EFLAGS register isn't dead in the terminator, then claim that it's
11372 // live into the sink and copy blocks.
11373 const MachineFunction *MF = BB->getParent();
11374 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
11375 BitVector ReservedRegs = TRI->getReservedRegs(*MF);
Bill Wendling730c07e2010-06-25 20:48:10 +000011376
Dan Gohman14152b42010-07-06 20:24:04 +000011377 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
11378 const MachineOperand &MO = MI->getOperand(I);
11379 if (!MO.isReg() || !MO.isUse() || MO.isKill()) continue;
Bill Wendling730c07e2010-06-25 20:48:10 +000011380 unsigned Reg = MO.getReg();
11381 if (Reg != X86::EFLAGS) continue;
11382 copy0MBB->addLiveIn(Reg);
11383 sinkMBB->addLiveIn(Reg);
11384 }
11385
Dan Gohman14152b42010-07-06 20:24:04 +000011386 // Transfer the remainder of BB and its successor edges to sinkMBB.
11387 sinkMBB->splice(sinkMBB->begin(), BB,
11388 llvm::next(MachineBasicBlock::iterator(MI)),
11389 BB->end());
11390 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
11391
11392 // Add the true and fallthrough blocks as its successors.
11393 BB->addSuccessor(copy0MBB);
11394 BB->addSuccessor(sinkMBB);
11395
11396 // Create the conditional branch instruction.
11397 unsigned Opc =
11398 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
11399 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
11400
Chris Lattner52600972009-09-02 05:57:00 +000011401 // copy0MBB:
11402 // %FalseValue = ...
11403 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000011404 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000011405
Chris Lattner52600972009-09-02 05:57:00 +000011406 // sinkMBB:
11407 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
11408 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000011409 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
11410 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000011411 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
11412 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
11413
Dan Gohman14152b42010-07-06 20:24:04 +000011414 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000011415 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000011416}
11417
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011418MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000011419X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000011420 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011421 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11422 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011423
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000011424 assert(!Subtarget->isTargetEnvMacho());
11425
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011426 // The lowering is pretty easy: we're just emitting the call to _alloca. The
11427 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011428
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000011429 if (Subtarget->isTargetWin64()) {
11430 if (Subtarget->isTargetCygMing()) {
11431 // ___chkstk(Mingw64):
11432 // Clobbers R10, R11, RAX and EFLAGS.
11433 // Updates RSP.
11434 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
11435 .addExternalSymbol("___chkstk")
11436 .addReg(X86::RAX, RegState::Implicit)
11437 .addReg(X86::RSP, RegState::Implicit)
11438 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
11439 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
11440 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
11441 } else {
11442 // __chkstk(MSVCRT): does not update stack pointer.
11443 // Clobbers R10, R11 and EFLAGS.
11444 // FIXME: RAX(allocated size) might be reused and not killed.
11445 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
11446 .addExternalSymbol("__chkstk")
11447 .addReg(X86::RAX, RegState::Implicit)
11448 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
11449 // RAX has the offset to subtracted from RSP.
11450 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
11451 .addReg(X86::RSP)
11452 .addReg(X86::RAX);
11453 }
11454 } else {
11455 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000011456 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
11457
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000011458 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
11459 .addExternalSymbol(StackProbeSymbol)
11460 .addReg(X86::EAX, RegState::Implicit)
11461 .addReg(X86::ESP, RegState::Implicit)
11462 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
11463 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
11464 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
11465 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011466
Dan Gohman14152b42010-07-06 20:24:04 +000011467 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011468 return BB;
11469}
Chris Lattner52600972009-09-02 05:57:00 +000011470
11471MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000011472X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
11473 MachineBasicBlock *BB) const {
11474 // This is pretty easy. We're taking the value that we received from
11475 // our load from the relocation, sticking it in either RDI (x86-64)
11476 // or EAX and doing an indirect call. The return value will then
11477 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000011478 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000011479 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000011480 DebugLoc DL = MI->getDebugLoc();
11481 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000011482
11483 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000011484 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000011485
Eric Christopher30ef0e52010-06-03 04:07:48 +000011486 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000011487 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
11488 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000011489 .addReg(X86::RIP)
11490 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000011491 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000011492 MI->getOperand(3).getTargetFlags())
11493 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000011494 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000011495 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +000011496 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000011497 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
11498 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000011499 .addReg(0)
11500 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000011501 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000011502 MI->getOperand(3).getTargetFlags())
11503 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000011504 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000011505 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000011506 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000011507 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
11508 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000011509 .addReg(TII->getGlobalBaseReg(F))
11510 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000011511 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000011512 MI->getOperand(3).getTargetFlags())
11513 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000011514 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000011515 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000011516 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000011517
Dan Gohman14152b42010-07-06 20:24:04 +000011518 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000011519 return BB;
11520}
11521
11522MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000011523X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000011524 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000011525 switch (MI->getOpcode()) {
11526 default: assert(false && "Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000011527 case X86::TAILJMPd64:
11528 case X86::TAILJMPr64:
11529 case X86::TAILJMPm64:
11530 assert(!"TAILJMP64 would not be touched here.");
11531 case X86::TCRETURNdi64:
11532 case X86::TCRETURNri64:
11533 case X86::TCRETURNmi64:
11534 // Defs of TCRETURNxx64 has Win64's callee-saved registers, as subset.
11535 // On AMD64, additional defs should be added before register allocation.
11536 if (!Subtarget->isTargetWin64()) {
11537 MI->addRegisterDefined(X86::RSI);
11538 MI->addRegisterDefined(X86::RDI);
11539 MI->addRegisterDefined(X86::XMM6);
11540 MI->addRegisterDefined(X86::XMM7);
11541 MI->addRegisterDefined(X86::XMM8);
11542 MI->addRegisterDefined(X86::XMM9);
11543 MI->addRegisterDefined(X86::XMM10);
11544 MI->addRegisterDefined(X86::XMM11);
11545 MI->addRegisterDefined(X86::XMM12);
11546 MI->addRegisterDefined(X86::XMM13);
11547 MI->addRegisterDefined(X86::XMM14);
11548 MI->addRegisterDefined(X86::XMM15);
11549 }
11550 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000011551 case X86::WIN_ALLOCA:
11552 return EmitLoweredWinAlloca(MI, BB);
Eric Christopher30ef0e52010-06-03 04:07:48 +000011553 case X86::TLSCall_32:
11554 case X86::TLSCall_64:
11555 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000011556 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000011557 case X86::CMOV_FR32:
11558 case X86::CMOV_FR64:
11559 case X86::CMOV_V4F32:
11560 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000011561 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000011562 case X86::CMOV_V8F32:
11563 case X86::CMOV_V4F64:
11564 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000011565 case X86::CMOV_GR16:
11566 case X86::CMOV_GR32:
11567 case X86::CMOV_RFP32:
11568 case X86::CMOV_RFP64:
11569 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000011570 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000011571
Dale Johannesen849f2142007-07-03 00:53:03 +000011572 case X86::FP32_TO_INT16_IN_MEM:
11573 case X86::FP32_TO_INT32_IN_MEM:
11574 case X86::FP32_TO_INT64_IN_MEM:
11575 case X86::FP64_TO_INT16_IN_MEM:
11576 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000011577 case X86::FP64_TO_INT64_IN_MEM:
11578 case X86::FP80_TO_INT16_IN_MEM:
11579 case X86::FP80_TO_INT32_IN_MEM:
11580 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000011581 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11582 DebugLoc DL = MI->getDebugLoc();
11583
Evan Cheng60c07e12006-07-05 22:17:51 +000011584 // Change the floating point control register to use "round towards zero"
11585 // mode when truncating to an integer value.
11586 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000011587 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000011588 addFrameReference(BuildMI(*BB, MI, DL,
11589 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000011590
11591 // Load the old value of the high byte of the control word...
11592 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000011593 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000011594 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000011595 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000011596
11597 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000011598 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000011599 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000011600
11601 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000011602 addFrameReference(BuildMI(*BB, MI, DL,
11603 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000011604
11605 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000011606 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000011607 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000011608
11609 // Get the X86 opcode to use.
11610 unsigned Opc;
11611 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000011612 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000011613 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
11614 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
11615 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
11616 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
11617 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
11618 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000011619 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
11620 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
11621 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000011622 }
11623
11624 X86AddressMode AM;
11625 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000011626 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000011627 AM.BaseType = X86AddressMode::RegBase;
11628 AM.Base.Reg = Op.getReg();
11629 } else {
11630 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000011631 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000011632 }
11633 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000011634 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000011635 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000011636 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000011637 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000011638 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000011639 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000011640 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000011641 AM.GV = Op.getGlobal();
11642 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000011643 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000011644 }
Dan Gohman14152b42010-07-06 20:24:04 +000011645 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011646 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000011647
11648 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000011649 addFrameReference(BuildMI(*BB, MI, DL,
11650 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000011651
Dan Gohman14152b42010-07-06 20:24:04 +000011652 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000011653 return BB;
11654 }
Eric Christopherb120ab42009-08-18 22:50:32 +000011655 // String/text processing lowering.
11656 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011657 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000011658 return EmitPCMP(MI, BB, 3, false /* in-mem */);
11659 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011660 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000011661 return EmitPCMP(MI, BB, 3, true /* in-mem */);
11662 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011663 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000011664 return EmitPCMP(MI, BB, 5, false /* in mem */);
11665 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011666 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000011667 return EmitPCMP(MI, BB, 5, true /* in mem */);
11668
Eric Christopher228232b2010-11-30 07:20:12 +000011669 // Thread synchronization.
11670 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011671 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000011672 case X86::MWAIT:
11673 return EmitMwait(MI, BB);
11674
Eric Christopherb120ab42009-08-18 22:50:32 +000011675 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000011676 case X86::ATOMAND32:
11677 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000011678 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011679 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011680 X86::NOT32r, X86::EAX,
11681 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000011682 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000011683 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
11684 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011685 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011686 X86::NOT32r, X86::EAX,
11687 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000011688 case X86::ATOMXOR32:
11689 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000011690 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011691 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011692 X86::NOT32r, X86::EAX,
11693 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011694 case X86::ATOMNAND32:
11695 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011696 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011697 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011698 X86::NOT32r, X86::EAX,
11699 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000011700 case X86::ATOMMIN32:
11701 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
11702 case X86::ATOMMAX32:
11703 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
11704 case X86::ATOMUMIN32:
11705 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
11706 case X86::ATOMUMAX32:
11707 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000011708
11709 case X86::ATOMAND16:
11710 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
11711 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011712 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011713 X86::NOT16r, X86::AX,
11714 X86::GR16RegisterClass);
11715 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000011716 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011717 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011718 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011719 X86::NOT16r, X86::AX,
11720 X86::GR16RegisterClass);
11721 case X86::ATOMXOR16:
11722 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
11723 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011724 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011725 X86::NOT16r, X86::AX,
11726 X86::GR16RegisterClass);
11727 case X86::ATOMNAND16:
11728 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
11729 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011730 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011731 X86::NOT16r, X86::AX,
11732 X86::GR16RegisterClass, true);
11733 case X86::ATOMMIN16:
11734 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
11735 case X86::ATOMMAX16:
11736 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
11737 case X86::ATOMUMIN16:
11738 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
11739 case X86::ATOMUMAX16:
11740 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
11741
11742 case X86::ATOMAND8:
11743 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
11744 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011745 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011746 X86::NOT8r, X86::AL,
11747 X86::GR8RegisterClass);
11748 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000011749 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011750 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011751 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011752 X86::NOT8r, X86::AL,
11753 X86::GR8RegisterClass);
11754 case X86::ATOMXOR8:
11755 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
11756 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011757 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011758 X86::NOT8r, X86::AL,
11759 X86::GR8RegisterClass);
11760 case X86::ATOMNAND8:
11761 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
11762 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011763 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011764 X86::NOT8r, X86::AL,
11765 X86::GR8RegisterClass, true);
11766 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011767 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000011768 case X86::ATOMAND64:
11769 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000011770 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011771 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000011772 X86::NOT64r, X86::RAX,
11773 X86::GR64RegisterClass);
11774 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000011775 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
11776 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011777 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000011778 X86::NOT64r, X86::RAX,
11779 X86::GR64RegisterClass);
11780 case X86::ATOMXOR64:
11781 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000011782 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011783 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000011784 X86::NOT64r, X86::RAX,
11785 X86::GR64RegisterClass);
11786 case X86::ATOMNAND64:
11787 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
11788 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011789 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000011790 X86::NOT64r, X86::RAX,
11791 X86::GR64RegisterClass, true);
11792 case X86::ATOMMIN64:
11793 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
11794 case X86::ATOMMAX64:
11795 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
11796 case X86::ATOMUMIN64:
11797 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
11798 case X86::ATOMUMAX64:
11799 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011800
11801 // This group does 64-bit operations on a 32-bit host.
11802 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000011803 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011804 X86::AND32rr, X86::AND32rr,
11805 X86::AND32ri, X86::AND32ri,
11806 false);
11807 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000011808 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011809 X86::OR32rr, X86::OR32rr,
11810 X86::OR32ri, X86::OR32ri,
11811 false);
11812 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000011813 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011814 X86::XOR32rr, X86::XOR32rr,
11815 X86::XOR32ri, X86::XOR32ri,
11816 false);
11817 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000011818 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011819 X86::AND32rr, X86::AND32rr,
11820 X86::AND32ri, X86::AND32ri,
11821 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011822 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000011823 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011824 X86::ADD32rr, X86::ADC32rr,
11825 X86::ADD32ri, X86::ADC32ri,
11826 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011827 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000011828 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011829 X86::SUB32rr, X86::SBB32rr,
11830 X86::SUB32ri, X86::SBB32ri,
11831 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000011832 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000011833 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000011834 X86::MOV32rr, X86::MOV32rr,
11835 X86::MOV32ri, X86::MOV32ri,
11836 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000011837 case X86::VASTART_SAVE_XMM_REGS:
11838 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000011839
11840 case X86::VAARG_64:
11841 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000011842 }
11843}
11844
11845//===----------------------------------------------------------------------===//
11846// X86 Optimization Hooks
11847//===----------------------------------------------------------------------===//
11848
Dan Gohman475871a2008-07-27 21:46:04 +000011849void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000011850 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000011851 APInt &KnownZero,
11852 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000011853 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000011854 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011855 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000011856 assert((Opc >= ISD::BUILTIN_OP_END ||
11857 Opc == ISD::INTRINSIC_WO_CHAIN ||
11858 Opc == ISD::INTRINSIC_W_CHAIN ||
11859 Opc == ISD::INTRINSIC_VOID) &&
11860 "Should use MaskedValueIsZero if you don't know whether Op"
11861 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011862
Dan Gohmanf4f92f52008-02-13 23:07:24 +000011863 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011864 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000011865 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000011866 case X86ISD::ADD:
11867 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000011868 case X86ISD::ADC:
11869 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000011870 case X86ISD::SMUL:
11871 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000011872 case X86ISD::INC:
11873 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000011874 case X86ISD::OR:
11875 case X86ISD::XOR:
11876 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000011877 // These nodes' second result is a boolean.
11878 if (Op.getResNo() == 0)
11879 break;
11880 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011881 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000011882 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
11883 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000011884 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011885 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011886}
Chris Lattner259e97c2006-01-31 19:43:35 +000011887
Owen Andersonbc146b02010-09-21 20:42:50 +000011888unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
11889 unsigned Depth) const {
11890 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
11891 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
11892 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000011893
Owen Andersonbc146b02010-09-21 20:42:50 +000011894 // Fallback case.
11895 return 1;
11896}
11897
Evan Cheng206ee9d2006-07-07 08:33:52 +000011898/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000011899/// node is a GlobalAddress + offset.
11900bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000011901 const GlobalValue* &GA,
11902 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000011903 if (N->getOpcode() == X86ISD::Wrapper) {
11904 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000011905 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000011906 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000011907 return true;
11908 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000011909 }
Evan Chengad4196b2008-05-12 19:56:52 +000011910 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000011911}
11912
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000011913/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
11914/// same as extracting the high 128-bit part of 256-bit vector and then
11915/// inserting the result into the low part of a new 256-bit vector
11916static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
11917 EVT VT = SVOp->getValueType(0);
11918 int NumElems = VT.getVectorNumElements();
11919
11920 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
11921 for (int i = 0, j = NumElems/2; i < NumElems/2; ++i, ++j)
11922 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
11923 SVOp->getMaskElt(j) >= 0)
11924 return false;
11925
11926 return true;
11927}
11928
11929/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
11930/// same as extracting the low 128-bit part of 256-bit vector and then
11931/// inserting the result into the high part of a new 256-bit vector
11932static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
11933 EVT VT = SVOp->getValueType(0);
11934 int NumElems = VT.getVectorNumElements();
11935
11936 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
11937 for (int i = NumElems/2, j = 0; i < NumElems; ++i, ++j)
11938 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
11939 SVOp->getMaskElt(j) >= 0)
11940 return false;
11941
11942 return true;
11943}
11944
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000011945/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
11946static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
11947 TargetLowering::DAGCombinerInfo &DCI) {
11948 DebugLoc dl = N->getDebugLoc();
11949 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
11950 SDValue V1 = SVOp->getOperand(0);
11951 SDValue V2 = SVOp->getOperand(1);
11952 EVT VT = SVOp->getValueType(0);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000011953 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000011954
11955 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
11956 V2.getOpcode() == ISD::CONCAT_VECTORS) {
11957 //
11958 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000011959 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000011960 // V UNDEF BUILD_VECTOR UNDEF
11961 // \ / \ /
11962 // CONCAT_VECTOR CONCAT_VECTOR
11963 // \ /
11964 // \ /
11965 // RESULT: V + zero extended
11966 //
11967 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
11968 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
11969 V1.getOperand(1).getOpcode() != ISD::UNDEF)
11970 return SDValue();
11971
11972 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
11973 return SDValue();
11974
11975 // To match the shuffle mask, the first half of the mask should
11976 // be exactly the first vector, and all the rest a splat with the
11977 // first element of the second one.
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000011978 for (int i = 0; i < NumElems/2; ++i)
11979 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
11980 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
11981 return SDValue();
11982
11983 // Emit a zeroed vector and insert the desired subvector on its
11984 // first half.
11985 SDValue Zeros = getZeroVector(VT, true /* HasSSE2 */, DAG, dl);
11986 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0),
11987 DAG.getConstant(0, MVT::i32), DAG, dl);
11988 return DCI.CombineTo(N, InsV);
11989 }
11990
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000011991 //===--------------------------------------------------------------------===//
11992 // Combine some shuffles into subvector extracts and inserts:
11993 //
11994
11995 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
11996 if (isShuffleHigh128VectorInsertLow(SVOp)) {
11997 SDValue V = Extract128BitVector(V1, DAG.getConstant(NumElems/2, MVT::i32),
11998 DAG, dl);
11999 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT),
12000 V, DAG.getConstant(0, MVT::i32), DAG, dl);
12001 return DCI.CombineTo(N, InsV);
12002 }
12003
12004 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
12005 if (isShuffleLow128VectorInsertHigh(SVOp)) {
12006 SDValue V = Extract128BitVector(V1, DAG.getConstant(0, MVT::i32), DAG, dl);
12007 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT),
12008 V, DAG.getConstant(NumElems/2, MVT::i32), DAG, dl);
12009 return DCI.CombineTo(N, InsV);
12010 }
12011
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012012 return SDValue();
12013}
12014
12015/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000012016static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000012017 TargetLowering::DAGCombinerInfo &DCI,
12018 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000012019 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000012020 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000012021
Mon P Wanga0fd0d52010-12-19 23:55:53 +000012022 // Don't create instructions with illegal types after legalize types has run.
12023 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12024 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
12025 return SDValue();
12026
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000012027 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
12028 if (Subtarget->hasAVX() && VT.getSizeInBits() == 256 &&
12029 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012030 return PerformShuffleCombine256(N, DAG, DCI);
12031
12032 // Only handle 128 wide vector from here on.
12033 if (VT.getSizeInBits() != 128)
12034 return SDValue();
12035
12036 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
12037 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
12038 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000012039 SmallVector<SDValue, 16> Elts;
12040 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012041 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000012042
Nate Begemanfdea31a2010-03-24 20:49:50 +000012043 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000012044}
Evan Chengd880b972008-05-09 21:53:03 +000012045
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000012046/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
12047/// generation and convert it from being a bunch of shuffles and extracts
12048/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012049static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
12050 const TargetLowering &TLI) {
12051 SDValue InputVector = N->getOperand(0);
12052
12053 // Only operate on vectors of 4 elements, where the alternative shuffling
12054 // gets to be more expensive.
12055 if (InputVector.getValueType() != MVT::v4i32)
12056 return SDValue();
12057
12058 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
12059 // single use which is a sign-extend or zero-extend, and all elements are
12060 // used.
12061 SmallVector<SDNode *, 4> Uses;
12062 unsigned ExtractedElements = 0;
12063 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
12064 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
12065 if (UI.getUse().getResNo() != InputVector.getResNo())
12066 return SDValue();
12067
12068 SDNode *Extract = *UI;
12069 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
12070 return SDValue();
12071
12072 if (Extract->getValueType(0) != MVT::i32)
12073 return SDValue();
12074 if (!Extract->hasOneUse())
12075 return SDValue();
12076 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
12077 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
12078 return SDValue();
12079 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
12080 return SDValue();
12081
12082 // Record which element was extracted.
12083 ExtractedElements |=
12084 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
12085
12086 Uses.push_back(Extract);
12087 }
12088
12089 // If not all the elements were used, this may not be worthwhile.
12090 if (ExtractedElements != 15)
12091 return SDValue();
12092
12093 // Ok, we've now decided to do the transformation.
12094 DebugLoc dl = InputVector.getDebugLoc();
12095
12096 // Store the value to a temporary stack slot.
12097 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000012098 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
12099 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012100
12101 // Replace each use (extract) with a load of the appropriate element.
12102 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
12103 UE = Uses.end(); UI != UE; ++UI) {
12104 SDNode *Extract = *UI;
12105
Nadav Rotem86694292011-05-17 08:31:57 +000012106 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012107 SDValue Idx = Extract->getOperand(1);
12108 unsigned EltSize =
12109 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
12110 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
12111 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
12112
Nadav Rotem86694292011-05-17 08:31:57 +000012113 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000012114 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012115
12116 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000012117 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000012118 ScalarAddr, MachinePointerInfo(),
12119 false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012120
12121 // Replace the exact with the load.
12122 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
12123 }
12124
12125 // The replacement was made in place; don't return anything.
12126 return SDValue();
12127}
12128
Chris Lattner83e6c992006-10-04 06:57:07 +000012129/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000012130static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000012131 const X86Subtarget *Subtarget) {
12132 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000012133 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000012134 // Get the LHS/RHS of the select.
12135 SDValue LHS = N->getOperand(1);
12136 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +000012137
Dan Gohman670e5392009-09-21 18:03:22 +000012138 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000012139 // instructions match the semantics of the common C idiom x<y?x:y but not
12140 // x<=y?x:y, because of how they handle negative zero (which can be
12141 // ignored in unsafe-math mode).
Chris Lattner83e6c992006-10-04 06:57:07 +000012142 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +000012143 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +000012144 Cond.getOpcode() == ISD::SETCC) {
12145 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012146
Chris Lattner47b4ce82009-03-11 05:48:52 +000012147 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000012148 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000012149 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
12150 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000012151 switch (CC) {
12152 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000012153 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000012154 // Converting this to a min would handle NaNs incorrectly, and swapping
12155 // the operands would cause it to handle comparisons between positive
12156 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000012157 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000012158 if (!UnsafeFPMath &&
12159 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
12160 break;
12161 std::swap(LHS, RHS);
12162 }
Dan Gohman670e5392009-09-21 18:03:22 +000012163 Opcode = X86ISD::FMIN;
12164 break;
12165 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000012166 // Converting this to a min would handle comparisons between positive
12167 // and negative zero incorrectly.
12168 if (!UnsafeFPMath &&
12169 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
12170 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012171 Opcode = X86ISD::FMIN;
12172 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000012173 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000012174 // Converting this to a min would handle both negative zeros and NaNs
12175 // incorrectly, but we can swap the operands to fix both.
12176 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012177 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012178 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000012179 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012180 Opcode = X86ISD::FMIN;
12181 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012182
Dan Gohman670e5392009-09-21 18:03:22 +000012183 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012184 // Converting this to a max would handle comparisons between positive
12185 // and negative zero incorrectly.
12186 if (!UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000012187 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012188 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012189 Opcode = X86ISD::FMAX;
12190 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000012191 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000012192 // Converting this to a max would handle NaNs incorrectly, and swapping
12193 // the operands would cause it to handle comparisons between positive
12194 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000012195 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000012196 if (!UnsafeFPMath &&
12197 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
12198 break;
12199 std::swap(LHS, RHS);
12200 }
Dan Gohman670e5392009-09-21 18:03:22 +000012201 Opcode = X86ISD::FMAX;
12202 break;
12203 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012204 // Converting this to a max would handle both negative zeros and NaNs
12205 // incorrectly, but we can swap the operands to fix both.
12206 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012207 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012208 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012209 case ISD::SETGE:
12210 Opcode = X86ISD::FMAX;
12211 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000012212 }
Dan Gohman670e5392009-09-21 18:03:22 +000012213 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000012214 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
12215 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000012216 switch (CC) {
12217 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000012218 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012219 // Converting this to a min would handle comparisons between positive
12220 // and negative zero incorrectly, and swapping the operands would
12221 // cause it to handle NaNs incorrectly.
12222 if (!UnsafeFPMath &&
12223 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000012224 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012225 break;
12226 std::swap(LHS, RHS);
12227 }
Dan Gohman670e5392009-09-21 18:03:22 +000012228 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000012229 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012230 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000012231 // Converting this to a min would handle NaNs incorrectly.
12232 if (!UnsafeFPMath &&
12233 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
12234 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012235 Opcode = X86ISD::FMIN;
12236 break;
12237 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012238 // Converting this to a min would handle both negative zeros and NaNs
12239 // incorrectly, but we can swap the operands to fix both.
12240 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012241 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012242 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012243 case ISD::SETGE:
12244 Opcode = X86ISD::FMIN;
12245 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012246
Dan Gohman670e5392009-09-21 18:03:22 +000012247 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000012248 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000012249 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012250 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012251 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000012252 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012253 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000012254 // Converting this to a max would handle comparisons between positive
12255 // and negative zero incorrectly, and swapping the operands would
12256 // cause it to handle NaNs incorrectly.
12257 if (!UnsafeFPMath &&
12258 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000012259 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012260 break;
12261 std::swap(LHS, RHS);
12262 }
Dan Gohman670e5392009-09-21 18:03:22 +000012263 Opcode = X86ISD::FMAX;
12264 break;
12265 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000012266 // Converting this to a max would handle both negative zeros and NaNs
12267 // incorrectly, but we can swap the operands to fix both.
12268 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012269 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012270 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000012271 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012272 Opcode = X86ISD::FMAX;
12273 break;
12274 }
Chris Lattner83e6c992006-10-04 06:57:07 +000012275 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012276
Chris Lattner47b4ce82009-03-11 05:48:52 +000012277 if (Opcode)
12278 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000012279 }
Eric Christopherfd179292009-08-27 18:07:15 +000012280
Chris Lattnerd1980a52009-03-12 06:52:53 +000012281 // If this is a select between two integer constants, try to do some
12282 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000012283 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
12284 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000012285 // Don't do this for crazy integer types.
12286 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
12287 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000012288 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000012289 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000012290
Chris Lattnercee56e72009-03-13 05:53:31 +000012291 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000012292 // Efficiently invertible.
12293 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
12294 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
12295 isa<ConstantSDNode>(Cond.getOperand(1))))) {
12296 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000012297 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000012298 }
Eric Christopherfd179292009-08-27 18:07:15 +000012299
Chris Lattnerd1980a52009-03-12 06:52:53 +000012300 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000012301 if (FalseC->getAPIntValue() == 0 &&
12302 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000012303 if (NeedsCondInvert) // Invert the condition if needed.
12304 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
12305 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000012306
Chris Lattnerd1980a52009-03-12 06:52:53 +000012307 // Zero extend the condition if needed.
12308 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000012309
Chris Lattnercee56e72009-03-13 05:53:31 +000012310 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000012311 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000012312 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000012313 }
Eric Christopherfd179292009-08-27 18:07:15 +000012314
Chris Lattner97a29a52009-03-13 05:22:11 +000012315 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000012316 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000012317 if (NeedsCondInvert) // Invert the condition if needed.
12318 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
12319 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000012320
Chris Lattner97a29a52009-03-13 05:22:11 +000012321 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000012322 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
12323 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000012324 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000012325 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000012326 }
Eric Christopherfd179292009-08-27 18:07:15 +000012327
Chris Lattnercee56e72009-03-13 05:53:31 +000012328 // Optimize cases that will turn into an LEA instruction. This requires
12329 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000012330 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000012331 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000012332 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000012333
Chris Lattnercee56e72009-03-13 05:53:31 +000012334 bool isFastMultiplier = false;
12335 if (Diff < 10) {
12336 switch ((unsigned char)Diff) {
12337 default: break;
12338 case 1: // result = add base, cond
12339 case 2: // result = lea base( , cond*2)
12340 case 3: // result = lea base(cond, cond*2)
12341 case 4: // result = lea base( , cond*4)
12342 case 5: // result = lea base(cond, cond*4)
12343 case 8: // result = lea base( , cond*8)
12344 case 9: // result = lea base(cond, cond*8)
12345 isFastMultiplier = true;
12346 break;
12347 }
12348 }
Eric Christopherfd179292009-08-27 18:07:15 +000012349
Chris Lattnercee56e72009-03-13 05:53:31 +000012350 if (isFastMultiplier) {
12351 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
12352 if (NeedsCondInvert) // Invert the condition if needed.
12353 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
12354 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000012355
Chris Lattnercee56e72009-03-13 05:53:31 +000012356 // Zero extend the condition if needed.
12357 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
12358 Cond);
12359 // Scale the condition by the difference.
12360 if (Diff != 1)
12361 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
12362 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000012363
Chris Lattnercee56e72009-03-13 05:53:31 +000012364 // Add the base if non-zero.
12365 if (FalseC->getAPIntValue() != 0)
12366 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
12367 SDValue(FalseC, 0));
12368 return Cond;
12369 }
Eric Christopherfd179292009-08-27 18:07:15 +000012370 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000012371 }
12372 }
Eric Christopherfd179292009-08-27 18:07:15 +000012373
Dan Gohman475871a2008-07-27 21:46:04 +000012374 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000012375}
12376
Chris Lattnerd1980a52009-03-12 06:52:53 +000012377/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
12378static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
12379 TargetLowering::DAGCombinerInfo &DCI) {
12380 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000012381
Chris Lattnerd1980a52009-03-12 06:52:53 +000012382 // If the flag operand isn't dead, don't touch this CMOV.
12383 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
12384 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000012385
Evan Chengb5a55d92011-05-24 01:48:22 +000012386 SDValue FalseOp = N->getOperand(0);
12387 SDValue TrueOp = N->getOperand(1);
12388 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
12389 SDValue Cond = N->getOperand(3);
12390 if (CC == X86::COND_E || CC == X86::COND_NE) {
12391 switch (Cond.getOpcode()) {
12392 default: break;
12393 case X86ISD::BSR:
12394 case X86ISD::BSF:
12395 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
12396 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
12397 return (CC == X86::COND_E) ? FalseOp : TrueOp;
12398 }
12399 }
12400
Chris Lattnerd1980a52009-03-12 06:52:53 +000012401 // If this is a select between two integer constants, try to do some
12402 // optimizations. Note that the operands are ordered the opposite of SELECT
12403 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000012404 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
12405 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000012406 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
12407 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000012408 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
12409 CC = X86::GetOppositeBranchCondition(CC);
12410 std::swap(TrueC, FalseC);
12411 }
Eric Christopherfd179292009-08-27 18:07:15 +000012412
Chris Lattnerd1980a52009-03-12 06:52:53 +000012413 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000012414 // This is efficient for any integer data type (including i8/i16) and
12415 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000012416 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012417 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
12418 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000012419
Chris Lattnerd1980a52009-03-12 06:52:53 +000012420 // Zero extend the condition if needed.
12421 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000012422
Chris Lattnerd1980a52009-03-12 06:52:53 +000012423 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
12424 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000012425 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000012426 if (N->getNumValues() == 2) // Dead flag value?
12427 return DCI.CombineTo(N, Cond, SDValue());
12428 return Cond;
12429 }
Eric Christopherfd179292009-08-27 18:07:15 +000012430
Chris Lattnercee56e72009-03-13 05:53:31 +000012431 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
12432 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000012433 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012434 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
12435 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000012436
Chris Lattner97a29a52009-03-13 05:22:11 +000012437 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000012438 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
12439 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000012440 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
12441 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000012442
Chris Lattner97a29a52009-03-13 05:22:11 +000012443 if (N->getNumValues() == 2) // Dead flag value?
12444 return DCI.CombineTo(N, Cond, SDValue());
12445 return Cond;
12446 }
Eric Christopherfd179292009-08-27 18:07:15 +000012447
Chris Lattnercee56e72009-03-13 05:53:31 +000012448 // Optimize cases that will turn into an LEA instruction. This requires
12449 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000012450 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000012451 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000012452 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000012453
Chris Lattnercee56e72009-03-13 05:53:31 +000012454 bool isFastMultiplier = false;
12455 if (Diff < 10) {
12456 switch ((unsigned char)Diff) {
12457 default: break;
12458 case 1: // result = add base, cond
12459 case 2: // result = lea base( , cond*2)
12460 case 3: // result = lea base(cond, cond*2)
12461 case 4: // result = lea base( , cond*4)
12462 case 5: // result = lea base(cond, cond*4)
12463 case 8: // result = lea base( , cond*8)
12464 case 9: // result = lea base(cond, cond*8)
12465 isFastMultiplier = true;
12466 break;
12467 }
12468 }
Eric Christopherfd179292009-08-27 18:07:15 +000012469
Chris Lattnercee56e72009-03-13 05:53:31 +000012470 if (isFastMultiplier) {
12471 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000012472 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
12473 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000012474 // Zero extend the condition if needed.
12475 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
12476 Cond);
12477 // Scale the condition by the difference.
12478 if (Diff != 1)
12479 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
12480 DAG.getConstant(Diff, Cond.getValueType()));
12481
12482 // Add the base if non-zero.
12483 if (FalseC->getAPIntValue() != 0)
12484 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
12485 SDValue(FalseC, 0));
12486 if (N->getNumValues() == 2) // Dead flag value?
12487 return DCI.CombineTo(N, Cond, SDValue());
12488 return Cond;
12489 }
Eric Christopherfd179292009-08-27 18:07:15 +000012490 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000012491 }
12492 }
12493 return SDValue();
12494}
12495
12496
Evan Cheng0b0cd912009-03-28 05:57:29 +000012497/// PerformMulCombine - Optimize a single multiply with constant into two
12498/// in order to implement it with two cheaper instructions, e.g.
12499/// LEA + SHL, LEA + LEA.
12500static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
12501 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000012502 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
12503 return SDValue();
12504
Owen Andersone50ed302009-08-10 22:56:29 +000012505 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012506 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000012507 return SDValue();
12508
12509 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
12510 if (!C)
12511 return SDValue();
12512 uint64_t MulAmt = C->getZExtValue();
12513 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
12514 return SDValue();
12515
12516 uint64_t MulAmt1 = 0;
12517 uint64_t MulAmt2 = 0;
12518 if ((MulAmt % 9) == 0) {
12519 MulAmt1 = 9;
12520 MulAmt2 = MulAmt / 9;
12521 } else if ((MulAmt % 5) == 0) {
12522 MulAmt1 = 5;
12523 MulAmt2 = MulAmt / 5;
12524 } else if ((MulAmt % 3) == 0) {
12525 MulAmt1 = 3;
12526 MulAmt2 = MulAmt / 3;
12527 }
12528 if (MulAmt2 &&
12529 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
12530 DebugLoc DL = N->getDebugLoc();
12531
12532 if (isPowerOf2_64(MulAmt2) &&
12533 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
12534 // If second multiplifer is pow2, issue it first. We want the multiply by
12535 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
12536 // is an add.
12537 std::swap(MulAmt1, MulAmt2);
12538
12539 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000012540 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000012541 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000012542 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000012543 else
Evan Cheng73f24c92009-03-30 21:36:47 +000012544 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000012545 DAG.getConstant(MulAmt1, VT));
12546
Eric Christopherfd179292009-08-27 18:07:15 +000012547 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000012548 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000012549 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000012550 else
Evan Cheng73f24c92009-03-30 21:36:47 +000012551 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000012552 DAG.getConstant(MulAmt2, VT));
12553
12554 // Do not add new nodes to DAG combiner worklist.
12555 DCI.CombineTo(N, NewMul, false);
12556 }
12557 return SDValue();
12558}
12559
Evan Chengad9c0a32009-12-15 00:53:42 +000012560static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
12561 SDValue N0 = N->getOperand(0);
12562 SDValue N1 = N->getOperand(1);
12563 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
12564 EVT VT = N0.getValueType();
12565
12566 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
12567 // since the result of setcc_c is all zero's or all ones.
12568 if (N1C && N0.getOpcode() == ISD::AND &&
12569 N0.getOperand(1).getOpcode() == ISD::Constant) {
12570 SDValue N00 = N0.getOperand(0);
12571 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
12572 ((N00.getOpcode() == ISD::ANY_EXTEND ||
12573 N00.getOpcode() == ISD::ZERO_EXTEND) &&
12574 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
12575 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
12576 APInt ShAmt = N1C->getAPIntValue();
12577 Mask = Mask.shl(ShAmt);
12578 if (Mask != 0)
12579 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
12580 N00, DAG.getConstant(Mask, VT));
12581 }
12582 }
12583
12584 return SDValue();
12585}
Evan Cheng0b0cd912009-03-28 05:57:29 +000012586
Nate Begeman740ab032009-01-26 00:52:55 +000012587/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
12588/// when possible.
12589static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
12590 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000012591 EVT VT = N->getValueType(0);
12592 if (!VT.isVector() && VT.isInteger() &&
12593 N->getOpcode() == ISD::SHL)
12594 return PerformSHLCombine(N, DAG);
12595
Nate Begeman740ab032009-01-26 00:52:55 +000012596 // On X86 with SSE2 support, we can transform this to a vector shift if
12597 // all elements are shifted by the same amount. We can't do this in legalize
12598 // because the a constant vector is typically transformed to a constant pool
12599 // so we have no knowledge of the shift amount.
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +000012600 if (!(Subtarget->hasSSE2() || Subtarget->hasAVX()))
Nate Begemanc2fd67f2009-01-26 03:15:31 +000012601 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000012602
Owen Anderson825b72b2009-08-11 20:47:22 +000012603 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +000012604 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000012605
Mon P Wang3becd092009-01-28 08:12:05 +000012606 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000012607 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000012608 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000012609 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000012610 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
12611 unsigned NumElts = VT.getVectorNumElements();
12612 unsigned i = 0;
12613 for (; i != NumElts; ++i) {
12614 SDValue Arg = ShAmtOp.getOperand(i);
12615 if (Arg.getOpcode() == ISD::UNDEF) continue;
12616 BaseShAmt = Arg;
12617 break;
12618 }
12619 for (; i != NumElts; ++i) {
12620 SDValue Arg = ShAmtOp.getOperand(i);
12621 if (Arg.getOpcode() == ISD::UNDEF) continue;
12622 if (Arg != BaseShAmt) {
12623 return SDValue();
12624 }
12625 }
12626 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000012627 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000012628 SDValue InVec = ShAmtOp.getOperand(0);
12629 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
12630 unsigned NumElts = InVec.getValueType().getVectorNumElements();
12631 unsigned i = 0;
12632 for (; i != NumElts; ++i) {
12633 SDValue Arg = InVec.getOperand(i);
12634 if (Arg.getOpcode() == ISD::UNDEF) continue;
12635 BaseShAmt = Arg;
12636 break;
12637 }
12638 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
12639 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000012640 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000012641 if (C->getZExtValue() == SplatIdx)
12642 BaseShAmt = InVec.getOperand(1);
12643 }
12644 }
12645 if (BaseShAmt.getNode() == 0)
12646 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
12647 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000012648 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000012649 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000012650
Mon P Wangefa42202009-09-03 19:56:25 +000012651 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000012652 if (EltVT.bitsGT(MVT::i32))
12653 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
12654 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000012655 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000012656
Nate Begemanc2fd67f2009-01-26 03:15:31 +000012657 // The shift amount is identical so we can do a vector shift.
12658 SDValue ValOp = N->getOperand(0);
12659 switch (N->getOpcode()) {
12660 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000012661 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000012662 break;
12663 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000012664 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000012665 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000012666 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000012667 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000012668 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000012669 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000012670 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000012671 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000012672 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000012673 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000012674 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000012675 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000012676 break;
12677 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000012678 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000012679 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000012680 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000012681 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000012682 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000012683 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000012684 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000012685 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000012686 break;
12687 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000012688 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000012689 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000012690 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000012691 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000012692 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000012693 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000012694 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000012695 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000012696 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000012697 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000012698 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000012699 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000012700 break;
Nate Begeman740ab032009-01-26 00:52:55 +000012701 }
12702 return SDValue();
12703}
12704
Nate Begemanb65c1752010-12-17 22:55:37 +000012705
Stuart Hastings865f0932011-06-03 23:53:54 +000012706// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
12707// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
12708// and friends. Likewise for OR -> CMPNEQSS.
12709static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
12710 TargetLowering::DAGCombinerInfo &DCI,
12711 const X86Subtarget *Subtarget) {
12712 unsigned opcode;
12713
12714 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
12715 // we're requiring SSE2 for both.
12716 if (Subtarget->hasSSE2() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
12717 SDValue N0 = N->getOperand(0);
12718 SDValue N1 = N->getOperand(1);
12719 SDValue CMP0 = N0->getOperand(1);
12720 SDValue CMP1 = N1->getOperand(1);
12721 DebugLoc DL = N->getDebugLoc();
12722
12723 // The SETCCs should both refer to the same CMP.
12724 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
12725 return SDValue();
12726
12727 SDValue CMP00 = CMP0->getOperand(0);
12728 SDValue CMP01 = CMP0->getOperand(1);
12729 EVT VT = CMP00.getValueType();
12730
12731 if (VT == MVT::f32 || VT == MVT::f64) {
12732 bool ExpectingFlags = false;
12733 // Check for any users that want flags:
12734 for (SDNode::use_iterator UI = N->use_begin(),
12735 UE = N->use_end();
12736 !ExpectingFlags && UI != UE; ++UI)
12737 switch (UI->getOpcode()) {
12738 default:
12739 case ISD::BR_CC:
12740 case ISD::BRCOND:
12741 case ISD::SELECT:
12742 ExpectingFlags = true;
12743 break;
12744 case ISD::CopyToReg:
12745 case ISD::SIGN_EXTEND:
12746 case ISD::ZERO_EXTEND:
12747 case ISD::ANY_EXTEND:
12748 break;
12749 }
12750
12751 if (!ExpectingFlags) {
12752 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
12753 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
12754
12755 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
12756 X86::CondCode tmp = cc0;
12757 cc0 = cc1;
12758 cc1 = tmp;
12759 }
12760
12761 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
12762 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
12763 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
12764 X86ISD::NodeType NTOperator = is64BitFP ?
12765 X86ISD::FSETCCsd : X86ISD::FSETCCss;
12766 // FIXME: need symbolic constants for these magic numbers.
12767 // See X86ATTInstPrinter.cpp:printSSECC().
12768 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
12769 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
12770 DAG.getConstant(x86cc, MVT::i8));
12771 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
12772 OnesOrZeroesF);
12773 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
12774 DAG.getConstant(1, MVT::i32));
12775 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
12776 return OneBitOfTruth;
12777 }
12778 }
12779 }
12780 }
12781 return SDValue();
12782}
12783
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000012784/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
12785/// so it can be folded inside ANDNP.
12786static bool CanFoldXORWithAllOnes(const SDNode *N) {
12787 EVT VT = N->getValueType(0);
12788
12789 // Match direct AllOnes for 128 and 256-bit vectors
12790 if (ISD::isBuildVectorAllOnes(N))
12791 return true;
12792
12793 // Look through a bit convert.
12794 if (N->getOpcode() == ISD::BITCAST)
12795 N = N->getOperand(0).getNode();
12796
12797 // Sometimes the operand may come from a insert_subvector building a 256-bit
12798 // allones vector
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000012799 if (VT.getSizeInBits() == 256 &&
Bill Wendling456a9252011-08-04 00:32:58 +000012800 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
12801 SDValue V1 = N->getOperand(0);
12802 SDValue V2 = N->getOperand(1);
12803
12804 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
12805 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
12806 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
12807 ISD::isBuildVectorAllOnes(V2.getNode()))
12808 return true;
12809 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000012810
12811 return false;
12812}
12813
Nate Begemanb65c1752010-12-17 22:55:37 +000012814static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
12815 TargetLowering::DAGCombinerInfo &DCI,
12816 const X86Subtarget *Subtarget) {
12817 if (DCI.isBeforeLegalizeOps())
12818 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012819
Stuart Hastings865f0932011-06-03 23:53:54 +000012820 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
12821 if (R.getNode())
12822 return R;
12823
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000012824 // Want to form ANDNP nodes:
12825 // 1) In the hopes of then easily combining them with OR and AND nodes
12826 // to form PBLEND/PSIGN.
12827 // 2) To match ANDN packed intrinsics
Nate Begemanb65c1752010-12-17 22:55:37 +000012828 EVT VT = N->getValueType(0);
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000012829 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000012830 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012831
Nate Begemanb65c1752010-12-17 22:55:37 +000012832 SDValue N0 = N->getOperand(0);
12833 SDValue N1 = N->getOperand(1);
12834 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012835
Nate Begemanb65c1752010-12-17 22:55:37 +000012836 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012837 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000012838 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
12839 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000012840 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000012841
12842 // Check RHS for vnot
12843 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000012844 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
12845 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000012846 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012847
Nate Begemanb65c1752010-12-17 22:55:37 +000012848 return SDValue();
12849}
12850
Evan Cheng760d1942010-01-04 21:22:48 +000012851static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000012852 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000012853 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000012854 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000012855 return SDValue();
12856
Stuart Hastings865f0932011-06-03 23:53:54 +000012857 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
12858 if (R.getNode())
12859 return R;
12860
Evan Cheng760d1942010-01-04 21:22:48 +000012861 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000012862 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64 && VT != MVT::v2i64)
Evan Cheng760d1942010-01-04 21:22:48 +000012863 return SDValue();
12864
Evan Cheng760d1942010-01-04 21:22:48 +000012865 SDValue N0 = N->getOperand(0);
12866 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012867
Nate Begemanb65c1752010-12-17 22:55:37 +000012868 // look for psign/blend
12869 if (Subtarget->hasSSSE3()) {
12870 if (VT == MVT::v2i64) {
12871 // Canonicalize pandn to RHS
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000012872 if (N0.getOpcode() == X86ISD::ANDNP)
Nate Begemanb65c1752010-12-17 22:55:37 +000012873 std::swap(N0, N1);
12874 // or (and (m, x), (pandn m, y))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000012875 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
Nate Begemanb65c1752010-12-17 22:55:37 +000012876 SDValue Mask = N1.getOperand(0);
12877 SDValue X = N1.getOperand(1);
12878 SDValue Y;
12879 if (N0.getOperand(0) == Mask)
12880 Y = N0.getOperand(1);
12881 if (N0.getOperand(1) == Mask)
12882 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012883
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000012884 // Check to see if the mask appeared in both the AND and ANDNP and
Nate Begemanb65c1752010-12-17 22:55:37 +000012885 if (!Y.getNode())
12886 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012887
Nate Begemanb65c1752010-12-17 22:55:37 +000012888 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
12889 if (Mask.getOpcode() != ISD::BITCAST ||
12890 X.getOpcode() != ISD::BITCAST ||
12891 Y.getOpcode() != ISD::BITCAST)
12892 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012893
Nate Begemanb65c1752010-12-17 22:55:37 +000012894 // Look through mask bitcast.
12895 Mask = Mask.getOperand(0);
12896 EVT MaskVT = Mask.getValueType();
12897
12898 // Validate that the Mask operand is a vector sra node. The sra node
12899 // will be an intrinsic.
12900 if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
12901 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012902
Nate Begemanb65c1752010-12-17 22:55:37 +000012903 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
12904 // there is no psrai.b
12905 switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
12906 case Intrinsic::x86_sse2_psrai_w:
12907 case Intrinsic::x86_sse2_psrai_d:
12908 break;
12909 default: return SDValue();
12910 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012911
Nate Begemanb65c1752010-12-17 22:55:37 +000012912 // Check that the SRA is all signbits.
12913 SDValue SraC = Mask.getOperand(2);
12914 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
12915 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
12916 if ((SraAmt + 1) != EltBits)
12917 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012918
Nate Begemanb65c1752010-12-17 22:55:37 +000012919 DebugLoc DL = N->getDebugLoc();
12920
12921 // Now we know we at least have a plendvb with the mask val. See if
12922 // we can form a psignb/w/d.
12923 // psign = x.type == y.type == mask.type && y = sub(0, x);
12924 X = X.getOperand(0);
12925 Y = Y.getOperand(0);
12926 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
12927 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
12928 X.getValueType() == MaskVT && X.getValueType() == Y.getValueType()){
12929 unsigned Opc = 0;
12930 switch (EltBits) {
12931 case 8: Opc = X86ISD::PSIGNB; break;
12932 case 16: Opc = X86ISD::PSIGNW; break;
12933 case 32: Opc = X86ISD::PSIGND; break;
12934 default: break;
12935 }
12936 if (Opc) {
12937 SDValue Sign = DAG.getNode(Opc, DL, MaskVT, X, Mask.getOperand(1));
12938 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Sign);
12939 }
12940 }
12941 // PBLENDVB only available on SSE 4.1
12942 if (!Subtarget->hasSSE41())
12943 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012944
Nate Begemanb65c1752010-12-17 22:55:37 +000012945 X = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, X);
12946 Y = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Y);
12947 Mask = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Mask);
Nate Begeman672fb622010-12-20 22:04:24 +000012948 Mask = DAG.getNode(X86ISD::PBLENDVB, DL, MVT::v16i8, X, Y, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000012949 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Mask);
12950 }
12951 }
12952 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012953
Nate Begemanb65c1752010-12-17 22:55:37 +000012954 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000012955 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
12956 std::swap(N0, N1);
12957 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
12958 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000012959 if (!N0.hasOneUse() || !N1.hasOneUse())
12960 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000012961
12962 SDValue ShAmt0 = N0.getOperand(1);
12963 if (ShAmt0.getValueType() != MVT::i8)
12964 return SDValue();
12965 SDValue ShAmt1 = N1.getOperand(1);
12966 if (ShAmt1.getValueType() != MVT::i8)
12967 return SDValue();
12968 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
12969 ShAmt0 = ShAmt0.getOperand(0);
12970 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
12971 ShAmt1 = ShAmt1.getOperand(0);
12972
12973 DebugLoc DL = N->getDebugLoc();
12974 unsigned Opc = X86ISD::SHLD;
12975 SDValue Op0 = N0.getOperand(0);
12976 SDValue Op1 = N1.getOperand(0);
12977 if (ShAmt0.getOpcode() == ISD::SUB) {
12978 Opc = X86ISD::SHRD;
12979 std::swap(Op0, Op1);
12980 std::swap(ShAmt0, ShAmt1);
12981 }
12982
Evan Cheng8b1190a2010-04-28 01:18:01 +000012983 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000012984 if (ShAmt1.getOpcode() == ISD::SUB) {
12985 SDValue Sum = ShAmt1.getOperand(0);
12986 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000012987 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
12988 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
12989 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
12990 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000012991 return DAG.getNode(Opc, DL, VT,
12992 Op0, Op1,
12993 DAG.getNode(ISD::TRUNCATE, DL,
12994 MVT::i8, ShAmt0));
12995 }
12996 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
12997 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
12998 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000012999 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000013000 return DAG.getNode(Opc, DL, VT,
13001 N0.getOperand(0), N1.getOperand(0),
13002 DAG.getNode(ISD::TRUNCATE, DL,
13003 MVT::i8, ShAmt0));
13004 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013005
Evan Cheng760d1942010-01-04 21:22:48 +000013006 return SDValue();
13007}
13008
Chris Lattner149a4e52008-02-22 02:09:43 +000013009/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000013010static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000013011 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000013012 StoreSDNode *St = cast<StoreSDNode>(N);
13013 EVT VT = St->getValue().getValueType();
13014 EVT StVT = St->getMemoryVT();
13015 DebugLoc dl = St->getDebugLoc();
Nadav Rotem5e742a32011-08-11 16:41:21 +000013016 SDValue StoredVal = St->getOperand(1);
13017 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13018
13019 // If we are saving a concatination of two XMM registers, perform two stores.
Nadav Rotem6236f7f2011-08-11 17:05:47 +000013020 // This is better in Sandy Bridge cause one 256-bit mem op is done via two
13021 // 128-bit ones. If in the future the cost becomes only one memory access the
13022 // first version would be better.
Nadav Rotem5e742a32011-08-11 16:41:21 +000013023 if (VT.getSizeInBits() == 256 &&
13024 StoredVal.getNode()->getOpcode() == ISD::CONCAT_VECTORS &&
13025 StoredVal.getNumOperands() == 2) {
13026
13027 SDValue Value0 = StoredVal.getOperand(0);
13028 SDValue Value1 = StoredVal.getOperand(1);
13029
13030 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
13031 SDValue Ptr0 = St->getBasePtr();
13032 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
13033
13034 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
13035 St->getPointerInfo(), St->isVolatile(),
13036 St->isNonTemporal(), St->getAlignment());
13037 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
13038 St->getPointerInfo(), St->isVolatile(),
13039 St->isNonTemporal(), St->getAlignment());
13040 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
13041 }
Nadav Rotem614061b2011-08-10 19:30:14 +000013042
13043 // Optimize trunc store (of multiple scalars) to shuffle and store.
13044 // First, pack all of the elements in one place. Next, store to memory
13045 // in fewer chunks.
13046 if (St->isTruncatingStore() && VT.isVector()) {
13047 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13048 unsigned NumElems = VT.getVectorNumElements();
13049 assert(StVT != VT && "Cannot truncate to the same type");
13050 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
13051 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
13052
13053 // From, To sizes and ElemCount must be pow of two
13054 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
13055 // We are going to use the original vector elt for storing.
13056 // accumulated smaller vector elements must be a multiple of bigger size.
13057 if (0 != (NumElems * ToSz) % FromSz) return SDValue();
13058 unsigned SizeRatio = FromSz / ToSz;
13059
13060 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
13061
13062 // Create a type on which we perform the shuffle
13063 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
13064 StVT.getScalarType(), NumElems*SizeRatio);
13065
13066 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
13067
13068 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
13069 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
13070 for (unsigned i = 0; i < NumElems; i++ ) ShuffleVec[i] = i * SizeRatio;
13071
13072 // Can't shuffle using an illegal type
13073 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
13074
13075 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
13076 DAG.getUNDEF(WideVec.getValueType()),
13077 ShuffleVec.data());
13078 // At this point all of the data is stored at the bottom of the
13079 // register. We now need to save it to mem.
13080
13081 // Find the largest store unit
13082 MVT StoreType = MVT::i8;
13083 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
13084 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
13085 MVT Tp = (MVT::SimpleValueType)tp;
13086 if (TLI.isTypeLegal(Tp) && StoreType.getSizeInBits() < NumElems * ToSz)
13087 StoreType = Tp;
13088 }
13089
13090 // Bitcast the original vector into a vector of store-size units
13091 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
13092 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
13093 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
13094 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
13095 SmallVector<SDValue, 8> Chains;
13096 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
13097 TLI.getPointerTy());
13098 SDValue Ptr = St->getBasePtr();
13099
13100 // Perform one or more big stores into memory.
13101 for (unsigned i = 0; i < (ToSz*NumElems)/StoreType.getSizeInBits() ; i++) {
13102 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
13103 StoreType, ShuffWide,
13104 DAG.getIntPtrConstant(i));
13105 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
13106 St->getPointerInfo(), St->isVolatile(),
13107 St->isNonTemporal(), St->getAlignment());
13108 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
13109 Chains.push_back(Ch);
13110 }
13111
13112 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
13113 Chains.size());
13114 }
13115
13116
Chris Lattner149a4e52008-02-22 02:09:43 +000013117 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
13118 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000013119 // A preferable solution to the general problem is to figure out the right
13120 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000013121
13122 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000013123 if (VT.getSizeInBits() != 64)
13124 return SDValue();
13125
Devang Patel578efa92009-06-05 21:57:13 +000013126 const Function *F = DAG.getMachineFunction().getFunction();
13127 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +000013128 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +000013129 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000013130 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000013131 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000013132 isa<LoadSDNode>(St->getValue()) &&
13133 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
13134 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000013135 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000013136 LoadSDNode *Ld = 0;
13137 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000013138 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000013139 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000013140 // Must be a store of a load. We currently handle two cases: the load
13141 // is a direct child, and it's under an intervening TokenFactor. It is
13142 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000013143 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000013144 Ld = cast<LoadSDNode>(St->getChain());
13145 else if (St->getValue().hasOneUse() &&
13146 ChainVal->getOpcode() == ISD::TokenFactor) {
13147 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000013148 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000013149 TokenFactorIndex = i;
13150 Ld = cast<LoadSDNode>(St->getValue());
13151 } else
13152 Ops.push_back(ChainVal->getOperand(i));
13153 }
13154 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000013155
Evan Cheng536e6672009-03-12 05:59:15 +000013156 if (!Ld || !ISD::isNormalLoad(Ld))
13157 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000013158
Evan Cheng536e6672009-03-12 05:59:15 +000013159 // If this is not the MMX case, i.e. we are just turning i64 load/store
13160 // into f64 load/store, avoid the transformation if there are multiple
13161 // uses of the loaded value.
13162 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
13163 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000013164
Evan Cheng536e6672009-03-12 05:59:15 +000013165 DebugLoc LdDL = Ld->getDebugLoc();
13166 DebugLoc StDL = N->getDebugLoc();
13167 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
13168 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
13169 // pair instead.
13170 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000013171 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000013172 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
13173 Ld->getPointerInfo(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000013174 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000013175 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000013176 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000013177 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000013178 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000013179 Ops.size());
13180 }
Evan Cheng536e6672009-03-12 05:59:15 +000013181 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000013182 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000013183 St->isVolatile(), St->isNonTemporal(),
13184 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000013185 }
Evan Cheng536e6672009-03-12 05:59:15 +000013186
13187 // Otherwise, lower to two pairs of 32-bit loads / stores.
13188 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000013189 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
13190 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000013191
Owen Anderson825b72b2009-08-11 20:47:22 +000013192 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000013193 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000013194 Ld->isVolatile(), Ld->isNonTemporal(),
13195 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000013196 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000013197 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000013198 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000013199 MinAlign(Ld->getAlignment(), 4));
13200
13201 SDValue NewChain = LoLd.getValue(1);
13202 if (TokenFactorIndex != -1) {
13203 Ops.push_back(LoLd);
13204 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000013205 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000013206 Ops.size());
13207 }
13208
13209 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000013210 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
13211 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000013212
13213 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000013214 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000013215 St->isVolatile(), St->isNonTemporal(),
13216 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000013217 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000013218 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000013219 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000013220 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000013221 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000013222 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000013223 }
Dan Gohman475871a2008-07-27 21:46:04 +000013224 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000013225}
13226
Chris Lattner6cf73262008-01-25 06:14:17 +000013227/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
13228/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000013229static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000013230 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
13231 // F[X]OR(0.0, x) -> x
13232 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000013233 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
13234 if (C->getValueAPF().isPosZero())
13235 return N->getOperand(1);
13236 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
13237 if (C->getValueAPF().isPosZero())
13238 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000013239 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000013240}
13241
13242/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000013243static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000013244 // FAND(0.0, x) -> 0.0
13245 // FAND(x, 0.0) -> 0.0
13246 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
13247 if (C->getValueAPF().isPosZero())
13248 return N->getOperand(0);
13249 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
13250 if (C->getValueAPF().isPosZero())
13251 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000013252 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000013253}
13254
Dan Gohmane5af2d32009-01-29 01:59:02 +000013255static SDValue PerformBTCombine(SDNode *N,
13256 SelectionDAG &DAG,
13257 TargetLowering::DAGCombinerInfo &DCI) {
13258 // BT ignores high bits in the bit index operand.
13259 SDValue Op1 = N->getOperand(1);
13260 if (Op1.hasOneUse()) {
13261 unsigned BitWidth = Op1.getValueSizeInBits();
13262 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
13263 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000013264 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
13265 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000013266 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000013267 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
13268 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
13269 DCI.CommitTargetLoweringOpt(TLO);
13270 }
13271 return SDValue();
13272}
Chris Lattner83e6c992006-10-04 06:57:07 +000013273
Eli Friedman7a5e5552009-06-07 06:52:44 +000013274static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
13275 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000013276 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000013277 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000013278 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000013279 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000013280 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000013281 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000013282 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000013283 }
13284 return SDValue();
13285}
13286
Evan Cheng2e489c42009-12-16 00:53:11 +000013287static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
13288 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
13289 // (and (i32 x86isd::setcc_carry), 1)
13290 // This eliminates the zext. This transformation is necessary because
13291 // ISD::SETCC is always legalized to i8.
13292 DebugLoc dl = N->getDebugLoc();
13293 SDValue N0 = N->getOperand(0);
13294 EVT VT = N->getValueType(0);
13295 if (N0.getOpcode() == ISD::AND &&
13296 N0.hasOneUse() &&
13297 N0.getOperand(0).hasOneUse()) {
13298 SDValue N00 = N0.getOperand(0);
13299 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
13300 return SDValue();
13301 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
13302 if (!C || C->getZExtValue() != 1)
13303 return SDValue();
13304 return DAG.getNode(ISD::AND, dl, VT,
13305 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
13306 N00.getOperand(0), N00.getOperand(1)),
13307 DAG.getConstant(1, VT));
13308 }
13309
13310 return SDValue();
13311}
13312
Chris Lattnerc19d1c32010-12-19 22:08:31 +000013313// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
13314static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
13315 unsigned X86CC = N->getConstantOperandVal(0);
13316 SDValue EFLAG = N->getOperand(1);
13317 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013318
Chris Lattnerc19d1c32010-12-19 22:08:31 +000013319 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
13320 // a zext and produces an all-ones bit which is more useful than 0/1 in some
13321 // cases.
13322 if (X86CC == X86::COND_B)
13323 return DAG.getNode(ISD::AND, DL, MVT::i8,
13324 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
13325 DAG.getConstant(X86CC, MVT::i8), EFLAG),
13326 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013327
Chris Lattnerc19d1c32010-12-19 22:08:31 +000013328 return SDValue();
13329}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013330
Benjamin Kramer1396c402011-06-18 11:09:41 +000013331static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
13332 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000013333 SDValue Op0 = N->getOperand(0);
13334 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
13335 // a 32-bit target where SSE doesn't support i64->FP operations.
13336 if (Op0.getOpcode() == ISD::LOAD) {
13337 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
13338 EVT VT = Ld->getValueType(0);
13339 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
13340 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
13341 !XTLI->getSubtarget()->is64Bit() &&
13342 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000013343 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
13344 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000013345 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
13346 return FILDChain;
13347 }
13348 }
13349 return SDValue();
13350}
13351
Chris Lattner23a01992010-12-20 01:37:09 +000013352// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
13353static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
13354 X86TargetLowering::DAGCombinerInfo &DCI) {
13355 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
13356 // the result is either zero or one (depending on the input carry bit).
13357 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
13358 if (X86::isZeroNode(N->getOperand(0)) &&
13359 X86::isZeroNode(N->getOperand(1)) &&
13360 // We don't have a good way to replace an EFLAGS use, so only do this when
13361 // dead right now.
13362 SDValue(N, 1).use_empty()) {
13363 DebugLoc DL = N->getDebugLoc();
13364 EVT VT = N->getValueType(0);
13365 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
13366 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
13367 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
13368 DAG.getConstant(X86::COND_B,MVT::i8),
13369 N->getOperand(2)),
13370 DAG.getConstant(1, VT));
13371 return DCI.CombineTo(N, Res1, CarryOut);
13372 }
13373
13374 return SDValue();
13375}
13376
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000013377// fold (add Y, (sete X, 0)) -> adc 0, Y
13378// (add Y, (setne X, 0)) -> sbb -1, Y
13379// (sub (sete X, 0), Y) -> sbb 0, Y
13380// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000013381static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000013382 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013383
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000013384 // Look through ZExts.
13385 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
13386 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
13387 return SDValue();
13388
13389 SDValue SetCC = Ext.getOperand(0);
13390 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
13391 return SDValue();
13392
13393 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
13394 if (CC != X86::COND_E && CC != X86::COND_NE)
13395 return SDValue();
13396
13397 SDValue Cmp = SetCC.getOperand(1);
13398 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000013399 !X86::isZeroNode(Cmp.getOperand(1)) ||
13400 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000013401 return SDValue();
13402
13403 SDValue CmpOp0 = Cmp.getOperand(0);
13404 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
13405 DAG.getConstant(1, CmpOp0.getValueType()));
13406
13407 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
13408 if (CC == X86::COND_NE)
13409 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
13410 DL, OtherVal.getValueType(), OtherVal,
13411 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
13412 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
13413 DL, OtherVal.getValueType(), OtherVal,
13414 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
13415}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000013416
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000013417static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG) {
13418 SDValue Op0 = N->getOperand(0);
13419 SDValue Op1 = N->getOperand(1);
13420
13421 // X86 can't encode an immediate LHS of a sub. See if we can push the
13422 // negation into a preceding instruction.
13423 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000013424 // If the RHS of the sub is a XOR with one use and a constant, invert the
13425 // immediate. Then add one to the LHS of the sub so we can turn
13426 // X-Y -> X+~Y+1, saving one register.
13427 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
13428 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000013429 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000013430 EVT VT = Op0.getValueType();
13431 SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
13432 Op1.getOperand(0),
13433 DAG.getConstant(~XorC, VT));
13434 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000013435 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000013436 }
13437 }
13438
13439 return OptimizeConditionalInDecrement(N, DAG);
13440}
13441
Dan Gohman475871a2008-07-27 21:46:04 +000013442SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000013443 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000013444 SelectionDAG &DAG = DCI.DAG;
13445 switch (N->getOpcode()) {
13446 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013447 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000013448 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +000013449 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000013450 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000013451 case ISD::ADD: return OptimizeConditionalInDecrement(N, DAG);
13452 case ISD::SUB: return PerformSubCombine(N, DAG);
Chris Lattner23a01992010-12-20 01:37:09 +000013453 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000013454 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000013455 case ISD::SHL:
13456 case ISD::SRA:
13457 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000013458 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000013459 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000013460 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000013461 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Chris Lattner6cf73262008-01-25 06:14:17 +000013462 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000013463 case X86ISD::FOR: return PerformFORCombine(N, DAG);
13464 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000013465 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000013466 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000013467 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000013468 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000013469 case X86ISD::SHUFPS: // Handle all target specific shuffles
13470 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000013471 case X86ISD::PALIGN:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000013472 case X86ISD::PUNPCKHBW:
13473 case X86ISD::PUNPCKHWD:
13474 case X86ISD::PUNPCKHDQ:
13475 case X86ISD::PUNPCKHQDQ:
13476 case X86ISD::UNPCKHPS:
13477 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +000013478 case X86ISD::VUNPCKHPSY:
13479 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000013480 case X86ISD::PUNPCKLBW:
13481 case X86ISD::PUNPCKLWD:
13482 case X86ISD::PUNPCKLDQ:
13483 case X86ISD::PUNPCKLQDQ:
13484 case X86ISD::UNPCKLPS:
13485 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +000013486 case X86ISD::VUNPCKLPSY:
13487 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000013488 case X86ISD::MOVHLPS:
13489 case X86ISD::MOVLHPS:
13490 case X86ISD::PSHUFD:
13491 case X86ISD::PSHUFHW:
13492 case X86ISD::PSHUFLW:
13493 case X86ISD::MOVSS:
13494 case X86ISD::MOVSD:
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +000013495 case X86ISD::VPERMILPS:
13496 case X86ISD::VPERMILPSY:
13497 case X86ISD::VPERMILPD:
13498 case X86ISD::VPERMILPDY:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +000013499 case X86ISD::VPERM2F128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000013500 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000013501 }
13502
Dan Gohman475871a2008-07-27 21:46:04 +000013503 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000013504}
13505
Evan Chenge5b51ac2010-04-17 06:13:15 +000013506/// isTypeDesirableForOp - Return true if the target has native support for
13507/// the specified value type and it is 'desirable' to use the type for the
13508/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
13509/// instruction encodings are longer and some i16 instructions are slow.
13510bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
13511 if (!isTypeLegal(VT))
13512 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000013513 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000013514 return true;
13515
13516 switch (Opc) {
13517 default:
13518 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000013519 case ISD::LOAD:
13520 case ISD::SIGN_EXTEND:
13521 case ISD::ZERO_EXTEND:
13522 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000013523 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000013524 case ISD::SRL:
13525 case ISD::SUB:
13526 case ISD::ADD:
13527 case ISD::MUL:
13528 case ISD::AND:
13529 case ISD::OR:
13530 case ISD::XOR:
13531 return false;
13532 }
13533}
13534
13535/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000013536/// beneficial for dag combiner to promote the specified node. If true, it
13537/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000013538bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000013539 EVT VT = Op.getValueType();
13540 if (VT != MVT::i16)
13541 return false;
13542
Evan Cheng4c26e932010-04-19 19:29:22 +000013543 bool Promote = false;
13544 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000013545 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000013546 default: break;
13547 case ISD::LOAD: {
13548 LoadSDNode *LD = cast<LoadSDNode>(Op);
13549 // If the non-extending load has a single use and it's not live out, then it
13550 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000013551 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
13552 Op.hasOneUse()*/) {
13553 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
13554 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
13555 // The only case where we'd want to promote LOAD (rather then it being
13556 // promoted as an operand is when it's only use is liveout.
13557 if (UI->getOpcode() != ISD::CopyToReg)
13558 return false;
13559 }
13560 }
Evan Cheng4c26e932010-04-19 19:29:22 +000013561 Promote = true;
13562 break;
13563 }
13564 case ISD::SIGN_EXTEND:
13565 case ISD::ZERO_EXTEND:
13566 case ISD::ANY_EXTEND:
13567 Promote = true;
13568 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000013569 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000013570 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000013571 SDValue N0 = Op.getOperand(0);
13572 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000013573 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000013574 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000013575 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000013576 break;
13577 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000013578 case ISD::ADD:
13579 case ISD::MUL:
13580 case ISD::AND:
13581 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000013582 case ISD::XOR:
13583 Commute = true;
13584 // fallthrough
13585 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000013586 SDValue N0 = Op.getOperand(0);
13587 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000013588 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000013589 return false;
13590 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000013591 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000013592 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000013593 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000013594 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000013595 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000013596 }
13597 }
13598
13599 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000013600 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000013601}
13602
Evan Cheng60c07e12006-07-05 22:17:51 +000013603//===----------------------------------------------------------------------===//
13604// X86 Inline Assembly Support
13605//===----------------------------------------------------------------------===//
13606
Chris Lattnerb8105652009-07-20 17:51:36 +000013607bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
13608 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000013609
13610 std::string AsmStr = IA->getAsmString();
13611
13612 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000013613 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000013614 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000013615
13616 switch (AsmPieces.size()) {
13617 default: return false;
13618 case 1:
13619 AsmStr = AsmPieces[0];
13620 AsmPieces.clear();
13621 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
13622
Chris Lattner7a2bdde2011-04-15 05:18:47 +000013623 // FIXME: this should verify that we are targeting a 486 or better. If not,
Evan Cheng55d42002011-01-08 01:24:27 +000013624 // we will turn this bswap into something that will be lowered to logical ops
13625 // instead of emitting the bswap asm. For now, we don't support 486 or lower
13626 // so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000013627 // bswap $0
13628 if (AsmPieces.size() == 2 &&
13629 (AsmPieces[0] == "bswap" ||
13630 AsmPieces[0] == "bswapq" ||
13631 AsmPieces[0] == "bswapl") &&
13632 (AsmPieces[1] == "$0" ||
13633 AsmPieces[1] == "${0:q}")) {
13634 // No need to check constraints, nothing other than the equivalent of
13635 // "=r,0" would be valid here.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000013636 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000013637 if (!Ty || Ty->getBitWidth() % 16 != 0)
13638 return false;
13639 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000013640 }
13641 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000013642 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000013643 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000013644 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000013645 AsmPieces[1] == "$$8," &&
13646 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000013647 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
13648 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000013649 const std::string &ConstraintsStr = IA->getConstraintString();
13650 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000013651 std::sort(AsmPieces.begin(), AsmPieces.end());
13652 if (AsmPieces.size() == 4 &&
13653 AsmPieces[0] == "~{cc}" &&
13654 AsmPieces[1] == "~{dirflag}" &&
13655 AsmPieces[2] == "~{flags}" &&
13656 AsmPieces[3] == "~{fpsr}") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000013657 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000013658 if (!Ty || Ty->getBitWidth() % 16 != 0)
13659 return false;
13660 return IntrinsicLowering::LowerToByteSwap(CI);
Dan Gohman0ef701e2010-03-04 19:58:08 +000013661 }
Chris Lattnerb8105652009-07-20 17:51:36 +000013662 }
13663 break;
13664 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000013665 if (CI->getType()->isIntegerTy(32) &&
13666 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
13667 SmallVector<StringRef, 4> Words;
13668 SplitString(AsmPieces[0], Words, " \t,");
13669 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
13670 Words[2] == "${0:w}") {
13671 Words.clear();
13672 SplitString(AsmPieces[1], Words, " \t,");
13673 if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
13674 Words[2] == "$0") {
13675 Words.clear();
13676 SplitString(AsmPieces[2], Words, " \t,");
13677 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
13678 Words[2] == "${0:w}") {
13679 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000013680 const std::string &ConstraintsStr = IA->getConstraintString();
13681 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Peter Collingbourne948cf022010-11-13 19:54:30 +000013682 std::sort(AsmPieces.begin(), AsmPieces.end());
13683 if (AsmPieces.size() == 4 &&
13684 AsmPieces[0] == "~{cc}" &&
13685 AsmPieces[1] == "~{dirflag}" &&
13686 AsmPieces[2] == "~{flags}" &&
13687 AsmPieces[3] == "~{fpsr}") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000013688 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000013689 if (!Ty || Ty->getBitWidth() % 16 != 0)
13690 return false;
13691 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000013692 }
13693 }
13694 }
13695 }
13696 }
Evan Cheng55d42002011-01-08 01:24:27 +000013697
13698 if (CI->getType()->isIntegerTy(64)) {
13699 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
13700 if (Constraints.size() >= 2 &&
13701 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
13702 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
13703 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
13704 SmallVector<StringRef, 4> Words;
13705 SplitString(AsmPieces[0], Words, " \t");
13706 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
Chris Lattnerb8105652009-07-20 17:51:36 +000013707 Words.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000013708 SplitString(AsmPieces[1], Words, " \t");
13709 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
13710 Words.clear();
13711 SplitString(AsmPieces[2], Words, " \t,");
13712 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
13713 Words[2] == "%edx") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000013714 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000013715 if (!Ty || Ty->getBitWidth() % 16 != 0)
13716 return false;
13717 return IntrinsicLowering::LowerToByteSwap(CI);
13718 }
Chris Lattnerb8105652009-07-20 17:51:36 +000013719 }
13720 }
13721 }
13722 }
13723 break;
13724 }
13725 return false;
13726}
13727
13728
13729
Chris Lattnerf4dff842006-07-11 02:54:03 +000013730/// getConstraintType - Given a constraint letter, return the type of
13731/// constraint it is for this target.
13732X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000013733X86TargetLowering::getConstraintType(const std::string &Constraint) const {
13734 if (Constraint.size() == 1) {
13735 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000013736 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000013737 case 'q':
13738 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000013739 case 'f':
13740 case 't':
13741 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000013742 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000013743 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000013744 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000013745 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000013746 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000013747 case 'a':
13748 case 'b':
13749 case 'c':
13750 case 'd':
13751 case 'S':
13752 case 'D':
13753 case 'A':
13754 return C_Register;
13755 case 'I':
13756 case 'J':
13757 case 'K':
13758 case 'L':
13759 case 'M':
13760 case 'N':
13761 case 'G':
13762 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000013763 case 'e':
13764 case 'Z':
13765 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000013766 default:
13767 break;
13768 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000013769 }
Chris Lattner4234f572007-03-25 02:14:49 +000013770 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000013771}
13772
John Thompson44ab89e2010-10-29 17:29:13 +000013773/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000013774/// This object must already have been set up with the operand type
13775/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000013776TargetLowering::ConstraintWeight
13777 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000013778 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000013779 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000013780 Value *CallOperandVal = info.CallOperandVal;
13781 // If we don't have a value, we can't do a match,
13782 // but allow it at the lowest weight.
13783 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000013784 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000013785 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000013786 // Look at the constraint type.
13787 switch (*constraint) {
13788 default:
John Thompson44ab89e2010-10-29 17:29:13 +000013789 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
13790 case 'R':
13791 case 'q':
13792 case 'Q':
13793 case 'a':
13794 case 'b':
13795 case 'c':
13796 case 'd':
13797 case 'S':
13798 case 'D':
13799 case 'A':
13800 if (CallOperandVal->getType()->isIntegerTy())
13801 weight = CW_SpecificReg;
13802 break;
13803 case 'f':
13804 case 't':
13805 case 'u':
13806 if (type->isFloatingPointTy())
13807 weight = CW_SpecificReg;
13808 break;
13809 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000013810 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000013811 weight = CW_SpecificReg;
13812 break;
13813 case 'x':
13814 case 'Y':
Nate Begeman2ea8ee72010-12-10 00:26:57 +000013815 if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
John Thompson44ab89e2010-10-29 17:29:13 +000013816 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000013817 break;
13818 case 'I':
13819 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
13820 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000013821 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000013822 }
13823 break;
John Thompson44ab89e2010-10-29 17:29:13 +000013824 case 'J':
13825 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
13826 if (C->getZExtValue() <= 63)
13827 weight = CW_Constant;
13828 }
13829 break;
13830 case 'K':
13831 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
13832 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
13833 weight = CW_Constant;
13834 }
13835 break;
13836 case 'L':
13837 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
13838 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
13839 weight = CW_Constant;
13840 }
13841 break;
13842 case 'M':
13843 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
13844 if (C->getZExtValue() <= 3)
13845 weight = CW_Constant;
13846 }
13847 break;
13848 case 'N':
13849 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
13850 if (C->getZExtValue() <= 0xff)
13851 weight = CW_Constant;
13852 }
13853 break;
13854 case 'G':
13855 case 'C':
13856 if (dyn_cast<ConstantFP>(CallOperandVal)) {
13857 weight = CW_Constant;
13858 }
13859 break;
13860 case 'e':
13861 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
13862 if ((C->getSExtValue() >= -0x80000000LL) &&
13863 (C->getSExtValue() <= 0x7fffffffLL))
13864 weight = CW_Constant;
13865 }
13866 break;
13867 case 'Z':
13868 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
13869 if (C->getZExtValue() <= 0xffffffff)
13870 weight = CW_Constant;
13871 }
13872 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000013873 }
13874 return weight;
13875}
13876
Dale Johannesenba2a0b92008-01-29 02:21:21 +000013877/// LowerXConstraint - try to replace an X constraint, which matches anything,
13878/// with another that has more specific requirements based on the type of the
13879/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000013880const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000013881LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000013882 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
13883 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000013884 if (ConstraintVT.isFloatingPoint()) {
Nate Begeman2ea8ee72010-12-10 00:26:57 +000013885 if (Subtarget->hasXMMInt())
Chris Lattner5e764232008-04-26 23:02:14 +000013886 return "Y";
Nate Begeman2ea8ee72010-12-10 00:26:57 +000013887 if (Subtarget->hasXMM())
Chris Lattner5e764232008-04-26 23:02:14 +000013888 return "x";
13889 }
Scott Michelfdc40a02009-02-17 22:15:04 +000013890
Chris Lattner5e764232008-04-26 23:02:14 +000013891 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000013892}
13893
Chris Lattner48884cd2007-08-25 00:47:38 +000013894/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
13895/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000013896void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000013897 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000013898 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000013899 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000013900 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000013901
Eric Christopher100c8332011-06-02 23:16:42 +000013902 // Only support length 1 constraints for now.
13903 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000013904
Eric Christopher100c8332011-06-02 23:16:42 +000013905 char ConstraintLetter = Constraint[0];
13906 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000013907 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000013908 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000013909 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000013910 if (C->getZExtValue() <= 31) {
13911 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000013912 break;
13913 }
Devang Patel84f7fd22007-03-17 00:13:28 +000013914 }
Chris Lattner48884cd2007-08-25 00:47:38 +000013915 return;
Evan Cheng364091e2008-09-22 23:57:37 +000013916 case 'J':
13917 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000013918 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000013919 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
13920 break;
13921 }
13922 }
13923 return;
13924 case 'K':
13925 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000013926 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000013927 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
13928 break;
13929 }
13930 }
13931 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000013932 case 'N':
13933 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000013934 if (C->getZExtValue() <= 255) {
13935 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000013936 break;
13937 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000013938 }
Chris Lattner48884cd2007-08-25 00:47:38 +000013939 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000013940 case 'e': {
13941 // 32-bit signed value
13942 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000013943 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
13944 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000013945 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000013946 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000013947 break;
13948 }
13949 // FIXME gcc accepts some relocatable values here too, but only in certain
13950 // memory models; it's complicated.
13951 }
13952 return;
13953 }
13954 case 'Z': {
13955 // 32-bit unsigned value
13956 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000013957 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
13958 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000013959 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
13960 break;
13961 }
13962 }
13963 // FIXME gcc accepts some relocatable values here too, but only in certain
13964 // memory models; it's complicated.
13965 return;
13966 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000013967 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000013968 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000013969 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000013970 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000013971 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000013972 break;
13973 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013974
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000013975 // In any sort of PIC mode addresses need to be computed at runtime by
13976 // adding in a register or some sort of table lookup. These can't
13977 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000013978 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000013979 return;
13980
Chris Lattnerdc43a882007-05-03 16:52:29 +000013981 // If we are in non-pic codegen mode, we allow the address of a global (with
13982 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000013983 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000013984 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000013985
Chris Lattner49921962009-05-08 18:23:14 +000013986 // Match either (GA), (GA+C), (GA+C1+C2), etc.
13987 while (1) {
13988 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
13989 Offset += GA->getOffset();
13990 break;
13991 } else if (Op.getOpcode() == ISD::ADD) {
13992 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
13993 Offset += C->getZExtValue();
13994 Op = Op.getOperand(0);
13995 continue;
13996 }
13997 } else if (Op.getOpcode() == ISD::SUB) {
13998 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
13999 Offset += -C->getZExtValue();
14000 Op = Op.getOperand(0);
14001 continue;
14002 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000014003 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000014004
Chris Lattner49921962009-05-08 18:23:14 +000014005 // Otherwise, this isn't something we can handle, reject it.
14006 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000014007 }
Eric Christopherfd179292009-08-27 18:07:15 +000014008
Dan Gohman46510a72010-04-15 01:51:59 +000014009 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000014010 // If we require an extra load to get this address, as in PIC mode, we
14011 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000014012 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
14013 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000014014 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000014015
Devang Patel0d881da2010-07-06 22:08:15 +000014016 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
14017 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000014018 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000014019 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000014020 }
Scott Michelfdc40a02009-02-17 22:15:04 +000014021
Gabor Greifba36cb52008-08-28 21:40:38 +000014022 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000014023 Ops.push_back(Result);
14024 return;
14025 }
Dale Johannesen1784d162010-06-25 21:55:36 +000014026 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000014027}
14028
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014029std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000014030X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000014031 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000014032 // First, see if this is a constraint that directly corresponds to an LLVM
14033 // register class.
14034 if (Constraint.size() == 1) {
14035 // GCC Constraint Letters
14036 switch (Constraint[0]) {
14037 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000014038 // TODO: Slight differences here in allocation order and leaving
14039 // RIP in the class. Do they matter any more here than they do
14040 // in the normal allocation?
14041 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
14042 if (Subtarget->is64Bit()) {
Nick Lewycky9bf45d02011-07-08 00:19:27 +000014043 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000014044 return std::make_pair(0U, X86::GR32RegisterClass);
14045 else if (VT == MVT::i16)
14046 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000014047 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000014048 return std::make_pair(0U, X86::GR8RegisterClass);
Nick Lewycky9bf45d02011-07-08 00:19:27 +000014049 else if (VT == MVT::i64 || VT == MVT::f64)
Eric Christopherd176af82011-06-29 17:23:50 +000014050 return std::make_pair(0U, X86::GR64RegisterClass);
14051 break;
14052 }
14053 // 32-bit fallthrough
14054 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000014055 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000014056 return std::make_pair(0U, X86::GR32_ABCDRegisterClass);
14057 else if (VT == MVT::i16)
14058 return std::make_pair(0U, X86::GR16_ABCDRegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000014059 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000014060 return std::make_pair(0U, X86::GR8_ABCD_LRegisterClass);
14061 else if (VT == MVT::i64)
14062 return std::make_pair(0U, X86::GR64_ABCDRegisterClass);
14063 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000014064 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000014065 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000014066 if (VT == MVT::i8 || VT == MVT::i1)
Chris Lattner0f65cad2007-04-09 05:49:22 +000014067 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000014068 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000014069 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000014070 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000014071 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000014072 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000014073 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000014074 if (VT == MVT::i8 || VT == MVT::i1)
Dale Johannesen5f3663e2009-10-07 22:47:20 +000014075 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
14076 if (VT == MVT::i16)
14077 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
14078 if (VT == MVT::i32 || !Subtarget->is64Bit())
14079 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
14080 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000014081 case 'f': // FP Stack registers.
14082 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
14083 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000014084 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000014085 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000014086 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000014087 return std::make_pair(0U, X86::RFP64RegisterClass);
14088 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000014089 case 'y': // MMX_REGS if MMX allowed.
14090 if (!Subtarget->hasMMX()) break;
14091 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000014092 case 'Y': // SSE_REGS if SSE2 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014093 if (!Subtarget->hasXMMInt()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000014094 // FALL THROUGH.
14095 case 'x': // SSE_REGS if SSE1 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014096 if (!Subtarget->hasXMM()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000014097
Owen Anderson825b72b2009-08-11 20:47:22 +000014098 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000014099 default: break;
14100 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000014101 case MVT::f32:
14102 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000014103 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000014104 case MVT::f64:
14105 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000014106 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000014107 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000014108 case MVT::v16i8:
14109 case MVT::v8i16:
14110 case MVT::v4i32:
14111 case MVT::v2i64:
14112 case MVT::v4f32:
14113 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000014114 return std::make_pair(0U, X86::VR128RegisterClass);
14115 }
Chris Lattnerad043e82007-04-09 05:11:28 +000014116 break;
14117 }
14118 }
Scott Michelfdc40a02009-02-17 22:15:04 +000014119
Chris Lattnerf76d1802006-07-31 23:26:50 +000014120 // Use the default implementation in TargetLowering to convert the register
14121 // constraint into a member of a register class.
14122 std::pair<unsigned, const TargetRegisterClass*> Res;
14123 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000014124
14125 // Not found as a standard register?
14126 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000014127 // Map st(0) -> st(7) -> ST0
14128 if (Constraint.size() == 7 && Constraint[0] == '{' &&
14129 tolower(Constraint[1]) == 's' &&
14130 tolower(Constraint[2]) == 't' &&
14131 Constraint[3] == '(' &&
14132 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
14133 Constraint[5] == ')' &&
14134 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000014135
Chris Lattner56d77c72009-09-13 22:41:48 +000014136 Res.first = X86::ST0+Constraint[4]-'0';
14137 Res.second = X86::RFP80RegisterClass;
14138 return Res;
14139 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000014140
Chris Lattner56d77c72009-09-13 22:41:48 +000014141 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000014142 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000014143 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000014144 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000014145 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000014146 }
Chris Lattner56d77c72009-09-13 22:41:48 +000014147
14148 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000014149 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000014150 Res.first = X86::EFLAGS;
14151 Res.second = X86::CCRRegisterClass;
14152 return Res;
14153 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000014154
Dale Johannesen330169f2008-11-13 21:52:36 +000014155 // 'A' means EAX + EDX.
14156 if (Constraint == "A") {
14157 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000014158 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000014159 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000014160 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000014161 return Res;
14162 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014163
Chris Lattnerf76d1802006-07-31 23:26:50 +000014164 // Otherwise, check to see if this is a register class of the wrong value
14165 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
14166 // turn into {ax},{dx}.
14167 if (Res.second->hasType(VT))
14168 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014169
Chris Lattnerf76d1802006-07-31 23:26:50 +000014170 // All of the single-register GCC register classes map their values onto
14171 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
14172 // really want an 8-bit or 32-bit register, map to the appropriate register
14173 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000014174 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000014175 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000014176 unsigned DestReg = 0;
14177 switch (Res.first) {
14178 default: break;
14179 case X86::AX: DestReg = X86::AL; break;
14180 case X86::DX: DestReg = X86::DL; break;
14181 case X86::CX: DestReg = X86::CL; break;
14182 case X86::BX: DestReg = X86::BL; break;
14183 }
14184 if (DestReg) {
14185 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000014186 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000014187 }
Owen Anderson825b72b2009-08-11 20:47:22 +000014188 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000014189 unsigned DestReg = 0;
14190 switch (Res.first) {
14191 default: break;
14192 case X86::AX: DestReg = X86::EAX; break;
14193 case X86::DX: DestReg = X86::EDX; break;
14194 case X86::CX: DestReg = X86::ECX; break;
14195 case X86::BX: DestReg = X86::EBX; break;
14196 case X86::SI: DestReg = X86::ESI; break;
14197 case X86::DI: DestReg = X86::EDI; break;
14198 case X86::BP: DestReg = X86::EBP; break;
14199 case X86::SP: DestReg = X86::ESP; break;
14200 }
14201 if (DestReg) {
14202 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000014203 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000014204 }
Owen Anderson825b72b2009-08-11 20:47:22 +000014205 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000014206 unsigned DestReg = 0;
14207 switch (Res.first) {
14208 default: break;
14209 case X86::AX: DestReg = X86::RAX; break;
14210 case X86::DX: DestReg = X86::RDX; break;
14211 case X86::CX: DestReg = X86::RCX; break;
14212 case X86::BX: DestReg = X86::RBX; break;
14213 case X86::SI: DestReg = X86::RSI; break;
14214 case X86::DI: DestReg = X86::RDI; break;
14215 case X86::BP: DestReg = X86::RBP; break;
14216 case X86::SP: DestReg = X86::RSP; break;
14217 }
14218 if (DestReg) {
14219 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000014220 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000014221 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000014222 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000014223 } else if (Res.second == X86::FR32RegisterClass ||
14224 Res.second == X86::FR64RegisterClass ||
14225 Res.second == X86::VR128RegisterClass) {
14226 // Handle references to XMM physical registers that got mapped into the
14227 // wrong class. This can happen with constraints like {xmm0} where the
14228 // target independent register mapper will just pick the first match it can
14229 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000014230 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000014231 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000014232 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000014233 Res.second = X86::FR64RegisterClass;
14234 else if (X86::VR128RegisterClass->hasType(VT))
14235 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000014236 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014237
Chris Lattnerf76d1802006-07-31 23:26:50 +000014238 return Res;
14239}