blob: 169e41b195b8dcbffa1a25860e873f197fb5d986 [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"
Chris Lattner589c6f62010-01-26 06:28:43 +000038#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000039#include "llvm/MC/MCContext.h"
Daniel Dunbar4e815f82010-03-15 23:51:06 +000040#include "llvm/MC/MCExpr.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000041#include "llvm/MC/MCSymbol.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000042#include "llvm/ADT/BitVector.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000043#include "llvm/ADT/SmallSet.h"
Evan Chengb1712452010-01-27 06:25:16 +000044#include "llvm/ADT/Statistic.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000045#include "llvm/ADT/StringExtras.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000046#include "llvm/ADT/VectorExtras.h"
Evan Cheng485fafc2011-03-21 01:19:09 +000047#include "llvm/Support/CallSite.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000048#include "llvm/Support/Debug.h"
Bill Wendlingec041eb2010-03-12 19:20:40 +000049#include "llvm/Support/Dwarf.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000050#include "llvm/Support/ErrorHandling.h"
51#include "llvm/Support/MathExtras.h"
Torok Edwindac237e2009-07-08 20:53:28 +000052#include "llvm/Support/raw_ostream.h"
Rafael Espindola151ab3e2011-08-30 19:47:04 +000053#include "llvm/Target/TargetOptions.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 Lopes0c4b9ff2011-09-15 18:27:36 +0000171 X86ScalarSSEf64 = Subtarget->hasXMMInt();
172 X86ScalarSSEf32 = Subtarget->hasXMM();
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);
Duncan Sands28b77e92011-09-06 19:07:46 +0000183 // X86-SSE is even stranger. It uses -1 or 0 for vector masks.
184 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Eric Christopher471e4222011-06-08 23:55:35 +0000185
Eric Christopherde5e1012011-03-11 01:05:58 +0000186 // For 64-bit since we have so many registers use the ILP scheduler, for
187 // 32-bit code use the register pressure specific scheduling.
188 if (Subtarget->is64Bit())
189 setSchedulingPreference(Sched::ILP);
190 else
191 setSchedulingPreference(Sched::RegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +0000192 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +0000193
Michael J. Spencer92bf38c2010-10-10 23:11:06 +0000194 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing()) {
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000195 // Setup Windows compiler runtime calls.
196 setLibcallName(RTLIB::SDIV_I64, "_alldiv");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000197 setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
Julien Lerougef2960822011-07-08 21:40:25 +0000198 setLibcallName(RTLIB::SREM_I64, "_allrem");
199 setLibcallName(RTLIB::UREM_I64, "_aullrem");
200 setLibcallName(RTLIB::MUL_I64, "_allmul");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000201 setLibcallName(RTLIB::FPTOUINT_F64_I64, "_ftol2");
Michael J. Spencer94f7eeb2010-10-19 07:32:52 +0000202 setLibcallName(RTLIB::FPTOUINT_F32_I64, "_ftol2");
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000203 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer335b8062010-10-11 05:29:15 +0000204 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
Julien Lerougef2960822011-07-08 21:40:25 +0000205 setLibcallCallingConv(RTLIB::SREM_I64, CallingConv::X86_StdCall);
206 setLibcallCallingConv(RTLIB::UREM_I64, CallingConv::X86_StdCall);
207 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::X86_StdCall);
Michael J. Spencer6dad10e2010-10-27 18:52:38 +0000208 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::C);
209 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::C);
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000210 }
211
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000212 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000213 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000214 setUseUnderscoreSetJmp(false);
215 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000216 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000217 // MS runtime is weird: it exports _setjmp, but longjmp!
218 setUseUnderscoreSetJmp(true);
219 setUseUnderscoreLongJmp(false);
220 } else {
221 setUseUnderscoreSetJmp(true);
222 setUseUnderscoreLongJmp(true);
223 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000224
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000225 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000226 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
Dan Gohman71edb242010-04-30 18:30:26 +0000227 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000228 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000229 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000230 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000231
Owen Anderson825b72b2009-08-11 20:47:22 +0000232 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000233
Scott Michelfdc40a02009-02-17 22:15:04 +0000234 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000235 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000236 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000237 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000238 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000239 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
240 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000241
242 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000243 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
244 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
245 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
246 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
247 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
248 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000249
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000250 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
251 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000252 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
253 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
254 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000255
Evan Cheng25ab6902006-09-08 06:48:29 +0000256 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000257 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
258 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000259 } else if (!TM.Options.UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000260 // We have an algorithm for SSE2->double, and we turn this into a
261 // 64-bit FILD followed by conditional FADD for other targets.
262 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000263 // We have an algorithm for SSE2, and we turn this into a 64-bit
264 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000265 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000266 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000267
268 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
269 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000270 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
271 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000272
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000273 if (!TM.Options.UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000274 // SSE has no i16 to fp conversion, only i32
275 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000276 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000277 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000278 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000279 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000280 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
281 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000282 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000283 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000284 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
285 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000286 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000287
Dale Johannesen73328d12007-09-19 23:55:34 +0000288 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
289 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000290 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
291 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000292
Evan Cheng02568ff2006-01-30 22:13:22 +0000293 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
294 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000295 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
296 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000297
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000298 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000299 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000300 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000301 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000302 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000303 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
304 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000305 }
306
307 // Handle FP_TO_UINT by promoting the destination to a larger signed
308 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000309 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
310 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
311 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000312
Evan Cheng25ab6902006-09-08 06:48:29 +0000313 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000314 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
315 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000316 } else if (!TM.Options.UseSoftFloat) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +0000317 // Since AVX is a superset of SSE3, only check for SSE here.
318 if (Subtarget->hasSSE1() && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000319 // Expand FP_TO_UINT into a select.
320 // FIXME: We would like to use a Custom expander here eventually to do
321 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000322 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000323 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000324 // With SSE3 we can use fisttpll to convert to a signed i64; without
325 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000326 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000327 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000328
Chris Lattner399610a2006-12-05 18:22:22 +0000329 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000330 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000331 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
332 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000333 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000334 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000335 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000336 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000337 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000338 }
Chris Lattner21f66852005-12-23 05:15:23 +0000339
Dan Gohmanb00ee212008-02-18 19:34:53 +0000340 // Scalar integer divide and remainder are lowered to use operations that
341 // produce two results, to match the available instructions. This exposes
342 // the two-result form to trivial CSE, which is able to combine x/y and x%y
343 // into a single instruction.
344 //
345 // Scalar integer multiply-high is also lowered to use two-result
346 // operations, to match the available instructions. However, plain multiply
347 // (low) operations are left as Legal, as there are single-result
348 // instructions for this in x86. Using the two-result multiply instructions
349 // when both high and low results are needed must be arranged by dagcombine.
Chris Lattnere019ec12010-12-19 20:07:10 +0000350 for (unsigned i = 0, e = 4; i != e; ++i) {
351 MVT VT = IntVTs[i];
352 setOperationAction(ISD::MULHS, VT, Expand);
353 setOperationAction(ISD::MULHU, VT, Expand);
354 setOperationAction(ISD::SDIV, VT, Expand);
355 setOperationAction(ISD::UDIV, VT, Expand);
356 setOperationAction(ISD::SREM, VT, Expand);
357 setOperationAction(ISD::UREM, VT, Expand);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000358
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000359 // Add/Sub overflow ops with MVT::Glues are lowered to EFLAGS dependences.
Chris Lattnerd8ff7ec2010-12-20 01:03:27 +0000360 setOperationAction(ISD::ADDC, VT, Custom);
361 setOperationAction(ISD::ADDE, VT, Custom);
362 setOperationAction(ISD::SUBC, VT, Custom);
363 setOperationAction(ISD::SUBE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000364 }
Dan Gohmana37c9f72007-09-25 18:23:27 +0000365
Owen Anderson825b72b2009-08-11 20:47:22 +0000366 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
367 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
368 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
369 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000370 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000371 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
372 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
373 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
374 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
375 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
376 setOperationAction(ISD::FREM , MVT::f32 , Expand);
377 setOperationAction(ISD::FREM , MVT::f64 , Expand);
378 setOperationAction(ISD::FREM , MVT::f80 , Expand);
379 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000380
Craig Topper909652f2011-10-14 03:21:46 +0000381 if (Subtarget->hasBMI()) {
382 setOperationAction(ISD::CTTZ , MVT::i8 , Promote);
383 } else {
384 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
385 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
386 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
387 if (Subtarget->is64Bit())
388 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
389 }
Craig Topper37f21672011-10-11 06:44:02 +0000390
391 if (Subtarget->hasLZCNT()) {
392 setOperationAction(ISD::CTLZ , MVT::i8 , Promote);
393 } else {
394 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
395 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
396 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
397 if (Subtarget->is64Bit())
398 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000399 }
400
Benjamin Kramer1292c222010-12-04 20:32:23 +0000401 if (Subtarget->hasPOPCNT()) {
402 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
403 } else {
404 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
405 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
406 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
407 if (Subtarget->is64Bit())
408 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
409 }
410
Owen Anderson825b72b2009-08-11 20:47:22 +0000411 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
412 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000413
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000414 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000415 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000416 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000417 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000418 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000419 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
420 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
421 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
422 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
423 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000424 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000425 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
426 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
427 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
428 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000429 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000430 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
Andrew Trickf6c39412011-03-23 23:11:02 +0000431 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000432 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000433 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000434
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000435 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000436 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
437 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
438 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
439 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000440 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000441 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
442 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000443 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000444 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000445 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
446 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
447 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
448 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000449 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000450 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000451 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000452 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
453 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
454 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000455 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000456 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
457 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
458 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000459 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000460
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000461 if (Subtarget->hasXMM())
Owen Anderson825b72b2009-08-11 20:47:22 +0000462 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000463
Eric Christopher9a9d2752010-07-22 02:48:34 +0000464 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000465 setOperationAction(ISD::ATOMIC_FENCE , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000466
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000467 // On X86 and X86-64, atomic operations are lowered to locked instructions.
468 // Locked instructions, in turn, have implicit fence semantics (all memory
469 // operations are flushed before issuing the locked instruction, and they
470 // are not buffered), so we can fold away the common pattern of
471 // fence-atomic-fence.
472 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000473
Mon P Wang63307c32008-05-05 19:05:59 +0000474 // Expand certain atomics
Chris Lattnere019ec12010-12-19 20:07:10 +0000475 for (unsigned i = 0, e = 4; i != e; ++i) {
476 MVT VT = IntVTs[i];
477 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
478 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
Eli Friedman327236c2011-08-24 20:50:09 +0000479 setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000480 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000481
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000482 if (!Subtarget->is64Bit()) {
Eli Friedmanf8f90f02011-08-24 22:33:28 +0000483 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000484 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
485 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
486 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
487 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
488 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
489 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
490 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000491 }
492
Eli Friedman43f51ae2011-08-26 21:21:21 +0000493 if (Subtarget->hasCmpxchg16b()) {
494 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i128, Custom);
495 }
496
Evan Cheng3c992d22006-03-07 02:02:57 +0000497 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000498 if (!Subtarget->isTargetDarwin() &&
499 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000500 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000501 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000502 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000503
Owen Anderson825b72b2009-08-11 20:47:22 +0000504 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
505 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
506 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
507 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000508 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000509 setExceptionPointerRegister(X86::RAX);
510 setExceptionSelectorRegister(X86::RDX);
511 } else {
512 setExceptionPointerRegister(X86::EAX);
513 setExceptionSelectorRegister(X86::EDX);
514 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000515 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
516 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000517
Duncan Sands4a544a72011-09-06 13:37:06 +0000518 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
519 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000520
Owen Anderson825b72b2009-08-11 20:47:22 +0000521 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000522
Nate Begemanacc398c2006-01-25 18:21:52 +0000523 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000524 setOperationAction(ISD::VASTART , MVT::Other, Custom);
525 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000526 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000527 setOperationAction(ISD::VAARG , MVT::Other, Custom);
528 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000529 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000530 setOperationAction(ISD::VAARG , MVT::Other, Expand);
531 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000532 }
Evan Chengae642192007-03-02 23:16:35 +0000533
Owen Anderson825b72b2009-08-11 20:47:22 +0000534 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
535 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eric Christopherc967ad82011-08-31 04:17:21 +0000536
537 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
538 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
539 MVT::i64 : MVT::i32, Custom);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000540 else if (TM.Options.EnableSegmentedStacks)
Eric Christopherc967ad82011-08-31 04:17:21 +0000541 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
542 MVT::i64 : MVT::i32, Custom);
543 else
544 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
545 MVT::i64 : MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000546
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000547 if (!TM.Options.UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000548 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000549 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000550 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
551 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000552
Evan Cheng223547a2006-01-31 22:28:30 +0000553 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000554 setOperationAction(ISD::FABS , MVT::f64, Custom);
555 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000556
557 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000558 setOperationAction(ISD::FNEG , MVT::f64, Custom);
559 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000560
Evan Cheng68c47cb2007-01-05 07:55:56 +0000561 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000562 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
563 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000564
Stuart Hastings4fd0dee2011-06-01 04:39:42 +0000565 // Lower this to FGETSIGNx86 plus an AND.
566 setOperationAction(ISD::FGETSIGN, MVT::i64, Custom);
567 setOperationAction(ISD::FGETSIGN, MVT::i32, Custom);
568
Evan Chengd25e9e82006-02-02 00:28:23 +0000569 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000570 setOperationAction(ISD::FSIN , MVT::f64, Expand);
571 setOperationAction(ISD::FCOS , MVT::f64, Expand);
572 setOperationAction(ISD::FSIN , MVT::f32, Expand);
573 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000574
Chris Lattnera54aa942006-01-29 06:26:08 +0000575 // Expand FP immediates into loads from the stack, except for the special
576 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000577 addLegalFPImmediate(APFloat(+0.0)); // xorpd
578 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000579 } else if (!TM.Options.UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000580 // Use SSE for f32, x87 for f64.
581 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000582 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
583 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000584
585 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000586 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000587
588 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000589 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000590
Owen Anderson825b72b2009-08-11 20:47:22 +0000591 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000592
593 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000594 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
595 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000596
597 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000598 setOperationAction(ISD::FSIN , MVT::f32, Expand);
599 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000600
Nate Begemane1795842008-02-14 08:57:00 +0000601 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000602 addLegalFPImmediate(APFloat(+0.0f)); // xorps
603 addLegalFPImmediate(APFloat(+0.0)); // FLD0
604 addLegalFPImmediate(APFloat(+1.0)); // FLD1
605 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
606 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
607
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000608 if (!TM.Options.UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000609 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
610 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000611 }
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000612 } else if (!TM.Options.UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000613 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000614 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000615 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
616 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000617
Owen Anderson825b72b2009-08-11 20:47:22 +0000618 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
619 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
620 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
621 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000622
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000623 if (!TM.Options.UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000624 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
625 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000626 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000627 addLegalFPImmediate(APFloat(+0.0)); // FLD0
628 addLegalFPImmediate(APFloat(+1.0)); // FLD1
629 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
630 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000631 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
632 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
633 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
634 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000635 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000636
Cameron Zwarich33390842011-07-08 21:39:21 +0000637 // We don't support FMA.
638 setOperationAction(ISD::FMA, MVT::f64, Expand);
639 setOperationAction(ISD::FMA, MVT::f32, Expand);
640
Dale Johannesen59a58732007-08-05 18:49:15 +0000641 // Long double always uses X87.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000642 if (!TM.Options.UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000643 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
644 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
645 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000646 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000647 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000648 addLegalFPImmediate(TmpFlt); // FLD0
649 TmpFlt.changeSign();
650 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000651
652 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000653 APFloat TmpFlt2(+1.0);
654 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
655 &ignored);
656 addLegalFPImmediate(TmpFlt2); // FLD1
657 TmpFlt2.changeSign();
658 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
659 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000660
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000661 if (!TM.Options.UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000662 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
663 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000664 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000665
666 setOperationAction(ISD::FMA, MVT::f80, Expand);
Dale Johannesen2f429012007-09-26 21:10:55 +0000667 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000668
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000669 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000670 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
671 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
672 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000673
Owen Anderson825b72b2009-08-11 20:47:22 +0000674 setOperationAction(ISD::FLOG, MVT::f80, Expand);
675 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
676 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
677 setOperationAction(ISD::FEXP, MVT::f80, Expand);
678 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000679
Mon P Wangf007a8b2008-11-06 05:31:54 +0000680 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000681 // (for widening) or expand (for scalarization). Then we will selectively
682 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000683 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
684 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
685 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
686 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
687 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
688 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
689 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
690 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
691 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
692 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
693 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
694 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
695 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
696 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
697 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
698 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
699 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000700 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
David Greenecfe33c42011-01-26 19:13:22 +0000701 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
702 setOperationAction(ISD::INSERT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000703 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
704 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
705 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
706 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
707 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
708 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
709 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
710 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
711 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
712 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
713 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
714 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
715 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
716 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
717 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
718 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
719 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
720 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
721 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
722 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
723 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
Duncan Sands28b77e92011-09-06 19:07:46 +0000724 setOperationAction(ISD::SETCC, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000725 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
726 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
727 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
728 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
729 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
730 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
731 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
732 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
733 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000734 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000735 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
736 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
737 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
738 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
Nadav Rotemaec58612011-09-13 19:17:42 +0000739 setOperationAction(ISD::VSELECT, (MVT::SimpleValueType)VT, Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000740 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
741 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
742 setTruncStoreAction((MVT::SimpleValueType)VT,
743 (MVT::SimpleValueType)InnerVT, Expand);
744 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
745 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
746 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000747 }
748
Evan Chengc7ce29b2009-02-13 22:36:38 +0000749 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
750 // with -msoft-float, disable use of MMX as well.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000751 if (!TM.Options.UseSoftFloat && Subtarget->hasMMX()) {
Dale Johannesene93d99c2010-10-20 21:32:10 +0000752 addRegisterClass(MVT::x86mmx, X86::VR64RegisterClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000753 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000754 }
755
Dale Johannesen0488fb62010-09-30 23:57:10 +0000756 // MMX-sized vectors (other than x86mmx) are expected to be expanded
757 // into smaller operations.
758 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
759 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
760 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
761 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
762 setOperationAction(ISD::AND, MVT::v8i8, Expand);
763 setOperationAction(ISD::AND, MVT::v4i16, Expand);
764 setOperationAction(ISD::AND, MVT::v2i32, Expand);
765 setOperationAction(ISD::AND, MVT::v1i64, Expand);
766 setOperationAction(ISD::OR, MVT::v8i8, Expand);
767 setOperationAction(ISD::OR, MVT::v4i16, Expand);
768 setOperationAction(ISD::OR, MVT::v2i32, Expand);
769 setOperationAction(ISD::OR, MVT::v1i64, Expand);
770 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
771 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
772 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
773 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
774 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
775 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
776 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
777 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
778 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
779 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
780 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
781 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
782 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000783 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
784 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
785 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
786 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000787
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000788 if (!TM.Options.UseSoftFloat && Subtarget->hasXMM()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000789 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000790
Owen Anderson825b72b2009-08-11 20:47:22 +0000791 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
792 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
793 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
794 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
795 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
796 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
797 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
798 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
799 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
800 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
801 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000802 setOperationAction(ISD::SETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000803 }
804
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000805 if (!TM.Options.UseSoftFloat && Subtarget->hasXMMInt()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000806 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000807
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000808 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
809 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000810 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
811 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
812 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
813 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000814
Owen Anderson825b72b2009-08-11 20:47:22 +0000815 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
816 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
817 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
818 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
819 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
820 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
821 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
822 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
823 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
824 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
825 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
826 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
827 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
828 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
829 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
830 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000831
Nadav Rotem354efd82011-09-18 14:57:03 +0000832 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000833 setOperationAction(ISD::SETCC, MVT::v16i8, Custom);
834 setOperationAction(ISD::SETCC, MVT::v8i16, Custom);
835 setOperationAction(ISD::SETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000836
Owen Anderson825b72b2009-08-11 20:47:22 +0000837 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
838 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
839 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
840 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
841 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000842
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000843 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
844 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
845 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
846 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
847 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
848
Evan Cheng2c3ae372006-04-12 21:21:57 +0000849 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000850 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
851 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000852 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000853 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000854 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000855 // Do not attempt to custom lower non-128-bit vectors
856 if (!VT.is128BitVector())
857 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000858 setOperationAction(ISD::BUILD_VECTOR,
859 VT.getSimpleVT().SimpleTy, Custom);
860 setOperationAction(ISD::VECTOR_SHUFFLE,
861 VT.getSimpleVT().SimpleTy, Custom);
862 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
863 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000864 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000865
Owen Anderson825b72b2009-08-11 20:47:22 +0000866 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
867 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
868 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
869 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
870 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
871 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000872
Nate Begemancdd1eec2008-02-12 22:51:28 +0000873 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000874 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
875 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000876 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000877
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000878 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000879 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
880 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000881 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000882
883 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000884 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000885 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000886
Owen Andersond6662ad2009-08-10 20:46:15 +0000887 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000888 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000889 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000890 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000891 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000892 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000893 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000894 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000895 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000896 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000897 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000898
Owen Anderson825b72b2009-08-11 20:47:22 +0000899 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000900
Evan Cheng2c3ae372006-04-12 21:21:57 +0000901 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000902 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
903 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
904 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
905 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000906
Owen Anderson825b72b2009-08-11 20:47:22 +0000907 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
908 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000909 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000910
Craig Topperc0d82852011-11-22 00:44:41 +0000911 if (Subtarget->hasSSE41orAVX()) {
Dale Johannesen54feef22010-05-27 20:12:41 +0000912 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
913 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
914 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
915 setOperationAction(ISD::FRINT, MVT::f32, Legal);
916 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
917 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
918 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
919 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
920 setOperationAction(ISD::FRINT, MVT::f64, Legal);
921 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
922
Nate Begeman14d12ca2008-02-11 04:19:36 +0000923 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000924 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000925
Nadav Rotemfbad25e2011-09-11 15:02:23 +0000926 setOperationAction(ISD::VSELECT, MVT::v2f64, Legal);
927 setOperationAction(ISD::VSELECT, MVT::v2i64, Legal);
928 setOperationAction(ISD::VSELECT, MVT::v16i8, Legal);
929 setOperationAction(ISD::VSELECT, MVT::v4i32, Legal);
930 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
Nadav Rotemffe3e7d2011-09-08 08:11:19 +0000931
Nate Begeman14d12ca2008-02-11 04:19:36 +0000932 // i8 and i16 vectors are custom , because the source register and source
933 // source memory operand types are not the same width. f32 vectors are
934 // custom since the immediate controlling the insert encodes additional
935 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000936 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
937 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
938 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
939 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000940
Owen Anderson825b72b2009-08-11 20:47:22 +0000941 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
942 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
943 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
944 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000945
Pete Coopera77214a2011-11-14 19:38:42 +0000946 // FIXME: these should be Legal but thats only for the case where
947 // the index is constant. For now custom expand to deal with that
Nate Begeman14d12ca2008-02-11 04:19:36 +0000948 if (Subtarget->is64Bit()) {
Pete Coopera77214a2011-11-14 19:38:42 +0000949 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
950 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000951 }
952 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000953
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +0000954 if (Subtarget->hasXMMInt()) {
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000955 setOperationAction(ISD::SRL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000956 setOperationAction(ISD::SRL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +0000957
Nadav Rotem43012222011-05-11 08:12:09 +0000958 setOperationAction(ISD::SHL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000959 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +0000960
Nadav Rotem43012222011-05-11 08:12:09 +0000961 setOperationAction(ISD::SRA, MVT::v8i16, Custom);
Eli Friedmanf6aa6b12011-11-01 21:18:39 +0000962 setOperationAction(ISD::SRA, MVT::v16i8, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000963
964 if (Subtarget->hasAVX2()) {
965 setOperationAction(ISD::SRL, MVT::v2i64, Legal);
966 setOperationAction(ISD::SRL, MVT::v4i32, Legal);
967
968 setOperationAction(ISD::SHL, MVT::v2i64, Legal);
969 setOperationAction(ISD::SHL, MVT::v4i32, Legal);
970
971 setOperationAction(ISD::SRA, MVT::v4i32, Legal);
972 } else {
973 setOperationAction(ISD::SRL, MVT::v2i64, Custom);
974 setOperationAction(ISD::SRL, MVT::v4i32, Custom);
975
976 setOperationAction(ISD::SHL, MVT::v2i64, Custom);
977 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
978
979 setOperationAction(ISD::SRA, MVT::v4i32, Custom);
980 }
Nadav Rotem43012222011-05-11 08:12:09 +0000981 }
982
Craig Topperc0d82852011-11-22 00:44:41 +0000983 if (Subtarget->hasSSE42orAVX())
Duncan Sands28b77e92011-09-06 19:07:46 +0000984 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000985
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000986 if (!TM.Options.UseSoftFloat && Subtarget->hasAVX()) {
Bruno Cardoso Lopesdbd4fe22011-07-21 02:24:08 +0000987 addRegisterClass(MVT::v32i8, X86::VR256RegisterClass);
988 addRegisterClass(MVT::v16i16, X86::VR256RegisterClass);
989 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
990 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
991 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
992 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000993
Owen Anderson825b72b2009-08-11 20:47:22 +0000994 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +0000995 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
996 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +0000997
Owen Anderson825b72b2009-08-11 20:47:22 +0000998 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
999 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
1000 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
1001 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
1002 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
1003 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001004
Owen Anderson825b72b2009-08-11 20:47:22 +00001005 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
1006 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
1007 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
1008 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
1009 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
1010 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001011
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001012 setOperationAction(ISD::FP_TO_SINT, MVT::v8i32, Legal);
1013 setOperationAction(ISD::SINT_TO_FP, MVT::v8i32, Legal);
Bruno Cardoso Lopes55244ce2011-08-01 21:54:09 +00001014 setOperationAction(ISD::FP_ROUND, MVT::v4f32, Legal);
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001015
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00001016 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4f64, Custom);
1017 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i64, Custom);
1018 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8f32, Custom);
1019 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i32, Custom);
1020 setOperationAction(ISD::CONCAT_VECTORS, MVT::v32i8, Custom);
1021 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i16, Custom);
1022
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001023 setOperationAction(ISD::SRL, MVT::v16i16, Custom);
1024 setOperationAction(ISD::SRL, MVT::v32i8, Custom);
1025
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001026 setOperationAction(ISD::SHL, MVT::v16i16, Custom);
1027 setOperationAction(ISD::SHL, MVT::v32i8, Custom);
1028
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001029 setOperationAction(ISD::SRA, MVT::v16i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001030 setOperationAction(ISD::SRA, MVT::v32i8, Custom);
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001031
Duncan Sands28b77e92011-09-06 19:07:46 +00001032 setOperationAction(ISD::SETCC, MVT::v32i8, Custom);
1033 setOperationAction(ISD::SETCC, MVT::v16i16, Custom);
1034 setOperationAction(ISD::SETCC, MVT::v8i32, Custom);
1035 setOperationAction(ISD::SETCC, MVT::v4i64, Custom);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00001036
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +00001037 setOperationAction(ISD::SELECT, MVT::v4f64, Custom);
1038 setOperationAction(ISD::SELECT, MVT::v4i64, Custom);
1039 setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
1040
Craig Topperaaa643c2011-11-09 07:28:55 +00001041 setOperationAction(ISD::VSELECT, MVT::v4f64, Legal);
1042 setOperationAction(ISD::VSELECT, MVT::v4i64, Legal);
1043 setOperationAction(ISD::VSELECT, MVT::v8i32, Legal);
1044 setOperationAction(ISD::VSELECT, MVT::v8f32, Legal);
Nadav Rotem8ffad562011-09-09 20:29:17 +00001045
Craig Topperaaa643c2011-11-09 07:28:55 +00001046 if (Subtarget->hasAVX2()) {
1047 setOperationAction(ISD::ADD, MVT::v4i64, Legal);
1048 setOperationAction(ISD::ADD, MVT::v8i32, Legal);
1049 setOperationAction(ISD::ADD, MVT::v16i16, Legal);
1050 setOperationAction(ISD::ADD, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001051
Craig Topperaaa643c2011-11-09 07:28:55 +00001052 setOperationAction(ISD::SUB, MVT::v4i64, Legal);
1053 setOperationAction(ISD::SUB, MVT::v8i32, Legal);
1054 setOperationAction(ISD::SUB, MVT::v16i16, Legal);
1055 setOperationAction(ISD::SUB, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001056
Craig Topperaaa643c2011-11-09 07:28:55 +00001057 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1058 setOperationAction(ISD::MUL, MVT::v8i32, Legal);
1059 setOperationAction(ISD::MUL, MVT::v16i16, Legal);
Craig Topper46154eb2011-11-11 07:39:23 +00001060 // Don't lower v32i8 because there is no 128-bit byte mul
Nadav Rotembb539bf2011-11-09 13:21:28 +00001061
1062 setOperationAction(ISD::VSELECT, MVT::v32i8, Legal);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001063
1064 setOperationAction(ISD::SRL, MVT::v4i64, Legal);
1065 setOperationAction(ISD::SRL, MVT::v8i32, Legal);
1066
1067 setOperationAction(ISD::SHL, MVT::v4i64, Legal);
1068 setOperationAction(ISD::SHL, MVT::v8i32, Legal);
1069
1070 setOperationAction(ISD::SRA, MVT::v8i32, Legal);
Craig Topperaaa643c2011-11-09 07:28:55 +00001071 } else {
1072 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
1073 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
1074 setOperationAction(ISD::ADD, MVT::v16i16, Custom);
1075 setOperationAction(ISD::ADD, MVT::v32i8, Custom);
1076
1077 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
1078 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
1079 setOperationAction(ISD::SUB, MVT::v16i16, Custom);
1080 setOperationAction(ISD::SUB, MVT::v32i8, Custom);
1081
1082 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1083 setOperationAction(ISD::MUL, MVT::v8i32, Custom);
1084 setOperationAction(ISD::MUL, MVT::v16i16, Custom);
1085 // Don't lower v32i8 because there is no 128-bit byte mul
Craig Topper7be5dfd2011-11-12 09:58:49 +00001086
1087 setOperationAction(ISD::SRL, MVT::v4i64, Custom);
1088 setOperationAction(ISD::SRL, MVT::v8i32, Custom);
1089
1090 setOperationAction(ISD::SHL, MVT::v4i64, Custom);
1091 setOperationAction(ISD::SHL, MVT::v8i32, Custom);
1092
1093 setOperationAction(ISD::SRA, MVT::v8i32, Custom);
Craig Topperaaa643c2011-11-09 07:28:55 +00001094 }
Craig Topper13894fa2011-08-24 06:14:18 +00001095
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001096 // Custom lower several nodes for 256-bit types.
David Greene54d8eba2011-01-27 22:38:56 +00001097 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001098 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
1099 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
1100 EVT VT = SVT;
1101
1102 // Extract subvector is special because the value type
1103 // (result) is 128-bit but the source is 256-bit wide.
1104 if (VT.is128BitVector())
1105 setOperationAction(ISD::EXTRACT_SUBVECTOR, SVT, Custom);
1106
1107 // Do not attempt to custom lower other non-256-bit vectors
1108 if (!VT.is256BitVector())
David Greene9b9838d2009-06-29 16:47:10 +00001109 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001110
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001111 setOperationAction(ISD::BUILD_VECTOR, SVT, Custom);
1112 setOperationAction(ISD::VECTOR_SHUFFLE, SVT, Custom);
1113 setOperationAction(ISD::INSERT_VECTOR_ELT, SVT, Custom);
1114 setOperationAction(ISD::EXTRACT_VECTOR_ELT, SVT, Custom);
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00001115 setOperationAction(ISD::SCALAR_TO_VECTOR, SVT, Custom);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001116 setOperationAction(ISD::INSERT_SUBVECTOR, SVT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001117 }
1118
David Greene54d8eba2011-01-27 22:38:56 +00001119 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001120 for (unsigned i = (unsigned)MVT::v32i8; i != (unsigned)MVT::v4i64; ++i) {
1121 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
1122 EVT VT = SVT;
David Greene54d8eba2011-01-27 22:38:56 +00001123
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001124 // Do not attempt to promote non-256-bit vectors
1125 if (!VT.is256BitVector())
David Greene54d8eba2011-01-27 22:38:56 +00001126 continue;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001127
1128 setOperationAction(ISD::AND, SVT, Promote);
1129 AddPromotedToType (ISD::AND, SVT, MVT::v4i64);
1130 setOperationAction(ISD::OR, SVT, Promote);
1131 AddPromotedToType (ISD::OR, SVT, MVT::v4i64);
1132 setOperationAction(ISD::XOR, SVT, Promote);
1133 AddPromotedToType (ISD::XOR, SVT, MVT::v4i64);
1134 setOperationAction(ISD::LOAD, SVT, Promote);
1135 AddPromotedToType (ISD::LOAD, SVT, MVT::v4i64);
1136 setOperationAction(ISD::SELECT, SVT, Promote);
1137 AddPromotedToType (ISD::SELECT, SVT, MVT::v4i64);
David Greene54d8eba2011-01-27 22:38:56 +00001138 }
David Greene9b9838d2009-06-29 16:47:10 +00001139 }
1140
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001141 // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
1142 // of this type with custom code.
1143 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1144 VT != (unsigned)MVT::LAST_VECTOR_VALUETYPE; VT++) {
1145 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT, Custom);
1146 }
1147
Evan Cheng6be2c582006-04-05 23:38:46 +00001148 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001149 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001150
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001151
Eli Friedman962f5492010-06-02 19:35:46 +00001152 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1153 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001154 //
Eli Friedman962f5492010-06-02 19:35:46 +00001155 // FIXME: We really should do custom legalization for addition and
1156 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1157 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001158 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1159 // Add/Sub/Mul with overflow operations are custom lowered.
1160 MVT VT = IntVTs[i];
1161 setOperationAction(ISD::SADDO, VT, Custom);
1162 setOperationAction(ISD::UADDO, VT, Custom);
1163 setOperationAction(ISD::SSUBO, VT, Custom);
1164 setOperationAction(ISD::USUBO, VT, Custom);
1165 setOperationAction(ISD::SMULO, VT, Custom);
1166 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001167 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001168
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001169 // There are no 8-bit 3-address imul/mul instructions
1170 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1171 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001172
Evan Chengd54f2d52009-03-31 19:38:51 +00001173 if (!Subtarget->is64Bit()) {
1174 // These libcalls are not available in 32-bit.
1175 setLibcallName(RTLIB::SHL_I128, 0);
1176 setLibcallName(RTLIB::SRL_I128, 0);
1177 setLibcallName(RTLIB::SRA_I128, 0);
1178 }
1179
Evan Cheng206ee9d2006-07-07 08:33:52 +00001180 // We have target-specific dag combine patterns for the following nodes:
1181 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001182 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +00001183 setTargetDAGCombine(ISD::BUILD_VECTOR);
Duncan Sands6bcd2192011-09-17 16:49:39 +00001184 setTargetDAGCombine(ISD::VSELECT);
Chris Lattner83e6c992006-10-04 06:57:07 +00001185 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001186 setTargetDAGCombine(ISD::SHL);
1187 setTargetDAGCombine(ISD::SRA);
1188 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001189 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001190 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001191 setTargetDAGCombine(ISD::ADD);
Duncan Sands17470be2011-09-22 20:15:48 +00001192 setTargetDAGCombine(ISD::FADD);
1193 setTargetDAGCombine(ISD::FSUB);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001194 setTargetDAGCombine(ISD::SUB);
Nadav Rotem91e43fd2011-09-18 10:39:32 +00001195 setTargetDAGCombine(ISD::LOAD);
Chris Lattner149a4e52008-02-22 02:09:43 +00001196 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001197 setTargetDAGCombine(ISD::ZERO_EXTEND);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001198 setTargetDAGCombine(ISD::SINT_TO_FP);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001199 if (Subtarget->is64Bit())
1200 setTargetDAGCombine(ISD::MUL);
Craig Topperb4c94572011-10-21 06:55:01 +00001201 if (Subtarget->hasBMI())
1202 setTargetDAGCombine(ISD::XOR);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001203
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001204 computeRegisterProperties();
1205
Evan Cheng05219282011-01-06 06:52:41 +00001206 // On Darwin, -Os means optimize for size without hurting performance,
1207 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001208 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001209 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001210 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001211 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1212 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1213 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001214 setPrefLoopAlignment(4); // 2^4 bytes.
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001215 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001216
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001217 setPrefFunctionAlignment(4); // 2^4 bytes.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001218}
1219
Scott Michel5b8f82e2008-03-10 15:42:14 +00001220
Duncan Sands28b77e92011-09-06 19:07:46 +00001221EVT X86TargetLowering::getSetCCResultType(EVT VT) const {
1222 if (!VT.isVector()) return MVT::i8;
1223 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +00001224}
1225
1226
Evan Cheng29286502008-01-23 23:17:41 +00001227/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1228/// the desired ByVal argument alignment.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001229static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
Evan Cheng29286502008-01-23 23:17:41 +00001230 if (MaxAlign == 16)
1231 return;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001232 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001233 if (VTy->getBitWidth() == 128)
1234 MaxAlign = 16;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001235 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001236 unsigned EltAlign = 0;
1237 getMaxByValAlign(ATy->getElementType(), EltAlign);
1238 if (EltAlign > MaxAlign)
1239 MaxAlign = EltAlign;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001240 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001241 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1242 unsigned EltAlign = 0;
1243 getMaxByValAlign(STy->getElementType(i), EltAlign);
1244 if (EltAlign > MaxAlign)
1245 MaxAlign = EltAlign;
1246 if (MaxAlign == 16)
1247 break;
1248 }
1249 }
1250 return;
1251}
1252
1253/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1254/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001255/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1256/// are at 4-byte boundaries.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001257unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001258 if (Subtarget->is64Bit()) {
1259 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001260 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001261 if (TyAlign > 8)
1262 return TyAlign;
1263 return 8;
1264 }
1265
Evan Cheng29286502008-01-23 23:17:41 +00001266 unsigned Align = 4;
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001267 if (Subtarget->hasXMM())
Dale Johannesen0c191872008-02-08 19:48:20 +00001268 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001269 return Align;
1270}
Chris Lattner2b02a442007-02-25 08:29:00 +00001271
Evan Chengf0df0312008-05-15 08:39:06 +00001272/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001273/// and store operations as a result of memset, memcpy, and memmove
1274/// lowering. If DstAlign is zero that means it's safe to destination
1275/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1276/// means there isn't a need to check it against alignment requirement,
1277/// probably because the source does not need to be loaded. If
Lang Hames15701f82011-10-26 23:50:43 +00001278/// 'IsZeroVal' is true, that means it's safe to return a
Evan Chengc3b0c342010-04-08 07:37:57 +00001279/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1280/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1281/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001282/// It returns EVT::Other if the type should be determined using generic
1283/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001284EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001285X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1286 unsigned DstAlign, unsigned SrcAlign,
Lang Hames15701f82011-10-26 23:50:43 +00001287 bool IsZeroVal,
Evan Chengc3b0c342010-04-08 07:37:57 +00001288 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001289 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001290 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1291 // linux. This is because the stack realignment code can't handle certain
1292 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001293 const Function *F = MF.getFunction();
Lang Hames15701f82011-10-26 23:50:43 +00001294 if (IsZeroVal &&
Evan Chenga5e13622011-01-07 19:35:30 +00001295 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001296 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001297 (Subtarget->isUnalignedMemAccessFast() ||
1298 ((DstAlign == 0 || DstAlign >= 16) &&
1299 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001300 Subtarget->getStackAlignment() >= 16) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00001301 if (Subtarget->hasAVX() &&
1302 Subtarget->getStackAlignment() >= 32)
1303 return MVT::v8f32;
1304 if (Subtarget->hasXMMInt())
Evan Cheng255f20f2010-04-01 06:04:33 +00001305 return MVT::v4i32;
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00001306 if (Subtarget->hasXMM())
Evan Cheng255f20f2010-04-01 06:04:33 +00001307 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001308 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001309 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001310 Subtarget->getStackAlignment() >= 8 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001311 Subtarget->hasXMMInt()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001312 // Do not use f64 to lower memcpy if source is string constant. It's
1313 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001314 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001315 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001316 }
Evan Chengf0df0312008-05-15 08:39:06 +00001317 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001318 return MVT::i64;
1319 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001320}
1321
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001322/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1323/// current function. The returned value is a member of the
1324/// MachineJumpTableInfo::JTEntryKind enum.
1325unsigned X86TargetLowering::getJumpTableEncoding() const {
1326 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1327 // symbol.
1328 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1329 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001330 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001331
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001332 // Otherwise, use the normal jump table encoding heuristics.
1333 return TargetLowering::getJumpTableEncoding();
1334}
1335
Chris Lattnerc64daab2010-01-26 05:02:42 +00001336const MCExpr *
1337X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1338 const MachineBasicBlock *MBB,
1339 unsigned uid,MCContext &Ctx) const{
1340 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1341 Subtarget->isPICStyleGOT());
1342 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1343 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001344 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1345 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001346}
1347
Evan Chengcc415862007-11-09 01:32:10 +00001348/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1349/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001350SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001351 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001352 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001353 // This doesn't have DebugLoc associated with it, but is not really the
1354 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001355 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001356 return Table;
1357}
1358
Chris Lattner589c6f62010-01-26 06:28:43 +00001359/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1360/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1361/// MCExpr.
1362const MCExpr *X86TargetLowering::
1363getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1364 MCContext &Ctx) const {
1365 // X86-64 uses RIP relative addressing based on the jump table label.
1366 if (Subtarget->isPICStyleRIPRel())
1367 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1368
1369 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001370 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001371}
1372
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001373// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001374std::pair<const TargetRegisterClass*, uint8_t>
1375X86TargetLowering::findRepresentativeClass(EVT VT) const{
1376 const TargetRegisterClass *RRC = 0;
1377 uint8_t Cost = 1;
1378 switch (VT.getSimpleVT().SimpleTy) {
1379 default:
1380 return TargetLowering::findRepresentativeClass(VT);
1381 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
1382 RRC = (Subtarget->is64Bit()
1383 ? X86::GR64RegisterClass : X86::GR32RegisterClass);
1384 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001385 case MVT::x86mmx:
Evan Chengdee81012010-07-26 21:50:05 +00001386 RRC = X86::VR64RegisterClass;
1387 break;
1388 case MVT::f32: case MVT::f64:
1389 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1390 case MVT::v4f32: case MVT::v2f64:
1391 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1392 case MVT::v4f64:
1393 RRC = X86::VR128RegisterClass;
1394 break;
1395 }
1396 return std::make_pair(RRC, Cost);
1397}
1398
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001399bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1400 unsigned &Offset) const {
1401 if (!Subtarget->isTargetLinux())
1402 return false;
1403
1404 if (Subtarget->is64Bit()) {
1405 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1406 Offset = 0x28;
1407 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1408 AddressSpace = 256;
1409 else
1410 AddressSpace = 257;
1411 } else {
1412 // %gs:0x14 on i386
1413 Offset = 0x14;
1414 AddressSpace = 256;
1415 }
1416 return true;
1417}
1418
1419
Chris Lattner2b02a442007-02-25 08:29:00 +00001420//===----------------------------------------------------------------------===//
1421// Return Value Calling Convention Implementation
1422//===----------------------------------------------------------------------===//
1423
Chris Lattner59ed56b2007-02-28 04:55:35 +00001424#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001425
Michael J. Spencerec38de22010-10-10 22:04:20 +00001426bool
Eric Christopher471e4222011-06-08 23:55:35 +00001427X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
1428 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001429 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001430 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001431 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001432 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001433 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001434 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001435}
1436
Dan Gohman98ca4f22009-08-05 01:29:28 +00001437SDValue
1438X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001439 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001440 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001441 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001442 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001443 MachineFunction &MF = DAG.getMachineFunction();
1444 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001445
Chris Lattner9774c912007-02-27 05:28:59 +00001446 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001447 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001448 RVLocs, *DAG.getContext());
1449 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001450
Evan Chengdcea1632010-02-04 02:40:39 +00001451 // Add the regs to the liveout set for the function.
1452 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1453 for (unsigned i = 0; i != RVLocs.size(); ++i)
1454 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1455 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001456
Dan Gohman475871a2008-07-27 21:46:04 +00001457 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001458
Dan Gohman475871a2008-07-27 21:46:04 +00001459 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001460 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1461 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001462 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1463 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001464
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001465 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001466 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1467 CCValAssign &VA = RVLocs[i];
1468 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001469 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001470 EVT ValVT = ValToCopy.getValueType();
1471
Dale Johannesenc4510512010-09-24 19:05:48 +00001472 // If this is x86-64, and we disabled SSE, we can't return FP values,
1473 // or SSE or MMX vectors.
1474 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1475 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001476 (Subtarget->is64Bit() && !Subtarget->hasXMM())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001477 report_fatal_error("SSE register return with SSE disabled");
1478 }
1479 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1480 // llvm-gcc has never done it right and no one has noticed, so this
1481 // should be OK for now.
1482 if (ValVT == MVT::f64 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001483 (Subtarget->is64Bit() && !Subtarget->hasXMMInt()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001484 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001485
Chris Lattner447ff682008-03-11 03:23:40 +00001486 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1487 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001488 if (VA.getLocReg() == X86::ST0 ||
1489 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001490 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1491 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001492 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001493 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001494 RetOps.push_back(ValToCopy);
1495 // Don't emit a copytoreg.
1496 continue;
1497 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001498
Evan Cheng242b38b2009-02-23 09:03:22 +00001499 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1500 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001501 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001502 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001503 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001504 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001505 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1506 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001507 // If we don't have SSE2 available, convert to v4f32 so the generated
1508 // register is legal.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00001509 if (!Subtarget->hasXMMInt())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001510 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001511 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001512 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001513 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001514
Dale Johannesendd64c412009-02-04 00:33:20 +00001515 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001516 Flag = Chain.getValue(1);
1517 }
Dan Gohman61a92132008-04-21 23:59:07 +00001518
1519 // The x86-64 ABI for returning structs by value requires that we copy
1520 // the sret argument into %rax for the return. We saved the argument into
1521 // a virtual register in the entry block, so now we copy the value out
1522 // and into %rax.
1523 if (Subtarget->is64Bit() &&
1524 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1525 MachineFunction &MF = DAG.getMachineFunction();
1526 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1527 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001528 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001529 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001530 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001531
Dale Johannesendd64c412009-02-04 00:33:20 +00001532 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001533 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001534
1535 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001536 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001537 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001538
Chris Lattner447ff682008-03-11 03:23:40 +00001539 RetOps[0] = Chain; // Update chain.
1540
1541 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001542 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001543 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001544
1545 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001546 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001547}
1548
Evan Cheng3d2125c2010-11-30 23:55:39 +00001549bool X86TargetLowering::isUsedByReturnOnly(SDNode *N) const {
1550 if (N->getNumValues() != 1)
1551 return false;
1552 if (!N->hasNUsesOfValue(1, 0))
1553 return false;
1554
1555 SDNode *Copy = *N->use_begin();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001556 if (Copy->getOpcode() != ISD::CopyToReg &&
1557 Copy->getOpcode() != ISD::FP_EXTEND)
Evan Cheng3d2125c2010-11-30 23:55:39 +00001558 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001559
1560 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001561 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001562 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001563 if (UI->getOpcode() != X86ISD::RET_FLAG)
1564 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001565 HasRet = true;
1566 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001567
Evan Cheng1bf891a2010-12-01 22:59:46 +00001568 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001569}
1570
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001571EVT
1572X86TargetLowering::getTypeForExtArgOrReturn(LLVMContext &Context, EVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001573 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001574 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001575 // TODO: Is this also valid on 32-bit?
1576 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001577 ReturnMVT = MVT::i8;
1578 else
1579 ReturnMVT = MVT::i32;
1580
1581 EVT MinVT = getRegisterType(Context, ReturnMVT);
1582 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001583}
1584
Dan Gohman98ca4f22009-08-05 01:29:28 +00001585/// LowerCallResult - Lower the result values of a call into the
1586/// appropriate copies out of appropriate physical registers.
1587///
1588SDValue
1589X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001590 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001591 const SmallVectorImpl<ISD::InputArg> &Ins,
1592 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001593 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001594
Chris Lattnere32bbf62007-02-28 07:09:55 +00001595 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001596 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001597 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001598 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1599 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001600 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001601
Chris Lattner3085e152007-02-25 08:59:22 +00001602 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001603 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001604 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001605 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001606
Torok Edwin3f142c32009-02-01 18:15:56 +00001607 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001608 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001609 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasXMM())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001610 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001611 }
1612
Evan Cheng79fb3b42009-02-20 20:43:02 +00001613 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001614
1615 // If this is a call to a function that returns an fp value on the floating
1616 // point stack, we must guarantee the the value is popped from the stack, so
1617 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001618 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001619 // instead.
1620 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1621 // If we prefer to use the value in xmm registers, copy it out as f80 and
1622 // use a truncate to move it from fp stack reg to xmm reg.
1623 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001624 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001625 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1626 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001627 Val = Chain.getValue(0);
1628
1629 // Round the f80 to the right size, which also moves it to the appropriate
1630 // xmm register.
1631 if (CopyVT != VA.getValVT())
1632 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1633 // This truncation won't change the value.
1634 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001635 } else {
1636 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1637 CopyVT, InFlag).getValue(1);
1638 Val = Chain.getValue(0);
1639 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001640 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001641 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001642 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001643
Dan Gohman98ca4f22009-08-05 01:29:28 +00001644 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001645}
1646
1647
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001648//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001649// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001650//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001651// StdCall calling convention seems to be standard for many Windows' API
1652// routines and around. It differs from C calling convention just a little:
1653// callee should clean up the stack, not caller. Symbols should be also
1654// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001655// For info on fast calling convention see Fast Calling Convention (tail call)
1656// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001657
Dan Gohman98ca4f22009-08-05 01:29:28 +00001658/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001659/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001660static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1661 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001662 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001663
Dan Gohman98ca4f22009-08-05 01:29:28 +00001664 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001665}
1666
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001667/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001668/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001669static bool
1670ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1671 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001672 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001673
Dan Gohman98ca4f22009-08-05 01:29:28 +00001674 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001675}
1676
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001677/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1678/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001679/// the specific parameter attribute. The copy will be passed as a byval
1680/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001681static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001682CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001683 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1684 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001685 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001686
Dale Johannesendd64c412009-02-04 00:33:20 +00001687 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001688 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001689 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001690}
1691
Chris Lattner29689432010-03-11 00:22:57 +00001692/// IsTailCallConvention - Return true if the calling convention is one that
1693/// supports tail call optimization.
1694static bool IsTailCallConvention(CallingConv::ID CC) {
1695 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1696}
1697
Evan Cheng485fafc2011-03-21 01:19:09 +00001698bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1699 if (!CI->isTailCall())
1700 return false;
1701
1702 CallSite CS(CI);
1703 CallingConv::ID CalleeCC = CS.getCallingConv();
1704 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1705 return false;
1706
1707 return true;
1708}
1709
Evan Cheng0c439eb2010-01-27 00:07:07 +00001710/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1711/// a tailcall target by changing its ABI.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001712static bool FuncIsMadeTailCallSafe(CallingConv::ID CC,
1713 bool GuaranteedTailCallOpt) {
Chris Lattner29689432010-03-11 00:22:57 +00001714 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001715}
1716
Dan Gohman98ca4f22009-08-05 01:29:28 +00001717SDValue
1718X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001719 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001720 const SmallVectorImpl<ISD::InputArg> &Ins,
1721 DebugLoc dl, SelectionDAG &DAG,
1722 const CCValAssign &VA,
1723 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001724 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001725 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001726 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001727 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv,
1728 getTargetMachine().Options.GuaranteedTailCallOpt);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001729 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001730 EVT ValVT;
1731
1732 // If value is passed by pointer we have address passed instead of the value
1733 // itself.
1734 if (VA.getLocInfo() == CCValAssign::Indirect)
1735 ValVT = VA.getLocVT();
1736 else
1737 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001738
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001739 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001740 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001741 // In case of tail call optimization mark all arguments mutable. Since they
1742 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001743 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001744 unsigned Bytes = Flags.getByValSize();
1745 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1746 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001747 return DAG.getFrameIndex(FI, getPointerTy());
1748 } else {
1749 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001750 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001751 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1752 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001753 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001754 false, false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001755 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001756}
1757
Dan Gohman475871a2008-07-27 21:46:04 +00001758SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001759X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001760 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001761 bool isVarArg,
1762 const SmallVectorImpl<ISD::InputArg> &Ins,
1763 DebugLoc dl,
1764 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001765 SmallVectorImpl<SDValue> &InVals)
1766 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001767 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001768 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001769
Gordon Henriksen86737662008-01-05 16:56:59 +00001770 const Function* Fn = MF.getFunction();
1771 if (Fn->hasExternalLinkage() &&
1772 Subtarget->isTargetCygMing() &&
1773 Fn->getName() == "main")
1774 FuncInfo->setForceFramePointer(true);
1775
Evan Cheng1bc78042006-04-26 01:20:17 +00001776 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001777 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001778 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001779
Chris Lattner29689432010-03-11 00:22:57 +00001780 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1781 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001782
Chris Lattner638402b2007-02-28 07:00:42 +00001783 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001784 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001785 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001786 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001787
1788 // Allocate shadow area for Win64
1789 if (IsWin64) {
1790 CCInfo.AllocateStack(32, 8);
1791 }
1792
Duncan Sands45907662010-10-31 13:21:44 +00001793 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001794
Chris Lattnerf39f7712007-02-28 05:46:49 +00001795 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001796 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001797 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1798 CCValAssign &VA = ArgLocs[i];
1799 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1800 // places.
1801 assert(VA.getValNo() != LastVal &&
1802 "Don't support value assigned to multiple locs yet");
Duncan Sands17001ce2011-10-18 12:44:00 +00001803 (void)LastVal;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001804 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001805
Chris Lattnerf39f7712007-02-28 05:46:49 +00001806 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001807 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001808 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001809 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001810 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001811 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001812 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001813 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001814 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001815 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001816 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001817 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1818 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001819 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001820 RC = X86::VR128RegisterClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001821 else if (RegVT == MVT::x86mmx)
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001822 RC = X86::VR64RegisterClass;
1823 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001824 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001825
Devang Patel68e6bee2011-02-21 23:21:26 +00001826 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001827 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001828
Chris Lattnerf39f7712007-02-28 05:46:49 +00001829 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1830 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1831 // right size.
1832 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001833 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001834 DAG.getValueType(VA.getValVT()));
1835 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001836 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001837 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001838 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001839 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001840
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001841 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001842 // Handle MMX values passed in XMM regs.
1843 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001844 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1845 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001846 } else
1847 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001848 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001849 } else {
1850 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001851 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001852 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001853
1854 // If value is passed via pointer - do a load.
1855 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001856 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001857 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001858
Dan Gohman98ca4f22009-08-05 01:29:28 +00001859 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001860 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001861
Dan Gohman61a92132008-04-21 23:59:07 +00001862 // The x86-64 ABI for returning structs by value requires that we copy
1863 // the sret argument into %rax for the return. Save the argument into
1864 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001865 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001866 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1867 unsigned Reg = FuncInfo->getSRetReturnReg();
1868 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001869 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001870 FuncInfo->setSRetReturnReg(Reg);
1871 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001872 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001873 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001874 }
1875
Chris Lattnerf39f7712007-02-28 05:46:49 +00001876 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001877 // Align stack specially for tail calls.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001878 if (FuncIsMadeTailCallSafe(CallConv,
1879 MF.getTarget().Options.GuaranteedTailCallOpt))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001880 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001881
Evan Cheng1bc78042006-04-26 01:20:17 +00001882 // If the function takes variable number of arguments, make a frame index for
1883 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001884 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001885 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1886 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001887 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001888 }
1889 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001890 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1891
1892 // FIXME: We should really autogenerate these arrays
1893 static const unsigned GPR64ArgRegsWin64[] = {
1894 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001895 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001896 static const unsigned GPR64ArgRegs64Bit[] = {
1897 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1898 };
1899 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001900 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1901 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1902 };
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001903 const unsigned *GPR64ArgRegs;
1904 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001905
1906 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001907 // The XMM registers which might contain var arg parameters are shadowed
1908 // in their paired GPR. So we only need to save the GPR to their home
1909 // slots.
1910 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001911 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001912 } else {
1913 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1914 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001915
1916 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit, TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001917 }
1918 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1919 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001920
Devang Patel578efa92009-06-05 21:57:13 +00001921 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001922 assert(!(NumXMMRegs && !Subtarget->hasXMM()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001923 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001924 assert(!(NumXMMRegs && MF.getTarget().Options.UseSoftFloat &&
1925 NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001926 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001927 if (MF.getTarget().Options.UseSoftFloat || NoImplicitFloatOps ||
1928 !Subtarget->hasXMM())
Torok Edwin3f142c32009-02-01 18:15:56 +00001929 // Kernel mode asks for SSE to be disabled, so don't push them
1930 // on the stack.
1931 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001932
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001933 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001934 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001935 // Get to the caller-allocated home save location. Add 8 to account
1936 // for the return address.
1937 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001938 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001939 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001940 // Fixup to set vararg frame on shadow area (4 x i64).
1941 if (NumIntRegs < 4)
1942 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001943 } else {
1944 // For X86-64, if there are vararg parameters that are passed via
1945 // registers, then we must store them to their spots on the stack so they
1946 // may be loaded by deferencing the result of va_next.
1947 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1948 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1949 FuncInfo->setRegSaveFrameIndex(
1950 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00001951 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001952 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001953
Gordon Henriksen86737662008-01-05 16:56:59 +00001954 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001955 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001956 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1957 getPointerTy());
1958 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001959 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001960 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1961 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001962 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001963 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001964 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001965 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001966 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001967 MachinePointerInfo::getFixedStack(
1968 FuncInfo->getRegSaveFrameIndex(), Offset),
1969 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001970 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001971 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001972 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001973
Dan Gohmanface41a2009-08-16 21:24:25 +00001974 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1975 // Now store the XMM (fp + vector) parameter registers.
1976 SmallVector<SDValue, 11> SaveXMMOps;
1977 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001978
Devang Patel68e6bee2011-02-21 23:21:26 +00001979 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001980 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1981 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001982
Dan Gohman1e93df62010-04-17 14:41:14 +00001983 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1984 FuncInfo->getRegSaveFrameIndex()));
1985 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1986 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001987
Dan Gohmanface41a2009-08-16 21:24:25 +00001988 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001989 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001990 X86::VR128RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001991 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1992 SaveXMMOps.push_back(Val);
1993 }
1994 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1995 MVT::Other,
1996 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001997 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001998
1999 if (!MemOps.empty())
2000 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2001 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002002 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002003 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002004
Gordon Henriksen86737662008-01-05 16:56:59 +00002005 // Some CCs need callee pop.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002006 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2007 MF.getTarget().Options.GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002008 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002009 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00002010 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00002011 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00002012 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00002013 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002014 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002015
Gordon Henriksen86737662008-01-05 16:56:59 +00002016 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002017 // RegSaveFrameIndex is X86-64 only.
2018 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00002019 if (CallConv == CallingConv::X86_FastCall ||
2020 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00002021 // fastcc functions can't have varargs.
2022 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00002023 }
Evan Cheng25caf632006-05-23 21:06:34 +00002024
Rafael Espindola76927d752011-08-30 19:39:58 +00002025 FuncInfo->setArgumentStackSize(StackSize);
2026
Dan Gohman98ca4f22009-08-05 01:29:28 +00002027 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002028}
2029
Dan Gohman475871a2008-07-27 21:46:04 +00002030SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002031X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
2032 SDValue StackPtr, SDValue Arg,
2033 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00002034 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00002035 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002036 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00002037 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00002038 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002039 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00002040 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002041
2042 return DAG.getStore(Chain, dl, Arg, PtrOff,
2043 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00002044 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00002045}
2046
Bill Wendling64e87322009-01-16 19:25:27 +00002047/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002048/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00002049SDValue
2050X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00002051 SDValue &OutRetAddr, SDValue Chain,
2052 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00002053 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002054 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00002055 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002056 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00002057
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002058 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00002059 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002060 false, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00002061 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002062}
2063
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002064/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002065/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00002066static SDValue
2067EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00002068 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00002069 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002070 // Store the return address to the appropriate stack slot.
2071 if (!FPDiff) return Chain;
2072 // Calculate the new stack slot for the return address.
2073 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00002074 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00002075 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00002076 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00002077 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002078 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00002079 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00002080 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002081 return Chain;
2082}
2083
Dan Gohman98ca4f22009-08-05 01:29:28 +00002084SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00002085X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002086 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00002087 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002088 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002089 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002090 const SmallVectorImpl<ISD::InputArg> &Ins,
2091 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002092 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002093 MachineFunction &MF = DAG.getMachineFunction();
2094 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002095 bool IsWin64 = Subtarget->isTargetWin64();
Dan Gohman98ca4f22009-08-05 01:29:28 +00002096 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00002097 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002098
Evan Cheng5f941932010-02-05 02:21:12 +00002099 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002100 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00002101 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
2102 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00002103 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00002104
2105 // Sibcalls are automatically detected tailcalls which do not require
2106 // ABI changes.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002107 if (!MF.getTarget().Options.GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002108 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002109
2110 if (isTailCall)
2111 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002112 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002113
Chris Lattner29689432010-03-11 00:22:57 +00002114 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
2115 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002116
Chris Lattner638402b2007-02-28 07:00:42 +00002117 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002118 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002119 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002120 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002121
2122 // Allocate shadow area for Win64
2123 if (IsWin64) {
2124 CCInfo.AllocateStack(32, 8);
2125 }
2126
Duncan Sands45907662010-10-31 13:21:44 +00002127 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002128
Chris Lattner423c5f42007-02-28 05:31:48 +00002129 // Get a count of how many bytes are to be pushed on the stack.
2130 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002131 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002132 // This is a sibcall. The memory operands are available in caller's
2133 // own caller's stack.
2134 NumBytes = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002135 else if (getTargetMachine().Options.GuaranteedTailCallOpt &&
2136 IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002137 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002138
Gordon Henriksen86737662008-01-05 16:56:59 +00002139 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002140 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002141 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00002142 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00002143 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2144 FPDiff = NumBytesCallerPushed - NumBytes;
2145
2146 // Set the delta of movement of the returnaddr stackslot.
2147 // But only set if delta is greater than previous delta.
2148 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2149 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2150 }
2151
Evan Chengf22f9b32010-02-06 03:28:46 +00002152 if (!IsSibcall)
2153 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002154
Dan Gohman475871a2008-07-27 21:46:04 +00002155 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002156 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002157 if (isTailCall && FPDiff)
2158 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2159 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002160
Dan Gohman475871a2008-07-27 21:46:04 +00002161 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2162 SmallVector<SDValue, 8> MemOpChains;
2163 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002164
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002165 // Walk the register/memloc assignments, inserting copies/loads. In the case
2166 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002167 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2168 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002169 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002170 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002171 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002172 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002173
Chris Lattner423c5f42007-02-28 05:31:48 +00002174 // Promote the value if needed.
2175 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002176 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002177 case CCValAssign::Full: break;
2178 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002179 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002180 break;
2181 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002182 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002183 break;
2184 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002185 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
2186 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002187 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002188 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2189 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002190 } else
2191 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2192 break;
2193 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002194 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002195 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002196 case CCValAssign::Indirect: {
2197 // Store the argument.
2198 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002199 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002200 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002201 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002202 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002203 Arg = SpillSlot;
2204 break;
2205 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002206 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002207
Chris Lattner423c5f42007-02-28 05:31:48 +00002208 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002209 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2210 if (isVarArg && IsWin64) {
2211 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2212 // shadow reg if callee is a varargs function.
2213 unsigned ShadowReg = 0;
2214 switch (VA.getLocReg()) {
2215 case X86::XMM0: ShadowReg = X86::RCX; break;
2216 case X86::XMM1: ShadowReg = X86::RDX; break;
2217 case X86::XMM2: ShadowReg = X86::R8; break;
2218 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002219 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002220 if (ShadowReg)
2221 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002222 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002223 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002224 assert(VA.isMemLoc());
2225 if (StackPtr.getNode() == 0)
2226 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2227 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2228 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002229 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002230 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002231
Evan Cheng32fe1032006-05-25 00:59:30 +00002232 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002233 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002234 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002235
Evan Cheng347d5f72006-04-28 21:29:37 +00002236 // Build a sequence of copy-to-reg nodes chained together with token chain
2237 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002238 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002239 // Tail call byval lowering might overwrite argument registers so in case of
2240 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002241 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002242 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002243 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002244 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002245 InFlag = Chain.getValue(1);
2246 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002247
Chris Lattner88e1fd52009-07-09 04:24:46 +00002248 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002249 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2250 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002251 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002252 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2253 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00002254 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002255 InFlag);
2256 InFlag = Chain.getValue(1);
2257 } else {
2258 // If we are tail calling and generating PIC/GOT style code load the
2259 // address of the callee into ECX. The value in ecx is used as target of
2260 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2261 // for tail calls on PIC/GOT architectures. Normally we would just put the
2262 // address of GOT into ebx and then call target@PLT. But for tail calls
2263 // ebx would be restored (since ebx is callee saved) before jumping to the
2264 // target@PLT.
2265
2266 // Note: The actual moving to ECX is done further down.
2267 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2268 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2269 !G->getGlobal()->hasProtectedVisibility())
2270 Callee = LowerGlobalAddress(Callee, DAG);
2271 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002272 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002273 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002274 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002275
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002276 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002277 // From AMD64 ABI document:
2278 // For calls that may call functions that use varargs or stdargs
2279 // (prototype-less calls or calls to functions containing ellipsis (...) in
2280 // the declaration) %al is used as hidden argument to specify the number
2281 // of SSE registers used. The contents of %al do not need to match exactly
2282 // the number of registers, but must be an ubound on the number of SSE
2283 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002284
Gordon Henriksen86737662008-01-05 16:56:59 +00002285 // Count the number of XMM registers allocated.
2286 static const unsigned XMMArgRegs[] = {
2287 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2288 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2289 };
2290 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00002291 assert((Subtarget->hasXMM() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002292 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002293
Dale Johannesendd64c412009-02-04 00:33:20 +00002294 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002295 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002296 InFlag = Chain.getValue(1);
2297 }
2298
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002299
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002300 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002301 if (isTailCall) {
2302 // Force all the incoming stack arguments to be loaded from the stack
2303 // before any new outgoing arguments are stored to the stack, because the
2304 // outgoing stack slots may alias the incoming argument stack slots, and
2305 // the alias isn't otherwise explicit. This is slightly more conservative
2306 // than necessary, because it means that each store effectively depends
2307 // on every argument instead of just those arguments it would clobber.
2308 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2309
Dan Gohman475871a2008-07-27 21:46:04 +00002310 SmallVector<SDValue, 8> MemOpChains2;
2311 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002312 int FI = 0;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002313 // Do not flag preceding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002314 InFlag = SDValue();
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002315 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002316 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2317 CCValAssign &VA = ArgLocs[i];
2318 if (VA.isRegLoc())
2319 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002320 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002321 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002322 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002323 // Create frame index.
2324 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002325 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002326 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002327 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002328
Duncan Sands276dcbd2008-03-21 09:14:45 +00002329 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002330 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002331 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002332 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002333 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002334 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002335 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002336
Dan Gohman98ca4f22009-08-05 01:29:28 +00002337 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2338 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002339 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002340 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002341 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002342 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002343 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002344 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002345 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002346 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002347 }
2348 }
2349
2350 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002351 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002352 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002353
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002354 // Copy arguments to their registers.
2355 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002356 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002357 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002358 InFlag = Chain.getValue(1);
2359 }
Dan Gohman475871a2008-07-27 21:46:04 +00002360 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002361
Gordon Henriksen86737662008-01-05 16:56:59 +00002362 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002363 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002364 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002365 }
2366
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002367 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2368 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2369 // In the 64-bit large code model, we have to make all calls
2370 // through a register, since the call instruction's 32-bit
2371 // pc-relative offset may not be large enough to hold the whole
2372 // address.
2373 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002374 // If the callee is a GlobalAddress node (quite common, every direct call
2375 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2376 // it.
2377
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002378 // We should use extra load for direct calls to dllimported functions in
2379 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002380 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002381 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002382 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002383 bool ExtraLoad = false;
2384 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002385
Chris Lattner48a7d022009-07-09 05:02:21 +00002386 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2387 // external symbols most go through the PLT in PIC mode. If the symbol
2388 // has hidden or protected visibility, or if it is static or local, then
2389 // we don't need to use the PLT - we can directly call it.
2390 if (Subtarget->isTargetELF() &&
2391 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002392 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002393 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002394 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002395 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002396 (!Subtarget->getTargetTriple().isMacOSX() ||
2397 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002398 // PC-relative references to external symbols should go through $stub,
2399 // unless we're building with the leopard linker or later, which
2400 // automatically synthesizes these stubs.
2401 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002402 } else if (Subtarget->isPICStyleRIPRel() &&
2403 isa<Function>(GV) &&
2404 cast<Function>(GV)->hasFnAttr(Attribute::NonLazyBind)) {
2405 // If the function is marked as non-lazy, generate an indirect call
2406 // which loads from the GOT directly. This avoids runtime overhead
2407 // at the cost of eager binding (and one extra byte of encoding).
2408 OpFlags = X86II::MO_GOTPCREL;
2409 WrapperKind = X86ISD::WrapperRIP;
2410 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002411 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002412
Devang Patel0d881da2010-07-06 22:08:15 +00002413 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002414 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002415
2416 // Add a wrapper if needed.
2417 if (WrapperKind != ISD::DELETED_NODE)
2418 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2419 // Add extra indirection if needed.
2420 if (ExtraLoad)
2421 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2422 MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002423 false, false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002424 }
Bill Wendling056292f2008-09-16 21:48:12 +00002425 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002426 unsigned char OpFlags = 0;
2427
Evan Cheng1bf891a2010-12-01 22:59:46 +00002428 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2429 // external symbols should go through the PLT.
2430 if (Subtarget->isTargetELF() &&
2431 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2432 OpFlags = X86II::MO_PLT;
2433 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002434 (!Subtarget->getTargetTriple().isMacOSX() ||
2435 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002436 // PC-relative references to external symbols should go through $stub,
2437 // unless we're building with the leopard linker or later, which
2438 // automatically synthesizes these stubs.
2439 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002440 }
Eric Christopherfd179292009-08-27 18:07:15 +00002441
Chris Lattner48a7d022009-07-09 05:02:21 +00002442 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2443 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002444 }
2445
Chris Lattnerd96d0722007-02-25 06:40:16 +00002446 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002447 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002448 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002449
Evan Chengf22f9b32010-02-06 03:28:46 +00002450 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002451 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2452 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002453 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002454 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002455
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002456 Ops.push_back(Chain);
2457 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002458
Dan Gohman98ca4f22009-08-05 01:29:28 +00002459 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002460 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002461
Gordon Henriksen86737662008-01-05 16:56:59 +00002462 // Add argument registers to the end of the list so that they are known live
2463 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002464 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2465 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2466 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002467
Evan Cheng586ccac2008-03-18 23:36:35 +00002468 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002469 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002470 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2471
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002472 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002473 if (Is64Bit && isVarArg && !IsWin64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002474 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002475
Gabor Greifba36cb52008-08-28 21:40:38 +00002476 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002477 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002478
Dan Gohman98ca4f22009-08-05 01:29:28 +00002479 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002480 // We used to do:
2481 //// If this is the first return lowered for this function, add the regs
2482 //// to the liveout set for the function.
2483 // This isn't right, although it's probably harmless on x86; liveouts
2484 // should be computed from returns not tail calls. Consider a void
2485 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002486 return DAG.getNode(X86ISD::TC_RETURN, dl,
2487 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002488 }
2489
Dale Johannesenace16102009-02-03 19:33:06 +00002490 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002491 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002492
Chris Lattner2d297092006-05-23 18:50:38 +00002493 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002494 unsigned NumBytesForCalleeToPush;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002495 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2496 getTargetMachine().Options.GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002497 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002498 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002499 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002500 // pops the hidden struct pointer, so we have to push it back.
2501 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002502 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002503 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002504 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002505
Gordon Henriksenae636f82008-01-03 16:47:34 +00002506 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002507 if (!IsSibcall) {
2508 Chain = DAG.getCALLSEQ_END(Chain,
2509 DAG.getIntPtrConstant(NumBytes, true),
2510 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2511 true),
2512 InFlag);
2513 InFlag = Chain.getValue(1);
2514 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002515
Chris Lattner3085e152007-02-25 08:59:22 +00002516 // Handle result values, copying them out of physregs into vregs that we
2517 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002518 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2519 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002520}
2521
Evan Cheng25ab6902006-09-08 06:48:29 +00002522
2523//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002524// Fast Calling Convention (tail call) implementation
2525//===----------------------------------------------------------------------===//
2526
2527// Like std call, callee cleans arguments, convention except that ECX is
2528// reserved for storing the tail called function address. Only 2 registers are
2529// free for argument passing (inreg). Tail call optimization is performed
2530// provided:
2531// * tailcallopt is enabled
2532// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002533// On X86_64 architecture with GOT-style position independent code only local
2534// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002535// To keep the stack aligned according to platform abi the function
2536// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2537// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002538// If a tail called function callee has more arguments than the caller the
2539// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002540// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002541// original REtADDR, but before the saved framepointer or the spilled registers
2542// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2543// stack layout:
2544// arg1
2545// arg2
2546// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002547// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002548// move area ]
2549// (possible EBP)
2550// ESI
2551// EDI
2552// local1 ..
2553
2554/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2555/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002556unsigned
2557X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2558 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002559 MachineFunction &MF = DAG.getMachineFunction();
2560 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002561 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002562 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002563 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002564 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002565 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002566 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2567 // Number smaller than 12 so just add the difference.
2568 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2569 } else {
2570 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002571 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002572 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002573 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002574 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002575}
2576
Evan Cheng5f941932010-02-05 02:21:12 +00002577/// MatchingStackOffset - Return true if the given stack call argument is
2578/// already available in the same position (relatively) of the caller's
2579/// incoming argument stack.
2580static
2581bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2582 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2583 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002584 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2585 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002586 if (Arg.getOpcode() == ISD::CopyFromReg) {
2587 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002588 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002589 return false;
2590 MachineInstr *Def = MRI->getVRegDef(VR);
2591 if (!Def)
2592 return false;
2593 if (!Flags.isByVal()) {
2594 if (!TII->isLoadFromStackSlot(Def, FI))
2595 return false;
2596 } else {
2597 unsigned Opcode = Def->getOpcode();
2598 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2599 Def->getOperand(1).isFI()) {
2600 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002601 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002602 } else
2603 return false;
2604 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002605 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2606 if (Flags.isByVal())
2607 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002608 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002609 // define @foo(%struct.X* %A) {
2610 // tail call @bar(%struct.X* byval %A)
2611 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002612 return false;
2613 SDValue Ptr = Ld->getBasePtr();
2614 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2615 if (!FINode)
2616 return false;
2617 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002618 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002619 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002620 FI = FINode->getIndex();
2621 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002622 } else
2623 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002624
Evan Cheng4cae1332010-03-05 08:38:04 +00002625 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002626 if (!MFI->isFixedObjectIndex(FI))
2627 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002628 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002629}
2630
Dan Gohman98ca4f22009-08-05 01:29:28 +00002631/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2632/// for tail call optimization. Targets which want to do tail call
2633/// optimization should implement this function.
2634bool
2635X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002636 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002637 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002638 bool isCalleeStructRet,
2639 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002640 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002641 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002642 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002643 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002644 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002645 CalleeCC != CallingConv::C)
2646 return false;
2647
Evan Cheng7096ae42010-01-29 06:45:59 +00002648 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002649 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002650 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002651 CallingConv::ID CallerCC = CallerF->getCallingConv();
2652 bool CCMatch = CallerCC == CalleeCC;
2653
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002654 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002655 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002656 return true;
2657 return false;
2658 }
2659
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002660 // Look for obvious safe cases to perform tail call optimization that do not
2661 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002662
Evan Cheng2c12cb42010-03-26 16:26:03 +00002663 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2664 // emit a special epilogue.
2665 if (RegInfo->needsStackRealignment(MF))
2666 return false;
2667
Evan Chenga375d472010-03-15 18:54:48 +00002668 // Also avoid sibcall optimization if either caller or callee uses struct
2669 // return semantics.
2670 if (isCalleeStructRet || isCallerStructRet)
2671 return false;
2672
Chad Rosier2416da32011-06-24 21:15:36 +00002673 // An stdcall caller is expected to clean up its arguments; the callee
2674 // isn't going to do that.
2675 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2676 return false;
2677
Chad Rosier871f6642011-05-18 19:59:50 +00002678 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002679 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002680 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002681
2682 // Optimizing for varargs on Win64 is unlikely to be safe without
2683 // additional testing.
2684 if (Subtarget->isTargetWin64())
2685 return false;
2686
Chad Rosier871f6642011-05-18 19:59:50 +00002687 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002688 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2689 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002690
Chad Rosier871f6642011-05-18 19:59:50 +00002691 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2692 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2693 if (!ArgLocs[i].isRegLoc())
2694 return false;
2695 }
2696
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002697 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2698 // Therefore if it's not used by the call it is not safe to optimize this into
2699 // a sibcall.
2700 bool Unused = false;
2701 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2702 if (!Ins[i].Used) {
2703 Unused = true;
2704 break;
2705 }
2706 }
2707 if (Unused) {
2708 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002709 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
2710 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002711 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002712 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002713 CCValAssign &VA = RVLocs[i];
2714 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2715 return false;
2716 }
2717 }
2718
Evan Cheng13617962010-04-30 01:12:32 +00002719 // If the calling conventions do not match, then we'd better make sure the
2720 // results are returned in the same way as what the caller expects.
2721 if (!CCMatch) {
2722 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002723 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
2724 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002725 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2726
2727 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002728 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
2729 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002730 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2731
2732 if (RVLocs1.size() != RVLocs2.size())
2733 return false;
2734 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2735 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2736 return false;
2737 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2738 return false;
2739 if (RVLocs1[i].isRegLoc()) {
2740 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2741 return false;
2742 } else {
2743 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2744 return false;
2745 }
2746 }
2747 }
2748
Evan Chenga6bff982010-01-30 01:22:00 +00002749 // If the callee takes no arguments then go on to check the results of the
2750 // call.
2751 if (!Outs.empty()) {
2752 // Check if stack adjustment is needed. For now, do not do this if any
2753 // argument is passed on the stack.
2754 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002755 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2756 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002757
2758 // Allocate shadow area for Win64
2759 if (Subtarget->isTargetWin64()) {
2760 CCInfo.AllocateStack(32, 8);
2761 }
2762
Duncan Sands45907662010-10-31 13:21:44 +00002763 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002764 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002765 MachineFunction &MF = DAG.getMachineFunction();
2766 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2767 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002768
2769 // Check if the arguments are already laid out in the right way as
2770 // the caller's fixed stack objects.
2771 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002772 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2773 const X86InstrInfo *TII =
2774 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002775 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2776 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002777 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002778 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002779 if (VA.getLocInfo() == CCValAssign::Indirect)
2780 return false;
2781 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002782 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2783 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002784 return false;
2785 }
2786 }
2787 }
Evan Cheng9c044672010-05-29 01:35:22 +00002788
2789 // If the tailcall address may be in a register, then make sure it's
2790 // possible to register allocate for it. In 32-bit, the call address can
2791 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002792 // callee-saved registers are restored. These happen to be the same
2793 // registers used to pass 'inreg' arguments so watch out for those.
2794 if (!Subtarget->is64Bit() &&
2795 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002796 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002797 unsigned NumInRegs = 0;
2798 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2799 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002800 if (!VA.isRegLoc())
2801 continue;
2802 unsigned Reg = VA.getLocReg();
2803 switch (Reg) {
2804 default: break;
2805 case X86::EAX: case X86::EDX: case X86::ECX:
2806 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002807 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002808 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002809 }
2810 }
2811 }
Evan Chenga6bff982010-01-30 01:22:00 +00002812 }
Evan Chengb1712452010-01-27 06:25:16 +00002813
Evan Cheng86809cc2010-02-03 03:28:02 +00002814 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002815}
2816
Dan Gohman3df24e62008-09-03 23:12:08 +00002817FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002818X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2819 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002820}
2821
2822
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002823//===----------------------------------------------------------------------===//
2824// Other Lowering Hooks
2825//===----------------------------------------------------------------------===//
2826
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002827static bool MayFoldLoad(SDValue Op) {
2828 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2829}
2830
2831static bool MayFoldIntoStore(SDValue Op) {
2832 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2833}
2834
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002835static bool isTargetShuffle(unsigned Opcode) {
2836 switch(Opcode) {
2837 default: return false;
2838 case X86ISD::PSHUFD:
2839 case X86ISD::PSHUFHW:
2840 case X86ISD::PSHUFLW:
2841 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002842 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002843 case X86ISD::SHUFPS:
2844 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002845 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002846 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002847 case X86ISD::MOVLPS:
2848 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002849 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002850 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002851 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002852 case X86ISD::MOVSS:
2853 case X86ISD::MOVSD:
Craig Topper06cb6802011-11-26 20:47:44 +00002854 case X86ISD::UNPCKLP:
2855 case X86ISD::PUNPCKL:
2856 case X86ISD::UNPCKHP:
2857 case X86ISD::PUNPCKH:
Craig Topper316cd2a2011-11-30 06:25:25 +00002858 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +00002859 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002860 return true;
2861 }
2862 return false;
2863}
2864
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002865static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002866 SDValue V1, SelectionDAG &DAG) {
2867 switch(Opc) {
2868 default: llvm_unreachable("Unknown x86 shuffle node");
2869 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002870 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002871 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002872 return DAG.getNode(Opc, dl, VT, V1);
2873 }
2874
2875 return SDValue();
2876}
2877
2878static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002879 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002880 switch(Opc) {
2881 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002882 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002883 case X86ISD::PSHUFHW:
2884 case X86ISD::PSHUFLW:
Craig Topper316cd2a2011-11-30 06:25:25 +00002885 case X86ISD::VPERMILP:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002886 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2887 }
2888
2889 return SDValue();
2890}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002891
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002892static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2893 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2894 switch(Opc) {
2895 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002896 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002897 case X86ISD::SHUFPD:
2898 case X86ISD::SHUFPS:
Craig Topperec24e612011-11-30 07:47:51 +00002899 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002900 return DAG.getNode(Opc, dl, VT, V1, V2,
2901 DAG.getConstant(TargetMask, MVT::i8));
2902 }
2903 return SDValue();
2904}
2905
2906static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2907 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2908 switch(Opc) {
2909 default: llvm_unreachable("Unknown x86 shuffle node");
2910 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002911 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002912 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002913 case X86ISD::MOVLPS:
2914 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002915 case X86ISD::MOVSS:
2916 case X86ISD::MOVSD:
Craig Topper06cb6802011-11-26 20:47:44 +00002917 case X86ISD::UNPCKLP:
2918 case X86ISD::PUNPCKL:
2919 case X86ISD::UNPCKHP:
2920 case X86ISD::PUNPCKH:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002921 return DAG.getNode(Opc, dl, VT, V1, V2);
2922 }
2923 return SDValue();
2924}
2925
Dan Gohmand858e902010-04-17 15:26:15 +00002926SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002927 MachineFunction &MF = DAG.getMachineFunction();
2928 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2929 int ReturnAddrIndex = FuncInfo->getRAIndex();
2930
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002931 if (ReturnAddrIndex == 0) {
2932 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002933 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002934 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002935 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002936 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002937 }
2938
Evan Cheng25ab6902006-09-08 06:48:29 +00002939 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002940}
2941
2942
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002943bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2944 bool hasSymbolicDisplacement) {
2945 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002946 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002947 return false;
2948
2949 // If we don't have a symbolic displacement - we don't have any extra
2950 // restrictions.
2951 if (!hasSymbolicDisplacement)
2952 return true;
2953
2954 // FIXME: Some tweaks might be needed for medium code model.
2955 if (M != CodeModel::Small && M != CodeModel::Kernel)
2956 return false;
2957
2958 // For small code model we assume that latest object is 16MB before end of 31
2959 // bits boundary. We may also accept pretty large negative constants knowing
2960 // that all objects are in the positive half of address space.
2961 if (M == CodeModel::Small && Offset < 16*1024*1024)
2962 return true;
2963
2964 // For kernel code model we know that all object resist in the negative half
2965 // of 32bits address space. We may not accept negative offsets, since they may
2966 // be just off and we may accept pretty large positive ones.
2967 if (M == CodeModel::Kernel && Offset > 0)
2968 return true;
2969
2970 return false;
2971}
2972
Evan Chengef41ff62011-06-23 17:54:54 +00002973/// isCalleePop - Determines whether the callee is required to pop its
2974/// own arguments. Callee pop is necessary to support tail calls.
2975bool X86::isCalleePop(CallingConv::ID CallingConv,
2976 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
2977 if (IsVarArg)
2978 return false;
2979
2980 switch (CallingConv) {
2981 default:
2982 return false;
2983 case CallingConv::X86_StdCall:
2984 return !is64Bit;
2985 case CallingConv::X86_FastCall:
2986 return !is64Bit;
2987 case CallingConv::X86_ThisCall:
2988 return !is64Bit;
2989 case CallingConv::Fast:
2990 return TailCallOpt;
2991 case CallingConv::GHC:
2992 return TailCallOpt;
2993 }
2994}
2995
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002996/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2997/// specific condition code, returning the condition code and the LHS/RHS of the
2998/// comparison to make.
2999static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
3000 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00003001 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003002 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
3003 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
3004 // X > -1 -> X == 0, jump !sign.
3005 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003006 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003007 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
3008 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003009 return X86::COND_S;
Andrew Trickf6c39412011-03-23 23:11:02 +00003010 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00003011 // X < 1 -> X <= 0
3012 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003013 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003014 }
Chris Lattnerf9570512006-09-13 03:22:10 +00003015 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003016
Evan Chengd9558e02006-01-06 00:43:03 +00003017 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003018 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003019 case ISD::SETEQ: return X86::COND_E;
3020 case ISD::SETGT: return X86::COND_G;
3021 case ISD::SETGE: return X86::COND_GE;
3022 case ISD::SETLT: return X86::COND_L;
3023 case ISD::SETLE: return X86::COND_LE;
3024 case ISD::SETNE: return X86::COND_NE;
3025 case ISD::SETULT: return X86::COND_B;
3026 case ISD::SETUGT: return X86::COND_A;
3027 case ISD::SETULE: return X86::COND_BE;
3028 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00003029 }
Chris Lattner4c78e022008-12-23 23:42:27 +00003030 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003031
Chris Lattner4c78e022008-12-23 23:42:27 +00003032 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00003033
Chris Lattner4c78e022008-12-23 23:42:27 +00003034 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00003035 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
3036 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00003037 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
3038 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00003039 }
3040
Chris Lattner4c78e022008-12-23 23:42:27 +00003041 switch (SetCCOpcode) {
3042 default: break;
3043 case ISD::SETOLT:
3044 case ISD::SETOLE:
3045 case ISD::SETUGT:
3046 case ISD::SETUGE:
3047 std::swap(LHS, RHS);
3048 break;
3049 }
3050
3051 // On a floating point condition, the flags are set as follows:
3052 // ZF PF CF op
3053 // 0 | 0 | 0 | X > Y
3054 // 0 | 0 | 1 | X < Y
3055 // 1 | 0 | 0 | X == Y
3056 // 1 | 1 | 1 | unordered
3057 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003058 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00003059 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003060 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00003061 case ISD::SETOLT: // flipped
3062 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003063 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00003064 case ISD::SETOLE: // flipped
3065 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003066 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003067 case ISD::SETUGT: // flipped
3068 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003069 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00003070 case ISD::SETUGE: // flipped
3071 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003072 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003073 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003074 case ISD::SETNE: return X86::COND_NE;
3075 case ISD::SETUO: return X86::COND_P;
3076 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003077 case ISD::SETOEQ:
3078 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003079 }
Evan Chengd9558e02006-01-06 00:43:03 +00003080}
3081
Evan Cheng4a460802006-01-11 00:33:36 +00003082/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3083/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003084/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003085static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003086 switch (X86CC) {
3087 default:
3088 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003089 case X86::COND_B:
3090 case X86::COND_BE:
3091 case X86::COND_E:
3092 case X86::COND_P:
3093 case X86::COND_A:
3094 case X86::COND_AE:
3095 case X86::COND_NE:
3096 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003097 return true;
3098 }
3099}
3100
Evan Chengeb2f9692009-10-27 19:56:55 +00003101/// isFPImmLegal - Returns true if the target can instruction select the
3102/// specified FP immediate natively. If false, the legalizer will
3103/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003104bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003105 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3106 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3107 return true;
3108 }
3109 return false;
3110}
3111
Nate Begeman9008ca62009-04-27 18:41:29 +00003112/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3113/// the specified range (L, H].
3114static bool isUndefOrInRange(int Val, int Low, int Hi) {
3115 return (Val < 0) || (Val >= Low && Val < Hi);
3116}
3117
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00003118/// isUndefOrInRange - Return true if every element in Mask, begining
3119/// from position Pos and ending in Pos+Size, falls within the specified
3120/// range (L, L+Pos]. or is undef.
3121static bool isUndefOrInRange(const SmallVectorImpl<int> &Mask,
3122 int Pos, int Size, int Low, int Hi) {
3123 for (int i = Pos, e = Pos+Size; i != e; ++i)
3124 if (!isUndefOrInRange(Mask[i], Low, Hi))
3125 return false;
3126 return true;
3127}
3128
Nate Begeman9008ca62009-04-27 18:41:29 +00003129/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3130/// specified value.
3131static bool isUndefOrEqual(int Val, int CmpVal) {
3132 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003133 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003134 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00003135}
3136
Bruno Cardoso Lopes4002d7e2011-08-12 21:54:42 +00003137/// isSequentialOrUndefInRange - Return true if every element in Mask, begining
3138/// from position Pos and ending in Pos+Size, falls within the specified
3139/// sequential range (L, L+Pos]. or is undef.
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003140static bool isSequentialOrUndefInRange(const SmallVectorImpl<int> &Mask,
3141 int Pos, int Size, int Low) {
3142 for (int i = Pos, e = Pos+Size; i != e; ++i, ++Low)
3143 if (!isUndefOrEqual(Mask[i], Low))
3144 return false;
3145 return true;
3146}
3147
Nate Begeman9008ca62009-04-27 18:41:29 +00003148/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3149/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3150/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00003151static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003152 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003153 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003154 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003155 return (Mask[0] < 2 && Mask[1] < 2);
3156 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003157}
3158
Nate Begeman9008ca62009-04-27 18:41:29 +00003159bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003160 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003161 N->getMask(M);
3162 return ::isPSHUFDMask(M, N->getValueType(0));
3163}
Evan Cheng0188ecb2006-03-22 18:59:22 +00003164
Nate Begeman9008ca62009-04-27 18:41:29 +00003165/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3166/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00003167static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003168 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00003169 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003170
Nate Begeman9008ca62009-04-27 18:41:29 +00003171 // Lower quadword copied in order or undef.
3172 for (int i = 0; i != 4; ++i)
3173 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00003174 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003175
Evan Cheng506d3df2006-03-29 23:07:14 +00003176 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003177 for (int i = 4; i != 8; ++i)
3178 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00003179 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003180
Evan Cheng506d3df2006-03-29 23:07:14 +00003181 return true;
3182}
3183
Nate Begeman9008ca62009-04-27 18:41:29 +00003184bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003185 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003186 N->getMask(M);
3187 return ::isPSHUFHWMask(M, N->getValueType(0));
3188}
Evan Cheng506d3df2006-03-29 23:07:14 +00003189
Nate Begeman9008ca62009-04-27 18:41:29 +00003190/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3191/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00003192static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003193 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00003194 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003195
Rafael Espindola15684b22009-04-24 12:40:33 +00003196 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00003197 for (int i = 4; i != 8; ++i)
3198 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00003199 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003200
Rafael Espindola15684b22009-04-24 12:40:33 +00003201 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003202 for (int i = 0; i != 4; ++i)
3203 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00003204 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003205
Rafael Espindola15684b22009-04-24 12:40:33 +00003206 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003207}
3208
Nate Begeman9008ca62009-04-27 18:41:29 +00003209bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003210 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003211 N->getMask(M);
3212 return ::isPSHUFLWMask(M, N->getValueType(0));
3213}
3214
Nate Begemana09008b2009-10-19 02:17:23 +00003215/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3216/// is suitable for input to PALIGNR.
3217static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00003218 bool hasSSSE3OrAVX) {
Nate Begemana09008b2009-10-19 02:17:23 +00003219 int i, e = VT.getVectorNumElements();
Craig Topper1dc0fbc2011-12-05 07:27:14 +00003220 if (VT.getSizeInBits() != 128)
Bruno Cardoso Lopes9065d4b2011-07-29 01:30:59 +00003221 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003222
Nate Begemana09008b2009-10-19 02:17:23 +00003223 // Do not handle v2i64 / v2f64 shuffles with palignr.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00003224 if (e < 4 || !hasSSSE3OrAVX)
Nate Begemana09008b2009-10-19 02:17:23 +00003225 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003226
Nate Begemana09008b2009-10-19 02:17:23 +00003227 for (i = 0; i != e; ++i)
3228 if (Mask[i] >= 0)
3229 break;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003230
Nate Begemana09008b2009-10-19 02:17:23 +00003231 // All undef, not a palignr.
3232 if (i == e)
3233 return false;
3234
Eli Friedman63f8dde2011-07-25 21:36:45 +00003235 // Make sure we're shifting in the right direction.
3236 if (Mask[i] <= i)
3237 return false;
Nate Begemana09008b2009-10-19 02:17:23 +00003238
3239 int s = Mask[i] - i;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003240
Nate Begemana09008b2009-10-19 02:17:23 +00003241 // Check the rest of the elements to see if they are consecutive.
3242 for (++i; i != e; ++i) {
3243 int m = Mask[i];
Eli Friedman63f8dde2011-07-25 21:36:45 +00003244 if (m >= 0 && m != s+i)
Nate Begemana09008b2009-10-19 02:17:23 +00003245 return false;
3246 }
3247 return true;
3248}
3249
Craig Topper9d7025b2011-11-27 21:41:12 +00003250/// isVSHUFPYMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003251/// specifies a shuffle of elements that is suitable for input to 256-bit
3252/// VSHUFPSY.
Craig Topper9d7025b2011-11-27 21:41:12 +00003253static bool isVSHUFPYMask(const SmallVectorImpl<int> &Mask, EVT VT,
Craig Topper1ff73d72011-12-06 04:59:07 +00003254 bool HasAVX, bool Commuted = false) {
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003255 int NumElems = VT.getVectorNumElements();
3256
Craig Topper71c4c122011-11-28 01:14:24 +00003257 if (!HasAVX || VT.getSizeInBits() != 256)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003258 return false;
3259
Craig Topper9d7025b2011-11-27 21:41:12 +00003260 if (NumElems != 4 && NumElems != 8)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003261 return false;
3262
3263 // VSHUFPSY divides the resulting vector into 4 chunks.
3264 // The sources are also splitted into 4 chunks, and each destination
3265 // chunk must come from a different source chunk.
3266 //
3267 // SRC1 => X7 X6 X5 X4 X3 X2 X1 X0
3268 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3269 //
3270 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3271 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3272 //
Craig Topper9d7025b2011-11-27 21:41:12 +00003273 // VSHUFPDY divides the resulting vector into 4 chunks.
3274 // The sources are also splitted into 4 chunks, and each destination
3275 // chunk must come from a different source chunk.
3276 //
3277 // SRC1 => X3 X2 X1 X0
3278 // SRC2 => Y3 Y2 Y1 Y0
3279 //
3280 // DST => Y3..Y2, X3..X2, Y1..Y0, X1..X0
3281 //
Craig Topper1ff73d72011-12-06 04:59:07 +00003282 unsigned QuarterSize = NumElems/4;
3283 unsigned HalfSize = QuarterSize*2;
3284 for (unsigned l = 0; l != 2; ++l) {
3285 unsigned LaneStart = l*HalfSize;
3286 for (unsigned s = 0; s != 2; ++s) {
3287 unsigned QuarterStart = s*QuarterSize;
3288 unsigned Src = (Commuted) ? (1-s) : s;
3289 unsigned SrcStart = Src*NumElems + LaneStart;
3290 for (unsigned i = 0; i != QuarterSize; ++i) {
3291 int Idx = Mask[i+QuarterStart+LaneStart];
3292 if (!isUndefOrInRange(Idx, SrcStart, SrcStart+HalfSize))
3293 return false;
3294 // For VSHUFPSY, the mask of the second half must be the same as the first
3295 // but with the appropriate offsets. This works in the same way as
3296 // VPERMILPS works with masks.
3297 if (NumElems == 4 || l == 0 || Mask[i+QuarterStart] < 0)
3298 continue;
3299 if (!isUndefOrEqual(Idx, Mask[i+QuarterStart]+HalfSize))
3300 return false;
3301 }
3302 }
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003303 }
3304
3305 return true;
3306}
3307
Craig Topper9d7025b2011-11-27 21:41:12 +00003308/// getShuffleVSHUFPYImmediate - Return the appropriate immediate to shuffle
3309/// the specified VECTOR_MASK mask with VSHUFPSY/VSHUFPDY instructions.
3310static unsigned getShuffleVSHUFPYImmediate(SDNode *N) {
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003311 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3312 EVT VT = SVOp->getValueType(0);
3313 int NumElems = VT.getVectorNumElements();
3314
Craig Topper9d7025b2011-11-27 21:41:12 +00003315 assert(VT.getSizeInBits() == 256 && "Only supports 256-bit types");
3316 assert((NumElems == 4 || NumElems == 8) && "Only supports v4 and v8 types");
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003317
3318 int HalfSize = NumElems/2;
Craig Topper9d7025b2011-11-27 21:41:12 +00003319 unsigned Mul = (NumElems == 8) ? 2 : 1;
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003320 unsigned Mask = 0;
Craig Topper71c4c122011-11-28 01:14:24 +00003321 for (int i = 0; i != NumElems; ++i) {
Craig Topper9d7025b2011-11-27 21:41:12 +00003322 int Elt = SVOp->getMaskElt(i);
3323 if (Elt < 0)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003324 continue;
Craig Topper9d7025b2011-11-27 21:41:12 +00003325 Elt %= HalfSize;
3326 unsigned Shamt = i;
3327 // For VSHUFPSY, the mask of the first half must be equal to the second one.
3328 if (NumElems == 8) Shamt %= HalfSize;
3329 Mask |= Elt << (Shamt*Mul);
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003330 }
3331
3332 return Mask;
3333}
3334
Elena Demikhovsky52a35a82011-11-23 10:23:16 +00003335/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3336/// the two vector operands have swapped position.
Craig Topperbeabc6c2011-12-05 06:56:46 +00003337static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask,
3338 unsigned NumElems) {
Elena Demikhovsky52a35a82011-11-23 10:23:16 +00003339 for (unsigned i = 0; i != NumElems; ++i) {
3340 int idx = Mask[i];
3341 if (idx < 0)
3342 continue;
3343 else if (idx < (int)NumElems)
3344 Mask[i] = idx + NumElems;
3345 else
3346 Mask[i] = idx - NumElems;
3347 }
3348}
3349
Evan Cheng14aed5e2006-03-24 01:18:28 +00003350/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopesaf002d82011-08-24 23:17:55 +00003351/// specifies a shuffle of elements that is suitable for input to 128-bit
Craig Topper1ff73d72011-12-06 04:59:07 +00003352/// SHUFPS and SHUFPD. If Commuted is true, then it checks for sources to be
3353/// reverse of what x86 shuffles want.
3354static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT,
3355 bool Commuted = false) {
3356 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesaf002d82011-08-24 23:17:55 +00003357
3358 if (VT.getSizeInBits() != 128)
3359 return false;
3360
Nate Begeman9008ca62009-04-27 18:41:29 +00003361 if (NumElems != 2 && NumElems != 4)
3362 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003363
Craig Topper1ff73d72011-12-06 04:59:07 +00003364 unsigned Half = NumElems / 2;
3365 unsigned SrcStart = Commuted ? NumElems : 0;
3366 for (unsigned i = 0; i != Half; ++i)
3367 if (!isUndefOrInRange(Mask[i], SrcStart, SrcStart+NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003368 return false;
Craig Topper1ff73d72011-12-06 04:59:07 +00003369 SrcStart = Commuted ? 0 : NumElems;
3370 for (unsigned i = Half; i != NumElems; ++i)
3371 if (!isUndefOrInRange(Mask[i], SrcStart, SrcStart+NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003372 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003373
Evan Cheng14aed5e2006-03-24 01:18:28 +00003374 return true;
3375}
3376
Nate Begeman9008ca62009-04-27 18:41:29 +00003377bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
3378 SmallVector<int, 8> M;
3379 N->getMask(M);
3380 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003381}
3382
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003383/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3384/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00003385bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003386 EVT VT = N->getValueType(0);
3387 unsigned NumElems = VT.getVectorNumElements();
3388
3389 if (VT.getSizeInBits() != 128)
3390 return false;
3391
3392 if (NumElems != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003393 return false;
3394
Evan Cheng2064a2b2006-03-28 06:50:32 +00003395 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00003396 return isUndefOrEqual(N->getMaskElt(0), 6) &&
3397 isUndefOrEqual(N->getMaskElt(1), 7) &&
3398 isUndefOrEqual(N->getMaskElt(2), 2) &&
3399 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003400}
3401
Nate Begeman0b10b912009-11-07 23:17:15 +00003402/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3403/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3404/// <2, 3, 2, 3>
3405bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003406 EVT VT = N->getValueType(0);
3407 unsigned NumElems = VT.getVectorNumElements();
3408
3409 if (VT.getSizeInBits() != 128)
3410 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003411
Nate Begeman0b10b912009-11-07 23:17:15 +00003412 if (NumElems != 4)
3413 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003414
Nate Begeman0b10b912009-11-07 23:17:15 +00003415 return isUndefOrEqual(N->getMaskElt(0), 2) &&
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003416 isUndefOrEqual(N->getMaskElt(1), 3) &&
3417 isUndefOrEqual(N->getMaskElt(2), 2) &&
3418 isUndefOrEqual(N->getMaskElt(3), 3);
Nate Begeman0b10b912009-11-07 23:17:15 +00003419}
3420
Evan Cheng5ced1d82006-04-06 23:23:56 +00003421/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3422/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003423bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3424 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003425
Evan Cheng5ced1d82006-04-06 23:23:56 +00003426 if (NumElems != 2 && NumElems != 4)
3427 return false;
3428
Evan Chengc5cdff22006-04-07 21:53:05 +00003429 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003430 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003431 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003432
Evan Chengc5cdff22006-04-07 21:53:05 +00003433 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003434 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003435 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003436
3437 return true;
3438}
3439
Nate Begeman0b10b912009-11-07 23:17:15 +00003440/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3441/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3442bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003443 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003444
David Greenea20244d2011-03-02 17:23:43 +00003445 if ((NumElems != 2 && NumElems != 4)
3446 || N->getValueType(0).getSizeInBits() > 128)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003447 return false;
3448
Evan Chengc5cdff22006-04-07 21:53:05 +00003449 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003450 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003451 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003452
Nate Begeman9008ca62009-04-27 18:41:29 +00003453 for (unsigned i = 0; i < NumElems/2; ++i)
3454 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003455 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003456
3457 return true;
3458}
3459
Evan Cheng0038e592006-03-28 00:39:58 +00003460/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3461/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003462static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Craig Topper6347e862011-11-21 06:57:39 +00003463 bool HasAVX2, bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003464 int NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003465
3466 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3467 "Unsupported vector type for unpckh");
3468
Craig Topper6347e862011-11-21 06:57:39 +00003469 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Craig Topper6fa583d2011-11-21 08:26:50 +00003470 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng0038e592006-03-28 00:39:58 +00003471 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003472
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003473 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3474 // independently on 128-bit lanes.
3475 unsigned NumLanes = VT.getSizeInBits()/128;
3476 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003477
3478 unsigned Start = 0;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003479 unsigned End = NumLaneElts;
3480 for (unsigned s = 0; s < NumLanes; ++s) {
3481 for (unsigned i = Start, j = s * NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003482 i != End;
3483 i += 2, ++j) {
3484 int BitI = Mask[i];
3485 int BitI1 = Mask[i+1];
3486 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003487 return false;
David Greenea20244d2011-03-02 17:23:43 +00003488 if (V2IsSplat) {
3489 if (!isUndefOrEqual(BitI1, NumElts))
3490 return false;
3491 } else {
3492 if (!isUndefOrEqual(BitI1, j + NumElts))
3493 return false;
3494 }
Evan Cheng39623da2006-04-20 08:58:49 +00003495 }
David Greenea20244d2011-03-02 17:23:43 +00003496 // Process the next 128 bits.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003497 Start += NumLaneElts;
3498 End += NumLaneElts;
Evan Cheng0038e592006-03-28 00:39:58 +00003499 }
David Greenea20244d2011-03-02 17:23:43 +00003500
Evan Cheng0038e592006-03-28 00:39:58 +00003501 return true;
3502}
3503
Craig Topper6347e862011-11-21 06:57:39 +00003504bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool HasAVX2, bool V2IsSplat) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003505 SmallVector<int, 8> M;
3506 N->getMask(M);
Craig Topper6347e862011-11-21 06:57:39 +00003507 return ::isUNPCKLMask(M, N->getValueType(0), HasAVX2, V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003508}
3509
Evan Cheng4fcb9222006-03-28 02:43:26 +00003510/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3511/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003512static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Craig Topper6347e862011-11-21 06:57:39 +00003513 bool HasAVX2, bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003514 int NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003515
3516 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3517 "Unsupported vector type for unpckh");
3518
Craig Topper6347e862011-11-21 06:57:39 +00003519 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Craig Topper6fa583d2011-11-21 08:26:50 +00003520 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng4fcb9222006-03-28 02:43:26 +00003521 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003522
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003523 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3524 // independently on 128-bit lanes.
3525 unsigned NumLanes = VT.getSizeInBits()/128;
3526 unsigned NumLaneElts = NumElts/NumLanes;
3527
3528 unsigned Start = 0;
3529 unsigned End = NumLaneElts;
3530 for (unsigned l = 0; l != NumLanes; ++l) {
3531 for (unsigned i = Start, j = (l*NumLaneElts)+NumLaneElts/2;
3532 i != End; i += 2, ++j) {
3533 int BitI = Mask[i];
3534 int BitI1 = Mask[i+1];
3535 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003536 return false;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003537 if (V2IsSplat) {
3538 if (isUndefOrEqual(BitI1, NumElts))
3539 return false;
3540 } else {
3541 if (!isUndefOrEqual(BitI1, j+NumElts))
3542 return false;
3543 }
Evan Cheng39623da2006-04-20 08:58:49 +00003544 }
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003545 // Process the next 128 bits.
3546 Start += NumLaneElts;
3547 End += NumLaneElts;
Evan Cheng4fcb9222006-03-28 02:43:26 +00003548 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003549 return true;
3550}
3551
Craig Topper6347e862011-11-21 06:57:39 +00003552bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool HasAVX2, bool V2IsSplat) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003553 SmallVector<int, 8> M;
3554 N->getMask(M);
Craig Topper6347e862011-11-21 06:57:39 +00003555 return ::isUNPCKHMask(M, N->getValueType(0), HasAVX2, V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003556}
3557
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003558/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3559/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3560/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003561static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003562 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003563 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003564 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003565
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003566 // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern
3567 // FIXME: Need a better way to get rid of this, there's no latency difference
3568 // between UNPCKLPD and MOVDDUP, the later should always be checked first and
3569 // the former later. We should also remove the "_undef" special mask.
3570 if (NumElems == 4 && VT.getSizeInBits() == 256)
3571 return false;
3572
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003573 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3574 // independently on 128-bit lanes.
3575 unsigned NumLanes = VT.getSizeInBits() / 128;
3576 unsigned NumLaneElts = NumElems / NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003577
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003578 for (unsigned s = 0; s < NumLanes; ++s) {
3579 for (unsigned i = s * NumLaneElts, j = s * NumLaneElts;
3580 i != NumLaneElts * (s + 1);
David Greenea20244d2011-03-02 17:23:43 +00003581 i += 2, ++j) {
3582 int BitI = Mask[i];
3583 int BitI1 = Mask[i+1];
3584
3585 if (!isUndefOrEqual(BitI, j))
3586 return false;
3587 if (!isUndefOrEqual(BitI1, j))
3588 return false;
3589 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003590 }
David Greenea20244d2011-03-02 17:23:43 +00003591
Rafael Espindola15684b22009-04-24 12:40:33 +00003592 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003593}
3594
Nate Begeman9008ca62009-04-27 18:41:29 +00003595bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3596 SmallVector<int, 8> M;
3597 N->getMask(M);
3598 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3599}
3600
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003601/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3602/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3603/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003604static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003605 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003606 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3607 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003608
Nate Begeman9008ca62009-04-27 18:41:29 +00003609 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3610 int BitI = Mask[i];
3611 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003612 if (!isUndefOrEqual(BitI, j))
3613 return false;
3614 if (!isUndefOrEqual(BitI1, j))
3615 return false;
3616 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003617 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003618}
3619
Nate Begeman9008ca62009-04-27 18:41:29 +00003620bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3621 SmallVector<int, 8> M;
3622 N->getMask(M);
3623 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3624}
3625
Evan Cheng017dcc62006-04-21 01:05:10 +00003626/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3627/// specifies a shuffle of elements that is suitable for input to MOVSS,
3628/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003629static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003630 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003631 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003632
3633 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003634
Nate Begeman9008ca62009-04-27 18:41:29 +00003635 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003636 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003637
Nate Begeman9008ca62009-04-27 18:41:29 +00003638 for (int i = 1; i < NumElts; ++i)
3639 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003640 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003641
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003642 return true;
3643}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003644
Nate Begeman9008ca62009-04-27 18:41:29 +00003645bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3646 SmallVector<int, 8> M;
3647 N->getMask(M);
3648 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003649}
3650
Craig Topper70b883b2011-11-28 10:14:51 +00003651/// isVPERM2X128Mask - Match 256-bit shuffles where the elements are considered
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003652/// as permutations between 128-bit chunks or halves. As an example: this
3653/// shuffle bellow:
3654/// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
3655/// The first half comes from the second half of V1 and the second half from the
3656/// the second half of V2.
Craig Topper70b883b2011-11-28 10:14:51 +00003657static bool isVPERM2X128Mask(const SmallVectorImpl<int> &Mask, EVT VT,
3658 bool HasAVX) {
3659 if (!HasAVX || VT.getSizeInBits() != 256)
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003660 return false;
3661
3662 // The shuffle result is divided into half A and half B. In total the two
3663 // sources have 4 halves, namely: C, D, E, F. The final values of A and
3664 // B must come from C, D, E or F.
3665 int HalfSize = VT.getVectorNumElements()/2;
3666 bool MatchA = false, MatchB = false;
3667
3668 // Check if A comes from one of C, D, E, F.
3669 for (int Half = 0; Half < 4; ++Half) {
3670 if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
3671 MatchA = true;
3672 break;
3673 }
3674 }
3675
3676 // Check if B comes from one of C, D, E, F.
3677 for (int Half = 0; Half < 4; ++Half) {
3678 if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) {
3679 MatchB = true;
3680 break;
3681 }
3682 }
3683
3684 return MatchA && MatchB;
3685}
3686
Craig Topper70b883b2011-11-28 10:14:51 +00003687/// getShuffleVPERM2X128Immediate - Return the appropriate immediate to shuffle
3688/// the specified VECTOR_MASK mask with VPERM2F128/VPERM2I128 instructions.
3689static unsigned getShuffleVPERM2X128Immediate(SDNode *N) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003690 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3691 EVT VT = SVOp->getValueType(0);
3692
3693 int HalfSize = VT.getVectorNumElements()/2;
3694
3695 int FstHalf = 0, SndHalf = 0;
3696 for (int i = 0; i < HalfSize; ++i) {
3697 if (SVOp->getMaskElt(i) > 0) {
3698 FstHalf = SVOp->getMaskElt(i)/HalfSize;
3699 break;
3700 }
3701 }
3702 for (int i = HalfSize; i < HalfSize*2; ++i) {
3703 if (SVOp->getMaskElt(i) > 0) {
3704 SndHalf = SVOp->getMaskElt(i)/HalfSize;
3705 break;
3706 }
3707 }
3708
3709 return (FstHalf | (SndHalf << 4));
3710}
3711
Craig Topper70b883b2011-11-28 10:14:51 +00003712/// isVPERMILPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003713/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
3714/// Note that VPERMIL mask matching is different depending whether theunderlying
3715/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3716/// to the same elements of the low, but to the higher half of the source.
3717/// In VPERMILPD the two lanes could be shuffled independently of each other
3718/// with the same restriction that lanes can't be crossed.
Craig Topper70b883b2011-11-28 10:14:51 +00003719static bool isVPERMILPMask(const SmallVectorImpl<int> &Mask, EVT VT,
3720 bool HasAVX) {
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003721 int NumElts = VT.getVectorNumElements();
3722 int NumLanes = VT.getSizeInBits()/128;
3723
Craig Topper70b883b2011-11-28 10:14:51 +00003724 if (!HasAVX)
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003725 return false;
3726
Craig Topper70b883b2011-11-28 10:14:51 +00003727 // Only match 256-bit with 32/64-bit types
3728 if (VT.getSizeInBits() != 256 || (NumElts != 4 && NumElts != 8))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003729 return false;
3730
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003731 int LaneSize = NumElts/NumLanes;
Craig Topper70b883b2011-11-28 10:14:51 +00003732 for (int l = 0; l != NumLanes; ++l) {
3733 int LaneStart = l*LaneSize;
3734 for (int i = 0; i != LaneSize; ++i) {
3735 if (!isUndefOrInRange(Mask[i+LaneStart], LaneStart, LaneStart+LaneSize))
3736 return false;
3737 if (NumElts == 4 || l == 0)
3738 continue;
3739 // VPERMILPS handling
3740 if (Mask[i] < 0)
3741 continue;
3742 if (!isUndefOrEqual(Mask[i+LaneStart], Mask[i]+LaneSize))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003743 return false;
3744 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003745 }
3746
3747 return true;
3748}
3749
Craig Topper70b883b2011-11-28 10:14:51 +00003750/// getShuffleVPERMILPImmediate - Return the appropriate immediate to shuffle
3751/// the specified VECTOR_MASK mask with VPERMILPS/D* instructions.
3752static unsigned getShuffleVPERMILPImmediate(SDNode *N) {
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003753 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3754 EVT VT = SVOp->getValueType(0);
3755
3756 int NumElts = VT.getVectorNumElements();
3757 int NumLanes = VT.getSizeInBits()/128;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003758 int LaneSize = NumElts/NumLanes;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003759
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003760 // Although the mask is equal for both lanes do it twice to get the cases
3761 // where a mask will match because the same mask element is undef on the
3762 // first half but valid on the second. This would get pathological cases
3763 // such as: shuffle <u, 0, 1, 2, 4, 4, 5, 6>, which is completely valid.
Craig Topper70b883b2011-11-28 10:14:51 +00003764 unsigned Shift = (LaneSize == 4) ? 2 : 1;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003765 unsigned Mask = 0;
Craig Topper70b883b2011-11-28 10:14:51 +00003766 for (int i = 0; i != NumElts; ++i) {
3767 int MaskElt = SVOp->getMaskElt(i);
3768 if (MaskElt < 0)
3769 continue;
3770 MaskElt %= LaneSize;
3771 unsigned Shamt = i;
3772 // VPERMILPSY, the mask of the first half must be equal to the second one
3773 if (NumElts == 8) Shamt %= LaneSize;
3774 Mask |= MaskElt << (Shamt*Shift);
Bruno Cardoso Lopes377baa52011-07-29 01:31:04 +00003775 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003776
3777 return Mask;
3778}
3779
Evan Cheng017dcc62006-04-21 01:05:10 +00003780/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3781/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003782/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003783static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003784 bool V2IsSplat = false, bool V2IsUndef = false) {
3785 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003786 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003787 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003788
Nate Begeman9008ca62009-04-27 18:41:29 +00003789 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003790 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003791
Nate Begeman9008ca62009-04-27 18:41:29 +00003792 for (int i = 1; i < NumOps; ++i)
3793 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3794 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3795 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003796 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003797
Evan Cheng39623da2006-04-20 08:58:49 +00003798 return true;
3799}
3800
Nate Begeman9008ca62009-04-27 18:41:29 +00003801static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003802 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003803 SmallVector<int, 8> M;
3804 N->getMask(M);
3805 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003806}
3807
Evan Chengd9539472006-04-14 21:59:03 +00003808/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3809/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003810/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
3811bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N,
3812 const X86Subtarget *Subtarget) {
Craig Topperc0d82852011-11-22 00:44:41 +00003813 if (!Subtarget->hasSSE3orAVX())
Evan Chengd9539472006-04-14 21:59:03 +00003814 return false;
3815
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003816 // The second vector must be undef
3817 if (N->getOperand(1).getOpcode() != ISD::UNDEF)
3818 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003819
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003820 EVT VT = N->getValueType(0);
3821 unsigned NumElems = VT.getVectorNumElements();
3822
3823 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3824 (VT.getSizeInBits() == 256 && NumElems != 8))
3825 return false;
3826
3827 // "i+1" is the value the indexed mask element must have
3828 for (unsigned i = 0; i < NumElems; i += 2)
3829 if (!isUndefOrEqual(N->getMaskElt(i), i+1) ||
3830 !isUndefOrEqual(N->getMaskElt(i+1), i+1))
Nate Begeman9008ca62009-04-27 18:41:29 +00003831 return false;
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003832
3833 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003834}
3835
3836/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3837/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003838/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
3839bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N,
3840 const X86Subtarget *Subtarget) {
Craig Topperc0d82852011-11-22 00:44:41 +00003841 if (!Subtarget->hasSSE3orAVX())
Evan Chengd9539472006-04-14 21:59:03 +00003842 return false;
3843
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003844 // The second vector must be undef
3845 if (N->getOperand(1).getOpcode() != ISD::UNDEF)
3846 return false;
3847
3848 EVT VT = N->getValueType(0);
3849 unsigned NumElems = VT.getVectorNumElements();
3850
3851 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3852 (VT.getSizeInBits() == 256 && NumElems != 8))
3853 return false;
3854
3855 // "i" is the value the indexed mask element must have
3856 for (unsigned i = 0; i < NumElems; i += 2)
3857 if (!isUndefOrEqual(N->getMaskElt(i), i) ||
3858 !isUndefOrEqual(N->getMaskElt(i+1), i))
Nate Begeman9008ca62009-04-27 18:41:29 +00003859 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003860
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003861 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003862}
3863
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003864/// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand
3865/// specifies a shuffle of elements that is suitable for input to 256-bit
3866/// version of MOVDDUP.
Craig Topperbeabc6c2011-12-05 06:56:46 +00003867static bool isMOVDDUPYMask(const SmallVectorImpl<int> &Mask, EVT VT,
3868 bool HasAVX) {
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003869 int NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003870
Craig Topperbeabc6c2011-12-05 06:56:46 +00003871 if (!HasAVX || VT.getSizeInBits() != 256 || NumElts != 4)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003872 return false;
3873
3874 for (int i = 0; i != NumElts/2; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003875 if (!isUndefOrEqual(Mask[i], 0))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003876 return false;
3877 for (int i = NumElts/2; i != NumElts; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003878 if (!isUndefOrEqual(Mask[i], NumElts/2))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003879 return false;
3880 return true;
3881}
3882
Evan Cheng0b457f02008-09-25 20:50:48 +00003883/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003884/// specifies a shuffle of elements that is suitable for input to 128-bit
3885/// version of MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003886bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003887 EVT VT = N->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00003888
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003889 if (VT.getSizeInBits() != 128)
3890 return false;
3891
3892 int e = VT.getVectorNumElements() / 2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003893 for (int i = 0; i < e; ++i)
3894 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003895 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003896 for (int i = 0; i < e; ++i)
3897 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003898 return false;
3899 return true;
3900}
3901
David Greenec38a03e2011-02-03 15:50:00 +00003902/// isVEXTRACTF128Index - Return true if the specified
3903/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3904/// suitable for input to VEXTRACTF128.
3905bool X86::isVEXTRACTF128Index(SDNode *N) {
3906 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3907 return false;
3908
3909 // The index should be aligned on a 128-bit boundary.
3910 uint64_t Index =
3911 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3912
3913 unsigned VL = N->getValueType(0).getVectorNumElements();
3914 unsigned VBits = N->getValueType(0).getSizeInBits();
3915 unsigned ElSize = VBits / VL;
3916 bool Result = (Index * ElSize) % 128 == 0;
3917
3918 return Result;
3919}
3920
David Greeneccacdc12011-02-04 16:08:29 +00003921/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
3922/// operand specifies a subvector insert that is suitable for input to
3923/// VINSERTF128.
3924bool X86::isVINSERTF128Index(SDNode *N) {
3925 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3926 return false;
3927
3928 // The index should be aligned on a 128-bit boundary.
3929 uint64_t Index =
3930 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
3931
3932 unsigned VL = N->getValueType(0).getVectorNumElements();
3933 unsigned VBits = N->getValueType(0).getSizeInBits();
3934 unsigned ElSize = VBits / VL;
3935 bool Result = (Index * ElSize) % 128 == 0;
3936
3937 return Result;
3938}
3939
Evan Cheng63d33002006-03-22 08:01:21 +00003940/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003941/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00003942unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003943 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3944 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
3945
Evan Chengb9df0ca2006-03-22 02:53:00 +00003946 unsigned Shift = (NumOperands == 4) ? 2 : 1;
3947 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003948 for (int i = 0; i < NumOperands; ++i) {
3949 int Val = SVOp->getMaskElt(NumOperands-i-1);
3950 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00003951 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00003952 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00003953 if (i != NumOperands - 1)
3954 Mask <<= Shift;
3955 }
Evan Cheng63d33002006-03-22 08:01:21 +00003956 return Mask;
3957}
3958
Evan Cheng506d3df2006-03-29 23:07:14 +00003959/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003960/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003961unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003962 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003963 unsigned Mask = 0;
3964 // 8 nodes, but we only care about the last 4.
3965 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003966 int Val = SVOp->getMaskElt(i);
3967 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003968 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00003969 if (i != 4)
3970 Mask <<= 2;
3971 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003972 return Mask;
3973}
3974
3975/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003976/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003977unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003978 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003979 unsigned Mask = 0;
3980 // 8 nodes, but we only care about the first 4.
3981 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003982 int Val = SVOp->getMaskElt(i);
3983 if (Val >= 0)
3984 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00003985 if (i != 0)
3986 Mask <<= 2;
3987 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003988 return Mask;
3989}
3990
Nate Begemana09008b2009-10-19 02:17:23 +00003991/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3992/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
3993unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
3994 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3995 EVT VVT = N->getValueType(0);
3996 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
3997 int Val = 0;
3998
3999 unsigned i, e;
4000 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
4001 Val = SVOp->getMaskElt(i);
4002 if (Val >= 0)
4003 break;
4004 }
Eli Friedman63f8dde2011-07-25 21:36:45 +00004005 assert(Val - i > 0 && "PALIGNR imm should be positive");
Nate Begemana09008b2009-10-19 02:17:23 +00004006 return (Val - i) * EltSize;
4007}
4008
David Greenec38a03e2011-02-03 15:50:00 +00004009/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
4010/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
4011/// instructions.
4012unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
4013 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4014 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
4015
4016 uint64_t Index =
4017 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4018
4019 EVT VecVT = N->getOperand(0).getValueType();
4020 EVT ElVT = VecVT.getVectorElementType();
4021
4022 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004023 return Index / NumElemsPerChunk;
4024}
4025
David Greeneccacdc12011-02-04 16:08:29 +00004026/// getInsertVINSERTF128Immediate - Return the appropriate immediate
4027/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
4028/// instructions.
4029unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
4030 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4031 llvm_unreachable("Illegal insert subvector for VINSERTF128");
4032
4033 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00004034 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00004035
4036 EVT VecVT = N->getValueType(0);
4037 EVT ElVT = VecVT.getVectorElementType();
4038
4039 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004040 return Index / NumElemsPerChunk;
4041}
4042
Evan Cheng37b73872009-07-30 08:33:02 +00004043/// isZeroNode - Returns true if Elt is a constant zero or a floating point
4044/// constant +0.0.
4045bool X86::isZeroNode(SDValue Elt) {
4046 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00004047 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00004048 (isa<ConstantFPSDNode>(Elt) &&
4049 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
4050}
4051
Nate Begeman9008ca62009-04-27 18:41:29 +00004052/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
4053/// their permute mask.
4054static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
4055 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004056 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004057 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004058 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00004059
Nate Begeman5a5ca152009-04-29 05:20:52 +00004060 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004061 int idx = SVOp->getMaskElt(i);
4062 if (idx < 0)
4063 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004064 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00004065 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004066 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004067 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004068 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004069 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
4070 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004071}
4072
Evan Cheng533a0aa2006-04-19 20:35:22 +00004073/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
4074/// match movhlps. The lower half elements should come from upper half of
4075/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004076/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00004077static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004078 EVT VT = Op->getValueType(0);
4079 if (VT.getSizeInBits() != 128)
4080 return false;
4081 if (VT.getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00004082 return false;
4083 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004084 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004085 return false;
4086 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004087 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004088 return false;
4089 return true;
4090}
4091
Evan Cheng5ced1d82006-04-06 23:23:56 +00004092/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00004093/// is promoted to a vector. It also returns the LoadSDNode by reference if
4094/// required.
4095static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00004096 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
4097 return false;
4098 N = N->getOperand(0).getNode();
4099 if (!ISD::isNON_EXTLoad(N))
4100 return false;
4101 if (LD)
4102 *LD = cast<LoadSDNode>(N);
4103 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004104}
4105
Dan Gohman65fd6562011-11-03 21:49:52 +00004106// Test whether the given value is a vector value which will be legalized
4107// into a load.
4108static bool WillBeConstantPoolLoad(SDNode *N) {
4109 if (N->getOpcode() != ISD::BUILD_VECTOR)
4110 return false;
4111
4112 // Check for any non-constant elements.
4113 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4114 switch (N->getOperand(i).getNode()->getOpcode()) {
4115 case ISD::UNDEF:
4116 case ISD::ConstantFP:
4117 case ISD::Constant:
4118 break;
4119 default:
4120 return false;
4121 }
4122
4123 // Vectors of all-zeros and all-ones are materialized with special
4124 // instructions rather than being loaded.
4125 return !ISD::isBuildVectorAllZeros(N) &&
4126 !ISD::isBuildVectorAllOnes(N);
4127}
4128
Evan Cheng533a0aa2006-04-19 20:35:22 +00004129/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4130/// match movlp{s|d}. The lower half elements should come from lower half of
4131/// V1 (and in order), and the upper half elements should come from the upper
4132/// half of V2 (and in order). And since V1 will become the source of the
4133/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004134static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
4135 ShuffleVectorSDNode *Op) {
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004136 EVT VT = Op->getValueType(0);
4137 if (VT.getSizeInBits() != 128)
4138 return false;
4139
Evan Cheng466685d2006-10-09 20:57:25 +00004140 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004141 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00004142 // Is V2 is a vector load, don't do this transformation. We will try to use
4143 // load folding shufps op.
Dan Gohman65fd6562011-11-03 21:49:52 +00004144 if (ISD::isNON_EXTLoad(V2) || WillBeConstantPoolLoad(V2))
Evan Cheng23425f52006-10-09 21:39:25 +00004145 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004146
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004147 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004148
Evan Cheng533a0aa2006-04-19 20:35:22 +00004149 if (NumElems != 2 && NumElems != 4)
4150 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004151 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004152 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004153 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004154 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004155 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004156 return false;
4157 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004158}
4159
Evan Cheng39623da2006-04-20 08:58:49 +00004160/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4161/// all the same.
4162static bool isSplatVector(SDNode *N) {
4163 if (N->getOpcode() != ISD::BUILD_VECTOR)
4164 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004165
Dan Gohman475871a2008-07-27 21:46:04 +00004166 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00004167 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4168 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00004169 return false;
4170 return true;
4171}
4172
Evan Cheng213d2cf2007-05-17 18:45:50 +00004173/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00004174/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00004175/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00004176static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00004177 SDValue V1 = N->getOperand(0);
4178 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004179 unsigned NumElems = N->getValueType(0).getVectorNumElements();
4180 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004181 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004182 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004183 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00004184 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4185 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004186 if (Opc != ISD::BUILD_VECTOR ||
4187 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00004188 return false;
4189 } else if (Idx >= 0) {
4190 unsigned Opc = V1.getOpcode();
4191 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4192 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004193 if (Opc != ISD::BUILD_VECTOR ||
4194 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00004195 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00004196 }
4197 }
4198 return true;
4199}
4200
4201/// getZeroVector - Returns a vector of specified type with all zero elements.
4202///
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004203static SDValue getZeroVector(EVT VT, bool HasXMMInt, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00004204 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004205 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004206
Dale Johannesen0488fb62010-09-30 23:57:10 +00004207 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004208 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00004209 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00004210 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004211 if (HasXMMInt) { // SSE2
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004212 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4213 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4214 } else { // SSE1
4215 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4216 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4217 }
4218 } else if (VT.getSizeInBits() == 256) { // AVX
4219 // 256-bit logic and arithmetic instructions in AVX are
4220 // all floating-point, no support for integer ops. Default
4221 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00004222 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004223 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4224 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00004225 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004226 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004227}
4228
Chris Lattner8a594482007-11-25 00:24:49 +00004229/// getOnesVector - Returns a vector of specified type with all bits set.
Craig Topper745a86b2011-11-19 22:34:59 +00004230/// Always build ones vectors as <4 x i32> or <8 x i32>. For 256-bit types with
4231/// no AVX2 supprt, use two <4 x i32> inserted in a <8 x i32> appropriately.
4232/// Then bitcast to their original type, ensuring they get CSE'd.
4233static SDValue getOnesVector(EVT VT, bool HasAVX2, SelectionDAG &DAG,
4234 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004235 assert(VT.isVector() && "Expected a vector type");
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004236 assert((VT.is128BitVector() || VT.is256BitVector())
4237 && "Expected a 128-bit or 256-bit vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004238
Owen Anderson825b72b2009-08-11 20:47:22 +00004239 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Craig Topper745a86b2011-11-19 22:34:59 +00004240 SDValue Vec;
4241 if (VT.getSizeInBits() == 256) {
4242 if (HasAVX2) { // AVX2
4243 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4244 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4245 } else { // AVX
4246 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4247 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, MVT::v8i32),
4248 Vec, DAG.getConstant(0, MVT::i32), DAG, dl);
4249 Vec = Insert128BitVector(InsV, Vec,
4250 DAG.getConstant(4 /* NumElems/2 */, MVT::i32), DAG, dl);
4251 }
4252 } else {
4253 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004254 }
4255
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004256 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00004257}
4258
Evan Cheng39623da2006-04-20 08:58:49 +00004259/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4260/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00004261static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004262 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004263 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004264
Evan Cheng39623da2006-04-20 08:58:49 +00004265 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00004266 SmallVector<int, 8> MaskVec;
4267 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00004268
Nate Begeman5a5ca152009-04-29 05:20:52 +00004269 for (unsigned i = 0; i != NumElems; ++i) {
4270 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004271 MaskVec[i] = NumElems;
4272 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00004273 }
Evan Cheng39623da2006-04-20 08:58:49 +00004274 }
Evan Cheng39623da2006-04-20 08:58:49 +00004275 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00004276 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
4277 SVOp->getOperand(1), &MaskVec[0]);
4278 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00004279}
4280
Evan Cheng017dcc62006-04-21 01:05:10 +00004281/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4282/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00004283static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004284 SDValue V2) {
4285 unsigned NumElems = VT.getVectorNumElements();
4286 SmallVector<int, 8> Mask;
4287 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00004288 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004289 Mask.push_back(i);
4290 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00004291}
4292
Nate Begeman9008ca62009-04-27 18:41:29 +00004293/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004294static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004295 SDValue V2) {
4296 unsigned NumElems = VT.getVectorNumElements();
4297 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00004298 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004299 Mask.push_back(i);
4300 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00004301 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004302 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00004303}
4304
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004305/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004306static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004307 SDValue V2) {
4308 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00004309 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00004310 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00004311 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004312 Mask.push_back(i + Half);
4313 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00004314 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004315 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004316}
4317
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004318// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004319// a generic shuffle instruction because the target has no such instructions.
4320// Generate shuffles which repeat i16 and i8 several times until they can be
4321// represented by v4f32 and then be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004322static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004323 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004324 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004325 DebugLoc dl = V.getDebugLoc();
Rafael Espindola15684b22009-04-24 12:40:33 +00004326
Nate Begeman9008ca62009-04-27 18:41:29 +00004327 while (NumElems > 4) {
4328 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004329 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004330 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004331 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004332 EltNo -= NumElems/2;
4333 }
4334 NumElems >>= 1;
4335 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004336 return V;
4337}
Eric Christopherfd179292009-08-27 18:07:15 +00004338
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004339/// getLegalSplat - Generate a legal splat with supported x86 shuffles
4340static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
4341 EVT VT = V.getValueType();
4342 DebugLoc dl = V.getDebugLoc();
4343 assert((VT.getSizeInBits() == 128 || VT.getSizeInBits() == 256)
4344 && "Vector size not supported");
4345
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004346 if (VT.getSizeInBits() == 128) {
4347 V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004348 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004349 V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
4350 &SplatMask[0]);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004351 } else {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004352 // To use VPERMILPS to splat scalars, the second half of indicies must
4353 // refer to the higher part, which is a duplication of the lower one,
4354 // because VPERMILPS can only handle in-lane permutations.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004355 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
4356 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004357
4358 V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
4359 V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
4360 &SplatMask[0]);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004361 }
4362
4363 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4364}
4365
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004366/// PromoteSplat - Splat is promoted to target supported vector shuffles.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004367static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4368 EVT SrcVT = SV->getValueType(0);
4369 SDValue V1 = SV->getOperand(0);
4370 DebugLoc dl = SV->getDebugLoc();
4371
4372 int EltNo = SV->getSplatIndex();
4373 int NumElems = SrcVT.getVectorNumElements();
4374 unsigned Size = SrcVT.getSizeInBits();
4375
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004376 assert(((Size == 128 && NumElems > 4) || Size == 256) &&
4377 "Unknown how to promote splat for type");
4378
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004379 // Extract the 128-bit part containing the splat element and update
4380 // the splat element index when it refers to the higher register.
4381 if (Size == 256) {
4382 unsigned Idx = (EltNo > NumElems/2) ? NumElems/2 : 0;
4383 V1 = Extract128BitVector(V1, DAG.getConstant(Idx, MVT::i32), DAG, dl);
4384 if (Idx > 0)
4385 EltNo -= NumElems/2;
4386 }
4387
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004388 // All i16 and i8 vector types can't be used directly by a generic shuffle
4389 // instruction because the target has no such instruction. Generate shuffles
4390 // which repeat i16 and i8 several times until they fit in i32, and then can
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004391 // be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004392 EVT EltVT = SrcVT.getVectorElementType();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004393 if (EltVT == MVT::i8 || EltVT == MVT::i16)
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004394 V1 = PromoteSplati8i16(V1, DAG, EltNo);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004395
4396 // Recreate the 256-bit vector and place the same 128-bit vector
4397 // into the low and high part. This is necessary because we want
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004398 // to use VPERM* to shuffle the vectors
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004399 if (Size == 256) {
4400 SDValue InsV = Insert128BitVector(DAG.getUNDEF(SrcVT), V1,
4401 DAG.getConstant(0, MVT::i32), DAG, dl);
4402 V1 = Insert128BitVector(InsV, V1,
4403 DAG.getConstant(NumElems/2, MVT::i32), DAG, dl);
4404 }
4405
4406 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004407}
4408
Evan Chengba05f722006-04-21 23:03:30 +00004409/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004410/// vector of zero or undef vector. This produces a shuffle where the low
4411/// element of V2 is swizzled into the zero/undef vector, landing at element
4412/// 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 +00004413static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004414 bool isZero, bool HasXMMInt,
4415 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004416 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00004417 SDValue V1 = isZero
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004418 ? getZeroVector(VT, HasXMMInt, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00004419 unsigned NumElems = VT.getVectorNumElements();
4420 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004421 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004422 // If this is the insertion idx, put the low elt of V2 here.
4423 MaskVec.push_back(i == Idx ? NumElems : i);
4424 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004425}
4426
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004427/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4428/// element of the result of the vector shuffle.
Benjamin Kramer050db522011-03-26 12:38:19 +00004429static SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
4430 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004431 if (Depth == 6)
4432 return SDValue(); // Limit search depth.
4433
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004434 SDValue V = SDValue(N, 0);
4435 EVT VT = V.getValueType();
4436 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004437
4438 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4439 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
4440 Index = SV->getMaskElt(Index);
4441
4442 if (Index < 0)
4443 return DAG.getUNDEF(VT.getVectorElementType());
4444
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004445 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004446 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004447 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004448 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004449
4450 // Recurse into target specific vector shuffles to find scalars.
4451 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004452 int NumElems = VT.getVectorNumElements();
4453 SmallVector<unsigned, 16> ShuffleMask;
4454 SDValue ImmN;
4455
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004456 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004457 case X86ISD::SHUFPS:
4458 case X86ISD::SHUFPD:
4459 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Topper36e36ac2011-11-29 07:49:05 +00004460 DecodeSHUFPMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4461 ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004462 break;
Craig Topper06cb6802011-11-26 20:47:44 +00004463 case X86ISD::PUNPCKH:
Craig Topper06cb6802011-11-26 20:47:44 +00004464 case X86ISD::UNPCKHP:
Craig Topper3d8c2ce2011-12-06 05:31:16 +00004465 DecodeUNPCKHMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004466 break;
Craig Topper06cb6802011-11-26 20:47:44 +00004467 case X86ISD::PUNPCKL:
Craig Topper06cb6802011-11-26 20:47:44 +00004468 case X86ISD::UNPCKLP:
Craig Topper3d8c2ce2011-12-06 05:31:16 +00004469 DecodeUNPCKLMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004470 break;
4471 case X86ISD::MOVHLPS:
4472 DecodeMOVHLPSMask(NumElems, ShuffleMask);
4473 break;
4474 case X86ISD::MOVLHPS:
4475 DecodeMOVLHPSMask(NumElems, ShuffleMask);
4476 break;
4477 case X86ISD::PSHUFD:
4478 ImmN = N->getOperand(N->getNumOperands()-1);
4479 DecodePSHUFMask(NumElems,
4480 cast<ConstantSDNode>(ImmN)->getZExtValue(),
4481 ShuffleMask);
4482 break;
4483 case X86ISD::PSHUFHW:
4484 ImmN = N->getOperand(N->getNumOperands()-1);
4485 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4486 ShuffleMask);
4487 break;
4488 case X86ISD::PSHUFLW:
4489 ImmN = N->getOperand(N->getNumOperands()-1);
4490 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4491 ShuffleMask);
4492 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004493 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004494 case X86ISD::MOVSD: {
4495 // The index 0 always comes from the first element of the second source,
4496 // this is why MOVSS and MOVSD are used in the first place. The other
4497 // elements come from the other positions of the first source vector.
4498 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004499 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
4500 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004501 }
Craig Topper316cd2a2011-11-30 06:25:25 +00004502 case X86ISD::VPERMILP:
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00004503 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Topper316cd2a2011-11-30 06:25:25 +00004504 DecodeVPERMILPMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(),
Bruno Cardoso Lopes2eb4c2b2011-07-29 01:31:11 +00004505 ShuffleMask);
4506 break;
Craig Topperec24e612011-11-30 07:47:51 +00004507 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00004508 ImmN = N->getOperand(N->getNumOperands()-1);
4509 DecodeVPERM2F128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4510 ShuffleMask);
4511 break;
Eli Friedman106f6e72011-09-10 02:01:42 +00004512 case X86ISD::MOVDDUP:
4513 case X86ISD::MOVLHPD:
4514 case X86ISD::MOVLPD:
4515 case X86ISD::MOVLPS:
4516 case X86ISD::MOVSHDUP:
4517 case X86ISD::MOVSLDUP:
4518 case X86ISD::PALIGN:
4519 return SDValue(); // Not yet implemented.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004520 default:
Eli Friedman106f6e72011-09-10 02:01:42 +00004521 assert(0 && "unknown target shuffle node");
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004522 return SDValue();
4523 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004524
4525 Index = ShuffleMask[Index];
4526 if (Index < 0)
4527 return DAG.getUNDEF(VT.getVectorElementType());
4528
4529 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
4530 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
4531 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004532 }
4533
4534 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004535 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004536 V = V.getOperand(0);
4537 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004538 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004539
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004540 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004541 return SDValue();
4542 }
4543
4544 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4545 return (Index == 0) ? V.getOperand(0)
4546 : DAG.getUNDEF(VT.getVectorElementType());
4547
4548 if (V.getOpcode() == ISD::BUILD_VECTOR)
4549 return V.getOperand(Index);
4550
4551 return SDValue();
4552}
4553
4554/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4555/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004556/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004557static
4558unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
4559 bool ZerosFromLeft, SelectionDAG &DAG) {
4560 int i = 0;
4561
4562 while (i < NumElems) {
4563 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004564 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004565 if (!(Elt.getNode() &&
4566 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4567 break;
4568 ++i;
4569 }
4570
4571 return i;
4572}
4573
4574/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
4575/// MaskE correspond consecutively to elements from one of the vector operands,
4576/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4577static
4578bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
4579 int OpIdx, int NumElems, unsigned &OpNum) {
4580 bool SeenV1 = false;
4581 bool SeenV2 = false;
4582
4583 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
4584 int Idx = SVOp->getMaskElt(i);
4585 // Ignore undef indicies
4586 if (Idx < 0)
4587 continue;
4588
4589 if (Idx < NumElems)
4590 SeenV1 = true;
4591 else
4592 SeenV2 = true;
4593
4594 // Only accept consecutive elements from the same vector
4595 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4596 return false;
4597 }
4598
4599 OpNum = SeenV1 ? 0 : 1;
4600 return true;
4601}
4602
4603/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4604/// logical left shift of a vector.
4605static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4606 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4607 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4608 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4609 false /* check zeros from right */, DAG);
4610 unsigned OpSrc;
4611
4612 if (!NumZeros)
4613 return false;
4614
4615 // Considering the elements in the mask that are not consecutive zeros,
4616 // check if they consecutively come from only one of the source vectors.
4617 //
4618 // V1 = {X, A, B, C} 0
4619 // \ \ \ /
4620 // vector_shuffle V1, V2 <1, 2, 3, X>
4621 //
4622 if (!isShuffleMaskConsecutive(SVOp,
4623 0, // Mask Start Index
4624 NumElems-NumZeros-1, // Mask End Index
4625 NumZeros, // Where to start looking in the src vector
4626 NumElems, // Number of elements in vector
4627 OpSrc)) // Which source operand ?
4628 return false;
4629
4630 isLeft = false;
4631 ShAmt = NumZeros;
4632 ShVal = SVOp->getOperand(OpSrc);
4633 return true;
4634}
4635
4636/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4637/// logical left shift of a vector.
4638static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4639 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4640 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4641 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4642 true /* check zeros from left */, DAG);
4643 unsigned OpSrc;
4644
4645 if (!NumZeros)
4646 return false;
4647
4648 // Considering the elements in the mask that are not consecutive zeros,
4649 // check if they consecutively come from only one of the source vectors.
4650 //
4651 // 0 { A, B, X, X } = V2
4652 // / \ / /
4653 // vector_shuffle V1, V2 <X, X, 4, 5>
4654 //
4655 if (!isShuffleMaskConsecutive(SVOp,
4656 NumZeros, // Mask Start Index
4657 NumElems-1, // Mask End Index
4658 0, // Where to start looking in the src vector
4659 NumElems, // Number of elements in vector
4660 OpSrc)) // Which source operand ?
4661 return false;
4662
4663 isLeft = true;
4664 ShAmt = NumZeros;
4665 ShVal = SVOp->getOperand(OpSrc);
4666 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004667}
4668
4669/// isVectorShift - Returns true if the shuffle can be implemented as a
4670/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004671static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004672 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004673 // Although the logic below support any bitwidth size, there are no
4674 // shift instructions which handle more than 128-bit vectors.
4675 if (SVOp->getValueType(0).getSizeInBits() > 128)
4676 return false;
4677
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004678 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4679 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4680 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004681
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004682 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004683}
4684
Evan Chengc78d3b42006-04-24 18:01:45 +00004685/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4686///
Dan Gohman475871a2008-07-27 21:46:04 +00004687static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004688 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004689 SelectionDAG &DAG,
4690 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004691 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004692 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004693
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004694 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004695 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004696 bool First = true;
4697 for (unsigned i = 0; i < 16; ++i) {
4698 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4699 if (ThisIsNonZero && First) {
4700 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004701 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004702 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004703 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004704 First = false;
4705 }
4706
4707 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004708 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004709 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4710 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004711 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004712 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004713 }
4714 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004715 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4716 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4717 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004718 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004719 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004720 } else
4721 ThisElt = LastElt;
4722
Gabor Greifba36cb52008-08-28 21:40:38 +00004723 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004724 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004725 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004726 }
4727 }
4728
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004729 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004730}
4731
Bill Wendlinga348c562007-03-22 18:42:45 +00004732/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004733///
Dan Gohman475871a2008-07-27 21:46:04 +00004734static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004735 unsigned NumNonZero, unsigned NumZero,
4736 SelectionDAG &DAG,
4737 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004738 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004739 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004740
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004741 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004742 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004743 bool First = true;
4744 for (unsigned i = 0; i < 8; ++i) {
4745 bool isNonZero = (NonZeros & (1 << i)) != 0;
4746 if (isNonZero) {
4747 if (First) {
4748 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004749 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004750 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004751 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004752 First = false;
4753 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004754 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004755 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004756 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004757 }
4758 }
4759
4760 return V;
4761}
4762
Evan Chengf26ffe92008-05-29 08:22:04 +00004763/// getVShift - Return a vector logical shift node.
4764///
Owen Andersone50ed302009-08-10 22:56:29 +00004765static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004766 unsigned NumBits, SelectionDAG &DAG,
4767 const TargetLowering &TLI, DebugLoc dl) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004768 assert(VT.getSizeInBits() == 128 && "Unknown type for VShift");
Dale Johannesen0488fb62010-09-30 23:57:10 +00004769 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00004770 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004771 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4772 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004773 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004774 DAG.getConstant(NumBits,
4775 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004776}
4777
Dan Gohman475871a2008-07-27 21:46:04 +00004778SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004779X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004780 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004781
Evan Chengc3630942009-12-09 21:00:30 +00004782 // Check if the scalar load can be widened into a vector load. And if
4783 // the address is "base + cst" see if the cst can be "absorbed" into
4784 // the shuffle mask.
4785 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4786 SDValue Ptr = LD->getBasePtr();
4787 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4788 return SDValue();
4789 EVT PVT = LD->getValueType(0);
4790 if (PVT != MVT::i32 && PVT != MVT::f32)
4791 return SDValue();
4792
4793 int FI = -1;
4794 int64_t Offset = 0;
4795 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4796 FI = FINode->getIndex();
4797 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004798 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004799 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4800 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4801 Offset = Ptr.getConstantOperandVal(1);
4802 Ptr = Ptr.getOperand(0);
4803 } else {
4804 return SDValue();
4805 }
4806
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004807 // FIXME: 256-bit vector instructions don't require a strict alignment,
4808 // improve this code to support it better.
4809 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00004810 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004811 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00004812 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004813 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00004814 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004815 // Can't change the alignment. FIXME: It's possible to compute
4816 // the exact stack offset and reference FI + adjust offset instead.
4817 // If someone *really* cares about this. That's the way to implement it.
4818 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004819 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004820 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00004821 }
4822 }
4823
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004824 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00004825 // Ptr + (Offset & ~15).
4826 if (Offset < 0)
4827 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004828 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00004829 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004830 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00004831 if (StartOffset)
4832 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4833 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4834
4835 int EltNo = (Offset - StartOffset) >> 2;
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004836 int NumElems = VT.getVectorNumElements();
4837
4838 EVT CanonVT = VT.getSizeInBits() == 128 ? MVT::v4i32 : MVT::v8i32;
4839 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
4840 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00004841 LD->getPointerInfo().getWithOffset(StartOffset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004842 false, false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004843
4844 // Canonicalize it to a v4i32 or v8i32 shuffle.
4845 SmallVector<int, 8> Mask;
4846 for (int i = 0; i < NumElems; ++i)
4847 Mask.push_back(EltNo);
4848
4849 V1 = DAG.getNode(ISD::BITCAST, dl, CanonVT, V1);
4850 return DAG.getNode(ISD::BITCAST, dl, NVT,
4851 DAG.getVectorShuffle(CanonVT, dl, V1,
4852 DAG.getUNDEF(CanonVT),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00004853 }
4854
4855 return SDValue();
4856}
4857
Michael J. Spencerec38de22010-10-10 22:04:20 +00004858/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4859/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004860/// load which has the same value as a build_vector whose operands are 'elts'.
4861///
4862/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004863///
Nate Begeman1449f292010-03-24 22:19:06 +00004864/// FIXME: we'd also like to handle the case where the last elements are zero
4865/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4866/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004867static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004868 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004869 EVT EltVT = VT.getVectorElementType();
4870 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004871
Nate Begemanfdea31a2010-03-24 20:49:50 +00004872 LoadSDNode *LDBase = NULL;
4873 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004874
Nate Begeman1449f292010-03-24 22:19:06 +00004875 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004876 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004877 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004878 for (unsigned i = 0; i < NumElems; ++i) {
4879 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004880
Nate Begemanfdea31a2010-03-24 20:49:50 +00004881 if (!Elt.getNode() ||
4882 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4883 return SDValue();
4884 if (!LDBase) {
4885 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4886 return SDValue();
4887 LDBase = cast<LoadSDNode>(Elt.getNode());
4888 LastLoadedElt = i;
4889 continue;
4890 }
4891 if (Elt.getOpcode() == ISD::UNDEF)
4892 continue;
4893
4894 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4895 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4896 return SDValue();
4897 LastLoadedElt = i;
4898 }
Nate Begeman1449f292010-03-24 22:19:06 +00004899
4900 // If we have found an entire vector of loads and undefs, then return a large
4901 // load of the entire vector width starting at the base pointer. If we found
4902 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004903 if (LastLoadedElt == NumElems - 1) {
4904 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00004905 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004906 LDBase->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004907 LDBase->isVolatile(), LDBase->isNonTemporal(),
4908 LDBase->isInvariant(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00004909 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004910 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004911 LDBase->isVolatile(), LDBase->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004912 LDBase->isInvariant(), LDBase->getAlignment());
Eli Friedman61cc47e2011-07-26 21:02:58 +00004913 } else if (NumElems == 4 && LastLoadedElt == 1 &&
4914 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004915 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
4916 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Eli Friedman322ea082011-09-14 23:42:45 +00004917 SDValue ResNode =
4918 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys, Ops, 2, MVT::i64,
4919 LDBase->getPointerInfo(),
4920 LDBase->getAlignment(),
4921 false/*isVolatile*/, true/*ReadMem*/,
4922 false/*WriteMem*/);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004923 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00004924 }
4925 return SDValue();
4926}
4927
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004928/// isVectorBroadcast - Check if the node chain is suitable to be xformed to
4929/// a vbroadcast node. We support two patterns:
4930/// 1. A splat BUILD_VECTOR which uses a single scalar load.
4931/// 2. A splat shuffle which uses a scalar_to_vector node which comes from
4932/// a scalar load.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004933/// The scalar load node is returned when a pattern is found,
4934/// or SDValue() otherwise.
4935static SDValue isVectorBroadcast(SDValue &Op, bool hasAVX2) {
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004936 EVT VT = Op.getValueType();
4937 SDValue V = Op;
4938
4939 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
4940 V = V.getOperand(0);
4941
4942 //A suspected load to be broadcasted.
4943 SDValue Ld;
4944
4945 switch (V.getOpcode()) {
4946 default:
4947 // Unknown pattern found.
4948 return SDValue();
4949
4950 case ISD::BUILD_VECTOR: {
4951 // The BUILD_VECTOR node must be a splat.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004952 if (!isSplatVector(V.getNode()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004953 return SDValue();
4954
4955 Ld = V.getOperand(0);
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004956
4957 // The suspected load node has several users. Make sure that all
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004958 // of its users are from the BUILD_VECTOR node.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004959 if (!Ld->hasNUsesOfValue(VT.getVectorNumElements(), 0))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004960 return SDValue();
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004961 break;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004962 }
4963
4964 case ISD::VECTOR_SHUFFLE: {
4965 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
4966
4967 // Shuffles must have a splat mask where the first element is
4968 // broadcasted.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004969 if ((!SVOp->isSplat()) || SVOp->getMaskElt(0) != 0)
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004970 return SDValue();
4971
4972 SDValue Sc = Op.getOperand(0);
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004973 if (Sc.getOpcode() != ISD::SCALAR_TO_VECTOR)
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004974 return SDValue();
4975
4976 Ld = Sc.getOperand(0);
4977
4978 // The scalar_to_vector node and the suspected
4979 // load node must have exactly one user.
4980 if (!Sc.hasOneUse() || !Ld.hasOneUse())
4981 return SDValue();
4982 break;
4983 }
4984 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004985
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004986 // The scalar source must be a normal load.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004987 if (!ISD::isNormalLoad(Ld.getNode()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004988 return SDValue();
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004989
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004990 bool Is256 = VT.getSizeInBits() == 256;
4991 bool Is128 = VT.getSizeInBits() == 128;
4992 unsigned ScalarSize = Ld.getValueType().getSizeInBits();
4993
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004994 if (hasAVX2) {
4995 // VBroadcast to YMM
4996 if (Is256 && (ScalarSize == 8 || ScalarSize == 16 ||
4997 ScalarSize == 32 || ScalarSize == 64 ))
4998 return Ld;
4999
5000 // VBroadcast to XMM
5001 if (Is128 && (ScalarSize == 8 || ScalarSize == 32 ||
5002 ScalarSize == 16 || ScalarSize == 64 ))
5003 return Ld;
5004 }
5005
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005006 // VBroadcast to YMM
5007 if (Is256 && (ScalarSize == 32 || ScalarSize == 64))
5008 return Ld;
5009
5010 // VBroadcast to XMM
5011 if (Is128 && (ScalarSize == 32))
5012 return Ld;
5013
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005014
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005015 // Unsupported broadcast.
5016 return SDValue();
5017}
5018
Evan Chengc3630942009-12-09 21:00:30 +00005019SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005020X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005021 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00005022
David Greenef125a292011-02-08 19:04:41 +00005023 EVT VT = Op.getValueType();
5024 EVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00005025 unsigned NumElems = Op.getNumOperands();
5026
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005027 // Vectors containing all zeros can be matched by pxor and xorps later
5028 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5029 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
5030 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00005031 if (Op.getValueType() == MVT::v4i32 ||
5032 Op.getValueType() == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00005033 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005034
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005035 return getZeroVector(Op.getValueType(), Subtarget->hasXMMInt(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00005036 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005037
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005038 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
Craig Topper745a86b2011-11-19 22:34:59 +00005039 // vectors or broken into v4i32 operations on 256-bit vectors. AVX2 can use
5040 // vpcmpeqd on 256-bit vectors.
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005041 if (ISD::isBuildVectorAllOnes(Op.getNode())) {
Craig Topper745a86b2011-11-19 22:34:59 +00005042 if (Op.getValueType() == MVT::v4i32 ||
5043 (Op.getValueType() == MVT::v8i32 && Subtarget->hasAVX2()))
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005044 return Op;
5045
Craig Topper745a86b2011-11-19 22:34:59 +00005046 return getOnesVector(Op.getValueType(), Subtarget->hasAVX2(), DAG, dl);
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005047 }
5048
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005049 SDValue LD = isVectorBroadcast(Op, Subtarget->hasAVX2());
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005050 if (Subtarget->hasAVX() && LD.getNode())
5051 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, LD);
5052
Owen Andersone50ed302009-08-10 22:56:29 +00005053 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005054
Evan Cheng0db9fe62006-04-25 20:13:52 +00005055 unsigned NumZero = 0;
5056 unsigned NumNonZero = 0;
5057 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005058 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005059 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005060 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005061 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00005062 if (Elt.getOpcode() == ISD::UNDEF)
5063 continue;
5064 Values.insert(Elt);
5065 if (Elt.getOpcode() != ISD::Constant &&
5066 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005067 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00005068 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00005069 NumZero++;
5070 else {
5071 NonZeros |= (1 << i);
5072 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005073 }
5074 }
5075
Chris Lattner97a2a562010-08-26 05:24:29 +00005076 // All undef vector. Return an UNDEF. All zero vectors were handled above.
5077 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00005078 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005079
Chris Lattner67f453a2008-03-09 05:42:06 +00005080 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00005081 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005082 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00005083 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005084
Chris Lattner62098042008-03-09 01:05:04 +00005085 // If this is an insertion of an i64 value on x86-32, and if the top bits of
5086 // the value are obviously zero, truncate the value to i32 and do the
5087 // insertion that way. Only do this if the value is non-constant or if the
5088 // value is a constant being inserted into element 0. It is cheaper to do
5089 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00005090 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00005091 (!IsAllConstants || Idx == 0)) {
5092 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00005093 // Handle SSE only.
5094 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
5095 EVT VecVT = MVT::v4i32;
5096 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00005097
Chris Lattner62098042008-03-09 01:05:04 +00005098 // Truncate the value (which may itself be a constant) to i32, and
5099 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00005100 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00005101 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00005102 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005103 Subtarget->hasXMMInt(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005104
Chris Lattner62098042008-03-09 01:05:04 +00005105 // Now we have our 32-bit value zero extended in the low element of
5106 // a vector. If Idx != 0, swizzle it into place.
5107 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005108 SmallVector<int, 4> Mask;
5109 Mask.push_back(Idx);
5110 for (unsigned i = 1; i != VecElts; ++i)
5111 Mask.push_back(i);
5112 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00005113 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00005114 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00005115 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005116 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00005117 }
5118 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005119
Chris Lattner19f79692008-03-08 22:59:52 +00005120 // If we have a constant or non-constant insertion into the low element of
5121 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5122 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00005123 // depending on what the source datatype is.
5124 if (Idx == 0) {
5125 if (NumZero == 0) {
5126 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00005127 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
5128 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00005129 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5130 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005131 return getShuffleVectorZeroOrUndef(Item, 0, true,Subtarget->hasXMMInt(),
Eli Friedman10415532009-06-06 06:05:10 +00005132 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00005133 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
5134 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005135 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
5136 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00005137 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
5138 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005139 Subtarget->hasXMMInt(), DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005140 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00005141 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005142 }
Evan Chengf26ffe92008-05-29 08:22:04 +00005143
5144 // Is it a vector logical left shift?
5145 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00005146 X86::isZeroNode(Op.getOperand(0)) &&
5147 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005148 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00005149 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00005150 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005151 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00005152 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005153 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005154
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005155 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00005156 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005157
Chris Lattner19f79692008-03-08 22:59:52 +00005158 // Otherwise, if this is a vector with i32 or f32 elements, and the element
5159 // is a non-constant being inserted into an element other than the low one,
5160 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
5161 // movd/movss) to move this into the low element, then shuffle it into
5162 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005163 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00005164 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00005165
Evan Cheng0db9fe62006-04-25 20:13:52 +00005166 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00005167 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005168 Subtarget->hasXMMInt(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00005169 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005170 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00005171 MaskVec.push_back(i == Idx ? 0 : 1);
5172 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005173 }
5174 }
5175
Chris Lattner67f453a2008-03-09 05:42:06 +00005176 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00005177 if (Values.size() == 1) {
5178 if (EVTBits == 32) {
5179 // Instead of a shuffle like this:
5180 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5181 // Check if it's possible to issue this instead.
5182 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
5183 unsigned Idx = CountTrailingZeros_32(NonZeros);
5184 SDValue Item = Op.getOperand(Idx);
5185 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5186 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5187 }
Dan Gohman475871a2008-07-27 21:46:04 +00005188 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005189 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005190
Dan Gohmana3941172007-07-24 22:55:08 +00005191 // A vector full of immediates; various special cases are already
5192 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005193 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00005194 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00005195
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005196 // For AVX-length vectors, build the individual 128-bit pieces and use
5197 // shuffles to put them in place.
5198 if (VT.getSizeInBits() == 256 && !ISD::isBuildVectorAllZeros(Op.getNode())) {
5199 SmallVector<SDValue, 32> V;
5200 for (unsigned i = 0; i < NumElems; ++i)
5201 V.push_back(Op.getOperand(i));
5202
5203 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
5204
5205 // Build both the lower and upper subvector.
5206 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
5207 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
5208 NumElems/2);
5209
5210 // Recreate the wider vector with the lower and upper part.
Bruno Cardoso Lopes15d03fb2011-07-28 01:26:53 +00005211 SDValue Vec = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), Lower,
5212 DAG.getConstant(0, MVT::i32), DAG, dl);
5213 return Insert128BitVector(Vec, Upper, DAG.getConstant(NumElems/2, MVT::i32),
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005214 DAG, dl);
5215 }
5216
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005217 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005218 if (EVTBits == 64) {
5219 if (NumNonZero == 1) {
5220 // One half is zero or undef.
5221 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005222 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005223 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00005224 return getShuffleVectorZeroOrUndef(V2, Idx, true,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005225 Subtarget->hasXMMInt(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005226 }
Dan Gohman475871a2008-07-27 21:46:04 +00005227 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005228 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005229
5230 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005231 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005232 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00005233 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005234 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005235 }
5236
Bill Wendling826f36f2007-03-28 00:57:11 +00005237 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005238 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00005239 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005240 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005241 }
5242
5243 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00005244 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00005245 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005246 if (NumElems == 4 && NumZero > 0) {
5247 for (unsigned i = 0; i < 4; ++i) {
5248 bool isZero = !(NonZeros & (1 << i));
5249 if (isZero)
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005250 V[i] = getZeroVector(VT, Subtarget->hasXMMInt(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005251 else
Dale Johannesenace16102009-02-03 19:33:06 +00005252 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005253 }
5254
5255 for (unsigned i = 0; i < 2; ++i) {
5256 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5257 default: break;
5258 case 0:
5259 V[i] = V[i*2]; // Must be a zero vector.
5260 break;
5261 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005262 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005263 break;
5264 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005265 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005266 break;
5267 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005268 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005269 break;
5270 }
5271 }
5272
Nate Begeman9008ca62009-04-27 18:41:29 +00005273 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005274 bool Reverse = (NonZeros & 0x3) == 2;
5275 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005276 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005277 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5278 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005279 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
5280 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005281 }
5282
Nate Begemanfdea31a2010-03-24 20:49:50 +00005283 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
5284 // Check for a build vector of consecutive loads.
5285 for (unsigned i = 0; i < NumElems; ++i)
5286 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005287
Nate Begemanfdea31a2010-03-24 20:49:50 +00005288 // Check for elements which are consecutive loads.
5289 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5290 if (LD.getNode())
5291 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005292
5293 // For SSE 4.1, use insertps to put the high elements into the low element.
Craig Topperc0d82852011-11-22 00:44:41 +00005294 if (getSubtarget()->hasSSE41orAVX()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005295 SDValue Result;
5296 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5297 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5298 else
5299 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005300
Chris Lattner24faf612010-08-28 17:59:08 +00005301 for (unsigned i = 1; i < NumElems; ++i) {
5302 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5303 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005304 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005305 }
5306 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005307 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005308
Chris Lattner6e80e442010-08-28 17:15:43 +00005309 // Otherwise, expand into a number of unpckl*, start by extending each of
5310 // our (non-undef) elements to the full vector width with the element in the
5311 // bottom slot of the vector (which generates no code for SSE).
5312 for (unsigned i = 0; i < NumElems; ++i) {
5313 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5314 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5315 else
5316 V[i] = DAG.getUNDEF(VT);
5317 }
5318
5319 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005320 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5321 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5322 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005323 unsigned EltStride = NumElems >> 1;
5324 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005325 for (unsigned i = 0; i < EltStride; ++i) {
5326 // If V[i+EltStride] is undef and this is the first round of mixing,
5327 // then it is safe to just drop this shuffle: V[i] is already in the
5328 // right place, the one element (since it's the first round) being
5329 // inserted as undef can be dropped. This isn't safe for successive
5330 // rounds because they will permute elements within both vectors.
5331 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5332 EltStride == NumElems/2)
5333 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005334
Chris Lattner6e80e442010-08-28 17:15:43 +00005335 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005336 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005337 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005338 }
5339 return V[0];
5340 }
Dan Gohman475871a2008-07-27 21:46:04 +00005341 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005342}
5343
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005344// LowerMMXCONCAT_VECTORS - We support concatenate two MMX registers and place
5345// them in a MMX register. This is better than doing a stack convert.
5346static SDValue LowerMMXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005347 DebugLoc dl = Op.getDebugLoc();
5348 EVT ResVT = Op.getValueType();
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005349
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005350 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
5351 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
5352 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005353 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005354 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
5355 InVec = Op.getOperand(1);
5356 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5357 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005358 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005359 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
5360 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
5361 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005362 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005363 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
5364 Mask[0] = 0; Mask[1] = 2;
5365 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
5366 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005367 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005368}
5369
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005370// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5371// to create 256-bit vectors from two other 128-bit ones.
5372static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5373 DebugLoc dl = Op.getDebugLoc();
5374 EVT ResVT = Op.getValueType();
5375
5376 assert(ResVT.getSizeInBits() == 256 && "Value type must be 256-bit wide");
5377
5378 SDValue V1 = Op.getOperand(0);
5379 SDValue V2 = Op.getOperand(1);
5380 unsigned NumElems = ResVT.getVectorNumElements();
5381
5382 SDValue V = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, ResVT), V1,
5383 DAG.getConstant(0, MVT::i32), DAG, dl);
5384 return Insert128BitVector(V, V2, DAG.getConstant(NumElems/2, MVT::i32),
5385 DAG, dl);
5386}
5387
5388SDValue
5389X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005390 EVT ResVT = Op.getValueType();
5391
5392 assert(Op.getNumOperands() == 2);
5393 assert((ResVT.getSizeInBits() == 128 || ResVT.getSizeInBits() == 256) &&
5394 "Unsupported CONCAT_VECTORS for value type");
5395
5396 // We support concatenate two MMX registers and place them in a MMX register.
5397 // This is better than doing a stack convert.
5398 if (ResVT.is128BitVector())
5399 return LowerMMXCONCAT_VECTORS(Op, DAG);
5400
5401 // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
5402 // from two other 128-bit ones.
5403 return LowerAVXCONCAT_VECTORS(Op, DAG);
5404}
5405
Nate Begemanb9a47b82009-02-23 08:49:38 +00005406// v8i16 shuffles - Prefer shuffles in the following order:
5407// 1. [all] pshuflw, pshufhw, optional move
5408// 2. [ssse3] 1 x pshufb
5409// 3. [ssse3] 2 x pshufb + 1 x por
5410// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005411SDValue
5412X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
5413 SelectionDAG &DAG) const {
5414 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00005415 SDValue V1 = SVOp->getOperand(0);
5416 SDValue V2 = SVOp->getOperand(1);
5417 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005418 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00005419
Nate Begemanb9a47b82009-02-23 08:49:38 +00005420 // Determine if more than 1 of the words in each of the low and high quadwords
5421 // of the result come from the same quadword of one of the two inputs. Undef
5422 // mask values count as coming from any quadword, for better codegen.
Benjamin Kramer003fad92011-10-15 13:28:31 +00005423 unsigned LoQuad[] = { 0, 0, 0, 0 };
5424 unsigned HiQuad[] = { 0, 0, 0, 0 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005425 BitVector InputQuads(4);
5426 for (unsigned i = 0; i < 8; ++i) {
Benjamin Kramer003fad92011-10-15 13:28:31 +00005427 unsigned *Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00005428 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005429 MaskVals.push_back(EltIdx);
5430 if (EltIdx < 0) {
5431 ++Quad[0];
5432 ++Quad[1];
5433 ++Quad[2];
5434 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00005435 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005436 }
5437 ++Quad[EltIdx / 4];
5438 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00005439 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005440
Nate Begemanb9a47b82009-02-23 08:49:38 +00005441 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005442 unsigned MaxQuad = 1;
5443 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005444 if (LoQuad[i] > MaxQuad) {
5445 BestLoQuad = i;
5446 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005447 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005448 }
5449
Nate Begemanb9a47b82009-02-23 08:49:38 +00005450 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005451 MaxQuad = 1;
5452 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005453 if (HiQuad[i] > MaxQuad) {
5454 BestHiQuad = i;
5455 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005456 }
5457 }
5458
Nate Begemanb9a47b82009-02-23 08:49:38 +00005459 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00005460 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00005461 // single pshufb instruction is necessary. If There are more than 2 input
5462 // quads, disable the next transformation since it does not help SSSE3.
5463 bool V1Used = InputQuads[0] || InputQuads[1];
5464 bool V2Used = InputQuads[2] || InputQuads[3];
Craig Topperc0d82852011-11-22 00:44:41 +00005465 if (Subtarget->hasSSSE3orAVX()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005466 if (InputQuads.count() == 2 && V1Used && V2Used) {
5467 BestLoQuad = InputQuads.find_first();
5468 BestHiQuad = InputQuads.find_next(BestLoQuad);
5469 }
5470 if (InputQuads.count() > 2) {
5471 BestLoQuad = -1;
5472 BestHiQuad = -1;
5473 }
5474 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005475
Nate Begemanb9a47b82009-02-23 08:49:38 +00005476 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5477 // the shuffle mask. If a quad is scored as -1, that means that it contains
5478 // words from all 4 input quadwords.
5479 SDValue NewV;
5480 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005481 SmallVector<int, 8> MaskV;
5482 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
5483 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00005484 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005485 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
5486 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
5487 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005488
Nate Begemanb9a47b82009-02-23 08:49:38 +00005489 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5490 // source words for the shuffle, to aid later transformations.
5491 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00005492 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00005493 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005494 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00005495 if (idx != (int)i)
5496 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005497 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00005498 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005499 AllWordsInNewV = false;
5500 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00005501 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005502
Nate Begemanb9a47b82009-02-23 08:49:38 +00005503 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5504 if (AllWordsInNewV) {
5505 for (int i = 0; i != 8; ++i) {
5506 int idx = MaskVals[i];
5507 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005508 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005509 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005510 if ((idx != i) && idx < 4)
5511 pshufhw = false;
5512 if ((idx != i) && idx > 3)
5513 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00005514 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005515 V1 = NewV;
5516 V2Used = false;
5517 BestLoQuad = 0;
5518 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005519 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005520
Nate Begemanb9a47b82009-02-23 08:49:38 +00005521 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5522 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005523 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005524 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5525 unsigned TargetMask = 0;
5526 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005527 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005528 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
5529 X86::getShufflePSHUFLWImmediate(NewV.getNode());
5530 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005531 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005532 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005533 }
Eric Christopherfd179292009-08-27 18:07:15 +00005534
Nate Begemanb9a47b82009-02-23 08:49:38 +00005535 // If we have SSSE3, and all words of the result are from 1 input vector,
5536 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5537 // is present, fall back to case 4.
Craig Topperc0d82852011-11-22 00:44:41 +00005538 if (Subtarget->hasSSSE3orAVX()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005539 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005540
Nate Begemanb9a47b82009-02-23 08:49:38 +00005541 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005542 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005543 // mask, and elements that come from V1 in the V2 mask, so that the two
5544 // results can be OR'd together.
5545 bool TwoInputs = V1Used && V2Used;
5546 for (unsigned i = 0; i != 8; ++i) {
5547 int EltIdx = MaskVals[i] * 2;
5548 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005549 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5550 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005551 continue;
5552 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005553 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
5554 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005555 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005556 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005557 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005558 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005559 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005560 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005561 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005562
Nate Begemanb9a47b82009-02-23 08:49:38 +00005563 // Calculate the shuffle mask for the second input, shuffle it, and
5564 // OR it with the first shuffled input.
5565 pshufbMask.clear();
5566 for (unsigned i = 0; i != 8; ++i) {
5567 int EltIdx = MaskVals[i] * 2;
5568 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005569 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5570 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005571 continue;
5572 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005573 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
5574 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005575 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005576 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005577 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005578 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005579 MVT::v16i8, &pshufbMask[0], 16));
5580 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005581 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005582 }
5583
5584 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5585 // and update MaskVals with new element order.
5586 BitVector InOrder(8);
5587 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005588 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005589 for (int i = 0; i != 4; ++i) {
5590 int idx = MaskVals[i];
5591 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005592 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005593 InOrder.set(i);
5594 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005595 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005596 InOrder.set(i);
5597 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005598 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005599 }
5600 }
5601 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005602 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00005603 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005604 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005605
Craig Topperc0d82852011-11-22 00:44:41 +00005606 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3orAVX())
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005607 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
5608 NewV.getOperand(0),
5609 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
5610 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005611 }
Eric Christopherfd179292009-08-27 18:07:15 +00005612
Nate Begemanb9a47b82009-02-23 08:49:38 +00005613 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5614 // and update MaskVals with the new element order.
5615 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005616 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005617 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005618 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005619 for (unsigned i = 4; i != 8; ++i) {
5620 int idx = MaskVals[i];
5621 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005622 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005623 InOrder.set(i);
5624 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005625 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005626 InOrder.set(i);
5627 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005628 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005629 }
5630 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005631 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005632 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005633
Craig Topperc0d82852011-11-22 00:44:41 +00005634 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3orAVX())
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005635 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
5636 NewV.getOperand(0),
5637 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
5638 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005639 }
Eric Christopherfd179292009-08-27 18:07:15 +00005640
Nate Begemanb9a47b82009-02-23 08:49:38 +00005641 // In case BestHi & BestLo were both -1, which means each quadword has a word
5642 // from each of the four input quadwords, calculate the InOrder bitvector now
5643 // before falling through to the insert/extract cleanup.
5644 if (BestLoQuad == -1 && BestHiQuad == -1) {
5645 NewV = V1;
5646 for (int i = 0; i != 8; ++i)
5647 if (MaskVals[i] < 0 || MaskVals[i] == i)
5648 InOrder.set(i);
5649 }
Eric Christopherfd179292009-08-27 18:07:15 +00005650
Nate Begemanb9a47b82009-02-23 08:49:38 +00005651 // The other elements are put in the right place using pextrw and pinsrw.
5652 for (unsigned i = 0; i != 8; ++i) {
5653 if (InOrder[i])
5654 continue;
5655 int EltIdx = MaskVals[i];
5656 if (EltIdx < 0)
5657 continue;
5658 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00005659 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005660 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00005661 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005662 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005663 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005664 DAG.getIntPtrConstant(i));
5665 }
5666 return NewV;
5667}
5668
5669// v16i8 shuffles - Prefer shuffles in the following order:
5670// 1. [ssse3] 1 x pshufb
5671// 2. [ssse3] 2 x pshufb + 1 x por
5672// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5673static
Nate Begeman9008ca62009-04-27 18:41:29 +00005674SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005675 SelectionDAG &DAG,
5676 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005677 SDValue V1 = SVOp->getOperand(0);
5678 SDValue V2 = SVOp->getOperand(1);
5679 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005680 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00005681 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00005682
Nate Begemanb9a47b82009-02-23 08:49:38 +00005683 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005684 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005685 // present, fall back to case 3.
5686 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
5687 bool V1Only = true;
5688 bool V2Only = true;
5689 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005690 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00005691 if (EltIdx < 0)
5692 continue;
5693 if (EltIdx < 16)
5694 V2Only = false;
5695 else
5696 V1Only = false;
5697 }
Eric Christopherfd179292009-08-27 18:07:15 +00005698
Nate Begemanb9a47b82009-02-23 08:49:38 +00005699 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
Craig Topperc0d82852011-11-22 00:44:41 +00005700 if (TLI.getSubtarget()->hasSSSE3orAVX()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005701 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005702
Nate Begemanb9a47b82009-02-23 08:49:38 +00005703 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005704 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005705 //
5706 // Otherwise, we have elements from both input vectors, and must zero out
5707 // elements that come from V2 in the first mask, and V1 in the second mask
5708 // so that we can OR them together.
5709 bool TwoInputs = !(V1Only || V2Only);
5710 for (unsigned i = 0; i != 16; ++i) {
5711 int EltIdx = MaskVals[i];
5712 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005713 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005714 continue;
5715 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005716 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005717 }
5718 // If all the elements are from V2, assign it to V1 and return after
5719 // building the first pshufb.
5720 if (V2Only)
5721 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00005722 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005723 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005724 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005725 if (!TwoInputs)
5726 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005727
Nate Begemanb9a47b82009-02-23 08:49:38 +00005728 // Calculate the shuffle mask for the second input, shuffle it, and
5729 // OR it with the first shuffled input.
5730 pshufbMask.clear();
5731 for (unsigned i = 0; i != 16; ++i) {
5732 int EltIdx = MaskVals[i];
5733 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005734 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005735 continue;
5736 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005737 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005738 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005739 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005740 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005741 MVT::v16i8, &pshufbMask[0], 16));
5742 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005743 }
Eric Christopherfd179292009-08-27 18:07:15 +00005744
Nate Begemanb9a47b82009-02-23 08:49:38 +00005745 // No SSSE3 - Calculate in place words and then fix all out of place words
5746 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5747 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005748 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5749 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005750 SDValue NewV = V2Only ? V2 : V1;
5751 for (int i = 0; i != 8; ++i) {
5752 int Elt0 = MaskVals[i*2];
5753 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005754
Nate Begemanb9a47b82009-02-23 08:49:38 +00005755 // This word of the result is all undef, skip it.
5756 if (Elt0 < 0 && Elt1 < 0)
5757 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005758
Nate Begemanb9a47b82009-02-23 08:49:38 +00005759 // This word of the result is already in the correct place, skip it.
5760 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
5761 continue;
5762 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
5763 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005764
Nate Begemanb9a47b82009-02-23 08:49:38 +00005765 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5766 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5767 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005768
5769 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5770 // using a single extract together, load it and store it.
5771 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005772 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005773 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005774 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005775 DAG.getIntPtrConstant(i));
5776 continue;
5777 }
5778
Nate Begemanb9a47b82009-02-23 08:49:38 +00005779 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00005780 // source byte is not also odd, shift the extracted word left 8 bits
5781 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005782 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005783 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005784 DAG.getIntPtrConstant(Elt1 / 2));
5785 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005786 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00005787 DAG.getConstant(8,
5788 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005789 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005790 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
5791 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005792 }
5793 // If Elt0 is defined, extract it from the appropriate source. If the
5794 // source byte is not also even, shift the extracted word right 8 bits. If
5795 // Elt1 was also defined, OR the extracted values together before
5796 // inserting them in the result.
5797 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005798 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005799 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
5800 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005801 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00005802 DAG.getConstant(8,
5803 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005804 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005805 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
5806 DAG.getConstant(0x00FF, MVT::i16));
5807 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00005808 : InsElt0;
5809 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005810 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005811 DAG.getIntPtrConstant(i));
5812 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005813 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005814}
5815
Evan Cheng7a831ce2007-12-15 03:00:47 +00005816/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005817/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00005818/// done when every pair / quad of shuffle mask elements point to elements in
5819/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005820/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00005821static
Nate Begeman9008ca62009-04-27 18:41:29 +00005822SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005823 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00005824 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00005825 SDValue V1 = SVOp->getOperand(0);
5826 SDValue V2 = SVOp->getOperand(1);
5827 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00005828 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005829 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00005830 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005831 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005832 case MVT::v4f32: NewVT = MVT::v2f64; break;
5833 case MVT::v4i32: NewVT = MVT::v2i64; break;
5834 case MVT::v8i16: NewVT = MVT::v4i32; break;
5835 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00005836 }
5837
Nate Begeman9008ca62009-04-27 18:41:29 +00005838 int Scale = NumElems / NewWidth;
5839 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00005840 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005841 int StartIdx = -1;
5842 for (int j = 0; j < Scale; ++j) {
5843 int EltIdx = SVOp->getMaskElt(i+j);
5844 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005845 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00005846 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00005847 StartIdx = EltIdx - (EltIdx % Scale);
5848 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00005849 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005850 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005851 if (StartIdx == -1)
5852 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00005853 else
Nate Begeman9008ca62009-04-27 18:41:29 +00005854 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005855 }
5856
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005857 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
5858 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00005859 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005860}
5861
Evan Chengd880b972008-05-09 21:53:03 +00005862/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005863///
Owen Andersone50ed302009-08-10 22:56:29 +00005864static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00005865 SDValue SrcOp, SelectionDAG &DAG,
5866 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005867 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005868 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00005869 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00005870 LD = dyn_cast<LoadSDNode>(SrcOp);
5871 if (!LD) {
5872 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
5873 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00005874 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00005875 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00005876 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005877 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00005878 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005879 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00005880 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005881 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005882 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
5883 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
5884 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00005885 SrcOp.getOperand(0)
5886 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005887 }
5888 }
5889 }
5890
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005891 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005892 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005893 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00005894 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005895}
5896
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00005897/// areShuffleHalvesWithinDisjointLanes - Check whether each half of a vector
5898/// shuffle node referes to only one lane in the sources.
5899static bool areShuffleHalvesWithinDisjointLanes(ShuffleVectorSDNode *SVOp) {
5900 EVT VT = SVOp->getValueType(0);
5901 int NumElems = VT.getVectorNumElements();
5902 int HalfSize = NumElems/2;
5903 SmallVector<int, 16> M;
5904 SVOp->getMask(M);
5905 bool MatchA = false, MatchB = false;
5906
5907 for (int l = 0; l < NumElems*2; l += HalfSize) {
5908 if (isUndefOrInRange(M, 0, HalfSize, l, l+HalfSize)) {
5909 MatchA = true;
5910 break;
5911 }
5912 }
5913
5914 for (int l = 0; l < NumElems*2; l += HalfSize) {
5915 if (isUndefOrInRange(M, HalfSize, HalfSize, l, l+HalfSize)) {
5916 MatchB = true;
5917 break;
5918 }
5919 }
5920
5921 return MatchA && MatchB;
5922}
5923
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00005924/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
5925/// which could not be matched by any known target speficic shuffle
5926static SDValue
5927LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00005928 if (areShuffleHalvesWithinDisjointLanes(SVOp)) {
5929 // If each half of a vector shuffle node referes to only one lane in the
5930 // source vectors, extract each used 128-bit lane and shuffle them using
5931 // 128-bit shuffles. Then, concatenate the results. Otherwise leave
5932 // the work to the legalizer.
5933 DebugLoc dl = SVOp->getDebugLoc();
5934 EVT VT = SVOp->getValueType(0);
5935 int NumElems = VT.getVectorNumElements();
5936 int HalfSize = NumElems/2;
5937
5938 // Extract the reference for each half
5939 int FstVecExtractIdx = 0, SndVecExtractIdx = 0;
5940 int FstVecOpNum = 0, SndVecOpNum = 0;
5941 for (int i = 0; i < HalfSize; ++i) {
5942 int Elt = SVOp->getMaskElt(i);
5943 if (SVOp->getMaskElt(i) < 0)
5944 continue;
5945 FstVecOpNum = Elt/NumElems;
5946 FstVecExtractIdx = Elt % NumElems < HalfSize ? 0 : HalfSize;
5947 break;
5948 }
5949 for (int i = HalfSize; i < NumElems; ++i) {
5950 int Elt = SVOp->getMaskElt(i);
5951 if (SVOp->getMaskElt(i) < 0)
5952 continue;
5953 SndVecOpNum = Elt/NumElems;
5954 SndVecExtractIdx = Elt % NumElems < HalfSize ? 0 : HalfSize;
5955 break;
5956 }
5957
5958 // Extract the subvectors
5959 SDValue V1 = Extract128BitVector(SVOp->getOperand(FstVecOpNum),
5960 DAG.getConstant(FstVecExtractIdx, MVT::i32), DAG, dl);
5961 SDValue V2 = Extract128BitVector(SVOp->getOperand(SndVecOpNum),
5962 DAG.getConstant(SndVecExtractIdx, MVT::i32), DAG, dl);
5963
5964 // Generate 128-bit shuffles
5965 SmallVector<int, 16> MaskV1, MaskV2;
5966 for (int i = 0; i < HalfSize; ++i) {
5967 int Elt = SVOp->getMaskElt(i);
5968 MaskV1.push_back(Elt < 0 ? Elt : Elt % HalfSize);
5969 }
5970 for (int i = HalfSize; i < NumElems; ++i) {
5971 int Elt = SVOp->getMaskElt(i);
5972 MaskV2.push_back(Elt < 0 ? Elt : Elt % HalfSize);
5973 }
5974
5975 EVT NVT = V1.getValueType();
5976 V1 = DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &MaskV1[0]);
5977 V2 = DAG.getVectorShuffle(NVT, dl, V2, DAG.getUNDEF(NVT), &MaskV2[0]);
5978
5979 // Concatenate the result back
5980 SDValue V = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), V1,
5981 DAG.getConstant(0, MVT::i32), DAG, dl);
5982 return Insert128BitVector(V, V2, DAG.getConstant(NumElems/2, MVT::i32),
5983 DAG, dl);
5984 }
5985
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00005986 return SDValue();
5987}
5988
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00005989/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
5990/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00005991static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00005992LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005993 SDValue V1 = SVOp->getOperand(0);
5994 SDValue V2 = SVOp->getOperand(1);
5995 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005996 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00005997
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00005998 assert(VT.getSizeInBits() == 128 && "Unsupported vector size");
5999
Evan Chengace3c172008-07-22 21:13:36 +00006000 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00006001 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00006002 SmallVector<int, 8> Mask1(4U, -1);
6003 SmallVector<int, 8> PermMask;
6004 SVOp->getMask(PermMask);
6005
Evan Chengace3c172008-07-22 21:13:36 +00006006 unsigned NumHi = 0;
6007 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00006008 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006009 int Idx = PermMask[i];
6010 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006011 Locs[i] = std::make_pair(-1, -1);
6012 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006013 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
6014 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006015 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00006016 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006017 NumLo++;
6018 } else {
6019 Locs[i] = std::make_pair(1, NumHi);
6020 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00006021 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006022 NumHi++;
6023 }
6024 }
6025 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006026
Evan Chengace3c172008-07-22 21:13:36 +00006027 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006028 // If no more than two elements come from either vector. This can be
6029 // implemented with two shuffles. First shuffle gather the elements.
6030 // The second shuffle, which takes the first shuffle as both of its
6031 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00006032 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006033
Nate Begeman9008ca62009-04-27 18:41:29 +00006034 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00006035
Evan Chengace3c172008-07-22 21:13:36 +00006036 for (unsigned i = 0; i != 4; ++i) {
6037 if (Locs[i].first == -1)
6038 continue;
6039 else {
6040 unsigned Idx = (i < 2) ? 0 : 4;
6041 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006042 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006043 }
6044 }
6045
Nate Begeman9008ca62009-04-27 18:41:29 +00006046 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006047 } else if (NumLo == 3 || NumHi == 3) {
6048 // Otherwise, we must have three elements from one vector, call it X, and
6049 // one element from the other, call it Y. First, use a shufps to build an
6050 // intermediate vector with the one element from Y and the element from X
6051 // that will be in the same half in the final destination (the indexes don't
6052 // matter). Then, use a shufps to build the final vector, taking the half
6053 // containing the element from Y from the intermediate, and the other half
6054 // from X.
6055 if (NumHi == 3) {
6056 // Normalize it so the 3 elements come from V1.
Craig Topperbeabc6c2011-12-05 06:56:46 +00006057 CommuteVectorShuffleMask(PermMask, 4);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006058 std::swap(V1, V2);
6059 }
6060
6061 // Find the element from V2.
6062 unsigned HiIndex;
6063 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006064 int Val = PermMask[HiIndex];
6065 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006066 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006067 if (Val >= 4)
6068 break;
6069 }
6070
Nate Begeman9008ca62009-04-27 18:41:29 +00006071 Mask1[0] = PermMask[HiIndex];
6072 Mask1[1] = -1;
6073 Mask1[2] = PermMask[HiIndex^1];
6074 Mask1[3] = -1;
6075 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006076
6077 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006078 Mask1[0] = PermMask[0];
6079 Mask1[1] = PermMask[1];
6080 Mask1[2] = HiIndex & 1 ? 6 : 4;
6081 Mask1[3] = HiIndex & 1 ? 4 : 6;
6082 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006083 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006084 Mask1[0] = HiIndex & 1 ? 2 : 0;
6085 Mask1[1] = HiIndex & 1 ? 0 : 2;
6086 Mask1[2] = PermMask[2];
6087 Mask1[3] = PermMask[3];
6088 if (Mask1[2] >= 0)
6089 Mask1[2] += 4;
6090 if (Mask1[3] >= 0)
6091 Mask1[3] += 4;
6092 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006093 }
Evan Chengace3c172008-07-22 21:13:36 +00006094 }
6095
6096 // Break it into (shuffle shuffle_hi, shuffle_lo).
6097 Locs.clear();
David Greenec4db4e52011-02-28 19:06:56 +00006098 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00006099 SmallVector<int,8> LoMask(4U, -1);
6100 SmallVector<int,8> HiMask(4U, -1);
6101
6102 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00006103 unsigned MaskIdx = 0;
6104 unsigned LoIdx = 0;
6105 unsigned HiIdx = 2;
6106 for (unsigned i = 0; i != 4; ++i) {
6107 if (i == 2) {
6108 MaskPtr = &HiMask;
6109 MaskIdx = 1;
6110 LoIdx = 0;
6111 HiIdx = 2;
6112 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006113 int Idx = PermMask[i];
6114 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006115 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00006116 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006117 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00006118 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006119 LoIdx++;
6120 } else {
6121 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00006122 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006123 HiIdx++;
6124 }
6125 }
6126
Nate Begeman9008ca62009-04-27 18:41:29 +00006127 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6128 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
6129 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00006130 for (unsigned i = 0; i != 4; ++i) {
6131 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006132 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00006133 } else {
6134 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006135 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00006136 }
6137 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006138 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006139}
6140
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006141static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006142 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006143 V = V.getOperand(0);
6144 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6145 V = V.getOperand(0);
Evan Cheng7bc389b2011-11-08 00:31:58 +00006146 if (V.hasOneUse() && V.getOpcode() == ISD::BUILD_VECTOR &&
6147 V.getNumOperands() == 2 && V.getOperand(1).getOpcode() == ISD::UNDEF)
6148 // BUILD_VECTOR (load), undef
6149 V = V.getOperand(0);
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006150 if (MayFoldLoad(V))
6151 return true;
6152 return false;
6153}
6154
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006155// FIXME: the version above should always be used. Since there's
6156// a bug where several vector shuffles can't be folded because the
6157// DAG is not updated during lowering and a node claims to have two
6158// uses while it only has one, use this version, and let isel match
6159// another instruction if the load really happens to have more than
6160// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00006161// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006162static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006163 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006164 V = V.getOperand(0);
6165 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6166 V = V.getOperand(0);
6167 if (ISD::isNormalLoad(V.getNode()))
6168 return true;
6169 return false;
6170}
6171
6172/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
6173/// a vector extract, and if both can be later optimized into a single load.
6174/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
6175/// here because otherwise a target specific shuffle node is going to be
6176/// emitted for this shuffle, and the optimization not done.
6177/// FIXME: This is probably not the best approach, but fix the problem
6178/// until the right path is decided.
6179static
6180bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
6181 const TargetLowering &TLI) {
6182 EVT VT = V.getValueType();
6183 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
6184
6185 // Be sure that the vector shuffle is present in a pattern like this:
6186 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
6187 if (!V.hasOneUse())
6188 return false;
6189
6190 SDNode *N = *V.getNode()->use_begin();
6191 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
6192 return false;
6193
6194 SDValue EltNo = N->getOperand(1);
6195 if (!isa<ConstantSDNode>(EltNo))
6196 return false;
6197
6198 // If the bit convert changed the number of elements, it is unsafe
6199 // to examine the mask.
6200 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006201 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006202 EVT SrcVT = V.getOperand(0).getValueType();
6203 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
6204 return false;
6205 V = V.getOperand(0);
6206 HasShuffleIntoBitcast = true;
6207 }
6208
6209 // Select the input vector, guarding against out of range extract vector.
6210 unsigned NumElems = VT.getVectorNumElements();
6211 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
6212 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
6213 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
6214
6215 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006216 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006217 V = V.getOperand(0);
6218
6219 if (ISD::isNormalLoad(V.getNode())) {
6220 // Is the original load suitable?
6221 LoadSDNode *LN0 = cast<LoadSDNode>(V);
6222
6223 // FIXME: avoid the multi-use bug that is preventing lots of
6224 // of foldings to be detected, this is still wrong of course, but
6225 // give the temporary desired behavior, and if it happens that
6226 // the load has real more uses, during isel it will not fold, and
6227 // will generate poor code.
6228 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
6229 return false;
6230
6231 if (!HasShuffleIntoBitcast)
6232 return true;
6233
6234 // If there's a bitcast before the shuffle, check if the load type and
6235 // alignment is valid.
6236 unsigned Align = LN0->getAlignment();
6237 unsigned NewAlign =
6238 TLI.getTargetData()->getABITypeAlignment(
6239 VT.getTypeForEVT(*DAG.getContext()));
6240
6241 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
6242 return false;
6243 }
6244
6245 return true;
6246}
6247
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006248static
Evan Cheng835580f2010-10-07 20:50:20 +00006249SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
6250 EVT VT = Op.getValueType();
6251
6252 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006253 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6254 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006255 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6256 V1, DAG));
6257}
6258
6259static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006260SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006261 bool HasXMMInt) {
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006262 SDValue V1 = Op.getOperand(0);
6263 SDValue V2 = Op.getOperand(1);
6264 EVT VT = Op.getValueType();
6265
6266 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6267
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006268 if (HasXMMInt && VT == MVT::v2f64)
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006269 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6270
Evan Cheng0899f5c2011-08-31 02:05:24 +00006271 // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
6272 return DAG.getNode(ISD::BITCAST, dl, VT,
6273 getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
6274 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
6275 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006276}
6277
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006278static
6279SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
6280 SDValue V1 = Op.getOperand(0);
6281 SDValue V2 = Op.getOperand(1);
6282 EVT VT = Op.getValueType();
6283
6284 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6285 "unsupported shuffle type");
6286
6287 if (V2.getOpcode() == ISD::UNDEF)
6288 V2 = V1;
6289
6290 // v4i32 or v4f32
6291 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6292}
6293
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006294static inline unsigned getSHUFPOpcode(EVT VT) {
6295 switch(VT.getSimpleVT().SimpleTy) {
6296 case MVT::v8i32: // Use fp unit for int unpack.
6297 case MVT::v8f32:
6298 case MVT::v4i32: // Use fp unit for int unpack.
6299 case MVT::v4f32: return X86ISD::SHUFPS;
6300 case MVT::v4i64: // Use fp unit for int unpack.
6301 case MVT::v4f64:
6302 case MVT::v2i64: // Use fp unit for int unpack.
6303 case MVT::v2f64: return X86ISD::SHUFPD;
6304 default:
6305 llvm_unreachable("Unknown type for shufp*");
6306 }
6307 return 0;
6308}
6309
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006310static
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006311SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasXMMInt) {
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006312 SDValue V1 = Op.getOperand(0);
6313 SDValue V2 = Op.getOperand(1);
6314 EVT VT = Op.getValueType();
6315 unsigned NumElems = VT.getVectorNumElements();
6316
6317 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6318 // operand of these instructions is only memory, so check if there's a
6319 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6320 // same masks.
6321 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006322
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006323 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006324 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006325 CanFoldLoad = true;
6326
6327 // When V1 is a load, it can be folded later into a store in isel, example:
6328 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6329 // turns into:
6330 // (MOVLPSmr addr:$src1, VR128:$src2)
6331 // So, recognize this potential and also use MOVLPS or MOVLPD
Evan Cheng7bc389b2011-11-08 00:31:58 +00006332 else if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006333 CanFoldLoad = true;
6334
Dan Gohman65fd6562011-11-03 21:49:52 +00006335 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006336 if (CanFoldLoad) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006337 if (HasXMMInt && NumElems == 2)
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006338 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6339
6340 if (NumElems == 4)
Dan Gohman65fd6562011-11-03 21:49:52 +00006341 // If we don't care about the second element, procede to use movss.
6342 if (SVOp->getMaskElt(1) != -1)
6343 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006344 }
6345
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006346 // movl and movlp will both match v2i64, but v2i64 is never matched by
6347 // movl earlier because we make it strict to avoid messing with the movlp load
6348 // folding logic (see the code above getMOVLP call). Match it here then,
6349 // this is horrible, but will stay like this until we move all shuffle
6350 // matching to x86 specific nodes. Note that for the 1st condition all
6351 // types are matched with movsd.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006352 if (HasXMMInt) {
Bruno Cardoso Lopes5ca0d142011-09-14 02:36:14 +00006353 // FIXME: isMOVLMask should be checked and matched before getMOVLP,
6354 // as to remove this logic from here, as much as possible
6355 if (NumElems == 2 || !X86::isMOVLMask(SVOp))
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006356 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006357 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006358 }
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006359
6360 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6361
6362 // Invert the operand order and use SHUFPS to match it.
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006363 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V2, V1,
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006364 X86::getShuffleSHUFImmediate(SVOp), DAG);
6365}
6366
Craig Topper6347e862011-11-21 06:57:39 +00006367static inline unsigned getUNPCKLOpcode(EVT VT, bool HasAVX2) {
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006368 switch(VT.getSimpleVT().SimpleTy) {
Craig Topper06cb6802011-11-26 20:47:44 +00006369 case MVT::v32i8:
6370 case MVT::v16i8:
6371 case MVT::v16i16:
6372 case MVT::v8i16:
6373 case MVT::v4i32:
6374 case MVT::v2i64: return X86ISD::PUNPCKL;
Craig Topper6347e862011-11-21 06:57:39 +00006375 case MVT::v8i32:
Craig Topper06cb6802011-11-26 20:47:44 +00006376 case MVT::v4i64:
6377 if (HasAVX2) return X86ISD::PUNPCKL;
Craig Topper6347e862011-11-21 06:57:39 +00006378 // else use fp unit for int unpack.
Craig Topper705f2432011-11-24 22:57:10 +00006379 case MVT::v8f32:
Craig Topper06cb6802011-11-26 20:47:44 +00006380 case MVT::v4f32:
Craig Topper705f2432011-11-24 22:57:10 +00006381 case MVT::v4f64:
Craig Topper06cb6802011-11-26 20:47:44 +00006382 case MVT::v2f64: return X86ISD::UNPCKLP;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006383 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00006384 llvm_unreachable("Unknown type for unpckl");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006385 }
6386 return 0;
6387}
6388
Craig Topper6347e862011-11-21 06:57:39 +00006389static inline unsigned getUNPCKHOpcode(EVT VT, bool HasAVX2) {
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006390 switch(VT.getSimpleVT().SimpleTy) {
Craig Topper06cb6802011-11-26 20:47:44 +00006391 case MVT::v32i8:
6392 case MVT::v16i8:
6393 case MVT::v16i16:
6394 case MVT::v8i16:
6395 case MVT::v4i32:
6396 case MVT::v2i64: return X86ISD::PUNPCKH;
6397 case MVT::v4i64:
Craig Topper6347e862011-11-21 06:57:39 +00006398 case MVT::v8i32:
Craig Topper06cb6802011-11-26 20:47:44 +00006399 if (HasAVX2) return X86ISD::PUNPCKH;
Craig Topper6347e862011-11-21 06:57:39 +00006400 // else use fp unit for int unpack.
Craig Topper705f2432011-11-24 22:57:10 +00006401 case MVT::v8f32:
Craig Topper06cb6802011-11-26 20:47:44 +00006402 case MVT::v4f32:
Craig Topper705f2432011-11-24 22:57:10 +00006403 case MVT::v4f64:
Craig Topper06cb6802011-11-26 20:47:44 +00006404 case MVT::v2f64: return X86ISD::UNPCKHP;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006405 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00006406 llvm_unreachable("Unknown type for unpckh");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006407 }
6408 return 0;
6409}
6410
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006411static
6412SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006413 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006414 const X86Subtarget *Subtarget) {
6415 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6416 EVT VT = Op.getValueType();
6417 DebugLoc dl = Op.getDebugLoc();
6418 SDValue V1 = Op.getOperand(0);
6419 SDValue V2 = Op.getOperand(1);
6420
6421 if (isZeroShuffle(SVOp))
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006422 return getZeroVector(VT, Subtarget->hasXMMInt(), DAG, dl);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006423
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006424 // Handle splat operations
6425 if (SVOp->isSplat()) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006426 unsigned NumElem = VT.getVectorNumElements();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006427 int Size = VT.getSizeInBits();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006428 // Special case, this is the only place now where it's allowed to return
6429 // a vector_shuffle operation without using a target specific node, because
6430 // *hopefully* it will be optimized away by the dag combiner. FIXME: should
6431 // this be moved to DAGCombine instead?
6432 if (NumElem <= 4 && CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006433 return Op;
6434
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006435 // Use vbroadcast whenever the splat comes from a foldable load
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00006436 SDValue LD = isVectorBroadcast(Op, Subtarget->hasAVX2());
Nadav Rotemf8c10e52011-11-15 22:50:37 +00006437 if (Subtarget->hasAVX() && LD.getNode())
6438 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, LD);
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006439
Bruno Cardoso Lopes6a32adc2011-07-25 23:05:25 +00006440 // Handle splats by matching through known shuffle masks
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006441 if ((Size == 128 && NumElem <= 4) ||
6442 (Size == 256 && NumElem < 8))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006443 return SDValue();
6444
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00006445 // All remaning splats are promoted to target supported vector shuffles.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006446 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006447 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006448
6449 // If the shuffle can be profitably rewritten as a narrower shuffle, then
6450 // do it!
6451 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
6452 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6453 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006454 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006455 } else if ((VT == MVT::v4i32 ||
6456 (VT == MVT::v4f32 && Subtarget->hasXMMInt()))) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006457 // FIXME: Figure out a cleaner way to do this.
6458 // Try to make use of movq to zero out the top part.
6459 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
6460 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6461 if (NewOp.getNode()) {
6462 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
6463 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
6464 DAG, Subtarget, dl);
6465 }
6466 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
6467 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6468 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
6469 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
6470 DAG, Subtarget, dl);
6471 }
6472 }
6473 return SDValue();
6474}
6475
Dan Gohman475871a2008-07-27 21:46:04 +00006476SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006477X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00006478 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00006479 SDValue V1 = Op.getOperand(0);
6480 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006481 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006482 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006483 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006484 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00006485 bool V1IsSplat = false;
6486 bool V2IsSplat = false;
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006487 bool HasXMMInt = Subtarget->hasXMMInt();
Craig Topperbeabc6c2011-12-05 06:56:46 +00006488 bool HasAVX = Subtarget->hasAVX();
Craig Topper6347e862011-11-21 06:57:39 +00006489 bool HasAVX2 = Subtarget->hasAVX2();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006490 MachineFunction &MF = DAG.getMachineFunction();
6491 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006492
Craig Topper3426a3e2011-11-14 06:46:21 +00006493 assert(VT.getSizeInBits() != 64 && "Can't lower MMX shuffles");
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00006494
Craig Topper38034c52011-11-26 22:55:48 +00006495 assert(V1.getOpcode() != ISD::UNDEF && "Op 1 of shuffle should not be undef");
6496
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006497 // Vector shuffle lowering takes 3 steps:
6498 //
6499 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
6500 // narrowing and commutation of operands should be handled.
6501 // 2) Matching of shuffles with known shuffle masks to x86 target specific
6502 // shuffle nodes.
6503 // 3) Rewriting of unmatched masks into new generic shuffle operations,
6504 // so the shuffle can be broken into other shuffles and the legalizer can
6505 // try the lowering again.
6506 //
Craig Topper3426a3e2011-11-14 06:46:21 +00006507 // The general idea is that no vector_shuffle operation should be left to
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006508 // be matched during isel, all of them must be converted to a target specific
6509 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00006510
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006511 // Normalize the input vectors. Here splats, zeroed vectors, profitable
6512 // narrowing and commutation of operands should be handled. The actual code
6513 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006514 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006515 if (NewOp.getNode())
6516 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00006517
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006518 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
6519 // unpckh_undef). Only use pshufd if speed is more important than size.
6520 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
Craig Topper6347e862011-11-21 06:57:39 +00006521 return getTargetShuffleNode(getUNPCKLOpcode(VT, HasAVX2), dl, VT, V1, V1,
6522 DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006523 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
Craig Topper6347e862011-11-21 06:57:39 +00006524 return getTargetShuffleNode(getUNPCKHOpcode(VT, HasAVX2), dl, VT, V1, V1,
6525 DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00006526
Craig Topperc0d82852011-11-22 00:44:41 +00006527 if (X86::isMOVDDUPMask(SVOp) && Subtarget->hasSSE3orAVX() &&
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006528 V2IsUndef && RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00006529 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006530
Dale Johannesen0488fb62010-09-30 23:57:10 +00006531 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006532 return getMOVHighToLow(Op, dl, DAG);
6533
6534 // Use to match splats
Craig Topperc0d82852011-11-22 00:44:41 +00006535 if (HasXMMInt && X86::isUNPCKHMask(SVOp, HasAVX2) && V2IsUndef &&
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006536 (VT == MVT::v2f64 || VT == MVT::v2i64))
Craig Topper6347e862011-11-21 06:57:39 +00006537 return getTargetShuffleNode(getUNPCKHOpcode(VT, HasAVX2), dl, VT, V1, V1,
6538 DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006539
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006540 if (X86::isPSHUFDMask(SVOp)) {
6541 // The actual implementation will match the mask in the if above and then
6542 // during isel it can match several different instructions, not only pshufd
6543 // as its name says, sad but true, emulate the behavior for now...
6544 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
6545 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
6546
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006547 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
6548
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006549 if (HasXMMInt && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006550 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
6551
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006552 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V1,
6553 TargetMask, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006554 }
Eric Christopherfd179292009-08-27 18:07:15 +00006555
Evan Chengf26ffe92008-05-29 08:22:04 +00006556 // Check if this can be converted into a logical shift.
6557 bool isLeft = false;
6558 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00006559 SDValue ShVal;
Craig Topperc0d82852011-11-22 00:44:41 +00006560 bool isShift = HasXMMInt && isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00006561 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006562 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00006563 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006564 EVT EltVT = VT.getVectorElementType();
6565 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006566 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006567 }
Eric Christopherfd179292009-08-27 18:07:15 +00006568
Nate Begeman9008ca62009-04-27 18:41:29 +00006569 if (X86::isMOVLMask(SVOp)) {
Gabor Greifba36cb52008-08-28 21:40:38 +00006570 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00006571 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00006572 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006573 if (HasXMMInt && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006574 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6575
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006576 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006577 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6578 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00006579 }
Eric Christopherfd179292009-08-27 18:07:15 +00006580
Nate Begeman9008ca62009-04-27 18:41:29 +00006581 // FIXME: fold these into legal mask.
Craig Topperc0d82852011-11-22 00:44:41 +00006582 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp, HasAVX2))
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006583 return getMOVLowToHigh(Op, dl, DAG, HasXMMInt);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006584
Dale Johannesen0488fb62010-09-30 23:57:10 +00006585 if (X86::isMOVHLPSMask(SVOp))
6586 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006587
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00006588 if (X86::isMOVSHDUPMask(SVOp, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006589 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006590
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00006591 if (X86::isMOVSLDUPMask(SVOp, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006592 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00006593
Dale Johannesen0488fb62010-09-30 23:57:10 +00006594 if (X86::isMOVLPMask(SVOp))
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006595 return getMOVLP(Op, dl, DAG, HasXMMInt);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006596
Nate Begeman9008ca62009-04-27 18:41:29 +00006597 if (ShouldXformToMOVHLPS(SVOp) ||
6598 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
6599 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006600
Evan Chengf26ffe92008-05-29 08:22:04 +00006601 if (isShift) {
6602 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006603 EVT EltVT = VT.getVectorElementType();
6604 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006605 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006606 }
Eric Christopherfd179292009-08-27 18:07:15 +00006607
Evan Cheng9eca5e82006-10-25 21:49:50 +00006608 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00006609 // FIXME: This should also accept a bitcast of a splat? Be careful, not
6610 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00006611 V1IsSplat = isSplatVector(V1.getNode());
6612 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006613
Chris Lattner8a594482007-11-25 00:24:49 +00006614 // Canonicalize the splat or undef, if present, to be on the RHS.
Craig Topper38034c52011-11-26 22:55:48 +00006615 if (V1IsSplat && !V2IsSplat) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006616 Op = CommuteVectorShuffle(SVOp, DAG);
6617 SVOp = cast<ShuffleVectorSDNode>(Op);
6618 V1 = SVOp->getOperand(0);
6619 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00006620 std::swap(V1IsSplat, V2IsSplat);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006621 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00006622 }
6623
Craig Topperbeabc6c2011-12-05 06:56:46 +00006624 SmallVector<int, 32> M;
6625 SVOp->getMask(M);
6626
6627 if (isCommutedMOVLMask(M, VT, V2IsSplat, V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006628 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00006629 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00006630 return V1;
6631 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6632 // the instruction selector will not match, so get a canonical MOVL with
6633 // swapped operands to undo the commute.
6634 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006635 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006636
Craig Topperbeabc6c2011-12-05 06:56:46 +00006637 if (isUNPCKLMask(M, VT, HasAVX2))
Craig Topper6347e862011-11-21 06:57:39 +00006638 return getTargetShuffleNode(getUNPCKLOpcode(VT, HasAVX2), dl, VT, V1, V2,
6639 DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006640
Craig Topperbeabc6c2011-12-05 06:56:46 +00006641 if (isUNPCKHMask(M, VT, HasAVX2))
Craig Topper6347e862011-11-21 06:57:39 +00006642 return getTargetShuffleNode(getUNPCKHOpcode(VT, HasAVX2), dl, VT, V1, V2,
6643 DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006644
Evan Cheng9bbbb982006-10-25 20:48:19 +00006645 if (V2IsSplat) {
6646 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006647 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00006648 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00006649 SDValue NewMask = NormalizeMask(SVOp, DAG);
6650 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
6651 if (NSVOp != SVOp) {
Craig Topperc0d82852011-11-22 00:44:41 +00006652 if (X86::isUNPCKLMask(NSVOp, HasAVX2, true)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006653 return NewMask;
Craig Topperc0d82852011-11-22 00:44:41 +00006654 } else if (X86::isUNPCKHMask(NSVOp, HasAVX2, true)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006655 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006656 }
6657 }
6658 }
6659
Evan Cheng9eca5e82006-10-25 21:49:50 +00006660 if (Commuted) {
6661 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006662 // FIXME: this seems wrong.
6663 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
6664 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006665
Craig Topperc0d82852011-11-22 00:44:41 +00006666 if (X86::isUNPCKLMask(NewSVOp, HasAVX2))
Craig Topper6347e862011-11-21 06:57:39 +00006667 return getTargetShuffleNode(getUNPCKLOpcode(VT, HasAVX2), dl, VT, V2, V1,
6668 DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006669
Craig Topperc0d82852011-11-22 00:44:41 +00006670 if (X86::isUNPCKHMask(NewSVOp, HasAVX2))
Craig Topper6347e862011-11-21 06:57:39 +00006671 return getTargetShuffleNode(getUNPCKHOpcode(VT, HasAVX2), dl, VT, V2, V1,
6672 DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006673 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006674
Nate Begeman9008ca62009-04-27 18:41:29 +00006675 // Normalize the node to match x86 shuffle ops if needed
Craig Topper1ff73d72011-12-06 04:59:07 +00006676 if (!V2IsUndef && (isSHUFPMask(M, VT, /* Commuted */ true) ||
6677 isVSHUFPYMask(M, VT, HasAVX, /* Commuted */ true)))
Nate Begeman9008ca62009-04-27 18:41:29 +00006678 return CommuteVectorShuffle(SVOp, DAG);
6679
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006680 // The checks below are all present in isShuffleMaskLegal, but they are
6681 // inlined here right now to enable us to directly emit target specific
6682 // nodes, and remove one by one until they don't return Op anymore.
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006683
Craig Topperc0d82852011-11-22 00:44:41 +00006684 if (isPALIGNRMask(M, VT, Subtarget->hasSSSE3orAVX()))
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006685 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
6686 X86::getShufflePALIGNRImmediate(SVOp),
6687 DAG);
6688
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006689 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6690 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Craig Topperbeabc6c2011-12-05 06:56:46 +00006691 if (VT == MVT::v2f64 || VT == MVT::v2i64)
6692 return getTargetShuffleNode(getUNPCKLOpcode(VT, HasAVX2), dl, VT, V1, V1,
6693 DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006694 }
6695
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006696 if (isPSHUFHWMask(M, VT))
6697 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
6698 X86::getShufflePSHUFHWImmediate(SVOp),
6699 DAG);
6700
6701 if (isPSHUFLWMask(M, VT))
6702 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
6703 X86::getShufflePSHUFLWImmediate(SVOp),
6704 DAG);
6705
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006706 if (isSHUFPMask(M, VT))
6707 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6708 X86::getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00006709
Craig Topperbeabc6c2011-12-05 06:56:46 +00006710 if (isUNPCKL_v_undef_Mask(M, VT))
Craig Topper6347e862011-11-21 06:57:39 +00006711 return getTargetShuffleNode(getUNPCKLOpcode(VT, HasAVX2), dl, VT, V1, V1,
6712 DAG);
Craig Topperbeabc6c2011-12-05 06:56:46 +00006713 if (isUNPCKH_v_undef_Mask(M, VT))
Craig Topper6347e862011-11-21 06:57:39 +00006714 return getTargetShuffleNode(getUNPCKHOpcode(VT, HasAVX2), dl, VT, V1, V1,
6715 DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006716
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006717 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006718 // Generate target specific nodes for 128 or 256-bit shuffles only
6719 // supported in the AVX instruction set.
6720 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006721
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006722 // Handle VMOVDDUPY permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006723 if (V2IsUndef && isMOVDDUPYMask(M, VT, HasAVX))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006724 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
6725
Craig Topper70b883b2011-11-28 10:14:51 +00006726 // Handle VPERMILPS/D* permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006727 if (isVPERMILPMask(M, VT, HasAVX))
Craig Topper316cd2a2011-11-30 06:25:25 +00006728 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1,
Craig Topper70b883b2011-11-28 10:14:51 +00006729 getShuffleVPERMILPImmediate(SVOp), DAG);
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006730
Craig Topper70b883b2011-11-28 10:14:51 +00006731 // Handle VPERM2F128/VPERM2I128 permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006732 if (isVPERM2X128Mask(M, VT, HasAVX))
Craig Topperec24e612011-11-30 07:47:51 +00006733 return getTargetShuffleNode(X86ISD::VPERM2X128, dl, VT, V1,
Craig Topper70b883b2011-11-28 10:14:51 +00006734 V2, getShuffleVPERM2X128Immediate(SVOp), DAG);
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006735
Craig Topper70b883b2011-11-28 10:14:51 +00006736 // Handle VSHUFPS/DY permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006737 if (isVSHUFPYMask(M, VT, HasAVX))
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006738 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
Craig Topper9d7025b2011-11-27 21:41:12 +00006739 getShuffleVSHUFPYImmediate(SVOp), DAG);
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006740
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006741 //===--------------------------------------------------------------------===//
6742 // Since no target specific shuffle was selected for this generic one,
6743 // lower it into other known shuffles. FIXME: this isn't true yet, but
6744 // this is the plan.
6745 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00006746
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006747 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
6748 if (VT == MVT::v8i16) {
6749 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
6750 if (NewOp.getNode())
6751 return NewOp;
6752 }
6753
6754 if (VT == MVT::v16i8) {
6755 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
6756 if (NewOp.getNode())
6757 return NewOp;
6758 }
6759
6760 // Handle all 128-bit wide vectors with 4 elements, and match them with
6761 // several different shuffle types.
6762 if (NumElems == 4 && VT.getSizeInBits() == 128)
6763 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
6764
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006765 // Handle general 256-bit shuffles
6766 if (VT.is256BitVector())
6767 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
6768
Dan Gohman475871a2008-07-27 21:46:04 +00006769 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006770}
6771
Dan Gohman475871a2008-07-27 21:46:04 +00006772SDValue
6773X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00006774 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006775 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006776 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006777
6778 if (Op.getOperand(0).getValueType().getSizeInBits() != 128)
6779 return SDValue();
6780
Duncan Sands83ec4b62008-06-06 12:08:01 +00006781 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006782 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006783 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006784 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006785 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006786 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006787 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00006788 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
6789 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
6790 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006791 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6792 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006793 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006794 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00006795 Op.getOperand(0)),
6796 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006797 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006798 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006799 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006800 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006801 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00006802 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00006803 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
6804 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006805 // result has a single use which is a store or a bitcast to i32. And in
6806 // the case of a store, it's not worth it if the index is a constant 0,
6807 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00006808 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00006809 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00006810 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006811 if ((User->getOpcode() != ISD::STORE ||
6812 (isa<ConstantSDNode>(Op.getOperand(1)) &&
6813 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006814 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00006815 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00006816 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00006817 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006818 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00006819 Op.getOperand(0)),
6820 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006821 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Pete Coopera77214a2011-11-14 19:38:42 +00006822 } else if (VT == MVT::i32 || VT == MVT::i64) {
6823 // ExtractPS/pextrq works with constant index.
Mon P Wangf0fcdd82009-01-15 21:10:20 +00006824 if (isa<ConstantSDNode>(Op.getOperand(1)))
6825 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006826 }
Dan Gohman475871a2008-07-27 21:46:04 +00006827 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006828}
6829
6830
Dan Gohman475871a2008-07-27 21:46:04 +00006831SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006832X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
6833 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006834 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00006835 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006836
David Greene74a579d2011-02-10 16:57:36 +00006837 SDValue Vec = Op.getOperand(0);
6838 EVT VecVT = Vec.getValueType();
6839
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006840 // If this is a 256-bit vector result, first extract the 128-bit vector and
6841 // then extract the element from the 128-bit vector.
6842 if (VecVT.getSizeInBits() == 256) {
David Greene74a579d2011-02-10 16:57:36 +00006843 DebugLoc dl = Op.getNode()->getDebugLoc();
6844 unsigned NumElems = VecVT.getVectorNumElements();
6845 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00006846 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
6847
6848 // Get the 128-bit vector.
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006849 bool Upper = IdxVal >= NumElems/2;
6850 Vec = Extract128BitVector(Vec,
6851 DAG.getConstant(Upper ? NumElems/2 : 0, MVT::i32), DAG, dl);
David Greene74a579d2011-02-10 16:57:36 +00006852
David Greene74a579d2011-02-10 16:57:36 +00006853 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006854 Upper ? DAG.getConstant(IdxVal-NumElems/2, MVT::i32) : Idx);
David Greene74a579d2011-02-10 16:57:36 +00006855 }
6856
6857 assert(Vec.getValueSizeInBits() <= 128 && "Unexpected vector length");
6858
Craig Topperc0d82852011-11-22 00:44:41 +00006859 if (Subtarget->hasSSE41orAVX()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006860 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006861 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00006862 return Res;
6863 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00006864
Owen Andersone50ed302009-08-10 22:56:29 +00006865 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006866 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006867 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006868 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00006869 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006870 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006871 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006872 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6873 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006874 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006875 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00006876 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006877 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00006878 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00006879 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006880 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00006881 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006882 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006883 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006884 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006885 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006886 if (Idx == 0)
6887 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00006888
Evan Cheng0db9fe62006-04-25 20:13:52 +00006889 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00006890 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006891 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006892 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006893 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006894 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006895 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00006896 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006897 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
6898 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
6899 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006900 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006901 if (Idx == 0)
6902 return Op;
6903
6904 // UNPCKHPD the element to the lowest double word, then movsd.
6905 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
6906 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00006907 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006908 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006909 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006910 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006911 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006912 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006913 }
6914
Dan Gohman475871a2008-07-27 21:46:04 +00006915 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006916}
6917
Dan Gohman475871a2008-07-27 21:46:04 +00006918SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006919X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
6920 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006921 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006922 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006923 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006924
Dan Gohman475871a2008-07-27 21:46:04 +00006925 SDValue N0 = Op.getOperand(0);
6926 SDValue N1 = Op.getOperand(1);
6927 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00006928
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006929 if (VT.getSizeInBits() == 256)
6930 return SDValue();
6931
Dan Gohman8a55ce42009-09-23 21:02:20 +00006932 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00006933 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006934 unsigned Opc;
6935 if (VT == MVT::v8i16)
6936 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006937 else if (VT == MVT::v16i8)
6938 Opc = X86ISD::PINSRB;
6939 else
6940 Opc = X86ISD::PINSRB;
6941
Nate Begeman14d12ca2008-02-11 04:19:36 +00006942 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
6943 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006944 if (N1.getValueType() != MVT::i32)
6945 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6946 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006947 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00006948 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006949 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006950 // Bits [7:6] of the constant are the source select. This will always be
6951 // zero here. The DAG Combiner may combine an extract_elt index into these
6952 // bits. For example (insert (extract, 3), 2) could be matched by putting
6953 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00006954 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00006955 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00006956 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00006957 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006958 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00006959 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00006960 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00006961 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Pete Coopera77214a2011-11-14 19:38:42 +00006962 } else if ((EltVT == MVT::i32 || EltVT == MVT::i64) &&
6963 isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00006964 // PINSR* works with constant index.
6965 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006966 }
Dan Gohman475871a2008-07-27 21:46:04 +00006967 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006968}
6969
Dan Gohman475871a2008-07-27 21:46:04 +00006970SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006971X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006972 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006973 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006974
David Greene6b381262011-02-09 15:32:06 +00006975 DebugLoc dl = Op.getDebugLoc();
6976 SDValue N0 = Op.getOperand(0);
6977 SDValue N1 = Op.getOperand(1);
6978 SDValue N2 = Op.getOperand(2);
6979
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006980 // If this is a 256-bit vector result, first extract the 128-bit vector,
6981 // insert the element into the extracted half and then place it back.
6982 if (VT.getSizeInBits() == 256) {
David Greene6b381262011-02-09 15:32:06 +00006983 if (!isa<ConstantSDNode>(N2))
6984 return SDValue();
6985
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006986 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00006987 unsigned NumElems = VT.getVectorNumElements();
6988 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006989 bool Upper = IdxVal >= NumElems/2;
6990 SDValue Ins128Idx = DAG.getConstant(Upper ? NumElems/2 : 0, MVT::i32);
6991 SDValue V = Extract128BitVector(N0, Ins128Idx, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00006992
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006993 // Insert the element into the desired half.
6994 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V,
6995 N1, Upper ? DAG.getConstant(IdxVal-NumElems/2, MVT::i32) : N2);
David Greene6b381262011-02-09 15:32:06 +00006996
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006997 // Insert the changed part back to the 256-bit vector
6998 return Insert128BitVector(N0, V, Ins128Idx, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00006999 }
7000
Craig Topperc0d82852011-11-22 00:44:41 +00007001 if (Subtarget->hasSSE41orAVX())
Nate Begeman14d12ca2008-02-11 04:19:36 +00007002 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
7003
Dan Gohman8a55ce42009-09-23 21:02:20 +00007004 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00007005 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00007006
Dan Gohman8a55ce42009-09-23 21:02:20 +00007007 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00007008 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
7009 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007010 if (N1.getValueType() != MVT::i32)
7011 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7012 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007013 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00007014 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007015 }
Dan Gohman475871a2008-07-27 21:46:04 +00007016 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007017}
7018
Dan Gohman475871a2008-07-27 21:46:04 +00007019SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007020X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007021 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007022 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00007023 EVT OpVT = Op.getValueType();
7024
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007025 // If this is a 256-bit vector result, first insert into a 128-bit
7026 // vector and then insert into the 256-bit vector.
7027 if (OpVT.getSizeInBits() > 128) {
7028 // Insert into a 128-bit vector.
7029 EVT VT128 = EVT::getVectorVT(*Context,
7030 OpVT.getVectorElementType(),
7031 OpVT.getVectorNumElements() / 2);
7032
7033 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
7034
7035 // Insert the 128-bit vector.
7036 return Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, OpVT), Op,
7037 DAG.getConstant(0, MVT::i32),
7038 DAG, dl);
7039 }
7040
Chris Lattnerf172ecd2010-07-04 23:07:25 +00007041 if (Op.getValueType() == MVT::v1i64 &&
7042 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00007043 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00007044
Owen Anderson825b72b2009-08-11 20:47:22 +00007045 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00007046 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
7047 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007048 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00007049 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007050}
7051
David Greene91585092011-01-26 15:38:49 +00007052// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
7053// a simple subregister reference or explicit instructions to grab
7054// upper bits of a vector.
7055SDValue
7056X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7057 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00007058 DebugLoc dl = Op.getNode()->getDebugLoc();
7059 SDValue Vec = Op.getNode()->getOperand(0);
7060 SDValue Idx = Op.getNode()->getOperand(1);
7061
7062 if (Op.getNode()->getValueType(0).getSizeInBits() == 128
7063 && Vec.getNode()->getValueType(0).getSizeInBits() == 256) {
7064 return Extract128BitVector(Vec, Idx, DAG, dl);
7065 }
David Greene91585092011-01-26 15:38:49 +00007066 }
7067 return SDValue();
7068}
7069
David Greenecfe33c42011-01-26 19:13:22 +00007070// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
7071// simple superregister reference or explicit instructions to insert
7072// the upper bits of a vector.
7073SDValue
7074X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7075 if (Subtarget->hasAVX()) {
7076 DebugLoc dl = Op.getNode()->getDebugLoc();
7077 SDValue Vec = Op.getNode()->getOperand(0);
7078 SDValue SubVec = Op.getNode()->getOperand(1);
7079 SDValue Idx = Op.getNode()->getOperand(2);
7080
7081 if (Op.getNode()->getValueType(0).getSizeInBits() == 256
7082 && SubVec.getNode()->getValueType(0).getSizeInBits() == 128) {
David Greenea5f26012011-02-07 19:36:54 +00007083 return Insert128BitVector(Vec, SubVec, Idx, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00007084 }
7085 }
7086 return SDValue();
7087}
7088
Bill Wendling056292f2008-09-16 21:48:12 +00007089// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7090// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7091// one of the above mentioned nodes. It has to be wrapped because otherwise
7092// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7093// be used to form addressing mode. These wrapped nodes will be selected
7094// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00007095SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007096X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007097 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007098
Chris Lattner41621a22009-06-26 19:22:52 +00007099 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7100 // global base reg.
7101 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007102 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007103 CodeModel::Model M = getTargetMachine().getCodeModel();
7104
Chris Lattner4f066492009-07-11 20:29:19 +00007105 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007106 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007107 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007108 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007109 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007110 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007111 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007112
Evan Cheng1606e8e2009-03-13 07:51:59 +00007113 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00007114 CP->getAlignment(),
7115 CP->getOffset(), OpFlag);
7116 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00007117 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007118 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00007119 if (OpFlag) {
7120 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007121 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007122 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007123 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007124 }
7125
7126 return Result;
7127}
7128
Dan Gohmand858e902010-04-17 15:26:15 +00007129SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007130 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007131
Chris Lattner18c59872009-06-27 04:16:01 +00007132 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7133 // global base reg.
7134 unsigned char OpFlag = 0;
7135 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007136 CodeModel::Model M = getTargetMachine().getCodeModel();
7137
Chris Lattner4f066492009-07-11 20:29:19 +00007138 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007139 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007140 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007141 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007142 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007143 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007144 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007145
Chris Lattner18c59872009-06-27 04:16:01 +00007146 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7147 OpFlag);
7148 DebugLoc DL = JT->getDebugLoc();
7149 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007150
Chris Lattner18c59872009-06-27 04:16:01 +00007151 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00007152 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00007153 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7154 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007155 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007156 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007157
Chris Lattner18c59872009-06-27 04:16:01 +00007158 return Result;
7159}
7160
7161SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007162X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007163 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00007164
Chris Lattner18c59872009-06-27 04:16:01 +00007165 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7166 // global base reg.
7167 unsigned char OpFlag = 0;
7168 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007169 CodeModel::Model M = getTargetMachine().getCodeModel();
7170
Chris Lattner4f066492009-07-11 20:29:19 +00007171 if (Subtarget->isPICStyleRIPRel() &&
Eli Friedman586272d2011-08-11 01:48:05 +00007172 (M == CodeModel::Small || M == CodeModel::Kernel)) {
7173 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
7174 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00007175 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00007176 } else if (Subtarget->isPICStyleGOT()) {
7177 OpFlag = X86II::MO_GOT;
7178 } else if (Subtarget->isPICStyleStubPIC()) {
7179 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
7180 } else if (Subtarget->isPICStyleStubNoDynamic()) {
7181 OpFlag = X86II::MO_DARWIN_NONLAZY;
7182 }
Eric Christopherfd179292009-08-27 18:07:15 +00007183
Chris Lattner18c59872009-06-27 04:16:01 +00007184 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00007185
Chris Lattner18c59872009-06-27 04:16:01 +00007186 DebugLoc DL = Op.getDebugLoc();
7187 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007188
7189
Chris Lattner18c59872009-06-27 04:16:01 +00007190 // With PIC, the address is actually $g + Offset.
7191 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007192 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007193 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7194 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007195 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007196 Result);
7197 }
Eric Christopherfd179292009-08-27 18:07:15 +00007198
Eli Friedman586272d2011-08-11 01:48:05 +00007199 // For symbols that require a load from a stub to get the address, emit the
7200 // load.
7201 if (isGlobalStubReference(OpFlag))
7202 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007203 MachinePointerInfo::getGOT(), false, false, false, 0);
Eli Friedman586272d2011-08-11 01:48:05 +00007204
Chris Lattner18c59872009-06-27 04:16:01 +00007205 return Result;
7206}
7207
Dan Gohman475871a2008-07-27 21:46:04 +00007208SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007209X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007210 // Create the TargetBlockAddressAddress node.
7211 unsigned char OpFlags =
7212 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007213 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007214 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00007215 DebugLoc dl = Op.getDebugLoc();
7216 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
7217 /*isTarget=*/true, OpFlags);
7218
Dan Gohmanf705adb2009-10-30 01:28:02 +00007219 if (Subtarget->isPICStyleRIPRel() &&
7220 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007221 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7222 else
7223 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007224
Dan Gohman29cbade2009-11-20 23:18:13 +00007225 // With PIC, the address is actually $g + Offset.
7226 if (isGlobalRelativeToPICBase(OpFlags)) {
7227 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7228 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7229 Result);
7230 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007231
7232 return Result;
7233}
7234
7235SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00007236X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00007237 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00007238 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007239 // Create the TargetGlobalAddress node, folding in the constant
7240 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007241 unsigned char OpFlags =
7242 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007243 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007244 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007245 if (OpFlags == X86II::MO_NO_FLAG &&
7246 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007247 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007248 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007249 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007250 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007251 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007252 }
Eric Christopherfd179292009-08-27 18:07:15 +00007253
Chris Lattner4f066492009-07-11 20:29:19 +00007254 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007255 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007256 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7257 else
7258 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007259
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007260 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007261 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007262 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7263 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007264 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007265 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007266
Chris Lattner36c25012009-07-10 07:34:39 +00007267 // For globals that require a load from a stub to get the address, emit the
7268 // load.
7269 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007270 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007271 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007272
Dan Gohman6520e202008-10-18 02:06:02 +00007273 // If there was a non-zero offset that we didn't fold, create an explicit
7274 // addition for it.
7275 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007276 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007277 DAG.getConstant(Offset, getPointerTy()));
7278
Evan Cheng0db9fe62006-04-25 20:13:52 +00007279 return Result;
7280}
7281
Evan Chengda43bcf2008-09-24 00:05:32 +00007282SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007283X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007284 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007285 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007286 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007287}
7288
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007289static SDValue
7290GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007291 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00007292 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007293 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007294 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007295 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007296 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007297 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007298 GA->getOffset(),
7299 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007300 if (InFlag) {
7301 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00007302 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007303 } else {
7304 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00007305 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007306 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007307
7308 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007309 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007310
Rafael Espindola15f1b662009-04-24 12:59:40 +00007311 SDValue Flag = Chain.getValue(1);
7312 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007313}
7314
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007315// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007316static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007317LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007318 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007319 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00007320 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
7321 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007322 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007323 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007324 InFlag = Chain.getValue(1);
7325
Chris Lattnerb903bed2009-06-26 21:20:29 +00007326 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007327}
7328
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007329// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007330static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007331LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007332 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007333 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7334 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007335}
7336
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007337// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
7338// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007339static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007340 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00007341 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007342 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00007343
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007344 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
7345 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
7346 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00007347
Michael J. Spencerec38de22010-10-10 22:04:20 +00007348 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007349 DAG.getIntPtrConstant(0),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007350 MachinePointerInfo(Ptr),
7351 false, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00007352
Chris Lattnerb903bed2009-06-26 21:20:29 +00007353 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007354 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
7355 // initialexec.
7356 unsigned WrapperKind = X86ISD::Wrapper;
7357 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007358 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00007359 } else if (is64Bit) {
7360 assert(model == TLSModel::InitialExec);
7361 OperandFlags = X86II::MO_GOTTPOFF;
7362 WrapperKind = X86ISD::WrapperRIP;
7363 } else {
7364 assert(model == TLSModel::InitialExec);
7365 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00007366 }
Eric Christopherfd179292009-08-27 18:07:15 +00007367
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007368 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
7369 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00007370 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00007371 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007372 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007373 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007374
Rafael Espindola9a580232009-02-27 13:37:18 +00007375 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007376 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007377 MachinePointerInfo::getGOT(), false, false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007378
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007379 // The address of the thread local variable is the add of the thread
7380 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00007381 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007382}
7383
Dan Gohman475871a2008-07-27 21:46:04 +00007384SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007385X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00007386
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007387 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00007388 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00007389
Eric Christopher30ef0e52010-06-03 04:07:48 +00007390 if (Subtarget->isTargetELF()) {
7391 // TODO: implement the "local dynamic" model
7392 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00007393
Eric Christopher30ef0e52010-06-03 04:07:48 +00007394 // If GV is an alias then use the aliasee for determining
7395 // thread-localness.
7396 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
7397 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007398
7399 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00007400 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00007401
Eric Christopher30ef0e52010-06-03 04:07:48 +00007402 switch (model) {
7403 case TLSModel::GeneralDynamic:
7404 case TLSModel::LocalDynamic: // not implemented
7405 if (Subtarget->is64Bit())
7406 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
7407 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00007408
Eric Christopher30ef0e52010-06-03 04:07:48 +00007409 case TLSModel::InitialExec:
7410 case TLSModel::LocalExec:
7411 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
7412 Subtarget->is64Bit());
7413 }
7414 } else if (Subtarget->isTargetDarwin()) {
7415 // Darwin only has one model of TLS. Lower to that.
7416 unsigned char OpFlag = 0;
7417 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7418 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007419
Eric Christopher30ef0e52010-06-03 04:07:48 +00007420 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7421 // global base reg.
7422 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7423 !Subtarget->is64Bit();
7424 if (PIC32)
7425 OpFlag = X86II::MO_TLVP_PIC_BASE;
7426 else
7427 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007428 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007429 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00007430 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007431 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007432 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007433
Eric Christopher30ef0e52010-06-03 04:07:48 +00007434 // With PIC32, the address is actually $g + Offset.
7435 if (PIC32)
7436 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7437 DAG.getNode(X86ISD::GlobalBaseReg,
7438 DebugLoc(), getPointerTy()),
7439 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007440
Eric Christopher30ef0e52010-06-03 04:07:48 +00007441 // Lowering the machine isd will make sure everything is in the right
7442 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007443 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007444 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00007445 SDValue Args[] = { Chain, Offset };
7446 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007447
Eric Christopher30ef0e52010-06-03 04:07:48 +00007448 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7449 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7450 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007451
Eric Christopher30ef0e52010-06-03 04:07:48 +00007452 // And our return value (tls address) is in the standard call return value
7453 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007454 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Evan Chengfd230df2011-10-19 22:22:54 +00007455 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(),
7456 Chain.getValue(1));
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007457 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007458
Eric Christopher30ef0e52010-06-03 04:07:48 +00007459 assert(false &&
7460 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00007461
Torok Edwinc23197a2009-07-14 16:55:14 +00007462 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00007463 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007464}
7465
Evan Cheng0db9fe62006-04-25 20:13:52 +00007466
Nadav Rotem43012222011-05-11 08:12:09 +00007467/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00007468/// take a 2 x i32 value to shift plus a shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +00007469SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00007470 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00007471 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007472 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007473 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007474 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00007475 SDValue ShOpLo = Op.getOperand(0);
7476 SDValue ShOpHi = Op.getOperand(1);
7477 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00007478 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00007479 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00007480 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00007481
Dan Gohman475871a2008-07-27 21:46:04 +00007482 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007483 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007484 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
7485 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007486 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007487 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
7488 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007489 }
Evan Chenge3413162006-01-09 18:33:28 +00007490
Owen Anderson825b72b2009-08-11 20:47:22 +00007491 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
7492 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00007493 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00007494 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00007495
Dan Gohman475871a2008-07-27 21:46:04 +00007496 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00007497 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00007498 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
7499 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00007500
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007501 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007502 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7503 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007504 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007505 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7506 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007507 }
7508
Dan Gohman475871a2008-07-27 21:46:04 +00007509 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00007510 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007511}
Evan Chenga3195e82006-01-12 22:54:21 +00007512
Dan Gohmand858e902010-04-17 15:26:15 +00007513SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
7514 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007515 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00007516
Dale Johannesen0488fb62010-09-30 23:57:10 +00007517 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007518 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007519
Owen Anderson825b72b2009-08-11 20:47:22 +00007520 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00007521 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007522
Eli Friedman36df4992009-05-27 00:47:34 +00007523 // These are really Legal; return the operand so the caller accepts it as
7524 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007525 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00007526 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00007527 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00007528 Subtarget->is64Bit()) {
7529 return Op;
7530 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007531
Bruno Cardoso Lopesa511b8e2011-08-09 17:39:01 +00007532 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007533 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007534 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00007535 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007536 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00007537 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00007538 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007539 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007540 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007541 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
7542}
Evan Cheng0db9fe62006-04-25 20:13:52 +00007543
Owen Andersone50ed302009-08-10 22:56:29 +00007544SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007545 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00007546 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007547 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00007548 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00007549 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00007550 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007551 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007552 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00007553 else
Owen Anderson825b72b2009-08-11 20:47:22 +00007554 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007555
Chris Lattner492a43e2010-09-22 01:28:21 +00007556 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007557
Stuart Hastings84be9582011-06-02 15:57:11 +00007558 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
7559 MachineMemOperand *MMO;
7560 if (FI) {
7561 int SSFI = FI->getIndex();
7562 MMO =
7563 DAG.getMachineFunction()
7564 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7565 MachineMemOperand::MOLoad, ByteSize, ByteSize);
7566 } else {
7567 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
7568 StackSlot = StackSlot.getOperand(1);
7569 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007570 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007571 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
7572 X86ISD::FILD, DL,
7573 Tys, Ops, array_lengthof(Ops),
7574 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007575
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007576 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007577 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00007578 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007579
7580 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
7581 // shouldn't be necessary except that RFP cannot be live across
7582 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007583 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007584 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
7585 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007586 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00007587 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007588 SDValue Ops[] = {
7589 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
7590 };
Chris Lattner492a43e2010-09-22 01:28:21 +00007591 MachineMemOperand *MMO =
7592 DAG.getMachineFunction()
7593 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007594 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007595
Chris Lattner492a43e2010-09-22 01:28:21 +00007596 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
7597 Ops, array_lengthof(Ops),
7598 Op.getValueType(), MMO);
7599 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007600 MachinePointerInfo::getFixedStack(SSFI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007601 false, false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007602 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007603
Evan Cheng0db9fe62006-04-25 20:13:52 +00007604 return Result;
7605}
7606
Bill Wendling8b8a6362009-01-17 03:56:04 +00007607// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007608SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
7609 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00007610 // This algorithm is not obvious. Here it is in C code, more or less:
7611 /*
7612 double uint64_to_double( uint32_t hi, uint32_t lo ) {
7613 static const __m128i exp = { 0x4330000045300000ULL, 0 };
7614 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00007615
Bill Wendling8b8a6362009-01-17 03:56:04 +00007616 // Copy ints to xmm registers.
7617 __m128i xh = _mm_cvtsi32_si128( hi );
7618 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00007619
Bill Wendling8b8a6362009-01-17 03:56:04 +00007620 // Combine into low half of a single xmm register.
7621 __m128i x = _mm_unpacklo_epi32( xh, xl );
7622 __m128d d;
7623 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00007624
Bill Wendling8b8a6362009-01-17 03:56:04 +00007625 // Merge in appropriate exponents to give the integer bits the right
7626 // magnitude.
7627 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00007628
Bill Wendling8b8a6362009-01-17 03:56:04 +00007629 // Subtract away the biases to deal with the IEEE-754 double precision
7630 // implicit 1.
7631 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00007632
Bill Wendling8b8a6362009-01-17 03:56:04 +00007633 // All conversions up to here are exact. The correctly rounded result is
7634 // calculated using the current rounding mode using the following
7635 // horizontal add.
7636 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
7637 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
7638 // store doesn't really need to be here (except
7639 // maybe to zero the other double)
7640 return sd;
7641 }
7642 */
Dale Johannesen040225f2008-10-21 23:07:49 +00007643
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007644 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00007645 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00007646
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007647 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00007648 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00007649 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
7650 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
7651 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
7652 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00007653 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007654 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007655
Bill Wendling8b8a6362009-01-17 03:56:04 +00007656 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00007657 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007658 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00007659 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007660 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00007661 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007662 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007663
Owen Anderson825b72b2009-08-11 20:47:22 +00007664 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7665 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00007666 Op.getOperand(0),
7667 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007668 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7669 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00007670 Op.getOperand(0),
7671 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007672 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
7673 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00007674 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007675 false, false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00007676 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007677 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
Owen Anderson825b72b2009-08-11 20:47:22 +00007678 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00007679 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007680 false, false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00007681 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007682
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007683 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00007684 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00007685 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
7686 DAG.getUNDEF(MVT::v2f64), ShufMask);
7687 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
7688 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007689 DAG.getIntPtrConstant(0));
7690}
7691
Bill Wendling8b8a6362009-01-17 03:56:04 +00007692// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007693SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
7694 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007695 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007696 // FP constant to bias correct the final result.
7697 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00007698 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007699
7700 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00007701 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00007702 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007703
Eli Friedmanf3704762011-08-29 21:15:46 +00007704 // Zero out the upper parts of the register.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00007705 Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget->hasXMMInt(),
7706 DAG);
Eli Friedmanf3704762011-08-29 21:15:46 +00007707
Owen Anderson825b72b2009-08-11 20:47:22 +00007708 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007709 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007710 DAG.getIntPtrConstant(0));
7711
7712 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007713 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007714 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007715 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007716 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007717 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007718 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007719 MVT::v2f64, Bias)));
7720 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007721 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007722 DAG.getIntPtrConstant(0));
7723
7724 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007725 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007726
7727 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00007728 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00007729
Owen Anderson825b72b2009-08-11 20:47:22 +00007730 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007731 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00007732 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007733 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007734 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00007735 }
7736
7737 // Handle final rounding.
7738 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00007739}
7740
Dan Gohmand858e902010-04-17 15:26:15 +00007741SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
7742 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00007743 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007744 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007745
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007746 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00007747 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
7748 // the optimization here.
7749 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00007750 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00007751
Owen Andersone50ed302009-08-10 22:56:29 +00007752 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007753 EVT DstVT = Op.getValueType();
7754 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007755 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007756 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007757 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00007758
7759 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00007760 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007761 if (SrcVT == MVT::i32) {
7762 SDValue WordOff = DAG.getConstant(4, getPointerTy());
7763 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
7764 getPointerTy(), StackSlot, WordOff);
7765 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007766 StackSlot, MachinePointerInfo(),
7767 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007768 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007769 OffsetSlot, MachinePointerInfo(),
7770 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007771 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
7772 return Fild;
7773 }
7774
7775 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
7776 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007777 StackSlot, MachinePointerInfo(),
7778 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007779 // For i64 source, we need to add the appropriate power of 2 if the input
7780 // was negative. This is the same as the optimization in
7781 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
7782 // we must be careful to do the computation in x87 extended precision, not
7783 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00007784 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
7785 MachineMemOperand *MMO =
7786 DAG.getMachineFunction()
7787 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7788 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007789
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007790 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
7791 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007792 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
7793 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007794
7795 APInt FF(32, 0x5F800000ULL);
7796
7797 // Check whether the sign bit is set.
7798 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
7799 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
7800 ISD::SETLT);
7801
7802 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
7803 SDValue FudgePtr = DAG.getConstantPool(
7804 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
7805 getPointerTy());
7806
7807 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
7808 SDValue Zero = DAG.getIntPtrConstant(0);
7809 SDValue Four = DAG.getIntPtrConstant(4);
7810 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
7811 Zero, Four);
7812 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
7813
7814 // Load the value out, extending it from f32 to f80.
7815 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00007816 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00007817 FudgePtr, MachinePointerInfo::getConstantPool(),
7818 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007819 // Extend everything to 80 bits to force it to be done on x87.
7820 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
7821 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007822}
7823
Dan Gohman475871a2008-07-27 21:46:04 +00007824std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00007825FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00007826 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00007827
Owen Andersone50ed302009-08-10 22:56:29 +00007828 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00007829
7830 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007831 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
7832 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00007833 }
7834
Owen Anderson825b72b2009-08-11 20:47:22 +00007835 assert(DstTy.getSimpleVT() <= MVT::i64 &&
7836 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00007837 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00007838
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007839 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007840 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00007841 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007842 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00007843 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00007844 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00007845 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007846 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007847
Evan Cheng87c89352007-10-15 20:11:21 +00007848 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
7849 // stack slot.
7850 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00007851 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00007852 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007853 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00007854
Michael J. Spencerec38de22010-10-10 22:04:20 +00007855
7856
Evan Cheng0db9fe62006-04-25 20:13:52 +00007857 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00007858 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007859 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007860 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
7861 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
7862 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007863 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007864
Dan Gohman475871a2008-07-27 21:46:04 +00007865 SDValue Chain = DAG.getEntryNode();
7866 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00007867 EVT TheVT = Op.getOperand(0).getValueType();
7868 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007869 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00007870 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007871 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007872 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007873 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00007874 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00007875 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00007876 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00007877
Chris Lattner492a43e2010-09-22 01:28:21 +00007878 MachineMemOperand *MMO =
7879 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7880 MachineMemOperand::MOLoad, MemSize, MemSize);
7881 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
7882 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007883 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00007884 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007885 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
7886 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007887
Chris Lattner07290932010-09-22 01:05:16 +00007888 MachineMemOperand *MMO =
7889 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7890 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007891
Evan Cheng0db9fe62006-04-25 20:13:52 +00007892 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00007893 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00007894 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
7895 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00007896
Chris Lattner27a6c732007-11-24 07:07:01 +00007897 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007898}
7899
Dan Gohmand858e902010-04-17 15:26:15 +00007900SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
7901 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00007902 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007903 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007904
Eli Friedman948e95a2009-05-23 09:59:16 +00007905 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00007906 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00007907 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
7908 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00007909
Chris Lattner27a6c732007-11-24 07:07:01 +00007910 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007911 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007912 FIST, StackSlot, MachinePointerInfo(),
7913 false, false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00007914}
7915
Dan Gohmand858e902010-04-17 15:26:15 +00007916SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
7917 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00007918 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
7919 SDValue FIST = Vals.first, StackSlot = Vals.second;
7920 assert(FIST.getNode() && "Unexpected failure");
7921
7922 // Load the result.
7923 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007924 FIST, StackSlot, MachinePointerInfo(),
7925 false, false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007926}
7927
Dan Gohmand858e902010-04-17 15:26:15 +00007928SDValue X86TargetLowering::LowerFABS(SDValue Op,
7929 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007930 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007931 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007932 EVT VT = Op.getValueType();
7933 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007934 if (VT.isVector())
7935 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007936 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007937 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007938 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00007939 CV.push_back(C);
7940 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007941 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007942 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00007943 CV.push_back(C);
7944 CV.push_back(C);
7945 CV.push_back(C);
7946 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007947 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007948 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007949 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007950 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007951 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007952 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007953 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007954}
7955
Dan Gohmand858e902010-04-17 15:26:15 +00007956SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007957 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007958 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007959 EVT VT = Op.getValueType();
7960 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00007961 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00007962 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007963 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007964 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007965 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00007966 CV.push_back(C);
7967 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007968 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007969 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00007970 CV.push_back(C);
7971 CV.push_back(C);
7972 CV.push_back(C);
7973 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007974 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007975 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007976 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007977 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007978 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007979 false, false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007980 if (VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007981 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007982 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007983 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007984 Op.getOperand(0)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007985 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00007986 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007987 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00007988 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00007989}
7990
Dan Gohmand858e902010-04-17 15:26:15 +00007991SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007992 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00007993 SDValue Op0 = Op.getOperand(0);
7994 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007995 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007996 EVT VT = Op.getValueType();
7997 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00007998
7999 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008000 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008001 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008002 SrcVT = VT;
8003 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008004 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008005 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008006 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008007 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008008 }
8009
8010 // At this point the operands and the result should have the same
8011 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00008012
Evan Cheng68c47cb2007-01-05 07:55:56 +00008013 // First get the sign bit of second operand.
8014 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008015 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008016 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
8017 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008018 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008019 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
8020 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8021 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8022 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008023 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008024 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008025 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008026 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008027 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008028 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008029 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008030
8031 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008032 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008033 // Op0 is MVT::f32, Op1 is MVT::f64.
8034 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
8035 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
8036 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008037 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00008038 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00008039 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008040 }
8041
Evan Cheng73d6cf12007-01-05 21:37:56 +00008042 // Clear first operand sign bit.
8043 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00008044 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008045 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
8046 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008047 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008048 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
8049 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8050 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8051 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008052 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008053 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008054 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008055 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008056 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008057 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008058 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008059
8060 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00008061 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008062}
8063
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008064SDValue X86TargetLowering::LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) const {
8065 SDValue N0 = Op.getOperand(0);
8066 DebugLoc dl = Op.getDebugLoc();
8067 EVT VT = Op.getValueType();
8068
8069 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
8070 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
8071 DAG.getConstant(1, VT));
8072 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
8073}
8074
Dan Gohman076aee32009-03-04 19:44:21 +00008075/// Emit nodes that will be selected as "test Op0,Op0", or something
8076/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008077SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008078 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008079 DebugLoc dl = Op.getDebugLoc();
8080
Dan Gohman31125812009-03-07 01:58:32 +00008081 // CF and OF aren't always set the way we want. Determine which
8082 // of these we need.
8083 bool NeedCF = false;
8084 bool NeedOF = false;
8085 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008086 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00008087 case X86::COND_A: case X86::COND_AE:
8088 case X86::COND_B: case X86::COND_BE:
8089 NeedCF = true;
8090 break;
8091 case X86::COND_G: case X86::COND_GE:
8092 case X86::COND_L: case X86::COND_LE:
8093 case X86::COND_O: case X86::COND_NO:
8094 NeedOF = true;
8095 break;
Dan Gohman31125812009-03-07 01:58:32 +00008096 }
8097
Dan Gohman076aee32009-03-04 19:44:21 +00008098 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00008099 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
8100 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008101 if (Op.getResNo() != 0 || NeedOF || NeedCF)
8102 // Emit a CMP with 0, which is the TEST pattern.
8103 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8104 DAG.getConstant(0, Op.getValueType()));
8105
8106 unsigned Opcode = 0;
8107 unsigned NumOperands = 0;
8108 switch (Op.getNode()->getOpcode()) {
8109 case ISD::ADD:
8110 // Due to an isel shortcoming, be conservative if this add is likely to be
8111 // selected as part of a load-modify-store instruction. When the root node
8112 // in a match is a store, isel doesn't know how to remap non-chain non-flag
8113 // uses of other nodes in the match, such as the ADD in this case. This
8114 // leads to the ADD being left around and reselected, with the result being
8115 // two adds in the output. Alas, even if none our users are stores, that
8116 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
8117 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
8118 // climbing the DAG back to the root, and it doesn't seem to be worth the
8119 // effort.
8120 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Pete Cooper2d496892011-11-15 21:57:53 +00008121 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8122 if (UI->getOpcode() != ISD::CopyToReg &&
8123 UI->getOpcode() != ISD::SETCC &&
8124 UI->getOpcode() != ISD::STORE)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008125 goto default_case;
8126
8127 if (ConstantSDNode *C =
8128 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
8129 // An add of one will be selected as an INC.
8130 if (C->getAPIntValue() == 1) {
8131 Opcode = X86ISD::INC;
8132 NumOperands = 1;
8133 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00008134 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008135
8136 // An add of negative one (subtract of one) will be selected as a DEC.
8137 if (C->getAPIntValue().isAllOnesValue()) {
8138 Opcode = X86ISD::DEC;
8139 NumOperands = 1;
8140 break;
8141 }
Dan Gohman076aee32009-03-04 19:44:21 +00008142 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008143
8144 // Otherwise use a regular EFLAGS-setting add.
8145 Opcode = X86ISD::ADD;
8146 NumOperands = 2;
8147 break;
8148 case ISD::AND: {
8149 // If the primary and result isn't used, don't bother using X86ISD::AND,
8150 // because a TEST instruction will be better.
8151 bool NonFlagUse = false;
8152 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8153 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
8154 SDNode *User = *UI;
8155 unsigned UOpNo = UI.getOperandNo();
8156 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
8157 // Look pass truncate.
8158 UOpNo = User->use_begin().getOperandNo();
8159 User = *User->use_begin();
8160 }
8161
8162 if (User->getOpcode() != ISD::BRCOND &&
8163 User->getOpcode() != ISD::SETCC &&
8164 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
8165 NonFlagUse = true;
8166 break;
8167 }
Dan Gohman076aee32009-03-04 19:44:21 +00008168 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008169
8170 if (!NonFlagUse)
8171 break;
8172 }
8173 // FALL THROUGH
8174 case ISD::SUB:
8175 case ISD::OR:
8176 case ISD::XOR:
8177 // Due to the ISEL shortcoming noted above, be conservative if this op is
8178 // likely to be selected as part of a load-modify-store instruction.
8179 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8180 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8181 if (UI->getOpcode() == ISD::STORE)
8182 goto default_case;
8183
8184 // Otherwise use a regular EFLAGS-setting instruction.
8185 switch (Op.getNode()->getOpcode()) {
8186 default: llvm_unreachable("unexpected operator!");
8187 case ISD::SUB: Opcode = X86ISD::SUB; break;
8188 case ISD::OR: Opcode = X86ISD::OR; break;
8189 case ISD::XOR: Opcode = X86ISD::XOR; break;
8190 case ISD::AND: Opcode = X86ISD::AND; break;
8191 }
8192
8193 NumOperands = 2;
8194 break;
8195 case X86ISD::ADD:
8196 case X86ISD::SUB:
8197 case X86ISD::INC:
8198 case X86ISD::DEC:
8199 case X86ISD::OR:
8200 case X86ISD::XOR:
8201 case X86ISD::AND:
8202 return SDValue(Op.getNode(), 1);
8203 default:
8204 default_case:
8205 break;
Dan Gohman076aee32009-03-04 19:44:21 +00008206 }
8207
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008208 if (Opcode == 0)
8209 // Emit a CMP with 0, which is the TEST pattern.
8210 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8211 DAG.getConstant(0, Op.getValueType()));
8212
8213 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
8214 SmallVector<SDValue, 4> Ops;
8215 for (unsigned i = 0; i != NumOperands; ++i)
8216 Ops.push_back(Op.getOperand(i));
8217
8218 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
8219 DAG.ReplaceAllUsesWith(Op, New);
8220 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00008221}
8222
8223/// Emit nodes that will be selected as "cmp Op0,Op1", or something
8224/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008225SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008226 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008227 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
8228 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00008229 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00008230
8231 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00008232 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00008233}
8234
Evan Chengd40d03e2010-01-06 19:38:29 +00008235/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
8236/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00008237SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
8238 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008239 SDValue Op0 = And.getOperand(0);
8240 SDValue Op1 = And.getOperand(1);
8241 if (Op0.getOpcode() == ISD::TRUNCATE)
8242 Op0 = Op0.getOperand(0);
8243 if (Op1.getOpcode() == ISD::TRUNCATE)
8244 Op1 = Op1.getOperand(0);
8245
Evan Chengd40d03e2010-01-06 19:38:29 +00008246 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008247 if (Op1.getOpcode() == ISD::SHL)
8248 std::swap(Op0, Op1);
8249 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008250 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
8251 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008252 // If we looked past a truncate, check that it's only truncating away
8253 // known zeros.
8254 unsigned BitWidth = Op0.getValueSizeInBits();
8255 unsigned AndBitWidth = And.getValueSizeInBits();
8256 if (BitWidth > AndBitWidth) {
8257 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
8258 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
8259 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
8260 return SDValue();
8261 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008262 LHS = Op1;
8263 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00008264 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008265 } else if (Op1.getOpcode() == ISD::Constant) {
8266 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
Benjamin Kramerf238f502011-11-23 13:54:17 +00008267 uint64_t AndRHSVal = AndRHS->getZExtValue();
Evan Cheng2c755ba2010-02-27 07:36:59 +00008268 SDValue AndLHS = Op0;
Benjamin Kramerf238f502011-11-23 13:54:17 +00008269
8270 if (AndRHSVal == 1 && AndLHS.getOpcode() == ISD::SRL) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008271 LHS = AndLHS.getOperand(0);
8272 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008273 }
Benjamin Kramerf238f502011-11-23 13:54:17 +00008274
8275 // Use BT if the immediate can't be encoded in a TEST instruction.
8276 if (!isUInt<32>(AndRHSVal) && isPowerOf2_64(AndRHSVal)) {
8277 LHS = AndLHS;
8278 RHS = DAG.getConstant(Log2_64_Ceil(AndRHSVal), LHS.getValueType());
8279 }
Evan Chengd40d03e2010-01-06 19:38:29 +00008280 }
Evan Cheng0488db92007-09-25 01:57:46 +00008281
Evan Chengd40d03e2010-01-06 19:38:29 +00008282 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00008283 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00008284 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00008285 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00008286 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00008287 // Also promote i16 to i32 for performance / code size reason.
8288 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00008289 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00008290 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00008291
Evan Chengd40d03e2010-01-06 19:38:29 +00008292 // If the operand types disagree, extend the shift amount to match. Since
8293 // BT ignores high bits (like shifts) we can use anyextend.
8294 if (LHS.getValueType() != RHS.getValueType())
8295 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008296
Evan Chengd40d03e2010-01-06 19:38:29 +00008297 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
8298 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
8299 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8300 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00008301 }
8302
Evan Cheng54de3ea2010-01-05 06:52:31 +00008303 return SDValue();
8304}
8305
Dan Gohmand858e902010-04-17 15:26:15 +00008306SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Duncan Sands28b77e92011-09-06 19:07:46 +00008307
8308 if (Op.getValueType().isVector()) return LowerVSETCC(Op, DAG);
8309
Evan Cheng54de3ea2010-01-05 06:52:31 +00008310 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
8311 SDValue Op0 = Op.getOperand(0);
8312 SDValue Op1 = Op.getOperand(1);
8313 DebugLoc dl = Op.getDebugLoc();
8314 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
8315
8316 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00008317 // Lower (X & (1 << N)) == 0 to BT(X, N).
8318 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
8319 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00008320 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008321 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00008322 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008323 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
8324 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
8325 if (NewSetCC.getNode())
8326 return NewSetCC;
8327 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00008328
Chris Lattner481eebc2010-12-19 21:23:48 +00008329 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
8330 // these.
8331 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00008332 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00008333 cast<ConstantSDNode>(Op1)->isNullValue()) &&
8334 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008335
Chris Lattner481eebc2010-12-19 21:23:48 +00008336 // If the input is a setcc, then reuse the input setcc or use a new one with
8337 // the inverted condition.
8338 if (Op0.getOpcode() == X86ISD::SETCC) {
8339 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
8340 bool Invert = (CC == ISD::SETNE) ^
8341 cast<ConstantSDNode>(Op1)->isNullValue();
8342 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008343
Evan Cheng2c755ba2010-02-27 07:36:59 +00008344 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00008345 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8346 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
8347 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008348 }
8349
Evan Chenge5b51ac2010-04-17 06:13:15 +00008350 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00008351 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008352 if (X86CC == X86::COND_INVALID)
8353 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008354
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008355 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00008356 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008357 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00008358}
8359
Craig Topper89af15e2011-09-18 08:03:58 +00008360// Lower256IntVSETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008361// ones, and then concatenate the result back.
Craig Topper89af15e2011-09-18 08:03:58 +00008362static SDValue Lower256IntVSETCC(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008363 EVT VT = Op.getValueType();
8364
Duncan Sands28b77e92011-09-06 19:07:46 +00008365 assert(VT.getSizeInBits() == 256 && Op.getOpcode() == ISD::SETCC &&
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008366 "Unsupported value type for operation");
8367
8368 int NumElems = VT.getVectorNumElements();
8369 DebugLoc dl = Op.getDebugLoc();
8370 SDValue CC = Op.getOperand(2);
8371 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
8372 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
8373
8374 // Extract the LHS vectors
8375 SDValue LHS = Op.getOperand(0);
8376 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
8377 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
8378
8379 // Extract the RHS vectors
8380 SDValue RHS = Op.getOperand(1);
8381 SDValue RHS1 = Extract128BitVector(RHS, Idx0, DAG, dl);
8382 SDValue RHS2 = Extract128BitVector(RHS, Idx1, DAG, dl);
8383
8384 // Issue the operation on the smaller types and concatenate the result back
8385 MVT EltVT = VT.getVectorElementType().getSimpleVT();
8386 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
8387 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
8388 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
8389 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
8390}
8391
8392
Dan Gohmand858e902010-04-17 15:26:15 +00008393SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008394 SDValue Cond;
8395 SDValue Op0 = Op.getOperand(0);
8396 SDValue Op1 = Op.getOperand(1);
8397 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00008398 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00008399 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
8400 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008401 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00008402
8403 if (isFP) {
8404 unsigned SSECC = 8;
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008405 EVT EltVT = Op0.getValueType().getVectorElementType();
8406 assert(EltVT == MVT::f32 || EltVT == MVT::f64);
8407
8408 unsigned Opc = EltVT == MVT::f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00008409 bool Swap = false;
8410
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008411 // SSE Condition code mapping:
8412 // 0 - EQ
8413 // 1 - LT
8414 // 2 - LE
8415 // 3 - UNORD
8416 // 4 - NEQ
8417 // 5 - NLT
8418 // 6 - NLE
8419 // 7 - ORD
Nate Begeman30a0de92008-07-17 16:51:19 +00008420 switch (SetCCOpcode) {
8421 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008422 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00008423 case ISD::SETEQ: SSECC = 0; break;
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008424 case ISD::SETOGT:
8425 case ISD::SETGT: Swap = true; // Fallthrough
Bruno Cardoso Lopes457d53d2011-09-12 21:24:07 +00008426 case ISD::SETLT:
8427 case ISD::SETOLT: SSECC = 1; break;
8428 case ISD::SETOGE:
8429 case ISD::SETGE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008430 case ISD::SETLE:
8431 case ISD::SETOLE: SSECC = 2; break;
8432 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008433 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00008434 case ISD::SETNE: SSECC = 4; break;
8435 case ISD::SETULE: Swap = true;
8436 case ISD::SETUGE: SSECC = 5; break;
8437 case ISD::SETULT: Swap = true;
8438 case ISD::SETUGT: SSECC = 6; break;
8439 case ISD::SETO: SSECC = 7; break;
8440 }
8441 if (Swap)
8442 std::swap(Op0, Op1);
8443
Nate Begemanfb8ead02008-07-25 19:05:58 +00008444 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00008445 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00008446 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00008447 SDValue UNORD, EQ;
Bruno Cardoso Lopes809f17f2011-09-13 19:33:00 +00008448 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
8449 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00008450 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Craig Topper0a150352011-11-09 08:06:13 +00008451 } else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00008452 SDValue ORD, NEQ;
Bruno Cardoso Lopes809f17f2011-09-13 19:33:00 +00008453 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
8454 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00008455 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00008456 }
Torok Edwinc23197a2009-07-14 16:55:14 +00008457 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00008458 }
8459 // Handle all other FP comparisons here.
Bruno Cardoso Lopes809f17f2011-09-13 19:33:00 +00008460 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00008461 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008462
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008463 // Break 256-bit integer vector compare into smaller ones.
Craig Topper0a150352011-11-09 08:06:13 +00008464 if (VT.getSizeInBits() == 256 && !Subtarget->hasAVX2())
Craig Topper89af15e2011-09-18 08:03:58 +00008465 return Lower256IntVSETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008466
Nate Begeman30a0de92008-07-17 16:51:19 +00008467 // We are handling one of the integer comparisons here. Since SSE only has
8468 // GT and EQ comparisons for integer, swapping operands and multiple
8469 // operations may be required for some comparisons.
8470 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
8471 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00008472
Craig Topper0a150352011-11-09 08:06:13 +00008473 switch (VT.getVectorElementType().getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00008474 default: break;
Craig Topper0a150352011-11-09 08:06:13 +00008475 case MVT::i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
8476 case MVT::i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
8477 case MVT::i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
8478 case MVT::i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008479 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008480
Nate Begeman30a0de92008-07-17 16:51:19 +00008481 switch (SetCCOpcode) {
8482 default: break;
8483 case ISD::SETNE: Invert = true;
8484 case ISD::SETEQ: Opc = EQOpc; break;
8485 case ISD::SETLT: Swap = true;
8486 case ISD::SETGT: Opc = GTOpc; break;
8487 case ISD::SETGE: Swap = true;
8488 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
8489 case ISD::SETULT: Swap = true;
8490 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
8491 case ISD::SETUGE: Swap = true;
8492 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
8493 }
8494 if (Swap)
8495 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008496
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008497 // Check that the operation in question is available (most are plain SSE2,
8498 // but PCMPGTQ and PCMPEQQ have different requirements).
Craig Topperc0d82852011-11-22 00:44:41 +00008499 if (Opc == X86ISD::PCMPGTQ && !Subtarget->hasSSE42orAVX())
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008500 return SDValue();
Craig Topperc0d82852011-11-22 00:44:41 +00008501 if (Opc == X86ISD::PCMPEQQ && !Subtarget->hasSSE41orAVX())
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008502 return SDValue();
8503
Nate Begeman30a0de92008-07-17 16:51:19 +00008504 // Since SSE has no unsigned integer comparisons, we need to flip the sign
8505 // bits of the inputs before performing those operations.
8506 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00008507 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00008508 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
8509 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00008510 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00008511 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
8512 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00008513 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
8514 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00008515 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008516
Dale Johannesenace16102009-02-03 19:33:06 +00008517 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00008518
8519 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00008520 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00008521 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00008522
Nate Begeman30a0de92008-07-17 16:51:19 +00008523 return Result;
8524}
Evan Cheng0488db92007-09-25 01:57:46 +00008525
Evan Cheng370e5342008-12-03 08:38:43 +00008526// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00008527static bool isX86LogicalCmp(SDValue Op) {
8528 unsigned Opc = Op.getNode()->getOpcode();
8529 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
8530 return true;
8531 if (Op.getResNo() == 1 &&
8532 (Opc == X86ISD::ADD ||
8533 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00008534 Opc == X86ISD::ADC ||
8535 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00008536 Opc == X86ISD::SMUL ||
8537 Opc == X86ISD::UMUL ||
8538 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00008539 Opc == X86ISD::DEC ||
8540 Opc == X86ISD::OR ||
8541 Opc == X86ISD::XOR ||
8542 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00008543 return true;
8544
Chris Lattner9637d5b2010-12-05 07:49:54 +00008545 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
8546 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008547
Dan Gohman076aee32009-03-04 19:44:21 +00008548 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00008549}
8550
Chris Lattnera2b56002010-12-05 01:23:24 +00008551static bool isZero(SDValue V) {
8552 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8553 return C && C->isNullValue();
8554}
8555
Chris Lattner96908b12010-12-05 02:00:51 +00008556static bool isAllOnes(SDValue V) {
8557 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8558 return C && C->isAllOnesValue();
8559}
8560
Dan Gohmand858e902010-04-17 15:26:15 +00008561SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008562 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008563 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00008564 SDValue Op1 = Op.getOperand(1);
8565 SDValue Op2 = Op.getOperand(2);
8566 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008567 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00008568
Dan Gohman1a492952009-10-20 16:22:37 +00008569 if (Cond.getOpcode() == ISD::SETCC) {
8570 SDValue NewCond = LowerSETCC(Cond, DAG);
8571 if (NewCond.getNode())
8572 Cond = NewCond;
8573 }
Evan Cheng734503b2006-09-11 02:19:56 +00008574
Chris Lattnera2b56002010-12-05 01:23:24 +00008575 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008576 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00008577 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008578 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008579 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00008580 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
8581 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008582 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008583
Chris Lattnera2b56002010-12-05 01:23:24 +00008584 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008585
8586 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00008587 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
8588 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00008589
8590 SDValue CmpOp0 = Cmp.getOperand(0);
8591 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
8592 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008593
Chris Lattner96908b12010-12-05 02:00:51 +00008594 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00008595 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8596 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008597
Chris Lattner96908b12010-12-05 02:00:51 +00008598 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
8599 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008600
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008601 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00008602 if (N2C == 0 || !N2C->isNullValue())
8603 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
8604 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008605 }
8606 }
8607
Chris Lattnera2b56002010-12-05 01:23:24 +00008608 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00008609 if (Cond.getOpcode() == ISD::AND &&
8610 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8611 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008612 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008613 Cond = Cond.getOperand(0);
8614 }
8615
Evan Cheng3f41d662007-10-08 22:16:29 +00008616 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8617 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00008618 unsigned CondOpcode = Cond.getOpcode();
8619 if (CondOpcode == X86ISD::SETCC ||
8620 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008621 CC = Cond.getOperand(0);
8622
Dan Gohman475871a2008-07-27 21:46:04 +00008623 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008624 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00008625 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00008626
Evan Cheng3f41d662007-10-08 22:16:29 +00008627 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008628 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00008629 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00008630 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00008631
Chris Lattnerd1980a52009-03-12 06:52:53 +00008632 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
8633 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00008634 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008635 addTest = false;
8636 }
Dan Gohman65fd6562011-11-03 21:49:52 +00008637 } else if (CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
8638 CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
8639 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
8640 Cond.getOperand(0).getValueType() != MVT::i8)) {
8641 SDValue LHS = Cond.getOperand(0);
8642 SDValue RHS = Cond.getOperand(1);
8643 unsigned X86Opcode;
8644 unsigned X86Cond;
8645 SDVTList VTs;
8646 switch (CondOpcode) {
8647 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
8648 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
8649 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
8650 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
8651 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
8652 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
8653 default: llvm_unreachable("unexpected overflowing operator");
8654 }
8655 if (CondOpcode == ISD::UMULO)
8656 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
8657 MVT::i32);
8658 else
8659 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
8660
8661 SDValue X86Op = DAG.getNode(X86Opcode, DL, VTs, LHS, RHS);
8662
8663 if (CondOpcode == ISD::UMULO)
8664 Cond = X86Op.getValue(2);
8665 else
8666 Cond = X86Op.getValue(1);
8667
8668 CC = DAG.getConstant(X86Cond, MVT::i8);
8669 addTest = false;
Evan Cheng0488db92007-09-25 01:57:46 +00008670 }
8671
8672 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008673 // Look pass the truncate.
8674 if (Cond.getOpcode() == ISD::TRUNCATE)
8675 Cond = Cond.getOperand(0);
8676
8677 // We know the result of AND is compared against zero. Try to match
8678 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00008679 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00008680 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00008681 if (NewSetCC.getNode()) {
8682 CC = NewSetCC.getOperand(0);
8683 Cond = NewSetCC.getOperand(1);
8684 addTest = false;
8685 }
8686 }
8687 }
8688
8689 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008690 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00008691 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00008692 }
8693
Benjamin Kramere915ff32010-12-22 23:09:28 +00008694 // a < b ? -1 : 0 -> RES = ~setcc_carry
8695 // a < b ? 0 : -1 -> RES = setcc_carry
8696 // a >= b ? -1 : 0 -> RES = setcc_carry
8697 // a >= b ? 0 : -1 -> RES = ~setcc_carry
8698 if (Cond.getOpcode() == X86ISD::CMP) {
8699 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
8700
8701 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
8702 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
8703 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8704 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
8705 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
8706 return DAG.getNOT(DL, Res, Res.getValueType());
8707 return Res;
8708 }
8709 }
8710
Evan Cheng0488db92007-09-25 01:57:46 +00008711 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
8712 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008713 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008714 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00008715 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00008716}
8717
Evan Cheng370e5342008-12-03 08:38:43 +00008718// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
8719// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
8720// from the AND / OR.
8721static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
8722 Opc = Op.getOpcode();
8723 if (Opc != ISD::OR && Opc != ISD::AND)
8724 return false;
8725 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8726 Op.getOperand(0).hasOneUse() &&
8727 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
8728 Op.getOperand(1).hasOneUse());
8729}
8730
Evan Cheng961d6d42009-02-02 08:19:07 +00008731// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
8732// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00008733static bool isXor1OfSetCC(SDValue Op) {
8734 if (Op.getOpcode() != ISD::XOR)
8735 return false;
8736 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
8737 if (N1C && N1C->getAPIntValue() == 1) {
8738 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8739 Op.getOperand(0).hasOneUse();
8740 }
8741 return false;
8742}
8743
Dan Gohmand858e902010-04-17 15:26:15 +00008744SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008745 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008746 SDValue Chain = Op.getOperand(0);
8747 SDValue Cond = Op.getOperand(1);
8748 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008749 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008750 SDValue CC;
Dan Gohman65fd6562011-11-03 21:49:52 +00008751 bool Inverted = false;
Evan Cheng734503b2006-09-11 02:19:56 +00008752
Dan Gohman1a492952009-10-20 16:22:37 +00008753 if (Cond.getOpcode() == ISD::SETCC) {
Dan Gohman65fd6562011-11-03 21:49:52 +00008754 // Check for setcc([su]{add,sub,mul}o == 0).
8755 if (cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETEQ &&
8756 isa<ConstantSDNode>(Cond.getOperand(1)) &&
8757 cast<ConstantSDNode>(Cond.getOperand(1))->isNullValue() &&
8758 Cond.getOperand(0).getResNo() == 1 &&
8759 (Cond.getOperand(0).getOpcode() == ISD::SADDO ||
8760 Cond.getOperand(0).getOpcode() == ISD::UADDO ||
8761 Cond.getOperand(0).getOpcode() == ISD::SSUBO ||
8762 Cond.getOperand(0).getOpcode() == ISD::USUBO ||
8763 Cond.getOperand(0).getOpcode() == ISD::SMULO ||
8764 Cond.getOperand(0).getOpcode() == ISD::UMULO)) {
8765 Inverted = true;
8766 Cond = Cond.getOperand(0);
8767 } else {
8768 SDValue NewCond = LowerSETCC(Cond, DAG);
8769 if (NewCond.getNode())
8770 Cond = NewCond;
8771 }
Dan Gohman1a492952009-10-20 16:22:37 +00008772 }
Chris Lattnere55484e2008-12-25 05:34:37 +00008773#if 0
8774 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00008775 else if (Cond.getOpcode() == X86ISD::ADD ||
8776 Cond.getOpcode() == X86ISD::SUB ||
8777 Cond.getOpcode() == X86ISD::SMUL ||
8778 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00008779 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00008780#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00008781
Evan Chengad9c0a32009-12-15 00:53:42 +00008782 // Look pass (and (setcc_carry (cmp ...)), 1).
8783 if (Cond.getOpcode() == ISD::AND &&
8784 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8785 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008786 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008787 Cond = Cond.getOperand(0);
8788 }
8789
Evan Cheng3f41d662007-10-08 22:16:29 +00008790 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8791 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00008792 unsigned CondOpcode = Cond.getOpcode();
8793 if (CondOpcode == X86ISD::SETCC ||
8794 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008795 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008796
Dan Gohman475871a2008-07-27 21:46:04 +00008797 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008798 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00008799 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00008800 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00008801 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008802 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00008803 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00008804 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00008805 default: break;
8806 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00008807 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00008808 // These can only come from an arithmetic instruction with overflow,
8809 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00008810 Cond = Cond.getNode()->getOperand(1);
8811 addTest = false;
8812 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00008813 }
Evan Cheng0488db92007-09-25 01:57:46 +00008814 }
Dan Gohman65fd6562011-11-03 21:49:52 +00008815 }
8816 CondOpcode = Cond.getOpcode();
8817 if (CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
8818 CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
8819 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
8820 Cond.getOperand(0).getValueType() != MVT::i8)) {
8821 SDValue LHS = Cond.getOperand(0);
8822 SDValue RHS = Cond.getOperand(1);
8823 unsigned X86Opcode;
8824 unsigned X86Cond;
8825 SDVTList VTs;
8826 switch (CondOpcode) {
8827 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
8828 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
8829 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
8830 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
8831 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
8832 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
8833 default: llvm_unreachable("unexpected overflowing operator");
8834 }
8835 if (Inverted)
8836 X86Cond = X86::GetOppositeBranchCondition((X86::CondCode)X86Cond);
8837 if (CondOpcode == ISD::UMULO)
8838 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
8839 MVT::i32);
8840 else
8841 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
8842
8843 SDValue X86Op = DAG.getNode(X86Opcode, dl, VTs, LHS, RHS);
8844
8845 if (CondOpcode == ISD::UMULO)
8846 Cond = X86Op.getValue(2);
8847 else
8848 Cond = X86Op.getValue(1);
8849
8850 CC = DAG.getConstant(X86Cond, MVT::i8);
8851 addTest = false;
Evan Cheng370e5342008-12-03 08:38:43 +00008852 } else {
8853 unsigned CondOpc;
8854 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
8855 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00008856 if (CondOpc == ISD::OR) {
8857 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
8858 // two branches instead of an explicit OR instruction with a
8859 // separate test.
8860 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00008861 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00008862 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008863 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00008864 Chain, Dest, CC, Cmp);
8865 CC = Cond.getOperand(1).getOperand(0);
8866 Cond = Cmp;
8867 addTest = false;
8868 }
8869 } else { // ISD::AND
8870 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
8871 // two branches instead of an explicit AND instruction with a
8872 // separate test. However, we only do this if this block doesn't
8873 // have a fall-through edge, because this requires an explicit
8874 // jmp when the condition is false.
8875 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00008876 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00008877 Op.getNode()->hasOneUse()) {
8878 X86::CondCode CCode =
8879 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
8880 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008881 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00008882 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00008883 // Look for an unconditional branch following this conditional branch.
8884 // We need this because we need to reverse the successors in order
8885 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00008886 if (User->getOpcode() == ISD::BR) {
8887 SDValue FalseBB = User->getOperand(1);
8888 SDNode *NewBR =
8889 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00008890 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00008891 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00008892 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00008893
Dale Johannesene4d209d2009-02-03 20:21:25 +00008894 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00008895 Chain, Dest, CC, Cmp);
8896 X86::CondCode CCode =
8897 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
8898 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008899 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00008900 Cond = Cmp;
8901 addTest = false;
8902 }
8903 }
Dan Gohman279c22e2008-10-21 03:29:32 +00008904 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00008905 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
8906 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
8907 // It should be transformed during dag combiner except when the condition
8908 // is set by a arithmetics with overflow node.
8909 X86::CondCode CCode =
8910 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
8911 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008912 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00008913 Cond = Cond.getOperand(0).getOperand(1);
8914 addTest = false;
Dan Gohman65fd6562011-11-03 21:49:52 +00008915 } else if (Cond.getOpcode() == ISD::SETCC &&
8916 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETOEQ) {
8917 // For FCMP_OEQ, we can emit
8918 // two branches instead of an explicit AND instruction with a
8919 // separate test. However, we only do this if this block doesn't
8920 // have a fall-through edge, because this requires an explicit
8921 // jmp when the condition is false.
8922 if (Op.getNode()->hasOneUse()) {
8923 SDNode *User = *Op.getNode()->use_begin();
8924 // Look for an unconditional branch following this conditional branch.
8925 // We need this because we need to reverse the successors in order
8926 // to implement FCMP_OEQ.
8927 if (User->getOpcode() == ISD::BR) {
8928 SDValue FalseBB = User->getOperand(1);
8929 SDNode *NewBR =
8930 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
8931 assert(NewBR == User);
8932 (void)NewBR;
8933 Dest = FalseBB;
8934
8935 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
8936 Cond.getOperand(0), Cond.getOperand(1));
8937 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
8938 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
8939 Chain, Dest, CC, Cmp);
8940 CC = DAG.getConstant(X86::COND_P, MVT::i8);
8941 Cond = Cmp;
8942 addTest = false;
8943 }
8944 }
8945 } else if (Cond.getOpcode() == ISD::SETCC &&
8946 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETUNE) {
8947 // For FCMP_UNE, we can emit
8948 // two branches instead of an explicit AND instruction with a
8949 // separate test. However, we only do this if this block doesn't
8950 // have a fall-through edge, because this requires an explicit
8951 // jmp when the condition is false.
8952 if (Op.getNode()->hasOneUse()) {
8953 SDNode *User = *Op.getNode()->use_begin();
8954 // Look for an unconditional branch following this conditional branch.
8955 // We need this because we need to reverse the successors in order
8956 // to implement FCMP_UNE.
8957 if (User->getOpcode() == ISD::BR) {
8958 SDValue FalseBB = User->getOperand(1);
8959 SDNode *NewBR =
8960 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
8961 assert(NewBR == User);
8962 (void)NewBR;
8963
8964 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
8965 Cond.getOperand(0), Cond.getOperand(1));
8966 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
8967 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
8968 Chain, Dest, CC, Cmp);
8969 CC = DAG.getConstant(X86::COND_NP, MVT::i8);
8970 Cond = Cmp;
8971 addTest = false;
8972 Dest = FalseBB;
8973 }
8974 }
Dan Gohman279c22e2008-10-21 03:29:32 +00008975 }
Evan Cheng0488db92007-09-25 01:57:46 +00008976 }
8977
8978 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008979 // Look pass the truncate.
8980 if (Cond.getOpcode() == ISD::TRUNCATE)
8981 Cond = Cond.getOperand(0);
8982
8983 // We know the result of AND is compared against zero. Try to match
8984 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00008985 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008986 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
8987 if (NewSetCC.getNode()) {
8988 CC = NewSetCC.getOperand(0);
8989 Cond = NewSetCC.getOperand(1);
8990 addTest = false;
8991 }
8992 }
8993 }
8994
8995 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008996 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00008997 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00008998 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00008999 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00009000 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00009001}
9002
Anton Korobeynikove060b532007-04-17 19:34:00 +00009003
9004// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
9005// Calls to _alloca is needed to probe the stack when allocating more than 4k
9006// bytes in one go. Touching the stack at 4K increments is necessary to ensure
9007// that the guard pages used by the OS virtual memory manager are allocated in
9008// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00009009SDValue
9010X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009011 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009012 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009013 getTargetMachine().Options.EnableSegmentedStacks) &&
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009014 "This should be used only on Windows targets or when segmented stacks "
Rafael Espindola96428ce2011-09-06 18:43:08 +00009015 "are being used");
9016 assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009017 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009018
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009019 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00009020 SDValue Chain = Op.getOperand(0);
9021 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009022 // FIXME: Ensure alignment here
9023
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009024 bool Is64Bit = Subtarget->is64Bit();
9025 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009026
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009027 if (getTargetMachine().Options.EnableSegmentedStacks) {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009028 MachineFunction &MF = DAG.getMachineFunction();
9029 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009030
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009031 if (Is64Bit) {
9032 // The 64 bit implementation of segmented stacks needs to clobber both r10
Rafael Espindola96428ce2011-09-06 18:43:08 +00009033 // r11. This makes it impossible to use it along with nested parameters.
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009034 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009035
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009036 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
9037 I != E; I++)
9038 if (I->hasNestAttr())
9039 report_fatal_error("Cannot use segmented stacks with functions that "
9040 "have nested arguments.");
9041 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009042
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009043 const TargetRegisterClass *AddrRegClass =
9044 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
9045 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
9046 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
9047 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
9048 DAG.getRegister(Vreg, SPTy));
9049 SDValue Ops1[2] = { Value, Chain };
9050 return DAG.getMergeValues(Ops1, 2, dl);
9051 } else {
9052 SDValue Flag;
9053 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009054
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009055 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
9056 Flag = Chain.getValue(1);
9057 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009058
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009059 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
9060 Flag = Chain.getValue(1);
9061
9062 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
9063
9064 SDValue Ops1[2] = { Chain.getValue(0), Chain };
9065 return DAG.getMergeValues(Ops1, 2, dl);
9066 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009067}
9068
Dan Gohmand858e902010-04-17 15:26:15 +00009069SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00009070 MachineFunction &MF = DAG.getMachineFunction();
9071 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
9072
Dan Gohman69de1932008-02-06 22:27:42 +00009073 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00009074 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00009075
Anton Korobeynikove7beda12010-10-03 22:52:07 +00009076 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00009077 // vastart just stores the address of the VarArgsFrameIndex slot into the
9078 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00009079 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9080 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009081 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
9082 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009083 }
9084
9085 // __va_list_tag:
9086 // gp_offset (0 - 6 * 8)
9087 // fp_offset (48 - 48 + 8 * 16)
9088 // overflow_arg_area (point to parameters coming in memory).
9089 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00009090 SmallVector<SDValue, 8> MemOps;
9091 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00009092 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009093 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009094 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
9095 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009096 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009097 MemOps.push_back(Store);
9098
9099 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009100 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009101 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009102 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009103 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
9104 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009105 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009106 MemOps.push_back(Store);
9107
9108 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00009109 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009110 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00009111 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9112 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009113 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
9114 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00009115 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009116 MemOps.push_back(Store);
9117
9118 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00009119 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009120 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00009121 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
9122 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009123 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
9124 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009125 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009126 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00009127 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00009128}
9129
Dan Gohmand858e902010-04-17 15:26:15 +00009130SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00009131 assert(Subtarget->is64Bit() &&
9132 "LowerVAARG only handles 64-bit va_arg!");
9133 assert((Subtarget->isTargetLinux() ||
9134 Subtarget->isTargetDarwin()) &&
9135 "Unhandled target in LowerVAARG");
9136 assert(Op.getNode()->getNumOperands() == 4);
9137 SDValue Chain = Op.getOperand(0);
9138 SDValue SrcPtr = Op.getOperand(1);
9139 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
9140 unsigned Align = Op.getConstantOperandVal(3);
9141 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00009142
Dan Gohman320afb82010-10-12 18:00:49 +00009143 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009144 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Dan Gohman320afb82010-10-12 18:00:49 +00009145 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
9146 uint8_t ArgMode;
9147
9148 // Decide which area this value should be read from.
9149 // TODO: Implement the AMD64 ABI in its entirety. This simple
9150 // selection mechanism works only for the basic types.
9151 if (ArgVT == MVT::f80) {
9152 llvm_unreachable("va_arg for f80 not yet implemented");
9153 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
9154 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
9155 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
9156 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
9157 } else {
9158 llvm_unreachable("Unhandled argument type in LowerVAARG");
9159 }
9160
9161 if (ArgMode == 2) {
9162 // Sanity Check: Make sure using fp_offset makes sense.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009163 assert(!getTargetMachine().Options.UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00009164 !(DAG.getMachineFunction()
9165 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00009166 Subtarget->hasXMM());
Dan Gohman320afb82010-10-12 18:00:49 +00009167 }
9168
9169 // Insert VAARG_64 node into the DAG
9170 // VAARG_64 returns two values: Variable Argument Address, Chain
9171 SmallVector<SDValue, 11> InstOps;
9172 InstOps.push_back(Chain);
9173 InstOps.push_back(SrcPtr);
9174 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
9175 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
9176 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
9177 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
9178 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
9179 VTs, &InstOps[0], InstOps.size(),
9180 MVT::i64,
9181 MachinePointerInfo(SV),
9182 /*Align=*/0,
9183 /*Volatile=*/false,
9184 /*ReadMem=*/true,
9185 /*WriteMem=*/true);
9186 Chain = VAARG.getValue(1);
9187
9188 // Load the next argument and return it
9189 return DAG.getLoad(ArgVT, dl,
9190 Chain,
9191 VAARG,
9192 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009193 false, false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00009194}
9195
Dan Gohmand858e902010-04-17 15:26:15 +00009196SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00009197 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00009198 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00009199 SDValue Chain = Op.getOperand(0);
9200 SDValue DstPtr = Op.getOperand(1);
9201 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00009202 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
9203 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00009204 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00009205
Chris Lattnere72f2022010-09-21 05:40:29 +00009206 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00009207 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00009208 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00009209 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00009210}
9211
Dan Gohman475871a2008-07-27 21:46:04 +00009212SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00009213X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009214 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009215 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00009216 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00009217 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00009218 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00009219 case Intrinsic::x86_sse_comieq_ss:
9220 case Intrinsic::x86_sse_comilt_ss:
9221 case Intrinsic::x86_sse_comile_ss:
9222 case Intrinsic::x86_sse_comigt_ss:
9223 case Intrinsic::x86_sse_comige_ss:
9224 case Intrinsic::x86_sse_comineq_ss:
9225 case Intrinsic::x86_sse_ucomieq_ss:
9226 case Intrinsic::x86_sse_ucomilt_ss:
9227 case Intrinsic::x86_sse_ucomile_ss:
9228 case Intrinsic::x86_sse_ucomigt_ss:
9229 case Intrinsic::x86_sse_ucomige_ss:
9230 case Intrinsic::x86_sse_ucomineq_ss:
9231 case Intrinsic::x86_sse2_comieq_sd:
9232 case Intrinsic::x86_sse2_comilt_sd:
9233 case Intrinsic::x86_sse2_comile_sd:
9234 case Intrinsic::x86_sse2_comigt_sd:
9235 case Intrinsic::x86_sse2_comige_sd:
9236 case Intrinsic::x86_sse2_comineq_sd:
9237 case Intrinsic::x86_sse2_ucomieq_sd:
9238 case Intrinsic::x86_sse2_ucomilt_sd:
9239 case Intrinsic::x86_sse2_ucomile_sd:
9240 case Intrinsic::x86_sse2_ucomigt_sd:
9241 case Intrinsic::x86_sse2_ucomige_sd:
9242 case Intrinsic::x86_sse2_ucomineq_sd: {
9243 unsigned Opc = 0;
9244 ISD::CondCode CC = ISD::SETCC_INVALID;
9245 switch (IntNo) {
9246 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009247 case Intrinsic::x86_sse_comieq_ss:
9248 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009249 Opc = X86ISD::COMI;
9250 CC = ISD::SETEQ;
9251 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009252 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009253 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009254 Opc = X86ISD::COMI;
9255 CC = ISD::SETLT;
9256 break;
9257 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009258 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009259 Opc = X86ISD::COMI;
9260 CC = ISD::SETLE;
9261 break;
9262 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009263 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009264 Opc = X86ISD::COMI;
9265 CC = ISD::SETGT;
9266 break;
9267 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009268 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009269 Opc = X86ISD::COMI;
9270 CC = ISD::SETGE;
9271 break;
9272 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009273 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009274 Opc = X86ISD::COMI;
9275 CC = ISD::SETNE;
9276 break;
9277 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009278 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009279 Opc = X86ISD::UCOMI;
9280 CC = ISD::SETEQ;
9281 break;
9282 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009283 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009284 Opc = X86ISD::UCOMI;
9285 CC = ISD::SETLT;
9286 break;
9287 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009288 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009289 Opc = X86ISD::UCOMI;
9290 CC = ISD::SETLE;
9291 break;
9292 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009293 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009294 Opc = X86ISD::UCOMI;
9295 CC = ISD::SETGT;
9296 break;
9297 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009298 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009299 Opc = X86ISD::UCOMI;
9300 CC = ISD::SETGE;
9301 break;
9302 case Intrinsic::x86_sse_ucomineq_ss:
9303 case Intrinsic::x86_sse2_ucomineq_sd:
9304 Opc = X86ISD::UCOMI;
9305 CC = ISD::SETNE;
9306 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009307 }
Evan Cheng734503b2006-09-11 02:19:56 +00009308
Dan Gohman475871a2008-07-27 21:46:04 +00009309 SDValue LHS = Op.getOperand(1);
9310 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00009311 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00009312 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00009313 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
9314 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9315 DAG.getConstant(X86CC, MVT::i8), Cond);
9316 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00009317 }
Duncan Sands04aa4ae2011-09-23 16:10:22 +00009318 // Arithmetic intrinsics.
9319 case Intrinsic::x86_sse3_hadd_ps:
9320 case Intrinsic::x86_sse3_hadd_pd:
9321 case Intrinsic::x86_avx_hadd_ps_256:
9322 case Intrinsic::x86_avx_hadd_pd_256:
9323 return DAG.getNode(X86ISD::FHADD, dl, Op.getValueType(),
9324 Op.getOperand(1), Op.getOperand(2));
9325 case Intrinsic::x86_sse3_hsub_ps:
9326 case Intrinsic::x86_sse3_hsub_pd:
9327 case Intrinsic::x86_avx_hsub_ps_256:
9328 case Intrinsic::x86_avx_hsub_pd_256:
9329 return DAG.getNode(X86ISD::FHSUB, dl, Op.getValueType(),
9330 Op.getOperand(1), Op.getOperand(2));
Craig Topper98fc7292011-11-19 17:46:46 +00009331 case Intrinsic::x86_avx2_psllv_d:
9332 case Intrinsic::x86_avx2_psllv_q:
9333 case Intrinsic::x86_avx2_psllv_d_256:
9334 case Intrinsic::x86_avx2_psllv_q_256:
9335 return DAG.getNode(ISD::SHL, dl, Op.getValueType(),
9336 Op.getOperand(1), Op.getOperand(2));
9337 case Intrinsic::x86_avx2_psrlv_d:
9338 case Intrinsic::x86_avx2_psrlv_q:
9339 case Intrinsic::x86_avx2_psrlv_d_256:
9340 case Intrinsic::x86_avx2_psrlv_q_256:
9341 return DAG.getNode(ISD::SRL, dl, Op.getValueType(),
9342 Op.getOperand(1), Op.getOperand(2));
9343 case Intrinsic::x86_avx2_psrav_d:
9344 case Intrinsic::x86_avx2_psrav_d_256:
9345 return DAG.getNode(ISD::SRA, dl, Op.getValueType(),
9346 Op.getOperand(1), Op.getOperand(2));
9347
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009348 // ptest and testp intrinsics. The intrinsic these come from are designed to
9349 // return an integer value, not just an instruction so lower it to the ptest
9350 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00009351 case Intrinsic::x86_sse41_ptestz:
9352 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009353 case Intrinsic::x86_sse41_ptestnzc:
9354 case Intrinsic::x86_avx_ptestz_256:
9355 case Intrinsic::x86_avx_ptestc_256:
9356 case Intrinsic::x86_avx_ptestnzc_256:
9357 case Intrinsic::x86_avx_vtestz_ps:
9358 case Intrinsic::x86_avx_vtestc_ps:
9359 case Intrinsic::x86_avx_vtestnzc_ps:
9360 case Intrinsic::x86_avx_vtestz_pd:
9361 case Intrinsic::x86_avx_vtestc_pd:
9362 case Intrinsic::x86_avx_vtestnzc_pd:
9363 case Intrinsic::x86_avx_vtestz_ps_256:
9364 case Intrinsic::x86_avx_vtestc_ps_256:
9365 case Intrinsic::x86_avx_vtestnzc_ps_256:
9366 case Intrinsic::x86_avx_vtestz_pd_256:
9367 case Intrinsic::x86_avx_vtestc_pd_256:
9368 case Intrinsic::x86_avx_vtestnzc_pd_256: {
9369 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00009370 unsigned X86CC = 0;
9371 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00009372 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009373 case Intrinsic::x86_avx_vtestz_ps:
9374 case Intrinsic::x86_avx_vtestz_pd:
9375 case Intrinsic::x86_avx_vtestz_ps_256:
9376 case Intrinsic::x86_avx_vtestz_pd_256:
9377 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009378 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009379 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009380 // ZF = 1
9381 X86CC = X86::COND_E;
9382 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009383 case Intrinsic::x86_avx_vtestc_ps:
9384 case Intrinsic::x86_avx_vtestc_pd:
9385 case Intrinsic::x86_avx_vtestc_ps_256:
9386 case Intrinsic::x86_avx_vtestc_pd_256:
9387 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009388 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009389 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009390 // CF = 1
9391 X86CC = X86::COND_B;
9392 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009393 case Intrinsic::x86_avx_vtestnzc_ps:
9394 case Intrinsic::x86_avx_vtestnzc_pd:
9395 case Intrinsic::x86_avx_vtestnzc_ps_256:
9396 case Intrinsic::x86_avx_vtestnzc_pd_256:
9397 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00009398 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009399 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009400 // ZF and CF = 0
9401 X86CC = X86::COND_A;
9402 break;
9403 }
Eric Christopherfd179292009-08-27 18:07:15 +00009404
Eric Christopher71c67532009-07-29 00:28:05 +00009405 SDValue LHS = Op.getOperand(1);
9406 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009407 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
9408 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00009409 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
9410 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
9411 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00009412 }
Evan Cheng5759f972008-05-04 09:15:50 +00009413
9414 // Fix vector shift instructions where the last operand is a non-immediate
9415 // i32 value.
Craig Topper7be5dfd2011-11-12 09:58:49 +00009416 case Intrinsic::x86_avx2_pslli_w:
9417 case Intrinsic::x86_avx2_pslli_d:
9418 case Intrinsic::x86_avx2_pslli_q:
9419 case Intrinsic::x86_avx2_psrli_w:
9420 case Intrinsic::x86_avx2_psrli_d:
9421 case Intrinsic::x86_avx2_psrli_q:
9422 case Intrinsic::x86_avx2_psrai_w:
9423 case Intrinsic::x86_avx2_psrai_d:
Evan Cheng5759f972008-05-04 09:15:50 +00009424 case Intrinsic::x86_sse2_pslli_w:
9425 case Intrinsic::x86_sse2_pslli_d:
9426 case Intrinsic::x86_sse2_pslli_q:
9427 case Intrinsic::x86_sse2_psrli_w:
9428 case Intrinsic::x86_sse2_psrli_d:
9429 case Intrinsic::x86_sse2_psrli_q:
9430 case Intrinsic::x86_sse2_psrai_w:
9431 case Intrinsic::x86_sse2_psrai_d:
9432 case Intrinsic::x86_mmx_pslli_w:
9433 case Intrinsic::x86_mmx_pslli_d:
9434 case Intrinsic::x86_mmx_pslli_q:
9435 case Intrinsic::x86_mmx_psrli_w:
9436 case Intrinsic::x86_mmx_psrli_d:
9437 case Intrinsic::x86_mmx_psrli_q:
9438 case Intrinsic::x86_mmx_psrai_w:
9439 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00009440 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00009441 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00009442 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00009443
9444 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00009445 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00009446 switch (IntNo) {
9447 case Intrinsic::x86_sse2_pslli_w:
9448 NewIntNo = Intrinsic::x86_sse2_psll_w;
9449 break;
9450 case Intrinsic::x86_sse2_pslli_d:
9451 NewIntNo = Intrinsic::x86_sse2_psll_d;
9452 break;
9453 case Intrinsic::x86_sse2_pslli_q:
9454 NewIntNo = Intrinsic::x86_sse2_psll_q;
9455 break;
9456 case Intrinsic::x86_sse2_psrli_w:
9457 NewIntNo = Intrinsic::x86_sse2_psrl_w;
9458 break;
9459 case Intrinsic::x86_sse2_psrli_d:
9460 NewIntNo = Intrinsic::x86_sse2_psrl_d;
9461 break;
9462 case Intrinsic::x86_sse2_psrli_q:
9463 NewIntNo = Intrinsic::x86_sse2_psrl_q;
9464 break;
9465 case Intrinsic::x86_sse2_psrai_w:
9466 NewIntNo = Intrinsic::x86_sse2_psra_w;
9467 break;
9468 case Intrinsic::x86_sse2_psrai_d:
9469 NewIntNo = Intrinsic::x86_sse2_psra_d;
9470 break;
Craig Topper7be5dfd2011-11-12 09:58:49 +00009471 case Intrinsic::x86_avx2_pslli_w:
9472 NewIntNo = Intrinsic::x86_avx2_psll_w;
9473 break;
9474 case Intrinsic::x86_avx2_pslli_d:
9475 NewIntNo = Intrinsic::x86_avx2_psll_d;
9476 break;
9477 case Intrinsic::x86_avx2_pslli_q:
9478 NewIntNo = Intrinsic::x86_avx2_psll_q;
9479 break;
9480 case Intrinsic::x86_avx2_psrli_w:
9481 NewIntNo = Intrinsic::x86_avx2_psrl_w;
9482 break;
9483 case Intrinsic::x86_avx2_psrli_d:
9484 NewIntNo = Intrinsic::x86_avx2_psrl_d;
9485 break;
9486 case Intrinsic::x86_avx2_psrli_q:
9487 NewIntNo = Intrinsic::x86_avx2_psrl_q;
9488 break;
9489 case Intrinsic::x86_avx2_psrai_w:
9490 NewIntNo = Intrinsic::x86_avx2_psra_w;
9491 break;
9492 case Intrinsic::x86_avx2_psrai_d:
9493 NewIntNo = Intrinsic::x86_avx2_psra_d;
9494 break;
Evan Cheng5759f972008-05-04 09:15:50 +00009495 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00009496 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00009497 switch (IntNo) {
9498 case Intrinsic::x86_mmx_pslli_w:
9499 NewIntNo = Intrinsic::x86_mmx_psll_w;
9500 break;
9501 case Intrinsic::x86_mmx_pslli_d:
9502 NewIntNo = Intrinsic::x86_mmx_psll_d;
9503 break;
9504 case Intrinsic::x86_mmx_pslli_q:
9505 NewIntNo = Intrinsic::x86_mmx_psll_q;
9506 break;
9507 case Intrinsic::x86_mmx_psrli_w:
9508 NewIntNo = Intrinsic::x86_mmx_psrl_w;
9509 break;
9510 case Intrinsic::x86_mmx_psrli_d:
9511 NewIntNo = Intrinsic::x86_mmx_psrl_d;
9512 break;
9513 case Intrinsic::x86_mmx_psrli_q:
9514 NewIntNo = Intrinsic::x86_mmx_psrl_q;
9515 break;
9516 case Intrinsic::x86_mmx_psrai_w:
9517 NewIntNo = Intrinsic::x86_mmx_psra_w;
9518 break;
9519 case Intrinsic::x86_mmx_psrai_d:
9520 NewIntNo = Intrinsic::x86_mmx_psra_d;
9521 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00009522 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00009523 }
9524 break;
9525 }
9526 }
Mon P Wangefa42202009-09-03 19:56:25 +00009527
9528 // The vector shift intrinsics with scalars uses 32b shift amounts but
9529 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
9530 // to be zero.
9531 SDValue ShOps[4];
9532 ShOps[0] = ShAmt;
9533 ShOps[1] = DAG.getConstant(0, MVT::i32);
9534 if (ShAmtVT == MVT::v4i32) {
9535 ShOps[2] = DAG.getUNDEF(MVT::i32);
9536 ShOps[3] = DAG.getUNDEF(MVT::i32);
9537 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
9538 } else {
9539 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00009540// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00009541 }
9542
Owen Andersone50ed302009-08-10 22:56:29 +00009543 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009544 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009545 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009546 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00009547 Op.getOperand(1), ShAmt);
9548 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00009549 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00009550}
Evan Cheng72261582005-12-20 06:22:03 +00009551
Dan Gohmand858e902010-04-17 15:26:15 +00009552SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
9553 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00009554 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9555 MFI->setReturnAddressIsTaken(true);
9556
Bill Wendling64e87322009-01-16 19:25:27 +00009557 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009558 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00009559
9560 if (Depth > 0) {
9561 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
9562 SDValue Offset =
9563 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00009564 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009565 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00009566 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009567 FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009568 MachinePointerInfo(), false, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00009569 }
9570
9571 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00009572 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00009573 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009574 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00009575}
9576
Dan Gohmand858e902010-04-17 15:26:15 +00009577SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00009578 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9579 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00009580
Owen Andersone50ed302009-08-10 22:56:29 +00009581 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009582 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00009583 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
9584 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00009585 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00009586 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00009587 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
9588 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009589 false, false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00009590 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00009591}
9592
Dan Gohman475871a2008-07-27 21:46:04 +00009593SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009594 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00009595 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009596}
9597
Dan Gohmand858e902010-04-17 15:26:15 +00009598SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009599 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00009600 SDValue Chain = Op.getOperand(0);
9601 SDValue Offset = Op.getOperand(1);
9602 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009603 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009604
Dan Gohmand8816272010-08-11 18:14:00 +00009605 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
9606 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
9607 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009608 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009609
Dan Gohmand8816272010-08-11 18:14:00 +00009610 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
9611 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009612 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009613 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
9614 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00009615 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009616 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009617
Dale Johannesene4d209d2009-02-03 20:21:25 +00009618 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00009619 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009620 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009621}
9622
Duncan Sands4a544a72011-09-06 13:37:06 +00009623SDValue X86TargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op,
9624 SelectionDAG &DAG) const {
9625 return Op.getOperand(0);
9626}
9627
9628SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
9629 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00009630 SDValue Root = Op.getOperand(0);
9631 SDValue Trmp = Op.getOperand(1); // trampoline
9632 SDValue FPtr = Op.getOperand(2); // nested function
9633 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009634 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009635
Dan Gohman69de1932008-02-06 22:27:42 +00009636 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009637
9638 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00009639 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00009640
9641 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00009642 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
9643 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00009644
Evan Cheng0e6a0522011-07-18 20:57:22 +00009645 const unsigned char N86R10 = X86_MC::getX86RegNum(X86::R10);
9646 const unsigned char N86R11 = X86_MC::getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00009647
9648 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
9649
9650 // Load the pointer to the nested function into R11.
9651 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00009652 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00009653 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009654 Addr, MachinePointerInfo(TrmpAddr),
9655 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009656
Owen Anderson825b72b2009-08-11 20:47:22 +00009657 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9658 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009659 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
9660 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00009661 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00009662
9663 // Load the 'nest' parameter value into R10.
9664 // R10 is specified in X86CallingConv.td
9665 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00009666 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9667 DAG.getConstant(10, MVT::i64));
9668 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009669 Addr, MachinePointerInfo(TrmpAddr, 10),
9670 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009671
Owen Anderson825b72b2009-08-11 20:47:22 +00009672 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9673 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009674 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
9675 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00009676 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00009677
9678 // Jump to the nested function.
9679 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00009680 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9681 DAG.getConstant(20, MVT::i64));
9682 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009683 Addr, MachinePointerInfo(TrmpAddr, 20),
9684 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009685
9686 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00009687 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9688 DAG.getConstant(22, MVT::i64));
9689 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00009690 MachinePointerInfo(TrmpAddr, 22),
9691 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009692
Duncan Sands4a544a72011-09-06 13:37:06 +00009693 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009694 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00009695 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00009696 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00009697 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00009698 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009699
9700 switch (CC) {
9701 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00009702 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00009703 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00009704 case CallingConv::X86_StdCall: {
9705 // Pass 'nest' parameter in ECX.
9706 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00009707 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009708
9709 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009710 FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00009711 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009712
Chris Lattner58d74912008-03-12 17:45:29 +00009713 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00009714 unsigned InRegCount = 0;
9715 unsigned Idx = 1;
9716
9717 for (FunctionType::param_iterator I = FTy->param_begin(),
9718 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00009719 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00009720 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00009721 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009722
9723 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00009724 report_fatal_error("Nest register in use - reduce number of inreg"
9725 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00009726 }
9727 }
9728 break;
9729 }
9730 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00009731 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00009732 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00009733 // Pass 'nest' parameter in EAX.
9734 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00009735 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009736 break;
9737 }
9738
Dan Gohman475871a2008-07-27 21:46:04 +00009739 SDValue OutChains[4];
9740 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009741
Owen Anderson825b72b2009-08-11 20:47:22 +00009742 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9743 DAG.getConstant(10, MVT::i32));
9744 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009745
Chris Lattnera62fe662010-02-05 19:20:30 +00009746 // This is storing the opcode for MOV32ri.
9747 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Evan Cheng0e6a0522011-07-18 20:57:22 +00009748 const unsigned char N86Reg = X86_MC::getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009749 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00009750 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009751 Trmp, MachinePointerInfo(TrmpAddr),
9752 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009753
Owen Anderson825b72b2009-08-11 20:47:22 +00009754 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9755 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009756 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
9757 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00009758 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009759
Chris Lattnera62fe662010-02-05 19:20:30 +00009760 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00009761 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9762 DAG.getConstant(5, MVT::i32));
9763 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00009764 MachinePointerInfo(TrmpAddr, 5),
9765 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009766
Owen Anderson825b72b2009-08-11 20:47:22 +00009767 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9768 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009769 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
9770 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00009771 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009772
Duncan Sands4a544a72011-09-06 13:37:06 +00009773 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009774 }
9775}
9776
Dan Gohmand858e902010-04-17 15:26:15 +00009777SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
9778 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009779 /*
9780 The rounding mode is in bits 11:10 of FPSR, and has the following
9781 settings:
9782 00 Round to nearest
9783 01 Round to -inf
9784 10 Round to +inf
9785 11 Round to 0
9786
9787 FLT_ROUNDS, on the other hand, expects the following:
9788 -1 Undefined
9789 0 Round to 0
9790 1 Round to nearest
9791 2 Round to +inf
9792 3 Round to -inf
9793
9794 To perform the conversion, we do:
9795 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
9796 */
9797
9798 MachineFunction &MF = DAG.getMachineFunction();
9799 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00009800 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009801 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00009802 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00009803 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009804
9805 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00009806 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00009807 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009808
Michael J. Spencerec38de22010-10-10 22:04:20 +00009809
Chris Lattner2156b792010-09-22 01:11:26 +00009810 MachineMemOperand *MMO =
9811 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
9812 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009813
Chris Lattner2156b792010-09-22 01:11:26 +00009814 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
9815 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
9816 DAG.getVTList(MVT::Other),
9817 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009818
9819 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00009820 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +00009821 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009822
9823 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00009824 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00009825 DAG.getNode(ISD::SRL, DL, MVT::i16,
9826 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00009827 CWD, DAG.getConstant(0x800, MVT::i16)),
9828 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00009829 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00009830 DAG.getNode(ISD::SRL, DL, MVT::i16,
9831 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00009832 CWD, DAG.getConstant(0x400, MVT::i16)),
9833 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009834
Dan Gohman475871a2008-07-27 21:46:04 +00009835 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00009836 DAG.getNode(ISD::AND, DL, MVT::i16,
9837 DAG.getNode(ISD::ADD, DL, MVT::i16,
9838 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00009839 DAG.getConstant(1, MVT::i16)),
9840 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009841
9842
Duncan Sands83ec4b62008-06-06 12:08:01 +00009843 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00009844 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009845}
9846
Dan Gohmand858e902010-04-17 15:26:15 +00009847SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009848 EVT VT = Op.getValueType();
9849 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009850 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009851 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00009852
9853 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009854 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00009855 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00009856 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00009857 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009858 }
Evan Cheng18efe262007-12-14 02:13:44 +00009859
Evan Cheng152804e2007-12-14 08:30:15 +00009860 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00009861 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009862 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00009863
9864 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00009865 SDValue Ops[] = {
9866 Op,
9867 DAG.getConstant(NumBits+NumBits-1, OpVT),
9868 DAG.getConstant(X86::COND_E, MVT::i8),
9869 Op.getValue(1)
9870 };
9871 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00009872
9873 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00009874 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00009875
Owen Anderson825b72b2009-08-11 20:47:22 +00009876 if (VT == MVT::i8)
9877 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009878 return Op;
9879}
9880
Dan Gohmand858e902010-04-17 15:26:15 +00009881SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009882 EVT VT = Op.getValueType();
9883 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009884 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009885 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00009886
9887 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009888 if (VT == MVT::i8) {
9889 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00009890 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009891 }
Evan Cheng152804e2007-12-14 08:30:15 +00009892
9893 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00009894 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009895 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00009896
9897 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00009898 SDValue Ops[] = {
9899 Op,
9900 DAG.getConstant(NumBits, OpVT),
9901 DAG.getConstant(X86::COND_E, MVT::i8),
9902 Op.getValue(1)
9903 };
9904 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00009905
Owen Anderson825b72b2009-08-11 20:47:22 +00009906 if (VT == MVT::i8)
9907 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009908 return Op;
9909}
9910
Craig Topper13894fa2011-08-24 06:14:18 +00009911// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
9912// ones, and then concatenate the result back.
9913static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00009914 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +00009915
9916 assert(VT.getSizeInBits() == 256 && VT.isInteger() &&
9917 "Unsupported value type for operation");
9918
9919 int NumElems = VT.getVectorNumElements();
9920 DebugLoc dl = Op.getDebugLoc();
9921 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
9922 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
9923
9924 // Extract the LHS vectors
9925 SDValue LHS = Op.getOperand(0);
9926 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
9927 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
9928
9929 // Extract the RHS vectors
9930 SDValue RHS = Op.getOperand(1);
9931 SDValue RHS1 = Extract128BitVector(RHS, Idx0, DAG, dl);
9932 SDValue RHS2 = Extract128BitVector(RHS, Idx1, DAG, dl);
9933
9934 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9935 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
9936
9937 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
9938 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
9939 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
9940}
9941
9942SDValue X86TargetLowering::LowerADD(SDValue Op, SelectionDAG &DAG) const {
9943 assert(Op.getValueType().getSizeInBits() == 256 &&
9944 Op.getValueType().isInteger() &&
9945 "Only handle AVX 256-bit vector integer operation");
9946 return Lower256IntArith(Op, DAG);
9947}
9948
9949SDValue X86TargetLowering::LowerSUB(SDValue Op, SelectionDAG &DAG) const {
9950 assert(Op.getValueType().getSizeInBits() == 256 &&
9951 Op.getValueType().isInteger() &&
9952 "Only handle AVX 256-bit vector integer operation");
9953 return Lower256IntArith(Op, DAG);
9954}
9955
9956SDValue X86TargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
9957 EVT VT = Op.getValueType();
9958
9959 // Decompose 256-bit ops into smaller 128-bit ops.
Craig Topperaaa643c2011-11-09 07:28:55 +00009960 if (VT.getSizeInBits() == 256 && !Subtarget->hasAVX2())
Craig Topper13894fa2011-08-24 06:14:18 +00009961 return Lower256IntArith(Op, DAG);
9962
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009963 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00009964
Craig Topperaaa643c2011-11-09 07:28:55 +00009965 SDValue A = Op.getOperand(0);
9966 SDValue B = Op.getOperand(1);
9967
9968 if (VT == MVT::v4i64) {
9969 assert(Subtarget->hasAVX2() && "Lowering v4i64 multiply requires AVX2");
9970
9971 // ulong2 Ahi = __builtin_ia32_psrlqi256( a, 32);
9972 // ulong2 Bhi = __builtin_ia32_psrlqi256( b, 32);
9973 // ulong2 AloBlo = __builtin_ia32_pmuludq256( a, b );
9974 // ulong2 AloBhi = __builtin_ia32_pmuludq256( a, Bhi );
9975 // ulong2 AhiBlo = __builtin_ia32_pmuludq256( Ahi, b );
9976 //
9977 // AloBhi = __builtin_ia32_psllqi256( AloBhi, 32 );
9978 // AhiBlo = __builtin_ia32_psllqi256( AhiBlo, 32 );
9979 // return AloBlo + AloBhi + AhiBlo;
9980
9981 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9982 DAG.getConstant(Intrinsic::x86_avx2_psrli_q, MVT::i32),
9983 A, DAG.getConstant(32, MVT::i32));
9984 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9985 DAG.getConstant(Intrinsic::x86_avx2_psrli_q, MVT::i32),
9986 B, DAG.getConstant(32, MVT::i32));
9987 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9988 DAG.getConstant(Intrinsic::x86_avx2_pmulu_dq, MVT::i32),
9989 A, B);
9990 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9991 DAG.getConstant(Intrinsic::x86_avx2_pmulu_dq, MVT::i32),
9992 A, Bhi);
9993 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9994 DAG.getConstant(Intrinsic::x86_avx2_pmulu_dq, MVT::i32),
9995 Ahi, B);
9996 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9997 DAG.getConstant(Intrinsic::x86_avx2_pslli_q, MVT::i32),
9998 AloBhi, DAG.getConstant(32, MVT::i32));
9999 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10000 DAG.getConstant(Intrinsic::x86_avx2_pslli_q, MVT::i32),
10001 AhiBlo, DAG.getConstant(32, MVT::i32));
10002 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
10003 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
10004 return Res;
10005 }
10006
10007 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
10008
Mon P Wangaf9b9522008-12-18 21:42:19 +000010009 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
10010 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
10011 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
10012 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
10013 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
10014 //
10015 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
10016 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
10017 // return AloBlo + AloBhi + AhiBlo;
10018
Dale Johannesene4d209d2009-02-03 20:21:25 +000010019 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010020 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
10021 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010022 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010023 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
10024 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010025 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010026 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +000010027 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010028 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010029 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +000010030 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010031 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010032 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +000010033 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010034 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010035 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
10036 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010037 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010038 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
10039 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010040 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
10041 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +000010042 return Res;
10043}
10044
Nadav Rotem43012222011-05-11 08:12:09 +000010045SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
10046
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010047 EVT VT = Op.getValueType();
10048 DebugLoc dl = Op.getDebugLoc();
10049 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +000010050 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010051 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010052
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010053 if (!Subtarget->hasXMMInt())
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +000010054 return SDValue();
10055
Nadav Rotem43012222011-05-11 08:12:09 +000010056 // Optimize shl/srl/sra with constant shift amount.
10057 if (isSplatVector(Amt.getNode())) {
10058 SDValue SclrAmt = Amt->getOperand(0);
10059 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
10060 uint64_t ShiftAmt = C->getZExtValue();
10061
Benjamin Kramerdade3c12011-10-30 17:31:21 +000010062 if (VT == MVT::v16i8 && Op.getOpcode() == ISD::SHL) {
10063 // Make a large shift.
10064 SDValue SHL =
10065 DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10066 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
10067 R, DAG.getConstant(ShiftAmt, MVT::i32));
10068 // Zero out the rightmost bits.
10069 SmallVector<SDValue, 16> V(16, DAG.getConstant(uint8_t(-1U << ShiftAmt),
10070 MVT::i8));
10071 return DAG.getNode(ISD::AND, dl, VT, SHL,
10072 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
10073 }
10074
Nadav Rotem43012222011-05-11 08:12:09 +000010075 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SHL)
10076 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10077 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
10078 R, DAG.getConstant(ShiftAmt, MVT::i32));
10079
10080 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SHL)
10081 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10082 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
10083 R, DAG.getConstant(ShiftAmt, MVT::i32));
10084
10085 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SHL)
10086 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10087 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
10088 R, DAG.getConstant(ShiftAmt, MVT::i32));
10089
Benjamin Kramerdade3c12011-10-30 17:31:21 +000010090 if (VT == MVT::v16i8 && Op.getOpcode() == ISD::SRL) {
10091 // Make a large shift.
10092 SDValue SRL =
10093 DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10094 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
10095 R, DAG.getConstant(ShiftAmt, MVT::i32));
10096 // Zero out the leftmost bits.
10097 SmallVector<SDValue, 16> V(16, DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
10098 MVT::i8));
10099 return DAG.getNode(ISD::AND, dl, VT, SRL,
10100 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
10101 }
10102
Nadav Rotem43012222011-05-11 08:12:09 +000010103 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SRL)
10104 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10105 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
10106 R, DAG.getConstant(ShiftAmt, MVT::i32));
10107
10108 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRL)
10109 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10110 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
10111 R, DAG.getConstant(ShiftAmt, MVT::i32));
10112
10113 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRL)
10114 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10115 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
10116 R, DAG.getConstant(ShiftAmt, MVT::i32));
10117
10118 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRA)
10119 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10120 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
10121 R, DAG.getConstant(ShiftAmt, MVT::i32));
10122
10123 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRA)
10124 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10125 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
10126 R, DAG.getConstant(ShiftAmt, MVT::i32));
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010127
10128 if (VT == MVT::v16i8 && Op.getOpcode() == ISD::SRA) {
10129 if (ShiftAmt == 7) {
10130 // R s>> 7 === R s< 0
10131 SDValue Zeros = getZeroVector(VT, true /* HasXMMInt */, DAG, dl);
10132 return DAG.getNode(X86ISD::PCMPGTB, dl, VT, Zeros, R);
10133 }
10134
10135 // R s>> a === ((R u>> a) ^ m) - m
10136 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
10137 SmallVector<SDValue, 16> V(16, DAG.getConstant(128 >> ShiftAmt,
10138 MVT::i8));
10139 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16);
10140 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
10141 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
10142 return Res;
10143 }
Craig Topper46154eb2011-11-11 07:39:23 +000010144
Craig Topper0d86d462011-11-20 00:12:05 +000010145 if (Subtarget->hasAVX2() && VT == MVT::v32i8) {
10146 if (Op.getOpcode() == ISD::SHL) {
10147 // Make a large shift.
10148 SDValue SHL =
10149 DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10150 DAG.getConstant(Intrinsic::x86_avx2_pslli_w, MVT::i32),
10151 R, DAG.getConstant(ShiftAmt, MVT::i32));
10152 // Zero out the rightmost bits.
10153 SmallVector<SDValue, 32> V(32, DAG.getConstant(uint8_t(-1U << ShiftAmt),
10154 MVT::i8));
10155 return DAG.getNode(ISD::AND, dl, VT, SHL,
10156 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
Craig Topper46154eb2011-11-11 07:39:23 +000010157 }
Craig Topper0d86d462011-11-20 00:12:05 +000010158 if (Op.getOpcode() == ISD::SRL) {
10159 // Make a large shift.
10160 SDValue SRL =
10161 DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10162 DAG.getConstant(Intrinsic::x86_avx2_psrli_w, MVT::i32),
10163 R, DAG.getConstant(ShiftAmt, MVT::i32));
10164 // Zero out the leftmost bits.
10165 SmallVector<SDValue, 32> V(32, DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
10166 MVT::i8));
10167 return DAG.getNode(ISD::AND, dl, VT, SRL,
10168 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
10169 }
10170 if (Op.getOpcode() == ISD::SRA) {
10171 if (ShiftAmt == 7) {
10172 // R s>> 7 === R s< 0
10173 SDValue Zeros = getZeroVector(VT, true /* HasXMMInt */, DAG, dl);
10174 return DAG.getNode(X86ISD::PCMPGTB, dl, VT, Zeros, R);
10175 }
10176
10177 // R s>> a === ((R u>> a) ^ m) - m
10178 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
10179 SmallVector<SDValue, 32> V(32, DAG.getConstant(128 >> ShiftAmt,
10180 MVT::i8));
10181 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32);
10182 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
10183 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
10184 return Res;
10185 }
10186 }
Nadav Rotem43012222011-05-11 08:12:09 +000010187 }
10188 }
10189
10190 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +000010191 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +000010192 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10193 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
10194 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
10195
10196 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010197
Nate Begeman51409212010-07-28 00:21:48 +000010198 std::vector<Constant*> CV(4, CI);
10199 Constant *C = ConstantVector::get(CV);
10200 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10201 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010202 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010203 false, false, false, 16);
Nate Begeman51409212010-07-28 00:21:48 +000010204
10205 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010206 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +000010207 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
10208 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
10209 }
Nadav Rotem43012222011-05-11 08:12:09 +000010210 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +000010211 // a = a << 5;
10212 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10213 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
10214 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
10215
10216 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
10217 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
10218
10219 std::vector<Constant*> CVM1(16, CM1);
10220 std::vector<Constant*> CVM2(16, CM2);
10221 Constant *C = ConstantVector::get(CVM1);
10222 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10223 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010224 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010225 false, false, false, 16);
Nate Begeman51409212010-07-28 00:21:48 +000010226
10227 // r = pblendv(r, psllw(r & (char16)15, 4), a);
10228 M = DAG.getNode(ISD::AND, dl, VT, R, M);
10229 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10230 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
10231 DAG.getConstant(4, MVT::i32));
Nadav Rotemfbad25e2011-09-11 15:02:23 +000010232 R = DAG.getNode(ISD::VSELECT, dl, VT, Op, R, M);
Nate Begeman51409212010-07-28 00:21:48 +000010233 // a += a
10234 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010235
Nate Begeman51409212010-07-28 00:21:48 +000010236 C = ConstantVector::get(CVM2);
10237 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10238 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010239 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010240 false, false, false, 16);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010241
Nate Begeman51409212010-07-28 00:21:48 +000010242 // r = pblendv(r, psllw(r & (char16)63, 2), a);
10243 M = DAG.getNode(ISD::AND, dl, VT, R, M);
10244 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10245 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
10246 DAG.getConstant(2, MVT::i32));
Nadav Rotemfbad25e2011-09-11 15:02:23 +000010247 R = DAG.getNode(ISD::VSELECT, dl, VT, Op, R, M);
Nate Begeman51409212010-07-28 00:21:48 +000010248 // a += a
10249 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010250
Nate Begeman51409212010-07-28 00:21:48 +000010251 // return pblendv(r, r+r, a);
Nadav Rotemfbad25e2011-09-11 15:02:23 +000010252 R = DAG.getNode(ISD::VSELECT, dl, VT, Op,
10253 R, DAG.getNode(ISD::ADD, dl, VT, R, R));
Nate Begeman51409212010-07-28 00:21:48 +000010254 return R;
10255 }
Craig Topper46154eb2011-11-11 07:39:23 +000010256
10257 // Decompose 256-bit shifts into smaller 128-bit shifts.
10258 if (VT.getSizeInBits() == 256) {
10259 int NumElems = VT.getVectorNumElements();
10260 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10261 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10262
10263 // Extract the two vectors
10264 SDValue V1 = Extract128BitVector(R, DAG.getConstant(0, MVT::i32), DAG, dl);
10265 SDValue V2 = Extract128BitVector(R, DAG.getConstant(NumElems/2, MVT::i32),
10266 DAG, dl);
10267
10268 // Recreate the shift amount vectors
10269 SDValue Amt1, Amt2;
10270 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
10271 // Constant shift amount
10272 SmallVector<SDValue, 4> Amt1Csts;
10273 SmallVector<SDValue, 4> Amt2Csts;
10274 for (int i = 0; i < NumElems/2; ++i)
10275 Amt1Csts.push_back(Amt->getOperand(i));
10276 for (int i = NumElems/2; i < NumElems; ++i)
10277 Amt2Csts.push_back(Amt->getOperand(i));
10278
10279 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
10280 &Amt1Csts[0], NumElems/2);
10281 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
10282 &Amt2Csts[0], NumElems/2);
10283 } else {
10284 // Variable shift amount
10285 Amt1 = Extract128BitVector(Amt, DAG.getConstant(0, MVT::i32), DAG, dl);
10286 Amt2 = Extract128BitVector(Amt, DAG.getConstant(NumElems/2, MVT::i32),
10287 DAG, dl);
10288 }
10289
10290 // Issue new vector shifts for the smaller types
10291 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
10292 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
10293
10294 // Concatenate the result back
10295 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
10296 }
10297
Nate Begeman51409212010-07-28 00:21:48 +000010298 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010299}
Mon P Wangaf9b9522008-12-18 21:42:19 +000010300
Dan Gohmand858e902010-04-17 15:26:15 +000010301SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +000010302 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
10303 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +000010304 // looks for this combo and may remove the "setcc" instruction if the "setcc"
10305 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +000010306 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +000010307 SDValue LHS = N->getOperand(0);
10308 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +000010309 unsigned BaseOp = 0;
10310 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010311 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +000010312 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010313 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +000010314 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +000010315 // A subtract of one will be selected as a INC. Note that INC doesn't
10316 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010317 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10318 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010319 BaseOp = X86ISD::INC;
10320 Cond = X86::COND_O;
10321 break;
10322 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010323 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +000010324 Cond = X86::COND_O;
10325 break;
10326 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010327 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +000010328 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010329 break;
10330 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +000010331 // A subtract of one will be selected as a DEC. Note that DEC doesn't
10332 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010333 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10334 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010335 BaseOp = X86ISD::DEC;
10336 Cond = X86::COND_O;
10337 break;
10338 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010339 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +000010340 Cond = X86::COND_O;
10341 break;
10342 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010343 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +000010344 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010345 break;
10346 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000010347 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000010348 Cond = X86::COND_O;
10349 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010350 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
10351 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
10352 MVT::i32);
10353 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010354
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010355 SDValue SetCC =
10356 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10357 DAG.getConstant(X86::COND_O, MVT::i32),
10358 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010359
Dan Gohman6e5fda22011-07-22 18:45:15 +000010360 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010361 }
Bill Wendling74c37652008-12-09 22:08:41 +000010362 }
Bill Wendling3fafd932008-11-26 22:37:40 +000010363
Bill Wendling61edeb52008-12-02 01:06:39 +000010364 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010365 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010366 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000010367
Bill Wendling61edeb52008-12-02 01:06:39 +000010368 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010369 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
10370 DAG.getConstant(Cond, MVT::i32),
10371 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000010372
Dan Gohman6e5fda22011-07-22 18:45:15 +000010373 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000010374}
10375
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010376SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op, SelectionDAG &DAG) const{
10377 DebugLoc dl = Op.getDebugLoc();
Craig Toppera124f942011-11-21 01:12:36 +000010378 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
10379 EVT VT = Op.getValueType();
10380
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010381 if (Subtarget->hasXMMInt() && VT.isVector()) {
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010382 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
10383 ExtraVT.getScalarType().getSizeInBits();
10384 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
10385
10386 unsigned SHLIntrinsicsID = 0;
10387 unsigned SRAIntrinsicsID = 0;
10388 switch (VT.getSimpleVT().SimpleTy) {
10389 default:
10390 return SDValue();
Craig Toppera124f942011-11-21 01:12:36 +000010391 case MVT::v4i32:
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010392 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_d;
10393 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_d;
10394 break;
Craig Toppera124f942011-11-21 01:12:36 +000010395 case MVT::v8i16:
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010396 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_w;
10397 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_w;
10398 break;
Craig Toppera124f942011-11-21 01:12:36 +000010399 case MVT::v8i32:
10400 case MVT::v16i16:
10401 if (!Subtarget->hasAVX())
10402 return SDValue();
10403 if (!Subtarget->hasAVX2()) {
10404 // needs to be split
10405 int NumElems = VT.getVectorNumElements();
10406 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
10407 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
10408
10409 // Extract the LHS vectors
10410 SDValue LHS = Op.getOperand(0);
10411 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
10412 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
10413
10414 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10415 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10416
10417 EVT ExtraEltVT = ExtraVT.getVectorElementType();
10418 int ExtraNumElems = ExtraVT.getVectorNumElements();
10419 ExtraVT = EVT::getVectorVT(*DAG.getContext(), ExtraEltVT,
10420 ExtraNumElems/2);
10421 SDValue Extra = DAG.getValueType(ExtraVT);
10422
10423 LHS1 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, Extra);
10424 LHS2 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, Extra);
10425
10426 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, LHS1, LHS2);;
10427 }
10428 if (VT == MVT::v8i32) {
10429 SHLIntrinsicsID = Intrinsic::x86_avx2_pslli_d;
10430 SRAIntrinsicsID = Intrinsic::x86_avx2_psrai_d;
10431 } else {
10432 SHLIntrinsicsID = Intrinsic::x86_avx2_pslli_w;
10433 SRAIntrinsicsID = Intrinsic::x86_avx2_psrai_w;
10434 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010435 }
10436
10437 SDValue Tmp1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10438 DAG.getConstant(SHLIntrinsicsID, MVT::i32),
Craig Toppera124f942011-11-21 01:12:36 +000010439 Op.getOperand(0), ShAmt);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010440
Nadav Rotema7934dd2011-10-10 19:31:45 +000010441 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10442 DAG.getConstant(SRAIntrinsicsID, MVT::i32),
10443 Tmp1, ShAmt);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010444 }
10445
10446 return SDValue();
10447}
10448
10449
Eric Christopher9a9d2752010-07-22 02:48:34 +000010450SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
10451 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010452
Eric Christopher77ed1352011-07-08 00:04:56 +000010453 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
10454 // There isn't any reason to disable it if the target processor supports it.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010455 if (!Subtarget->hasXMMInt() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +000010456 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +000010457 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +000010458 SDValue Ops[] = {
10459 DAG.getRegister(X86::ESP, MVT::i32), // Base
10460 DAG.getTargetConstant(1, MVT::i8), // Scale
10461 DAG.getRegister(0, MVT::i32), // Index
10462 DAG.getTargetConstant(0, MVT::i32), // Disp
10463 DAG.getRegister(0, MVT::i32), // Segment.
10464 Zero,
10465 Chain
10466 };
Michael J. Spencerec38de22010-10-10 22:04:20 +000010467 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +000010468 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
10469 array_lengthof(Ops));
10470 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +000010471 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010472
Eric Christopher9a9d2752010-07-22 02:48:34 +000010473 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +000010474 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +000010475 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010476
Chris Lattner132929a2010-08-14 17:26:09 +000010477 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10478 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
10479 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
10480 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010481
Chris Lattner132929a2010-08-14 17:26:09 +000010482 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
10483 if (!Op1 && !Op2 && !Op3 && Op4)
10484 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010485
Chris Lattner132929a2010-08-14 17:26:09 +000010486 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
10487 if (Op1 && !Op2 && !Op3 && !Op4)
10488 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010489
10490 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +000010491 // (MFENCE)>;
10492 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +000010493}
10494
Eli Friedman14648462011-07-27 22:21:52 +000010495SDValue X86TargetLowering::LowerATOMIC_FENCE(SDValue Op,
10496 SelectionDAG &DAG) const {
10497 DebugLoc dl = Op.getDebugLoc();
10498 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
10499 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
10500 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
10501 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
10502
10503 // The only fence that needs an instruction is a sequentially-consistent
10504 // cross-thread fence.
10505 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
10506 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
10507 // no-sse2). There isn't any reason to disable it if the target processor
10508 // supports it.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010509 if (Subtarget->hasXMMInt() || Subtarget->is64Bit())
Eli Friedman14648462011-07-27 22:21:52 +000010510 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
10511
10512 SDValue Chain = Op.getOperand(0);
10513 SDValue Zero = DAG.getConstant(0, MVT::i32);
10514 SDValue Ops[] = {
10515 DAG.getRegister(X86::ESP, MVT::i32), // Base
10516 DAG.getTargetConstant(1, MVT::i8), // Scale
10517 DAG.getRegister(0, MVT::i32), // Index
10518 DAG.getTargetConstant(0, MVT::i32), // Disp
10519 DAG.getRegister(0, MVT::i32), // Segment.
10520 Zero,
10521 Chain
10522 };
10523 SDNode *Res =
10524 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
10525 array_lengthof(Ops));
10526 return SDValue(Res, 0);
10527 }
10528
10529 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
10530 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
10531}
10532
10533
Dan Gohmand858e902010-04-17 15:26:15 +000010534SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010535 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010536 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000010537 unsigned Reg = 0;
10538 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000010539 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000010540 default:
10541 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000010542 case MVT::i8: Reg = X86::AL; size = 1; break;
10543 case MVT::i16: Reg = X86::AX; size = 2; break;
10544 case MVT::i32: Reg = X86::EAX; size = 4; break;
10545 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000010546 assert(Subtarget->is64Bit() && "Node not type legal!");
10547 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000010548 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000010549 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010550 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000010551 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000010552 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000010553 Op.getOperand(1),
10554 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000010555 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000010556 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010557 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010558 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
10559 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
10560 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000010561 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010562 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000010563 return cpOut;
10564}
10565
Duncan Sands1607f052008-12-01 11:39:25 +000010566SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010567 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +000010568 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010569 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000010570 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010571 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +000010572 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010573 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
10574 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000010575 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000010576 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
10577 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000010578 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000010579 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000010580 rdx.getValue(1)
10581 };
Dale Johannesene4d209d2009-02-03 20:21:25 +000010582 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010583}
10584
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010585SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +000010586 SelectionDAG &DAG) const {
10587 EVT SrcVT = Op.getOperand(0).getValueType();
10588 EVT DstVT = Op.getValueType();
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010589 assert(Subtarget->is64Bit() && !Subtarget->hasXMMInt() &&
Chris Lattner2a786eb2010-12-19 20:19:20 +000010590 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000010591 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000010592 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010593 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000010594 // i64 <=> MMX conversions are Legal.
10595 if (SrcVT==MVT::i64 && DstVT.isVector())
10596 return Op;
10597 if (DstVT==MVT::i64 && SrcVT.isVector())
10598 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000010599 // MMX <=> MMX conversions are Legal.
10600 if (SrcVT.isVector() && DstVT.isVector())
10601 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000010602 // All other conversions need to be expanded.
10603 return SDValue();
10604}
Chris Lattner5b856542010-12-20 00:59:46 +000010605
Dan Gohmand858e902010-04-17 15:26:15 +000010606SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000010607 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +000010608 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000010609 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010610 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000010611 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010612 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010613 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000010614 Node->getOperand(0),
10615 Node->getOperand(1), negOp,
10616 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000010617 cast<AtomicSDNode>(Node)->getAlignment(),
10618 cast<AtomicSDNode>(Node)->getOrdering(),
10619 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000010620}
10621
Eli Friedman327236c2011-08-24 20:50:09 +000010622static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
10623 SDNode *Node = Op.getNode();
10624 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010625 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000010626
10627 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010628 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
10629 // FIXME: On 32-bit, store -> fist or movq would be more efficient
10630 // (The only way to get a 16-byte store is cmpxchg16b)
10631 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
10632 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
10633 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000010634 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
10635 cast<AtomicSDNode>(Node)->getMemoryVT(),
10636 Node->getOperand(0),
10637 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010638 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000010639 cast<AtomicSDNode>(Node)->getOrdering(),
10640 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000010641 return Swap.getValue(1);
10642 }
10643 // Other atomic stores have a simple pattern.
10644 return Op;
10645}
10646
Chris Lattner5b856542010-12-20 00:59:46 +000010647static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
10648 EVT VT = Op.getNode()->getValueType(0);
10649
10650 // Let legalize expand this if it isn't a legal type yet.
10651 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
10652 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010653
Chris Lattner5b856542010-12-20 00:59:46 +000010654 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010655
Chris Lattner5b856542010-12-20 00:59:46 +000010656 unsigned Opc;
10657 bool ExtraOp = false;
10658 switch (Op.getOpcode()) {
10659 default: assert(0 && "Invalid code");
10660 case ISD::ADDC: Opc = X86ISD::ADD; break;
10661 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
10662 case ISD::SUBC: Opc = X86ISD::SUB; break;
10663 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
10664 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010665
Chris Lattner5b856542010-12-20 00:59:46 +000010666 if (!ExtraOp)
10667 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
10668 Op.getOperand(1));
10669 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
10670 Op.getOperand(1), Op.getOperand(2));
10671}
10672
Evan Cheng0db9fe62006-04-25 20:13:52 +000010673/// LowerOperation - Provide custom lowering hooks for some operations.
10674///
Dan Gohmand858e902010-04-17 15:26:15 +000010675SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000010676 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010677 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010678 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Eric Christopher9a9d2752010-07-22 02:48:34 +000010679 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Eli Friedman14648462011-07-27 22:21:52 +000010680 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010681 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
10682 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000010683 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010684 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000010685 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010686 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
10687 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
10688 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +000010689 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +000010690 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010691 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
10692 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
10693 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000010694 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000010695 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000010696 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010697 case ISD::SHL_PARTS:
10698 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000010699 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010700 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000010701 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010702 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000010703 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010704 case ISD::FABS: return LowerFABS(Op, DAG);
10705 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000010706 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000010707 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000010708 case ISD::SETCC: return LowerSETCC(Op, DAG);
10709 case ISD::SELECT: return LowerSELECT(Op, DAG);
10710 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010711 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010712 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000010713 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +000010714 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010715 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000010716 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
10717 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010718 case ISD::FRAME_TO_ARGS_OFFSET:
10719 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010720 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010721 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +000010722 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
10723 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000010724 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000010725 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
10726 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000010727 case ISD::MUL: return LowerMUL(Op, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000010728 case ISD::SRA:
10729 case ISD::SRL:
10730 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000010731 case ISD::SADDO:
10732 case ISD::UADDO:
10733 case ISD::SSUBO:
10734 case ISD::USUBO:
10735 case ISD::SMULO:
10736 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +000010737 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010738 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000010739 case ISD::ADDC:
10740 case ISD::ADDE:
10741 case ISD::SUBC:
10742 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000010743 case ISD::ADD: return LowerADD(Op, DAG);
10744 case ISD::SUB: return LowerSUB(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010745 }
Chris Lattner27a6c732007-11-24 07:07:01 +000010746}
10747
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010748static void ReplaceATOMIC_LOAD(SDNode *Node,
10749 SmallVectorImpl<SDValue> &Results,
10750 SelectionDAG &DAG) {
10751 DebugLoc dl = Node->getDebugLoc();
10752 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
10753
10754 // Convert wide load -> cmpxchg8b/cmpxchg16b
10755 // FIXME: On 32-bit, load -> fild or movq would be more efficient
10756 // (The only way to get a 16-byte load is cmpxchg16b)
10757 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000010758 SDValue Zero = DAG.getConstant(0, VT);
10759 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010760 Node->getOperand(0),
10761 Node->getOperand(1), Zero, Zero,
10762 cast<AtomicSDNode>(Node)->getMemOperand(),
10763 cast<AtomicSDNode>(Node)->getOrdering(),
10764 cast<AtomicSDNode>(Node)->getSynchScope());
10765 Results.push_back(Swap.getValue(0));
10766 Results.push_back(Swap.getValue(1));
10767}
10768
Duncan Sands1607f052008-12-01 11:39:25 +000010769void X86TargetLowering::
10770ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000010771 SelectionDAG &DAG, unsigned NewOp) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010772 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +000010773 assert (Node->getValueType(0) == MVT::i64 &&
10774 "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000010775
10776 SDValue Chain = Node->getOperand(0);
10777 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010778 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010779 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000010780 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010781 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000010782 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000010783 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000010784 SDValue Result =
10785 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
10786 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000010787 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000010788 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010789 Results.push_back(Result.getValue(2));
10790}
10791
Duncan Sands126d9072008-07-04 11:47:58 +000010792/// ReplaceNodeResults - Replace a node with an illegal result type
10793/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000010794void X86TargetLowering::ReplaceNodeResults(SDNode *N,
10795 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000010796 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010797 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +000010798 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000010799 default:
Duncan Sands1607f052008-12-01 11:39:25 +000010800 assert(false && "Do not know how to custom type legalize this operation!");
10801 return;
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010802 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000010803 case ISD::ADDC:
10804 case ISD::ADDE:
10805 case ISD::SUBC:
10806 case ISD::SUBE:
10807 // We don't want to expand or promote these.
10808 return;
Duncan Sands1607f052008-12-01 11:39:25 +000010809 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +000010810 std::pair<SDValue,SDValue> Vals =
10811 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +000010812 SDValue FIST = Vals.first, StackSlot = Vals.second;
10813 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000010814 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000010815 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +000010816 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +000010817 MachinePointerInfo(),
10818 false, false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +000010819 }
10820 return;
10821 }
10822 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010823 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000010824 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010825 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010826 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000010827 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000010828 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010829 eax.getValue(2));
10830 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
10831 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +000010832 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010833 Results.push_back(edx.getValue(1));
10834 return;
10835 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010836 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000010837 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000010838 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000010839 bool Regs64bit = T == MVT::i128;
10840 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000010841 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000010842 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
10843 DAG.getConstant(0, HalfT));
10844 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
10845 DAG.getConstant(1, HalfT));
10846 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
10847 Regs64bit ? X86::RAX : X86::EAX,
10848 cpInL, SDValue());
10849 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
10850 Regs64bit ? X86::RDX : X86::EDX,
10851 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000010852 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000010853 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
10854 DAG.getConstant(0, HalfT));
10855 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
10856 DAG.getConstant(1, HalfT));
10857 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
10858 Regs64bit ? X86::RBX : X86::EBX,
10859 swapInL, cpInH.getValue(1));
10860 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
10861 Regs64bit ? X86::RCX : X86::ECX,
10862 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000010863 SDValue Ops[] = { swapInH.getValue(0),
10864 N->getOperand(1),
10865 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010866 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000010867 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000010868 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
10869 X86ISD::LCMPXCHG8_DAG;
10870 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000010871 Ops, 3, T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000010872 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
10873 Regs64bit ? X86::RAX : X86::EAX,
10874 HalfT, Result.getValue(1));
10875 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
10876 Regs64bit ? X86::RDX : X86::EDX,
10877 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000010878 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000010879 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010880 Results.push_back(cpOutH.getValue(1));
10881 return;
10882 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010883 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +000010884 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
10885 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010886 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +000010887 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
10888 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010889 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +000010890 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
10891 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010892 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +000010893 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
10894 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010895 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +000010896 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
10897 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010898 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +000010899 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
10900 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010901 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +000010902 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
10903 return;
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010904 case ISD::ATOMIC_LOAD:
10905 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000010906 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000010907}
10908
Evan Cheng72261582005-12-20 06:22:03 +000010909const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
10910 switch (Opcode) {
10911 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000010912 case X86ISD::BSF: return "X86ISD::BSF";
10913 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000010914 case X86ISD::SHLD: return "X86ISD::SHLD";
10915 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000010916 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000010917 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000010918 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000010919 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000010920 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000010921 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000010922 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
10923 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
10924 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000010925 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000010926 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000010927 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000010928 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000010929 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000010930 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000010931 case X86ISD::COMI: return "X86ISD::COMI";
10932 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000010933 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000010934 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000010935 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
10936 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000010937 case X86ISD::CMOV: return "X86ISD::CMOV";
10938 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000010939 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000010940 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
10941 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000010942 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000010943 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000010944 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000010945 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000010946 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000010947 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
10948 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000010949 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000010950 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000010951 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Craig Topper31133842011-11-19 07:33:10 +000010952 case X86ISD::PSIGN: return "X86ISD::PSIGN";
Craig Toppere6a62772011-11-13 17:31:07 +000010953 case X86ISD::BLENDV: return "X86ISD::BLENDV";
10954 case X86ISD::FHADD: return "X86ISD::FHADD";
10955 case X86ISD::FHSUB: return "X86ISD::FHSUB";
Evan Cheng8ca29322006-11-10 21:43:37 +000010956 case X86ISD::FMAX: return "X86ISD::FMAX";
10957 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +000010958 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
10959 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000010960 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000010961 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010962 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000010963 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010964 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +000010965 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
10966 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010967 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
10968 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
10969 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
10970 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
10971 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
10972 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000010973 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
10974 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +000010975 case X86ISD::VSHL: return "X86ISD::VSHL";
10976 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +000010977 case X86ISD::CMPPD: return "X86ISD::CMPPD";
10978 case X86ISD::CMPPS: return "X86ISD::CMPPS";
10979 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
10980 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
10981 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
10982 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
10983 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
10984 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
10985 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
10986 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010987 case X86ISD::ADD: return "X86ISD::ADD";
10988 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000010989 case X86ISD::ADC: return "X86ISD::ADC";
10990 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000010991 case X86ISD::SMUL: return "X86ISD::SMUL";
10992 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000010993 case X86ISD::INC: return "X86ISD::INC";
10994 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000010995 case X86ISD::OR: return "X86ISD::OR";
10996 case X86ISD::XOR: return "X86ISD::XOR";
10997 case X86ISD::AND: return "X86ISD::AND";
Craig Topper54a11172011-10-14 07:06:56 +000010998 case X86ISD::ANDN: return "X86ISD::ANDN";
Craig Toppere6a62772011-11-13 17:31:07 +000010999 case X86ISD::BLSI: return "X86ISD::BLSI";
11000 case X86ISD::BLSMSK: return "X86ISD::BLSMSK";
11001 case X86ISD::BLSR: return "X86ISD::BLSR";
Evan Cheng73f24c92009-03-30 21:36:47 +000011002 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000011003 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000011004 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011005 case X86ISD::PALIGN: return "X86ISD::PALIGN";
11006 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
11007 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
11008 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
11009 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
11010 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
11011 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
11012 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
11013 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011014 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000011015 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011016 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000011017 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
11018 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011019 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
11020 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
11021 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
11022 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
11023 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
11024 case X86ISD::MOVSD: return "X86ISD::MOVSD";
11025 case X86ISD::MOVSS: return "X86ISD::MOVSS";
Craig Topper06cb6802011-11-26 20:47:44 +000011026 case X86ISD::UNPCKLP: return "X86ISD::UNPCKLP";
11027 case X86ISD::UNPCKHP: return "X86ISD::UNPCKHP";
11028 case X86ISD::PUNPCKL: return "X86ISD::PUNPCKL";
11029 case X86ISD::PUNPCKH: return "X86ISD::PUNPCKH";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000011030 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Craig Topper316cd2a2011-11-30 06:25:25 +000011031 case X86ISD::VPERMILP: return "X86ISD::VPERMILP";
Craig Topperec24e612011-11-30 07:47:51 +000011032 case X86ISD::VPERM2X128: return "X86ISD::VPERM2X128";
Dan Gohmand6708ea2009-08-15 01:38:56 +000011033 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000011034 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000011035 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000011036 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000011037 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +000011038 }
11039}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011040
Chris Lattnerc9addb72007-03-30 23:15:24 +000011041// isLegalAddressingMode - Return true if the addressing mode represented
11042// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000011043bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011044 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000011045 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011046 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000011047 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000011048
Chris Lattnerc9addb72007-03-30 23:15:24 +000011049 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011050 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000011051 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000011052
Chris Lattnerc9addb72007-03-30 23:15:24 +000011053 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000011054 unsigned GVFlags =
11055 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011056
Chris Lattnerdfed4132009-07-10 07:38:24 +000011057 // If a reference to this global requires an extra load, we can't fold it.
11058 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000011059 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011060
Chris Lattnerdfed4132009-07-10 07:38:24 +000011061 // If BaseGV requires a register for the PIC base, we cannot also have a
11062 // BaseReg specified.
11063 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000011064 return false;
Evan Cheng52787842007-08-01 23:46:47 +000011065
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011066 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000011067 if ((M != CodeModel::Small || R != Reloc::Static) &&
11068 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011069 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000011070 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011071
Chris Lattnerc9addb72007-03-30 23:15:24 +000011072 switch (AM.Scale) {
11073 case 0:
11074 case 1:
11075 case 2:
11076 case 4:
11077 case 8:
11078 // These scales always work.
11079 break;
11080 case 3:
11081 case 5:
11082 case 9:
11083 // These scales are formed with basereg+scalereg. Only accept if there is
11084 // no basereg yet.
11085 if (AM.HasBaseReg)
11086 return false;
11087 break;
11088 default: // Other stuff never works.
11089 return false;
11090 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011091
Chris Lattnerc9addb72007-03-30 23:15:24 +000011092 return true;
11093}
11094
11095
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011096bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011097 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000011098 return false;
Evan Chenge127a732007-10-29 07:57:50 +000011099 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
11100 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000011101 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +000011102 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000011103 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +000011104}
11105
Owen Andersone50ed302009-08-10 22:56:29 +000011106bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000011107 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011108 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000011109 unsigned NumBits1 = VT1.getSizeInBits();
11110 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000011111 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011112 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000011113 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011114}
Evan Cheng2bd122c2007-10-26 01:56:11 +000011115
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011116bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000011117 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011118 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000011119}
11120
Owen Andersone50ed302009-08-10 22:56:29 +000011121bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000011122 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000011123 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000011124}
11125
Owen Andersone50ed302009-08-10 22:56:29 +000011126bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000011127 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000011128 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000011129}
11130
Evan Cheng60c07e12006-07-05 22:17:51 +000011131/// isShuffleMaskLegal - Targets can use this to indicate that they only
11132/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
11133/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
11134/// are assumed to be legal.
11135bool
Eric Christopherfd179292009-08-27 18:07:15 +000011136X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000011137 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000011138 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000011139 if (VT.getSizeInBits() == 64)
Craig Topper1dc0fbc2011-12-05 07:27:14 +000011140 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +000011141
Nate Begemana09008b2009-10-19 02:17:23 +000011142 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000011143 return (VT.getVectorNumElements() == 2 ||
11144 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
11145 isMOVLMask(M, VT) ||
11146 isSHUFPMask(M, VT) ||
11147 isPSHUFDMask(M, VT) ||
11148 isPSHUFHWMask(M, VT) ||
11149 isPSHUFLWMask(M, VT) ||
Craig Topperc0d82852011-11-22 00:44:41 +000011150 isPALIGNRMask(M, VT, Subtarget->hasSSSE3orAVX()) ||
Craig Topper6347e862011-11-21 06:57:39 +000011151 isUNPCKLMask(M, VT, Subtarget->hasAVX2()) ||
11152 isUNPCKHMask(M, VT, Subtarget->hasAVX2()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000011153 isUNPCKL_v_undef_Mask(M, VT) ||
11154 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +000011155}
11156
Dan Gohman7d8143f2008-04-09 20:09:42 +000011157bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000011158X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000011159 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000011160 unsigned NumElts = VT.getVectorNumElements();
11161 // FIXME: This collection of masks seems suspect.
11162 if (NumElts == 2)
11163 return true;
11164 if (NumElts == 4 && VT.getSizeInBits() == 128) {
11165 return (isMOVLMask(Mask, VT) ||
11166 isCommutedMOVLMask(Mask, VT, true) ||
11167 isSHUFPMask(Mask, VT) ||
Craig Topper1ff73d72011-12-06 04:59:07 +000011168 isSHUFPMask(Mask, VT, /* Commuted */ true));
Evan Cheng60c07e12006-07-05 22:17:51 +000011169 }
11170 return false;
11171}
11172
11173//===----------------------------------------------------------------------===//
11174// X86 Scheduler Hooks
11175//===----------------------------------------------------------------------===//
11176
Mon P Wang63307c32008-05-05 19:05:59 +000011177// private utility function
11178MachineBasicBlock *
11179X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
11180 MachineBasicBlock *MBB,
11181 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011182 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011183 unsigned LoadOpc,
11184 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011185 unsigned notOpc,
11186 unsigned EAXreg,
11187 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011188 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000011189 // For the atomic bitwise operator, we generate
11190 // thisMBB:
11191 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000011192 // ld t1 = [bitinstr.addr]
11193 // op t2 = t1, [bitinstr.val]
11194 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000011195 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
11196 // bz newMBB
11197 // fallthrough -->nextMBB
11198 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11199 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011200 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000011201 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011202
Mon P Wang63307c32008-05-05 19:05:59 +000011203 /// First build the CFG
11204 MachineFunction *F = MBB->getParent();
11205 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011206 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11207 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11208 F->insert(MBBIter, newMBB);
11209 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011210
Dan Gohman14152b42010-07-06 20:24:04 +000011211 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11212 nextMBB->splice(nextMBB->begin(), thisMBB,
11213 llvm::next(MachineBasicBlock::iterator(bInstr)),
11214 thisMBB->end());
11215 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011216
Mon P Wang63307c32008-05-05 19:05:59 +000011217 // Update thisMBB to fall through to newMBB
11218 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011219
Mon P Wang63307c32008-05-05 19:05:59 +000011220 // newMBB jumps to itself and fall through to nextMBB
11221 newMBB->addSuccessor(nextMBB);
11222 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011223
Mon P Wang63307c32008-05-05 19:05:59 +000011224 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011225 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011226 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +000011227 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000011228 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011229 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000011230 int numArgs = bInstr->getNumOperands() - 1;
11231 for (int i=0; i < numArgs; ++i)
11232 argOpers[i] = &bInstr->getOperand(i+1);
11233
11234 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011235 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011236 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000011237
Dale Johannesen140be2d2008-08-19 18:47:28 +000011238 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011239 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011240 for (int i=0; i <= lastAddrIndx; ++i)
11241 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011242
Dale Johannesen140be2d2008-08-19 18:47:28 +000011243 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011244 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011245 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011246 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011247 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011248 tt = t1;
11249
Dale Johannesen140be2d2008-08-19 18:47:28 +000011250 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +000011251 assert((argOpers[valArgIndx]->isReg() ||
11252 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000011253 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +000011254 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011255 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000011256 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011257 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011258 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +000011259 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011260
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011261 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +000011262 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +000011263
Dale Johannesene4d209d2009-02-03 20:21:25 +000011264 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +000011265 for (int i=0; i <= lastAddrIndx; ++i)
11266 (*MIB).addOperand(*argOpers[i]);
11267 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +000011268 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011269 (*MIB).setMemRefs(bInstr->memoperands_begin(),
11270 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +000011271
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011272 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +000011273 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +000011274
Mon P Wang63307c32008-05-05 19:05:59 +000011275 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011276 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000011277
Dan Gohman14152b42010-07-06 20:24:04 +000011278 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000011279 return nextMBB;
11280}
11281
Dale Johannesen1b54c7f2008-10-03 19:41:08 +000011282// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +000011283MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011284X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
11285 MachineBasicBlock *MBB,
11286 unsigned regOpcL,
11287 unsigned regOpcH,
11288 unsigned immOpcL,
11289 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011290 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011291 // For the atomic bitwise operator, we generate
11292 // thisMBB (instructions are in pairs, except cmpxchg8b)
11293 // ld t1,t2 = [bitinstr.addr]
11294 // newMBB:
11295 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
11296 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +000011297 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011298 // mov ECX, EBX <- t5, t6
11299 // mov EAX, EDX <- t1, t2
11300 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
11301 // mov t3, t4 <- EAX, EDX
11302 // bz newMBB
11303 // result in out1, out2
11304 // fallthrough -->nextMBB
11305
11306 const TargetRegisterClass *RC = X86::GR32RegisterClass;
11307 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011308 const unsigned NotOpc = X86::NOT32r;
11309 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11310 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11311 MachineFunction::iterator MBBIter = MBB;
11312 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011313
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011314 /// First build the CFG
11315 MachineFunction *F = MBB->getParent();
11316 MachineBasicBlock *thisMBB = MBB;
11317 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11318 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11319 F->insert(MBBIter, newMBB);
11320 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011321
Dan Gohman14152b42010-07-06 20:24:04 +000011322 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11323 nextMBB->splice(nextMBB->begin(), thisMBB,
11324 llvm::next(MachineBasicBlock::iterator(bInstr)),
11325 thisMBB->end());
11326 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011327
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011328 // Update thisMBB to fall through to newMBB
11329 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011330
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011331 // newMBB jumps to itself and fall through to nextMBB
11332 newMBB->addSuccessor(nextMBB);
11333 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011334
Dale Johannesene4d209d2009-02-03 20:21:25 +000011335 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011336 // Insert instructions into newMBB based on incoming instruction
11337 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011338 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011339 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011340 MachineOperand& dest1Oper = bInstr->getOperand(0);
11341 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011342 MachineOperand* argOpers[2 + X86::AddrNumOperands];
11343 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011344 argOpers[i] = &bInstr->getOperand(i+2);
11345
Dan Gohman71ea4e52010-05-14 21:01:44 +000011346 // We use some of the operands multiple times, so conservatively just
11347 // clear any kill flags that might be present.
11348 if (argOpers[i]->isReg() && argOpers[i]->isUse())
11349 argOpers[i]->setIsKill(false);
11350 }
11351
Evan Chengad5b52f2010-01-08 19:14:57 +000011352 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011353 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +000011354
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011355 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011356 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011357 for (int i=0; i <= lastAddrIndx; ++i)
11358 (*MIB).addOperand(*argOpers[i]);
11359 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011360 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +000011361 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +000011362 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011363 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +000011364 MachineOperand newOp3 = *(argOpers[3]);
11365 if (newOp3.isImm())
11366 newOp3.setImm(newOp3.getImm()+4);
11367 else
11368 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011369 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +000011370 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011371
11372 // t3/4 are defined later, at the bottom of the loop
11373 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
11374 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011375 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011376 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011377 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011378 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
11379
Evan Cheng306b4ca2010-01-08 23:41:50 +000011380 // The subsequent operations should be using the destination registers of
11381 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +000011382 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +000011383 t1 = F->getRegInfo().createVirtualRegister(RC);
11384 t2 = F->getRegInfo().createVirtualRegister(RC);
11385 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
11386 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011387 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +000011388 t1 = dest1Oper.getReg();
11389 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011390 }
11391
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011392 int valArgIndx = lastAddrIndx + 1;
11393 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +000011394 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011395 "invalid operand");
11396 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
11397 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011398 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011399 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011400 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011401 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +000011402 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011403 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011404 (*MIB).addOperand(*argOpers[valArgIndx]);
11405 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011406 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011407 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011408 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011409 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011410 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011411 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011412 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +000011413 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011414 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011415 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011416
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011417 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011418 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011419 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011420 MIB.addReg(t2);
11421
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011422 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011423 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011424 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011425 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +000011426
Dale Johannesene4d209d2009-02-03 20:21:25 +000011427 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011428 for (int i=0; i <= lastAddrIndx; ++i)
11429 (*MIB).addOperand(*argOpers[i]);
11430
11431 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011432 (*MIB).setMemRefs(bInstr->memoperands_begin(),
11433 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011434
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011435 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011436 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011437 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011438 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000011439
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011440 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011441 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011442
Dan Gohman14152b42010-07-06 20:24:04 +000011443 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011444 return nextMBB;
11445}
11446
11447// private utility function
11448MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +000011449X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
11450 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011451 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000011452 // For the atomic min/max operator, we generate
11453 // thisMBB:
11454 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000011455 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +000011456 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +000011457 // cmp t1, t2
11458 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +000011459 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000011460 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
11461 // bz newMBB
11462 // fallthrough -->nextMBB
11463 //
11464 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11465 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011466 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000011467 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011468
Mon P Wang63307c32008-05-05 19:05:59 +000011469 /// First build the CFG
11470 MachineFunction *F = MBB->getParent();
11471 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011472 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11473 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11474 F->insert(MBBIter, newMBB);
11475 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011476
Dan Gohman14152b42010-07-06 20:24:04 +000011477 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11478 nextMBB->splice(nextMBB->begin(), thisMBB,
11479 llvm::next(MachineBasicBlock::iterator(mInstr)),
11480 thisMBB->end());
11481 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011482
Mon P Wang63307c32008-05-05 19:05:59 +000011483 // Update thisMBB to fall through to newMBB
11484 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011485
Mon P Wang63307c32008-05-05 19:05:59 +000011486 // newMBB jumps to newMBB and fall through to nextMBB
11487 newMBB->addSuccessor(nextMBB);
11488 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011489
Dale Johannesene4d209d2009-02-03 20:21:25 +000011490 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000011491 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011492 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011493 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +000011494 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011495 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000011496 int numArgs = mInstr->getNumOperands() - 1;
11497 for (int i=0; i < numArgs; ++i)
11498 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +000011499
Mon P Wang63307c32008-05-05 19:05:59 +000011500 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011501 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011502 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000011503
Mon P Wangab3e7472008-05-05 22:56:23 +000011504 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011505 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011506 for (int i=0; i <= lastAddrIndx; ++i)
11507 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +000011508
Mon P Wang63307c32008-05-05 19:05:59 +000011509 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +000011510 assert((argOpers[valArgIndx]->isReg() ||
11511 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000011512 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +000011513
11514 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +000011515 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011516 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +000011517 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011518 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000011519 (*MIB).addOperand(*argOpers[valArgIndx]);
11520
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011521 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +000011522 MIB.addReg(t1);
11523
Dale Johannesene4d209d2009-02-03 20:21:25 +000011524 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +000011525 MIB.addReg(t1);
11526 MIB.addReg(t2);
11527
11528 // Generate movc
11529 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011530 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +000011531 MIB.addReg(t2);
11532 MIB.addReg(t1);
11533
11534 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +000011535 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +000011536 for (int i=0; i <= lastAddrIndx; ++i)
11537 (*MIB).addOperand(*argOpers[i]);
11538 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +000011539 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011540 (*MIB).setMemRefs(mInstr->memoperands_begin(),
11541 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +000011542
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011543 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +000011544 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +000011545
Mon P Wang63307c32008-05-05 19:05:59 +000011546 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011547 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000011548
Dan Gohman14152b42010-07-06 20:24:04 +000011549 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000011550 return nextMBB;
11551}
11552
Eric Christopherf83a5de2009-08-27 18:08:16 +000011553// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011554// or XMM0_V32I8 in AVX all of this code can be replaced with that
11555// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +000011556MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +000011557X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +000011558 unsigned numArgs, bool memArg) const {
Craig Topperc0d82852011-11-22 00:44:41 +000011559 assert(Subtarget->hasSSE42orAVX() &&
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011560 "Target must have SSE4.2 or AVX features enabled");
11561
Eric Christopherb120ab42009-08-18 22:50:32 +000011562 DebugLoc dl = MI->getDebugLoc();
11563 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +000011564 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011565 if (!Subtarget->hasAVX()) {
11566 if (memArg)
11567 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
11568 else
11569 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
11570 } else {
11571 if (memArg)
11572 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
11573 else
11574 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
11575 }
Eric Christopherb120ab42009-08-18 22:50:32 +000011576
Eric Christopher41c902f2010-11-30 08:20:21 +000011577 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +000011578 for (unsigned i = 0; i < numArgs; ++i) {
11579 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +000011580 if (!(Op.isReg() && Op.isImplicit()))
11581 MIB.addOperand(Op);
11582 }
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011583 BuildMI(*BB, MI, dl,
11584 TII->get(Subtarget->hasAVX() ? X86::VMOVAPSrr : X86::MOVAPSrr),
11585 MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000011586 .addReg(X86::XMM0);
11587
Dan Gohman14152b42010-07-06 20:24:04 +000011588 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000011589 return BB;
11590}
11591
11592MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +000011593X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000011594 DebugLoc dl = MI->getDebugLoc();
11595 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011596
Eric Christopher228232b2010-11-30 07:20:12 +000011597 // Address into RAX/EAX, other two args into ECX, EDX.
11598 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
11599 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
11600 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
11601 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000011602 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011603
Eric Christopher228232b2010-11-30 07:20:12 +000011604 unsigned ValOps = X86::AddrNumOperands;
11605 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
11606 .addReg(MI->getOperand(ValOps).getReg());
11607 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
11608 .addReg(MI->getOperand(ValOps+1).getReg());
11609
11610 // The instruction doesn't actually take any operands though.
11611 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011612
Eric Christopher228232b2010-11-30 07:20:12 +000011613 MI->eraseFromParent(); // The pseudo is gone now.
11614 return BB;
11615}
11616
11617MachineBasicBlock *
11618X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000011619 DebugLoc dl = MI->getDebugLoc();
11620 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011621
Eric Christopher228232b2010-11-30 07:20:12 +000011622 // First arg in ECX, the second in EAX.
11623 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
11624 .addReg(MI->getOperand(0).getReg());
11625 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
11626 .addReg(MI->getOperand(1).getReg());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011627
Eric Christopher228232b2010-11-30 07:20:12 +000011628 // The instruction doesn't actually take any operands though.
11629 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011630
Eric Christopher228232b2010-11-30 07:20:12 +000011631 MI->eraseFromParent(); // The pseudo is gone now.
11632 return BB;
11633}
11634
11635MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000011636X86TargetLowering::EmitVAARG64WithCustomInserter(
11637 MachineInstr *MI,
11638 MachineBasicBlock *MBB) const {
11639 // Emit va_arg instruction on X86-64.
11640
11641 // Operands to this pseudo-instruction:
11642 // 0 ) Output : destination address (reg)
11643 // 1-5) Input : va_list address (addr, i64mem)
11644 // 6 ) ArgSize : Size (in bytes) of vararg type
11645 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
11646 // 8 ) Align : Alignment of type
11647 // 9 ) EFLAGS (implicit-def)
11648
11649 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
11650 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
11651
11652 unsigned DestReg = MI->getOperand(0).getReg();
11653 MachineOperand &Base = MI->getOperand(1);
11654 MachineOperand &Scale = MI->getOperand(2);
11655 MachineOperand &Index = MI->getOperand(3);
11656 MachineOperand &Disp = MI->getOperand(4);
11657 MachineOperand &Segment = MI->getOperand(5);
11658 unsigned ArgSize = MI->getOperand(6).getImm();
11659 unsigned ArgMode = MI->getOperand(7).getImm();
11660 unsigned Align = MI->getOperand(8).getImm();
11661
11662 // Memory Reference
11663 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
11664 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
11665 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
11666
11667 // Machine Information
11668 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11669 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
11670 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
11671 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
11672 DebugLoc DL = MI->getDebugLoc();
11673
11674 // struct va_list {
11675 // i32 gp_offset
11676 // i32 fp_offset
11677 // i64 overflow_area (address)
11678 // i64 reg_save_area (address)
11679 // }
11680 // sizeof(va_list) = 24
11681 // alignment(va_list) = 8
11682
11683 unsigned TotalNumIntRegs = 6;
11684 unsigned TotalNumXMMRegs = 8;
11685 bool UseGPOffset = (ArgMode == 1);
11686 bool UseFPOffset = (ArgMode == 2);
11687 unsigned MaxOffset = TotalNumIntRegs * 8 +
11688 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
11689
11690 /* Align ArgSize to a multiple of 8 */
11691 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
11692 bool NeedsAlign = (Align > 8);
11693
11694 MachineBasicBlock *thisMBB = MBB;
11695 MachineBasicBlock *overflowMBB;
11696 MachineBasicBlock *offsetMBB;
11697 MachineBasicBlock *endMBB;
11698
11699 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
11700 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
11701 unsigned OffsetReg = 0;
11702
11703 if (!UseGPOffset && !UseFPOffset) {
11704 // If we only pull from the overflow region, we don't create a branch.
11705 // We don't need to alter control flow.
11706 OffsetDestReg = 0; // unused
11707 OverflowDestReg = DestReg;
11708
11709 offsetMBB = NULL;
11710 overflowMBB = thisMBB;
11711 endMBB = thisMBB;
11712 } else {
11713 // First emit code to check if gp_offset (or fp_offset) is below the bound.
11714 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
11715 // If not, pull from overflow_area. (branch to overflowMBB)
11716 //
11717 // thisMBB
11718 // | .
11719 // | .
11720 // offsetMBB overflowMBB
11721 // | .
11722 // | .
11723 // endMBB
11724
11725 // Registers for the PHI in endMBB
11726 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
11727 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
11728
11729 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11730 MachineFunction *MF = MBB->getParent();
11731 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11732 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11733 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11734
11735 MachineFunction::iterator MBBIter = MBB;
11736 ++MBBIter;
11737
11738 // Insert the new basic blocks
11739 MF->insert(MBBIter, offsetMBB);
11740 MF->insert(MBBIter, overflowMBB);
11741 MF->insert(MBBIter, endMBB);
11742
11743 // Transfer the remainder of MBB and its successor edges to endMBB.
11744 endMBB->splice(endMBB->begin(), thisMBB,
11745 llvm::next(MachineBasicBlock::iterator(MI)),
11746 thisMBB->end());
11747 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
11748
11749 // Make offsetMBB and overflowMBB successors of thisMBB
11750 thisMBB->addSuccessor(offsetMBB);
11751 thisMBB->addSuccessor(overflowMBB);
11752
11753 // endMBB is a successor of both offsetMBB and overflowMBB
11754 offsetMBB->addSuccessor(endMBB);
11755 overflowMBB->addSuccessor(endMBB);
11756
11757 // Load the offset value into a register
11758 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
11759 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
11760 .addOperand(Base)
11761 .addOperand(Scale)
11762 .addOperand(Index)
11763 .addDisp(Disp, UseFPOffset ? 4 : 0)
11764 .addOperand(Segment)
11765 .setMemRefs(MMOBegin, MMOEnd);
11766
11767 // Check if there is enough room left to pull this argument.
11768 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
11769 .addReg(OffsetReg)
11770 .addImm(MaxOffset + 8 - ArgSizeA8);
11771
11772 // Branch to "overflowMBB" if offset >= max
11773 // Fall through to "offsetMBB" otherwise
11774 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
11775 .addMBB(overflowMBB);
11776 }
11777
11778 // In offsetMBB, emit code to use the reg_save_area.
11779 if (offsetMBB) {
11780 assert(OffsetReg != 0);
11781
11782 // Read the reg_save_area address.
11783 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
11784 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
11785 .addOperand(Base)
11786 .addOperand(Scale)
11787 .addOperand(Index)
11788 .addDisp(Disp, 16)
11789 .addOperand(Segment)
11790 .setMemRefs(MMOBegin, MMOEnd);
11791
11792 // Zero-extend the offset
11793 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
11794 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
11795 .addImm(0)
11796 .addReg(OffsetReg)
11797 .addImm(X86::sub_32bit);
11798
11799 // Add the offset to the reg_save_area to get the final address.
11800 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
11801 .addReg(OffsetReg64)
11802 .addReg(RegSaveReg);
11803
11804 // Compute the offset for the next argument
11805 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
11806 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
11807 .addReg(OffsetReg)
11808 .addImm(UseFPOffset ? 16 : 8);
11809
11810 // Store it back into the va_list.
11811 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
11812 .addOperand(Base)
11813 .addOperand(Scale)
11814 .addOperand(Index)
11815 .addDisp(Disp, UseFPOffset ? 4 : 0)
11816 .addOperand(Segment)
11817 .addReg(NextOffsetReg)
11818 .setMemRefs(MMOBegin, MMOEnd);
11819
11820 // Jump to endMBB
11821 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
11822 .addMBB(endMBB);
11823 }
11824
11825 //
11826 // Emit code to use overflow area
11827 //
11828
11829 // Load the overflow_area address into a register.
11830 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
11831 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
11832 .addOperand(Base)
11833 .addOperand(Scale)
11834 .addOperand(Index)
11835 .addDisp(Disp, 8)
11836 .addOperand(Segment)
11837 .setMemRefs(MMOBegin, MMOEnd);
11838
11839 // If we need to align it, do so. Otherwise, just copy the address
11840 // to OverflowDestReg.
11841 if (NeedsAlign) {
11842 // Align the overflow address
11843 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
11844 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
11845
11846 // aligned_addr = (addr + (align-1)) & ~(align-1)
11847 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
11848 .addReg(OverflowAddrReg)
11849 .addImm(Align-1);
11850
11851 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
11852 .addReg(TmpReg)
11853 .addImm(~(uint64_t)(Align-1));
11854 } else {
11855 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
11856 .addReg(OverflowAddrReg);
11857 }
11858
11859 // Compute the next overflow address after this argument.
11860 // (the overflow address should be kept 8-byte aligned)
11861 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
11862 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
11863 .addReg(OverflowDestReg)
11864 .addImm(ArgSizeA8);
11865
11866 // Store the new overflow address.
11867 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
11868 .addOperand(Base)
11869 .addOperand(Scale)
11870 .addOperand(Index)
11871 .addDisp(Disp, 8)
11872 .addOperand(Segment)
11873 .addReg(NextAddrReg)
11874 .setMemRefs(MMOBegin, MMOEnd);
11875
11876 // If we branched, emit the PHI to the front of endMBB.
11877 if (offsetMBB) {
11878 BuildMI(*endMBB, endMBB->begin(), DL,
11879 TII->get(X86::PHI), DestReg)
11880 .addReg(OffsetDestReg).addMBB(offsetMBB)
11881 .addReg(OverflowDestReg).addMBB(overflowMBB);
11882 }
11883
11884 // Erase the pseudo instruction
11885 MI->eraseFromParent();
11886
11887 return endMBB;
11888}
11889
11890MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000011891X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
11892 MachineInstr *MI,
11893 MachineBasicBlock *MBB) const {
11894 // Emit code to save XMM registers to the stack. The ABI says that the
11895 // number of registers to save is given in %al, so it's theoretically
11896 // possible to do an indirect jump trick to avoid saving all of them,
11897 // however this code takes a simpler approach and just executes all
11898 // of the stores if %al is non-zero. It's less code, and it's probably
11899 // easier on the hardware branch predictor, and stores aren't all that
11900 // expensive anyway.
11901
11902 // Create the new basic blocks. One block contains all the XMM stores,
11903 // and one block is the final destination regardless of whether any
11904 // stores were performed.
11905 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11906 MachineFunction *F = MBB->getParent();
11907 MachineFunction::iterator MBBIter = MBB;
11908 ++MBBIter;
11909 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
11910 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
11911 F->insert(MBBIter, XMMSaveMBB);
11912 F->insert(MBBIter, EndMBB);
11913
Dan Gohman14152b42010-07-06 20:24:04 +000011914 // Transfer the remainder of MBB and its successor edges to EndMBB.
11915 EndMBB->splice(EndMBB->begin(), MBB,
11916 llvm::next(MachineBasicBlock::iterator(MI)),
11917 MBB->end());
11918 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
11919
Dan Gohmand6708ea2009-08-15 01:38:56 +000011920 // The original block will now fall through to the XMM save block.
11921 MBB->addSuccessor(XMMSaveMBB);
11922 // The XMMSaveMBB will fall through to the end block.
11923 XMMSaveMBB->addSuccessor(EndMBB);
11924
11925 // Now add the instructions.
11926 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11927 DebugLoc DL = MI->getDebugLoc();
11928
11929 unsigned CountReg = MI->getOperand(0).getReg();
11930 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
11931 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
11932
11933 if (!Subtarget->isTargetWin64()) {
11934 // If %al is 0, branch around the XMM save block.
11935 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011936 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000011937 MBB->addSuccessor(EndMBB);
11938 }
11939
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011940 unsigned MOVOpc = Subtarget->hasAVX() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000011941 // In the XMM save block, save all the XMM argument registers.
11942 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
11943 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000011944 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000011945 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000011946 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000011947 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000011948 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011949 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000011950 .addFrameIndex(RegSaveFrameIndex)
11951 .addImm(/*Scale=*/1)
11952 .addReg(/*IndexReg=*/0)
11953 .addImm(/*Disp=*/Offset)
11954 .addReg(/*Segment=*/0)
11955 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000011956 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000011957 }
11958
Dan Gohman14152b42010-07-06 20:24:04 +000011959 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000011960
11961 return EndMBB;
11962}
Mon P Wang63307c32008-05-05 19:05:59 +000011963
Evan Cheng60c07e12006-07-05 22:17:51 +000011964MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000011965X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000011966 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000011967 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11968 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000011969
Chris Lattner52600972009-09-02 05:57:00 +000011970 // To "insert" a SELECT_CC instruction, we actually have to insert the
11971 // diamond control-flow pattern. The incoming instruction knows the
11972 // destination vreg to set, the condition code register to branch on, the
11973 // true/false values to select between, and a branch opcode to use.
11974 const BasicBlock *LLVM_BB = BB->getBasicBlock();
11975 MachineFunction::iterator It = BB;
11976 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000011977
Chris Lattner52600972009-09-02 05:57:00 +000011978 // thisMBB:
11979 // ...
11980 // TrueVal = ...
11981 // cmpTY ccX, r1, r2
11982 // bCC copy1MBB
11983 // fallthrough --> copy0MBB
11984 MachineBasicBlock *thisMBB = BB;
11985 MachineFunction *F = BB->getParent();
11986 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
11987 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000011988 F->insert(It, copy0MBB);
11989 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000011990
Bill Wendling730c07e2010-06-25 20:48:10 +000011991 // If the EFLAGS register isn't dead in the terminator, then claim that it's
11992 // live into the sink and copy blocks.
Jakob Stoklund Olesen4a1b9d82011-09-02 23:52:49 +000011993 if (!MI->killsRegister(X86::EFLAGS)) {
11994 copy0MBB->addLiveIn(X86::EFLAGS);
11995 sinkMBB->addLiveIn(X86::EFLAGS);
Bill Wendling730c07e2010-06-25 20:48:10 +000011996 }
11997
Dan Gohman14152b42010-07-06 20:24:04 +000011998 // Transfer the remainder of BB and its successor edges to sinkMBB.
11999 sinkMBB->splice(sinkMBB->begin(), BB,
12000 llvm::next(MachineBasicBlock::iterator(MI)),
12001 BB->end());
12002 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
12003
12004 // Add the true and fallthrough blocks as its successors.
12005 BB->addSuccessor(copy0MBB);
12006 BB->addSuccessor(sinkMBB);
12007
12008 // Create the conditional branch instruction.
12009 unsigned Opc =
12010 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
12011 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
12012
Chris Lattner52600972009-09-02 05:57:00 +000012013 // copy0MBB:
12014 // %FalseValue = ...
12015 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000012016 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000012017
Chris Lattner52600972009-09-02 05:57:00 +000012018 // sinkMBB:
12019 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
12020 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000012021 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12022 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000012023 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
12024 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
12025
Dan Gohman14152b42010-07-06 20:24:04 +000012026 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000012027 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000012028}
12029
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012030MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012031X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
12032 bool Is64Bit) const {
12033 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12034 DebugLoc DL = MI->getDebugLoc();
12035 MachineFunction *MF = BB->getParent();
12036 const BasicBlock *LLVM_BB = BB->getBasicBlock();
12037
Nick Lewycky8a8d4792011-12-02 22:16:29 +000012038 assert(getTargetMachine().Options.EnableSegmentedStacks);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012039
12040 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
12041 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
12042
12043 // BB:
12044 // ... [Till the alloca]
12045 // If stacklet is not large enough, jump to mallocMBB
12046 //
12047 // bumpMBB:
12048 // Allocate by subtracting from RSP
12049 // Jump to continueMBB
12050 //
12051 // mallocMBB:
12052 // Allocate by call to runtime
12053 //
12054 // continueMBB:
12055 // ...
12056 // [rest of original BB]
12057 //
12058
12059 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12060 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12061 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12062
12063 MachineRegisterInfo &MRI = MF->getRegInfo();
12064 const TargetRegisterClass *AddrRegClass =
12065 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
12066
12067 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
12068 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
12069 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola66bf7432011-10-26 21:16:41 +000012070 SPLimitVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012071 sizeVReg = MI->getOperand(1).getReg(),
12072 physSPReg = Is64Bit ? X86::RSP : X86::ESP;
12073
12074 MachineFunction::iterator MBBIter = BB;
12075 ++MBBIter;
12076
12077 MF->insert(MBBIter, bumpMBB);
12078 MF->insert(MBBIter, mallocMBB);
12079 MF->insert(MBBIter, continueMBB);
12080
12081 continueMBB->splice(continueMBB->begin(), BB, llvm::next
12082 (MachineBasicBlock::iterator(MI)), BB->end());
12083 continueMBB->transferSuccessorsAndUpdatePHIs(BB);
12084
12085 // Add code to the main basic block to check if the stack limit has been hit,
12086 // and if so, jump to mallocMBB otherwise to bumpMBB.
12087 BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
Rafael Espindola66bf7432011-10-26 21:16:41 +000012088 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), SPLimitVReg)
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012089 .addReg(tmpSPVReg).addReg(sizeVReg);
12090 BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
12091 .addReg(0).addImm(0).addReg(0).addImm(TlsOffset).addReg(TlsReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012092 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012093 BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
12094
12095 // bumpMBB simply decreases the stack pointer, since we know the current
12096 // stacklet has enough space.
12097 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012098 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012099 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012100 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012101 BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
12102
12103 // Calls into a routine in libgcc to allocate more space from the heap.
12104 if (Is64Bit) {
12105 BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
12106 .addReg(sizeVReg);
12107 BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
12108 .addExternalSymbol("__morestack_allocate_stack_space").addReg(X86::RDI);
12109 } else {
12110 BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
12111 .addImm(12);
12112 BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
12113 BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
12114 .addExternalSymbol("__morestack_allocate_stack_space");
12115 }
12116
12117 if (!Is64Bit)
12118 BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
12119 .addImm(16);
12120
12121 BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
12122 .addReg(Is64Bit ? X86::RAX : X86::EAX);
12123 BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
12124
12125 // Set up the CFG correctly.
12126 BB->addSuccessor(bumpMBB);
12127 BB->addSuccessor(mallocMBB);
12128 mallocMBB->addSuccessor(continueMBB);
12129 bumpMBB->addSuccessor(continueMBB);
12130
12131 // Take care of the PHI nodes.
12132 BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
12133 MI->getOperand(0).getReg())
12134 .addReg(mallocPtrVReg).addMBB(mallocMBB)
12135 .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
12136
12137 // Delete the original pseudo instruction.
12138 MI->eraseFromParent();
12139
12140 // And we're done.
12141 return continueMBB;
12142}
12143
12144MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012145X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012146 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012147 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12148 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012149
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012150 assert(!Subtarget->isTargetEnvMacho());
12151
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012152 // The lowering is pretty easy: we're just emitting the call to _alloca. The
12153 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012154
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012155 if (Subtarget->isTargetWin64()) {
12156 if (Subtarget->isTargetCygMing()) {
12157 // ___chkstk(Mingw64):
12158 // Clobbers R10, R11, RAX and EFLAGS.
12159 // Updates RSP.
12160 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
12161 .addExternalSymbol("___chkstk")
12162 .addReg(X86::RAX, RegState::Implicit)
12163 .addReg(X86::RSP, RegState::Implicit)
12164 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
12165 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
12166 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12167 } else {
12168 // __chkstk(MSVCRT): does not update stack pointer.
12169 // Clobbers R10, R11 and EFLAGS.
12170 // FIXME: RAX(allocated size) might be reused and not killed.
12171 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
12172 .addExternalSymbol("__chkstk")
12173 .addReg(X86::RAX, RegState::Implicit)
12174 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12175 // RAX has the offset to subtracted from RSP.
12176 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
12177 .addReg(X86::RSP)
12178 .addReg(X86::RAX);
12179 }
12180 } else {
12181 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012182 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
12183
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012184 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
12185 .addExternalSymbol(StackProbeSymbol)
12186 .addReg(X86::EAX, RegState::Implicit)
12187 .addReg(X86::ESP, RegState::Implicit)
12188 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
12189 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
12190 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12191 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012192
Dan Gohman14152b42010-07-06 20:24:04 +000012193 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012194 return BB;
12195}
Chris Lattner52600972009-09-02 05:57:00 +000012196
12197MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000012198X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
12199 MachineBasicBlock *BB) const {
12200 // This is pretty easy. We're taking the value that we received from
12201 // our load from the relocation, sticking it in either RDI (x86-64)
12202 // or EAX and doing an indirect call. The return value will then
12203 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000012204 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000012205 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000012206 DebugLoc DL = MI->getDebugLoc();
12207 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000012208
12209 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000012210 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000012211
Eric Christopher30ef0e52010-06-03 04:07:48 +000012212 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000012213 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12214 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000012215 .addReg(X86::RIP)
12216 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012217 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000012218 MI->getOperand(3).getTargetFlags())
12219 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000012220 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000012221 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +000012222 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000012223 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12224 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000012225 .addReg(0)
12226 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012227 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000012228 MI->getOperand(3).getTargetFlags())
12229 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000012230 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000012231 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012232 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000012233 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12234 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000012235 .addReg(TII->getGlobalBaseReg(F))
12236 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012237 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000012238 MI->getOperand(3).getTargetFlags())
12239 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000012240 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000012241 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012242 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000012243
Dan Gohman14152b42010-07-06 20:24:04 +000012244 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000012245 return BB;
12246}
12247
12248MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000012249X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012250 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000012251 switch (MI->getOpcode()) {
Richard Trieu23946fc2011-09-21 03:09:09 +000012252 default: assert(0 && "Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000012253 case X86::TAILJMPd64:
12254 case X86::TAILJMPr64:
12255 case X86::TAILJMPm64:
Richard Trieu23946fc2011-09-21 03:09:09 +000012256 assert(0 && "TAILJMP64 would not be touched here.");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000012257 case X86::TCRETURNdi64:
12258 case X86::TCRETURNri64:
12259 case X86::TCRETURNmi64:
12260 // Defs of TCRETURNxx64 has Win64's callee-saved registers, as subset.
12261 // On AMD64, additional defs should be added before register allocation.
12262 if (!Subtarget->isTargetWin64()) {
12263 MI->addRegisterDefined(X86::RSI);
12264 MI->addRegisterDefined(X86::RDI);
12265 MI->addRegisterDefined(X86::XMM6);
12266 MI->addRegisterDefined(X86::XMM7);
12267 MI->addRegisterDefined(X86::XMM8);
12268 MI->addRegisterDefined(X86::XMM9);
12269 MI->addRegisterDefined(X86::XMM10);
12270 MI->addRegisterDefined(X86::XMM11);
12271 MI->addRegisterDefined(X86::XMM12);
12272 MI->addRegisterDefined(X86::XMM13);
12273 MI->addRegisterDefined(X86::XMM14);
12274 MI->addRegisterDefined(X86::XMM15);
12275 }
12276 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012277 case X86::WIN_ALLOCA:
12278 return EmitLoweredWinAlloca(MI, BB);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012279 case X86::SEG_ALLOCA_32:
12280 return EmitLoweredSegAlloca(MI, BB, false);
12281 case X86::SEG_ALLOCA_64:
12282 return EmitLoweredSegAlloca(MI, BB, true);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012283 case X86::TLSCall_32:
12284 case X86::TLSCall_64:
12285 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000012286 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000012287 case X86::CMOV_FR32:
12288 case X86::CMOV_FR64:
12289 case X86::CMOV_V4F32:
12290 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000012291 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000012292 case X86::CMOV_V8F32:
12293 case X86::CMOV_V4F64:
12294 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000012295 case X86::CMOV_GR16:
12296 case X86::CMOV_GR32:
12297 case X86::CMOV_RFP32:
12298 case X86::CMOV_RFP64:
12299 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012300 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000012301
Dale Johannesen849f2142007-07-03 00:53:03 +000012302 case X86::FP32_TO_INT16_IN_MEM:
12303 case X86::FP32_TO_INT32_IN_MEM:
12304 case X86::FP32_TO_INT64_IN_MEM:
12305 case X86::FP64_TO_INT16_IN_MEM:
12306 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000012307 case X86::FP64_TO_INT64_IN_MEM:
12308 case X86::FP80_TO_INT16_IN_MEM:
12309 case X86::FP80_TO_INT32_IN_MEM:
12310 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000012311 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12312 DebugLoc DL = MI->getDebugLoc();
12313
Evan Cheng60c07e12006-07-05 22:17:51 +000012314 // Change the floating point control register to use "round towards zero"
12315 // mode when truncating to an integer value.
12316 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000012317 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000012318 addFrameReference(BuildMI(*BB, MI, DL,
12319 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012320
12321 // Load the old value of the high byte of the control word...
12322 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000012323 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000012324 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000012325 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012326
12327 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000012328 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012329 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000012330
12331 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000012332 addFrameReference(BuildMI(*BB, MI, DL,
12333 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012334
12335 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000012336 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012337 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000012338
12339 // Get the X86 opcode to use.
12340 unsigned Opc;
12341 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000012342 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000012343 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
12344 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
12345 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
12346 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
12347 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
12348 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000012349 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
12350 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
12351 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000012352 }
12353
12354 X86AddressMode AM;
12355 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000012356 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012357 AM.BaseType = X86AddressMode::RegBase;
12358 AM.Base.Reg = Op.getReg();
12359 } else {
12360 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000012361 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000012362 }
12363 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000012364 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012365 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012366 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000012367 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012368 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012369 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000012370 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012371 AM.GV = Op.getGlobal();
12372 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000012373 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012374 }
Dan Gohman14152b42010-07-06 20:24:04 +000012375 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000012376 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000012377
12378 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000012379 addFrameReference(BuildMI(*BB, MI, DL,
12380 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012381
Dan Gohman14152b42010-07-06 20:24:04 +000012382 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000012383 return BB;
12384 }
Eric Christopherb120ab42009-08-18 22:50:32 +000012385 // String/text processing lowering.
12386 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012387 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000012388 return EmitPCMP(MI, BB, 3, false /* in-mem */);
12389 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012390 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000012391 return EmitPCMP(MI, BB, 3, true /* in-mem */);
12392 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012393 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000012394 return EmitPCMP(MI, BB, 5, false /* in mem */);
12395 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012396 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000012397 return EmitPCMP(MI, BB, 5, true /* in mem */);
12398
Eric Christopher228232b2010-11-30 07:20:12 +000012399 // Thread synchronization.
12400 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012401 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000012402 case X86::MWAIT:
12403 return EmitMwait(MI, BB);
12404
Eric Christopherb120ab42009-08-18 22:50:32 +000012405 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000012406 case X86::ATOMAND32:
12407 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012408 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012409 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012410 X86::NOT32r, X86::EAX,
12411 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000012412 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000012413 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
12414 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012415 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012416 X86::NOT32r, X86::EAX,
12417 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000012418 case X86::ATOMXOR32:
12419 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012420 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012421 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012422 X86::NOT32r, X86::EAX,
12423 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000012424 case X86::ATOMNAND32:
12425 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012426 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012427 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012428 X86::NOT32r, X86::EAX,
12429 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000012430 case X86::ATOMMIN32:
12431 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
12432 case X86::ATOMMAX32:
12433 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
12434 case X86::ATOMUMIN32:
12435 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
12436 case X86::ATOMUMAX32:
12437 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000012438
12439 case X86::ATOMAND16:
12440 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
12441 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012442 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012443 X86::NOT16r, X86::AX,
12444 X86::GR16RegisterClass);
12445 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000012446 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012447 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012448 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012449 X86::NOT16r, X86::AX,
12450 X86::GR16RegisterClass);
12451 case X86::ATOMXOR16:
12452 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
12453 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012454 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012455 X86::NOT16r, X86::AX,
12456 X86::GR16RegisterClass);
12457 case X86::ATOMNAND16:
12458 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
12459 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012460 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012461 X86::NOT16r, X86::AX,
12462 X86::GR16RegisterClass, true);
12463 case X86::ATOMMIN16:
12464 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
12465 case X86::ATOMMAX16:
12466 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
12467 case X86::ATOMUMIN16:
12468 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
12469 case X86::ATOMUMAX16:
12470 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
12471
12472 case X86::ATOMAND8:
12473 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
12474 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012475 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012476 X86::NOT8r, X86::AL,
12477 X86::GR8RegisterClass);
12478 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000012479 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012480 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012481 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012482 X86::NOT8r, X86::AL,
12483 X86::GR8RegisterClass);
12484 case X86::ATOMXOR8:
12485 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
12486 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012487 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012488 X86::NOT8r, X86::AL,
12489 X86::GR8RegisterClass);
12490 case X86::ATOMNAND8:
12491 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
12492 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012493 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012494 X86::NOT8r, X86::AL,
12495 X86::GR8RegisterClass, true);
12496 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012497 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000012498 case X86::ATOMAND64:
12499 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012500 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012501 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012502 X86::NOT64r, X86::RAX,
12503 X86::GR64RegisterClass);
12504 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000012505 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
12506 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012507 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012508 X86::NOT64r, X86::RAX,
12509 X86::GR64RegisterClass);
12510 case X86::ATOMXOR64:
12511 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012512 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012513 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012514 X86::NOT64r, X86::RAX,
12515 X86::GR64RegisterClass);
12516 case X86::ATOMNAND64:
12517 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
12518 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012519 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012520 X86::NOT64r, X86::RAX,
12521 X86::GR64RegisterClass, true);
12522 case X86::ATOMMIN64:
12523 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
12524 case X86::ATOMMAX64:
12525 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
12526 case X86::ATOMUMIN64:
12527 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
12528 case X86::ATOMUMAX64:
12529 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012530
12531 // This group does 64-bit operations on a 32-bit host.
12532 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012533 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012534 X86::AND32rr, X86::AND32rr,
12535 X86::AND32ri, X86::AND32ri,
12536 false);
12537 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012538 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012539 X86::OR32rr, X86::OR32rr,
12540 X86::OR32ri, X86::OR32ri,
12541 false);
12542 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012543 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012544 X86::XOR32rr, X86::XOR32rr,
12545 X86::XOR32ri, X86::XOR32ri,
12546 false);
12547 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012548 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012549 X86::AND32rr, X86::AND32rr,
12550 X86::AND32ri, X86::AND32ri,
12551 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012552 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012553 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012554 X86::ADD32rr, X86::ADC32rr,
12555 X86::ADD32ri, X86::ADC32ri,
12556 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012557 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012558 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012559 X86::SUB32rr, X86::SBB32rr,
12560 X86::SUB32ri, X86::SBB32ri,
12561 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000012562 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012563 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000012564 X86::MOV32rr, X86::MOV32rr,
12565 X86::MOV32ri, X86::MOV32ri,
12566 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012567 case X86::VASTART_SAVE_XMM_REGS:
12568 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000012569
12570 case X86::VAARG_64:
12571 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000012572 }
12573}
12574
12575//===----------------------------------------------------------------------===//
12576// X86 Optimization Hooks
12577//===----------------------------------------------------------------------===//
12578
Dan Gohman475871a2008-07-27 21:46:04 +000012579void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000012580 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000012581 APInt &KnownZero,
12582 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000012583 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000012584 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012585 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000012586 assert((Opc >= ISD::BUILTIN_OP_END ||
12587 Opc == ISD::INTRINSIC_WO_CHAIN ||
12588 Opc == ISD::INTRINSIC_W_CHAIN ||
12589 Opc == ISD::INTRINSIC_VOID) &&
12590 "Should use MaskedValueIsZero if you don't know whether Op"
12591 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012592
Dan Gohmanf4f92f52008-02-13 23:07:24 +000012593 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012594 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000012595 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012596 case X86ISD::ADD:
12597 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000012598 case X86ISD::ADC:
12599 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012600 case X86ISD::SMUL:
12601 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000012602 case X86ISD::INC:
12603 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000012604 case X86ISD::OR:
12605 case X86ISD::XOR:
12606 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012607 // These nodes' second result is a boolean.
12608 if (Op.getResNo() == 0)
12609 break;
12610 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012611 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000012612 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
12613 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000012614 break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000012615 case ISD::INTRINSIC_WO_CHAIN: {
12616 unsigned IntId = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
12617 unsigned NumLoBits = 0;
12618 switch (IntId) {
12619 default: break;
12620 case Intrinsic::x86_sse_movmsk_ps:
12621 case Intrinsic::x86_avx_movmsk_ps_256:
12622 case Intrinsic::x86_sse2_movmsk_pd:
12623 case Intrinsic::x86_avx_movmsk_pd_256:
12624 case Intrinsic::x86_mmx_pmovmskb:
12625 case Intrinsic::x86_sse2_pmovmskb_128: {
12626 // High bits of movmskp{s|d}, pmovmskb are known zero.
12627 switch (IntId) {
12628 case Intrinsic::x86_sse_movmsk_ps: NumLoBits = 4; break;
12629 case Intrinsic::x86_avx_movmsk_ps_256: NumLoBits = 8; break;
12630 case Intrinsic::x86_sse2_movmsk_pd: NumLoBits = 2; break;
12631 case Intrinsic::x86_avx_movmsk_pd_256: NumLoBits = 4; break;
12632 case Intrinsic::x86_mmx_pmovmskb: NumLoBits = 8; break;
12633 case Intrinsic::x86_sse2_pmovmskb_128: NumLoBits = 16; break;
12634 }
12635 KnownZero = APInt::getHighBitsSet(Mask.getBitWidth(),
12636 Mask.getBitWidth() - NumLoBits);
12637 break;
12638 }
12639 }
12640 break;
12641 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012642 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012643}
Chris Lattner259e97c2006-01-31 19:43:35 +000012644
Owen Andersonbc146b02010-09-21 20:42:50 +000012645unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
12646 unsigned Depth) const {
12647 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
12648 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
12649 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000012650
Owen Andersonbc146b02010-09-21 20:42:50 +000012651 // Fallback case.
12652 return 1;
12653}
12654
Evan Cheng206ee9d2006-07-07 08:33:52 +000012655/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000012656/// node is a GlobalAddress + offset.
12657bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000012658 const GlobalValue* &GA,
12659 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000012660 if (N->getOpcode() == X86ISD::Wrapper) {
12661 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000012662 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000012663 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000012664 return true;
12665 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000012666 }
Evan Chengad4196b2008-05-12 19:56:52 +000012667 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000012668}
12669
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012670/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
12671/// same as extracting the high 128-bit part of 256-bit vector and then
12672/// inserting the result into the low part of a new 256-bit vector
12673static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
12674 EVT VT = SVOp->getValueType(0);
12675 int NumElems = VT.getVectorNumElements();
12676
12677 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
12678 for (int i = 0, j = NumElems/2; i < NumElems/2; ++i, ++j)
12679 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
12680 SVOp->getMaskElt(j) >= 0)
12681 return false;
12682
12683 return true;
12684}
12685
12686/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
12687/// same as extracting the low 128-bit part of 256-bit vector and then
12688/// inserting the result into the high part of a new 256-bit vector
12689static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
12690 EVT VT = SVOp->getValueType(0);
12691 int NumElems = VT.getVectorNumElements();
12692
12693 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
12694 for (int i = NumElems/2, j = 0; i < NumElems; ++i, ++j)
12695 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
12696 SVOp->getMaskElt(j) >= 0)
12697 return false;
12698
12699 return true;
12700}
12701
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012702/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
12703static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
12704 TargetLowering::DAGCombinerInfo &DCI) {
12705 DebugLoc dl = N->getDebugLoc();
12706 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
12707 SDValue V1 = SVOp->getOperand(0);
12708 SDValue V2 = SVOp->getOperand(1);
12709 EVT VT = SVOp->getValueType(0);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012710 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012711
12712 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
12713 V2.getOpcode() == ISD::CONCAT_VECTORS) {
12714 //
12715 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000012716 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012717 // V UNDEF BUILD_VECTOR UNDEF
12718 // \ / \ /
12719 // CONCAT_VECTOR CONCAT_VECTOR
12720 // \ /
12721 // \ /
12722 // RESULT: V + zero extended
12723 //
12724 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
12725 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
12726 V1.getOperand(1).getOpcode() != ISD::UNDEF)
12727 return SDValue();
12728
12729 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
12730 return SDValue();
12731
12732 // To match the shuffle mask, the first half of the mask should
12733 // be exactly the first vector, and all the rest a splat with the
12734 // first element of the second one.
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012735 for (int i = 0; i < NumElems/2; ++i)
12736 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
12737 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
12738 return SDValue();
12739
12740 // Emit a zeroed vector and insert the desired subvector on its
12741 // first half.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000012742 SDValue Zeros = getZeroVector(VT, true /* HasXMMInt */, DAG, dl);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012743 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0),
12744 DAG.getConstant(0, MVT::i32), DAG, dl);
12745 return DCI.CombineTo(N, InsV);
12746 }
12747
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012748 //===--------------------------------------------------------------------===//
12749 // Combine some shuffles into subvector extracts and inserts:
12750 //
12751
12752 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
12753 if (isShuffleHigh128VectorInsertLow(SVOp)) {
12754 SDValue V = Extract128BitVector(V1, DAG.getConstant(NumElems/2, MVT::i32),
12755 DAG, dl);
12756 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT),
12757 V, DAG.getConstant(0, MVT::i32), DAG, dl);
12758 return DCI.CombineTo(N, InsV);
12759 }
12760
12761 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
12762 if (isShuffleLow128VectorInsertHigh(SVOp)) {
12763 SDValue V = Extract128BitVector(V1, DAG.getConstant(0, MVT::i32), DAG, dl);
12764 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT),
12765 V, DAG.getConstant(NumElems/2, MVT::i32), DAG, dl);
12766 return DCI.CombineTo(N, InsV);
12767 }
12768
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012769 return SDValue();
12770}
12771
12772/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000012773static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000012774 TargetLowering::DAGCombinerInfo &DCI,
12775 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000012776 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000012777 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000012778
Mon P Wanga0fd0d52010-12-19 23:55:53 +000012779 // Don't create instructions with illegal types after legalize types has run.
12780 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12781 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
12782 return SDValue();
12783
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000012784 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
12785 if (Subtarget->hasAVX() && VT.getSizeInBits() == 256 &&
12786 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012787 return PerformShuffleCombine256(N, DAG, DCI);
12788
12789 // Only handle 128 wide vector from here on.
12790 if (VT.getSizeInBits() != 128)
12791 return SDValue();
12792
12793 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
12794 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
12795 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000012796 SmallVector<SDValue, 16> Elts;
12797 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012798 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000012799
Nate Begemanfdea31a2010-03-24 20:49:50 +000012800 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000012801}
Evan Chengd880b972008-05-09 21:53:03 +000012802
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000012803/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
12804/// generation and convert it from being a bunch of shuffles and extracts
12805/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012806static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
12807 const TargetLowering &TLI) {
12808 SDValue InputVector = N->getOperand(0);
12809
12810 // Only operate on vectors of 4 elements, where the alternative shuffling
12811 // gets to be more expensive.
12812 if (InputVector.getValueType() != MVT::v4i32)
12813 return SDValue();
12814
12815 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
12816 // single use which is a sign-extend or zero-extend, and all elements are
12817 // used.
12818 SmallVector<SDNode *, 4> Uses;
12819 unsigned ExtractedElements = 0;
12820 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
12821 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
12822 if (UI.getUse().getResNo() != InputVector.getResNo())
12823 return SDValue();
12824
12825 SDNode *Extract = *UI;
12826 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
12827 return SDValue();
12828
12829 if (Extract->getValueType(0) != MVT::i32)
12830 return SDValue();
12831 if (!Extract->hasOneUse())
12832 return SDValue();
12833 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
12834 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
12835 return SDValue();
12836 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
12837 return SDValue();
12838
12839 // Record which element was extracted.
12840 ExtractedElements |=
12841 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
12842
12843 Uses.push_back(Extract);
12844 }
12845
12846 // If not all the elements were used, this may not be worthwhile.
12847 if (ExtractedElements != 15)
12848 return SDValue();
12849
12850 // Ok, we've now decided to do the transformation.
12851 DebugLoc dl = InputVector.getDebugLoc();
12852
12853 // Store the value to a temporary stack slot.
12854 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000012855 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
12856 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012857
12858 // Replace each use (extract) with a load of the appropriate element.
12859 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
12860 UE = Uses.end(); UI != UE; ++UI) {
12861 SDNode *Extract = *UI;
12862
Nadav Rotem86694292011-05-17 08:31:57 +000012863 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012864 SDValue Idx = Extract->getOperand(1);
12865 unsigned EltSize =
12866 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
12867 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
12868 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
12869
Nadav Rotem86694292011-05-17 08:31:57 +000012870 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000012871 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012872
12873 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000012874 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000012875 ScalarAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000012876 false, false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012877
12878 // Replace the exact with the load.
12879 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
12880 }
12881
12882 // The replacement was made in place; don't return anything.
12883 return SDValue();
12884}
12885
Duncan Sands6bcd2192011-09-17 16:49:39 +000012886/// PerformSELECTCombine - Do target-specific dag combines on SELECT and VSELECT
12887/// nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000012888static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000012889 const X86Subtarget *Subtarget) {
12890 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000012891 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000012892 // Get the LHS/RHS of the select.
12893 SDValue LHS = N->getOperand(1);
12894 SDValue RHS = N->getOperand(2);
Bruno Cardoso Lopes149f29f2011-09-20 22:34:45 +000012895 EVT VT = LHS.getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +000012896
Dan Gohman670e5392009-09-21 18:03:22 +000012897 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000012898 // instructions match the semantics of the common C idiom x<y?x:y but not
12899 // x<=y?x:y, because of how they handle negative zero (which can be
12900 // ignored in unsafe-math mode).
Benjamin Kramer2c2ccbf2011-09-22 03:27:22 +000012901 if (Cond.getOpcode() == ISD::SETCC && VT.isFloatingPoint() &&
12902 VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT) &&
12903 (Subtarget->hasXMMInt() ||
12904 (Subtarget->hasSSE1() && VT.getScalarType() == MVT::f32))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000012905 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012906
Chris Lattner47b4ce82009-03-11 05:48:52 +000012907 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000012908 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000012909 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
12910 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000012911 switch (CC) {
12912 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000012913 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000012914 // Converting this to a min would handle NaNs incorrectly, and swapping
12915 // the operands would cause it to handle comparisons between positive
12916 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000012917 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000012918 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000012919 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
12920 break;
12921 std::swap(LHS, RHS);
12922 }
Dan Gohman670e5392009-09-21 18:03:22 +000012923 Opcode = X86ISD::FMIN;
12924 break;
12925 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000012926 // Converting this to a min would handle comparisons between positive
12927 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000012928 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000012929 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
12930 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012931 Opcode = X86ISD::FMIN;
12932 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000012933 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000012934 // Converting this to a min would handle both negative zeros and NaNs
12935 // incorrectly, but we can swap the operands to fix both.
12936 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012937 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012938 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000012939 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012940 Opcode = X86ISD::FMIN;
12941 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012942
Dan Gohman670e5392009-09-21 18:03:22 +000012943 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012944 // Converting this to a max would handle comparisons between positive
12945 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000012946 if (!DAG.getTarget().Options.UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000012947 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012948 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012949 Opcode = X86ISD::FMAX;
12950 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000012951 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000012952 // Converting this to a max would handle NaNs incorrectly, and swapping
12953 // the operands would cause it to handle comparisons between positive
12954 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000012955 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000012956 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000012957 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
12958 break;
12959 std::swap(LHS, RHS);
12960 }
Dan Gohman670e5392009-09-21 18:03:22 +000012961 Opcode = X86ISD::FMAX;
12962 break;
12963 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012964 // Converting this to a max would handle both negative zeros and NaNs
12965 // incorrectly, but we can swap the operands to fix both.
12966 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012967 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012968 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012969 case ISD::SETGE:
12970 Opcode = X86ISD::FMAX;
12971 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000012972 }
Dan Gohman670e5392009-09-21 18:03:22 +000012973 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000012974 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
12975 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000012976 switch (CC) {
12977 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000012978 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012979 // Converting this to a min would handle comparisons between positive
12980 // and negative zero incorrectly, and swapping the operands would
12981 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000012982 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000012983 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000012984 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012985 break;
12986 std::swap(LHS, RHS);
12987 }
Dan Gohman670e5392009-09-21 18:03:22 +000012988 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000012989 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012990 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000012991 // Converting this to a min would handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000012992 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000012993 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
12994 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012995 Opcode = X86ISD::FMIN;
12996 break;
12997 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012998 // Converting this to a min would handle both negative zeros and NaNs
12999 // incorrectly, but we can swap the operands to fix both.
13000 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013001 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013002 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013003 case ISD::SETGE:
13004 Opcode = X86ISD::FMIN;
13005 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013006
Dan Gohman670e5392009-09-21 18:03:22 +000013007 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000013008 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000013009 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013010 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013011 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000013012 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013013 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000013014 // Converting this to a max would handle comparisons between positive
13015 // and negative zero incorrectly, and swapping the operands would
13016 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013017 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013018 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000013019 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013020 break;
13021 std::swap(LHS, RHS);
13022 }
Dan Gohman670e5392009-09-21 18:03:22 +000013023 Opcode = X86ISD::FMAX;
13024 break;
13025 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000013026 // Converting this to a max would handle both negative zeros and NaNs
13027 // incorrectly, but we can swap the operands to fix both.
13028 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013029 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013030 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000013031 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013032 Opcode = X86ISD::FMAX;
13033 break;
13034 }
Chris Lattner83e6c992006-10-04 06:57:07 +000013035 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013036
Chris Lattner47b4ce82009-03-11 05:48:52 +000013037 if (Opcode)
13038 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000013039 }
Eric Christopherfd179292009-08-27 18:07:15 +000013040
Chris Lattnerd1980a52009-03-12 06:52:53 +000013041 // If this is a select between two integer constants, try to do some
13042 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000013043 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
13044 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000013045 // Don't do this for crazy integer types.
13046 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
13047 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000013048 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000013049 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000013050
Chris Lattnercee56e72009-03-13 05:53:31 +000013051 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000013052 // Efficiently invertible.
13053 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
13054 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
13055 isa<ConstantSDNode>(Cond.getOperand(1))))) {
13056 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000013057 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000013058 }
Eric Christopherfd179292009-08-27 18:07:15 +000013059
Chris Lattnerd1980a52009-03-12 06:52:53 +000013060 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000013061 if (FalseC->getAPIntValue() == 0 &&
13062 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000013063 if (NeedsCondInvert) // Invert the condition if needed.
13064 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13065 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013066
Chris Lattnerd1980a52009-03-12 06:52:53 +000013067 // Zero extend the condition if needed.
13068 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013069
Chris Lattnercee56e72009-03-13 05:53:31 +000013070 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000013071 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000013072 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000013073 }
Eric Christopherfd179292009-08-27 18:07:15 +000013074
Chris Lattner97a29a52009-03-13 05:22:11 +000013075 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000013076 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000013077 if (NeedsCondInvert) // Invert the condition if needed.
13078 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13079 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013080
Chris Lattner97a29a52009-03-13 05:22:11 +000013081 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000013082 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
13083 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000013084 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000013085 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000013086 }
Eric Christopherfd179292009-08-27 18:07:15 +000013087
Chris Lattnercee56e72009-03-13 05:53:31 +000013088 // Optimize cases that will turn into an LEA instruction. This requires
13089 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000013090 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000013091 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000013092 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000013093
Chris Lattnercee56e72009-03-13 05:53:31 +000013094 bool isFastMultiplier = false;
13095 if (Diff < 10) {
13096 switch ((unsigned char)Diff) {
13097 default: break;
13098 case 1: // result = add base, cond
13099 case 2: // result = lea base( , cond*2)
13100 case 3: // result = lea base(cond, cond*2)
13101 case 4: // result = lea base( , cond*4)
13102 case 5: // result = lea base(cond, cond*4)
13103 case 8: // result = lea base( , cond*8)
13104 case 9: // result = lea base(cond, cond*8)
13105 isFastMultiplier = true;
13106 break;
13107 }
13108 }
Eric Christopherfd179292009-08-27 18:07:15 +000013109
Chris Lattnercee56e72009-03-13 05:53:31 +000013110 if (isFastMultiplier) {
13111 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
13112 if (NeedsCondInvert) // Invert the condition if needed.
13113 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13114 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013115
Chris Lattnercee56e72009-03-13 05:53:31 +000013116 // Zero extend the condition if needed.
13117 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
13118 Cond);
13119 // Scale the condition by the difference.
13120 if (Diff != 1)
13121 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
13122 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013123
Chris Lattnercee56e72009-03-13 05:53:31 +000013124 // Add the base if non-zero.
13125 if (FalseC->getAPIntValue() != 0)
13126 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
13127 SDValue(FalseC, 0));
13128 return Cond;
13129 }
Eric Christopherfd179292009-08-27 18:07:15 +000013130 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000013131 }
13132 }
Eric Christopherfd179292009-08-27 18:07:15 +000013133
Dan Gohman475871a2008-07-27 21:46:04 +000013134 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000013135}
13136
Chris Lattnerd1980a52009-03-12 06:52:53 +000013137/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
13138static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
13139 TargetLowering::DAGCombinerInfo &DCI) {
13140 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000013141
Chris Lattnerd1980a52009-03-12 06:52:53 +000013142 // If the flag operand isn't dead, don't touch this CMOV.
13143 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
13144 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000013145
Evan Chengb5a55d92011-05-24 01:48:22 +000013146 SDValue FalseOp = N->getOperand(0);
13147 SDValue TrueOp = N->getOperand(1);
13148 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
13149 SDValue Cond = N->getOperand(3);
13150 if (CC == X86::COND_E || CC == X86::COND_NE) {
13151 switch (Cond.getOpcode()) {
13152 default: break;
13153 case X86ISD::BSR:
13154 case X86ISD::BSF:
13155 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
13156 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
13157 return (CC == X86::COND_E) ? FalseOp : TrueOp;
13158 }
13159 }
13160
Chris Lattnerd1980a52009-03-12 06:52:53 +000013161 // If this is a select between two integer constants, try to do some
13162 // optimizations. Note that the operands are ordered the opposite of SELECT
13163 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000013164 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
13165 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000013166 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
13167 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000013168 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
13169 CC = X86::GetOppositeBranchCondition(CC);
13170 std::swap(TrueC, FalseC);
13171 }
Eric Christopherfd179292009-08-27 18:07:15 +000013172
Chris Lattnerd1980a52009-03-12 06:52:53 +000013173 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000013174 // This is efficient for any integer data type (including i8/i16) and
13175 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000013176 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000013177 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
13178 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013179
Chris Lattnerd1980a52009-03-12 06:52:53 +000013180 // Zero extend the condition if needed.
13181 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013182
Chris Lattnerd1980a52009-03-12 06:52:53 +000013183 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
13184 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000013185 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000013186 if (N->getNumValues() == 2) // Dead flag value?
13187 return DCI.CombineTo(N, Cond, SDValue());
13188 return Cond;
13189 }
Eric Christopherfd179292009-08-27 18:07:15 +000013190
Chris Lattnercee56e72009-03-13 05:53:31 +000013191 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
13192 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000013193 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000013194 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
13195 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013196
Chris Lattner97a29a52009-03-13 05:22:11 +000013197 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000013198 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
13199 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000013200 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
13201 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000013202
Chris Lattner97a29a52009-03-13 05:22:11 +000013203 if (N->getNumValues() == 2) // Dead flag value?
13204 return DCI.CombineTo(N, Cond, SDValue());
13205 return Cond;
13206 }
Eric Christopherfd179292009-08-27 18:07:15 +000013207
Chris Lattnercee56e72009-03-13 05:53:31 +000013208 // Optimize cases that will turn into an LEA instruction. This requires
13209 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000013210 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000013211 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000013212 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000013213
Chris Lattnercee56e72009-03-13 05:53:31 +000013214 bool isFastMultiplier = false;
13215 if (Diff < 10) {
13216 switch ((unsigned char)Diff) {
13217 default: break;
13218 case 1: // result = add base, cond
13219 case 2: // result = lea base( , cond*2)
13220 case 3: // result = lea base(cond, cond*2)
13221 case 4: // result = lea base( , cond*4)
13222 case 5: // result = lea base(cond, cond*4)
13223 case 8: // result = lea base( , cond*8)
13224 case 9: // result = lea base(cond, cond*8)
13225 isFastMultiplier = true;
13226 break;
13227 }
13228 }
Eric Christopherfd179292009-08-27 18:07:15 +000013229
Chris Lattnercee56e72009-03-13 05:53:31 +000013230 if (isFastMultiplier) {
13231 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000013232 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
13233 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000013234 // Zero extend the condition if needed.
13235 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
13236 Cond);
13237 // Scale the condition by the difference.
13238 if (Diff != 1)
13239 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
13240 DAG.getConstant(Diff, Cond.getValueType()));
13241
13242 // Add the base if non-zero.
13243 if (FalseC->getAPIntValue() != 0)
13244 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
13245 SDValue(FalseC, 0));
13246 if (N->getNumValues() == 2) // Dead flag value?
13247 return DCI.CombineTo(N, Cond, SDValue());
13248 return Cond;
13249 }
Eric Christopherfd179292009-08-27 18:07:15 +000013250 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000013251 }
13252 }
13253 return SDValue();
13254}
13255
13256
Evan Cheng0b0cd912009-03-28 05:57:29 +000013257/// PerformMulCombine - Optimize a single multiply with constant into two
13258/// in order to implement it with two cheaper instructions, e.g.
13259/// LEA + SHL, LEA + LEA.
13260static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
13261 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000013262 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
13263 return SDValue();
13264
Owen Andersone50ed302009-08-10 22:56:29 +000013265 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000013266 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000013267 return SDValue();
13268
13269 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
13270 if (!C)
13271 return SDValue();
13272 uint64_t MulAmt = C->getZExtValue();
13273 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
13274 return SDValue();
13275
13276 uint64_t MulAmt1 = 0;
13277 uint64_t MulAmt2 = 0;
13278 if ((MulAmt % 9) == 0) {
13279 MulAmt1 = 9;
13280 MulAmt2 = MulAmt / 9;
13281 } else if ((MulAmt % 5) == 0) {
13282 MulAmt1 = 5;
13283 MulAmt2 = MulAmt / 5;
13284 } else if ((MulAmt % 3) == 0) {
13285 MulAmt1 = 3;
13286 MulAmt2 = MulAmt / 3;
13287 }
13288 if (MulAmt2 &&
13289 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
13290 DebugLoc DL = N->getDebugLoc();
13291
13292 if (isPowerOf2_64(MulAmt2) &&
13293 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
13294 // If second multiplifer is pow2, issue it first. We want the multiply by
13295 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
13296 // is an add.
13297 std::swap(MulAmt1, MulAmt2);
13298
13299 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000013300 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000013301 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000013302 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000013303 else
Evan Cheng73f24c92009-03-30 21:36:47 +000013304 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000013305 DAG.getConstant(MulAmt1, VT));
13306
Eric Christopherfd179292009-08-27 18:07:15 +000013307 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000013308 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000013309 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000013310 else
Evan Cheng73f24c92009-03-30 21:36:47 +000013311 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000013312 DAG.getConstant(MulAmt2, VT));
13313
13314 // Do not add new nodes to DAG combiner worklist.
13315 DCI.CombineTo(N, NewMul, false);
13316 }
13317 return SDValue();
13318}
13319
Evan Chengad9c0a32009-12-15 00:53:42 +000013320static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
13321 SDValue N0 = N->getOperand(0);
13322 SDValue N1 = N->getOperand(1);
13323 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
13324 EVT VT = N0.getValueType();
13325
13326 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
13327 // since the result of setcc_c is all zero's or all ones.
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000013328 if (VT.isInteger() && !VT.isVector() &&
13329 N1C && N0.getOpcode() == ISD::AND &&
Evan Chengad9c0a32009-12-15 00:53:42 +000013330 N0.getOperand(1).getOpcode() == ISD::Constant) {
13331 SDValue N00 = N0.getOperand(0);
13332 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
13333 ((N00.getOpcode() == ISD::ANY_EXTEND ||
13334 N00.getOpcode() == ISD::ZERO_EXTEND) &&
13335 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
13336 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
13337 APInt ShAmt = N1C->getAPIntValue();
13338 Mask = Mask.shl(ShAmt);
13339 if (Mask != 0)
13340 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
13341 N00, DAG.getConstant(Mask, VT));
13342 }
13343 }
13344
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000013345
13346 // Hardware support for vector shifts is sparse which makes us scalarize the
13347 // vector operations in many cases. Also, on sandybridge ADD is faster than
13348 // shl.
13349 // (shl V, 1) -> add V,V
13350 if (isSplatVector(N1.getNode())) {
13351 assert(N0.getValueType().isVector() && "Invalid vector shift type");
13352 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1->getOperand(0));
13353 // We shift all of the values by one. In many cases we do not have
13354 // hardware support for this operation. This is better expressed as an ADD
13355 // of two values.
13356 if (N1C && (1 == N1C->getZExtValue())) {
13357 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, N0);
13358 }
13359 }
13360
Evan Chengad9c0a32009-12-15 00:53:42 +000013361 return SDValue();
13362}
Evan Cheng0b0cd912009-03-28 05:57:29 +000013363
Nate Begeman740ab032009-01-26 00:52:55 +000013364/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
13365/// when possible.
13366static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
13367 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000013368 EVT VT = N->getValueType(0);
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000013369 if (N->getOpcode() == ISD::SHL) {
13370 SDValue V = PerformSHLCombine(N, DAG);
13371 if (V.getNode()) return V;
13372 }
Evan Chengad9c0a32009-12-15 00:53:42 +000013373
Nate Begeman740ab032009-01-26 00:52:55 +000013374 // On X86 with SSE2 support, we can transform this to a vector shift if
13375 // all elements are shifted by the same amount. We can't do this in legalize
13376 // because the a constant vector is typically transformed to a constant pool
13377 // so we have no knowledge of the shift amount.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013378 if (!Subtarget->hasXMMInt())
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013379 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000013380
Craig Topper7be5dfd2011-11-12 09:58:49 +000013381 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16 &&
13382 (!Subtarget->hasAVX2() ||
13383 (VT != MVT::v4i64 && VT != MVT::v8i32 && VT != MVT::v16i16)))
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013384 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000013385
Mon P Wang3becd092009-01-28 08:12:05 +000013386 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000013387 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000013388 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000013389 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000013390 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
13391 unsigned NumElts = VT.getVectorNumElements();
13392 unsigned i = 0;
13393 for (; i != NumElts; ++i) {
13394 SDValue Arg = ShAmtOp.getOperand(i);
13395 if (Arg.getOpcode() == ISD::UNDEF) continue;
13396 BaseShAmt = Arg;
13397 break;
13398 }
13399 for (; i != NumElts; ++i) {
13400 SDValue Arg = ShAmtOp.getOperand(i);
13401 if (Arg.getOpcode() == ISD::UNDEF) continue;
13402 if (Arg != BaseShAmt) {
13403 return SDValue();
13404 }
13405 }
13406 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000013407 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000013408 SDValue InVec = ShAmtOp.getOperand(0);
13409 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
13410 unsigned NumElts = InVec.getValueType().getVectorNumElements();
13411 unsigned i = 0;
13412 for (; i != NumElts; ++i) {
13413 SDValue Arg = InVec.getOperand(i);
13414 if (Arg.getOpcode() == ISD::UNDEF) continue;
13415 BaseShAmt = Arg;
13416 break;
13417 }
13418 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
13419 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000013420 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000013421 if (C->getZExtValue() == SplatIdx)
13422 BaseShAmt = InVec.getOperand(1);
13423 }
13424 }
13425 if (BaseShAmt.getNode() == 0)
13426 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
13427 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000013428 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013429 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000013430
Mon P Wangefa42202009-09-03 19:56:25 +000013431 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000013432 if (EltVT.bitsGT(MVT::i32))
13433 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
13434 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000013435 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000013436
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013437 // The shift amount is identical so we can do a vector shift.
13438 SDValue ValOp = N->getOperand(0);
13439 switch (N->getOpcode()) {
13440 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000013441 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013442 break;
13443 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000013444 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013445 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013446 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013447 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013448 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013449 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013450 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013451 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013452 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013453 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013454 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013455 ValOp, BaseShAmt);
Craig Topper7be5dfd2011-11-12 09:58:49 +000013456 if (VT == MVT::v4i64)
13457 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13458 DAG.getConstant(Intrinsic::x86_avx2_pslli_q, MVT::i32),
13459 ValOp, BaseShAmt);
13460 if (VT == MVT::v8i32)
13461 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13462 DAG.getConstant(Intrinsic::x86_avx2_pslli_d, MVT::i32),
13463 ValOp, BaseShAmt);
13464 if (VT == MVT::v16i16)
13465 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13466 DAG.getConstant(Intrinsic::x86_avx2_pslli_w, MVT::i32),
13467 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013468 break;
13469 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000013470 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013471 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013472 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013473 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013474 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013475 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013476 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013477 ValOp, BaseShAmt);
Craig Topper7be5dfd2011-11-12 09:58:49 +000013478 if (VT == MVT::v8i32)
13479 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13480 DAG.getConstant(Intrinsic::x86_avx2_psrai_d, MVT::i32),
13481 ValOp, BaseShAmt);
13482 if (VT == MVT::v16i16)
13483 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13484 DAG.getConstant(Intrinsic::x86_avx2_psrai_w, MVT::i32),
13485 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013486 break;
13487 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000013488 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013489 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013490 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013491 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013492 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013493 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013494 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013495 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013496 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013497 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013498 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013499 ValOp, BaseShAmt);
Craig Topper7be5dfd2011-11-12 09:58:49 +000013500 if (VT == MVT::v4i64)
13501 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13502 DAG.getConstant(Intrinsic::x86_avx2_psrli_q, MVT::i32),
13503 ValOp, BaseShAmt);
13504 if (VT == MVT::v8i32)
13505 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13506 DAG.getConstant(Intrinsic::x86_avx2_psrli_d, MVT::i32),
13507 ValOp, BaseShAmt);
13508 if (VT == MVT::v16i16)
13509 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13510 DAG.getConstant(Intrinsic::x86_avx2_psrli_w, MVT::i32),
13511 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013512 break;
Nate Begeman740ab032009-01-26 00:52:55 +000013513 }
13514 return SDValue();
13515}
13516
Nate Begemanb65c1752010-12-17 22:55:37 +000013517
Stuart Hastings865f0932011-06-03 23:53:54 +000013518// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
13519// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
13520// and friends. Likewise for OR -> CMPNEQSS.
13521static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
13522 TargetLowering::DAGCombinerInfo &DCI,
13523 const X86Subtarget *Subtarget) {
13524 unsigned opcode;
13525
13526 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
13527 // we're requiring SSE2 for both.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013528 if (Subtarget->hasXMMInt() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
Stuart Hastings865f0932011-06-03 23:53:54 +000013529 SDValue N0 = N->getOperand(0);
13530 SDValue N1 = N->getOperand(1);
13531 SDValue CMP0 = N0->getOperand(1);
13532 SDValue CMP1 = N1->getOperand(1);
13533 DebugLoc DL = N->getDebugLoc();
13534
13535 // The SETCCs should both refer to the same CMP.
13536 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
13537 return SDValue();
13538
13539 SDValue CMP00 = CMP0->getOperand(0);
13540 SDValue CMP01 = CMP0->getOperand(1);
13541 EVT VT = CMP00.getValueType();
13542
13543 if (VT == MVT::f32 || VT == MVT::f64) {
13544 bool ExpectingFlags = false;
13545 // Check for any users that want flags:
13546 for (SDNode::use_iterator UI = N->use_begin(),
13547 UE = N->use_end();
13548 !ExpectingFlags && UI != UE; ++UI)
13549 switch (UI->getOpcode()) {
13550 default:
13551 case ISD::BR_CC:
13552 case ISD::BRCOND:
13553 case ISD::SELECT:
13554 ExpectingFlags = true;
13555 break;
13556 case ISD::CopyToReg:
13557 case ISD::SIGN_EXTEND:
13558 case ISD::ZERO_EXTEND:
13559 case ISD::ANY_EXTEND:
13560 break;
13561 }
13562
13563 if (!ExpectingFlags) {
13564 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
13565 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
13566
13567 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
13568 X86::CondCode tmp = cc0;
13569 cc0 = cc1;
13570 cc1 = tmp;
13571 }
13572
13573 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
13574 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
13575 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
13576 X86ISD::NodeType NTOperator = is64BitFP ?
13577 X86ISD::FSETCCsd : X86ISD::FSETCCss;
13578 // FIXME: need symbolic constants for these magic numbers.
13579 // See X86ATTInstPrinter.cpp:printSSECC().
13580 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
13581 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
13582 DAG.getConstant(x86cc, MVT::i8));
13583 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
13584 OnesOrZeroesF);
13585 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
13586 DAG.getConstant(1, MVT::i32));
13587 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
13588 return OneBitOfTruth;
13589 }
13590 }
13591 }
13592 }
13593 return SDValue();
13594}
13595
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013596/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
13597/// so it can be folded inside ANDNP.
13598static bool CanFoldXORWithAllOnes(const SDNode *N) {
13599 EVT VT = N->getValueType(0);
13600
13601 // Match direct AllOnes for 128 and 256-bit vectors
13602 if (ISD::isBuildVectorAllOnes(N))
13603 return true;
13604
13605 // Look through a bit convert.
13606 if (N->getOpcode() == ISD::BITCAST)
13607 N = N->getOperand(0).getNode();
13608
13609 // Sometimes the operand may come from a insert_subvector building a 256-bit
13610 // allones vector
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013611 if (VT.getSizeInBits() == 256 &&
Bill Wendling456a9252011-08-04 00:32:58 +000013612 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
13613 SDValue V1 = N->getOperand(0);
13614 SDValue V2 = N->getOperand(1);
13615
13616 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
13617 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
13618 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
13619 ISD::isBuildVectorAllOnes(V2.getNode()))
13620 return true;
13621 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013622
13623 return false;
13624}
13625
Nate Begemanb65c1752010-12-17 22:55:37 +000013626static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
13627 TargetLowering::DAGCombinerInfo &DCI,
13628 const X86Subtarget *Subtarget) {
13629 if (DCI.isBeforeLegalizeOps())
13630 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013631
Stuart Hastings865f0932011-06-03 23:53:54 +000013632 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
13633 if (R.getNode())
13634 return R;
13635
Craig Topper54a11172011-10-14 07:06:56 +000013636 EVT VT = N->getValueType(0);
13637
Craig Topperb4c94572011-10-21 06:55:01 +000013638 // Create ANDN, BLSI, and BLSR instructions
13639 // BLSI is X & (-X)
13640 // BLSR is X & (X-1)
Craig Topper54a11172011-10-14 07:06:56 +000013641 if (Subtarget->hasBMI() && (VT == MVT::i32 || VT == MVT::i64)) {
13642 SDValue N0 = N->getOperand(0);
13643 SDValue N1 = N->getOperand(1);
13644 DebugLoc DL = N->getDebugLoc();
13645
13646 // Check LHS for not
13647 if (N0.getOpcode() == ISD::XOR && isAllOnes(N0.getOperand(1)))
13648 return DAG.getNode(X86ISD::ANDN, DL, VT, N0.getOperand(0), N1);
13649 // Check RHS for not
13650 if (N1.getOpcode() == ISD::XOR && isAllOnes(N1.getOperand(1)))
13651 return DAG.getNode(X86ISD::ANDN, DL, VT, N1.getOperand(0), N0);
13652
Craig Topperb4c94572011-10-21 06:55:01 +000013653 // Check LHS for neg
13654 if (N0.getOpcode() == ISD::SUB && N0.getOperand(1) == N1 &&
13655 isZero(N0.getOperand(0)))
13656 return DAG.getNode(X86ISD::BLSI, DL, VT, N1);
13657
13658 // Check RHS for neg
13659 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1) == N0 &&
13660 isZero(N1.getOperand(0)))
13661 return DAG.getNode(X86ISD::BLSI, DL, VT, N0);
13662
13663 // Check LHS for X-1
13664 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
13665 isAllOnes(N0.getOperand(1)))
13666 return DAG.getNode(X86ISD::BLSR, DL, VT, N1);
13667
13668 // Check RHS for X-1
13669 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
13670 isAllOnes(N1.getOperand(1)))
13671 return DAG.getNode(X86ISD::BLSR, DL, VT, N0);
13672
Craig Topper54a11172011-10-14 07:06:56 +000013673 return SDValue();
13674 }
13675
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000013676 // Want to form ANDNP nodes:
13677 // 1) In the hopes of then easily combining them with OR and AND nodes
13678 // to form PBLEND/PSIGN.
13679 // 2) To match ANDN packed intrinsics
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000013680 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000013681 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013682
Nate Begemanb65c1752010-12-17 22:55:37 +000013683 SDValue N0 = N->getOperand(0);
13684 SDValue N1 = N->getOperand(1);
13685 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013686
Nate Begemanb65c1752010-12-17 22:55:37 +000013687 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013688 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013689 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
13690 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013691 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000013692
13693 // Check RHS for vnot
13694 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013695 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
13696 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013697 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013698
Nate Begemanb65c1752010-12-17 22:55:37 +000013699 return SDValue();
13700}
13701
Evan Cheng760d1942010-01-04 21:22:48 +000013702static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000013703 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000013704 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000013705 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000013706 return SDValue();
13707
Stuart Hastings865f0932011-06-03 23:53:54 +000013708 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
13709 if (R.getNode())
13710 return R;
13711
Evan Cheng760d1942010-01-04 21:22:48 +000013712 EVT VT = N->getValueType(0);
Evan Cheng760d1942010-01-04 21:22:48 +000013713
Evan Cheng760d1942010-01-04 21:22:48 +000013714 SDValue N0 = N->getOperand(0);
13715 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013716
Nate Begemanb65c1752010-12-17 22:55:37 +000013717 // look for psign/blend
Craig Topper1666cb62011-11-19 07:07:26 +000013718 if (VT == MVT::v2i64 || VT == MVT::v4i64) {
Craig Topperc0d82852011-11-22 00:44:41 +000013719 if (!Subtarget->hasSSSE3orAVX() ||
Craig Topper1666cb62011-11-19 07:07:26 +000013720 (VT == MVT::v4i64 && !Subtarget->hasAVX2()))
13721 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013722
Craig Topper1666cb62011-11-19 07:07:26 +000013723 // Canonicalize pandn to RHS
13724 if (N0.getOpcode() == X86ISD::ANDNP)
13725 std::swap(N0, N1);
13726 // or (and (m, x), (pandn m, y))
13727 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
13728 SDValue Mask = N1.getOperand(0);
13729 SDValue X = N1.getOperand(1);
13730 SDValue Y;
13731 if (N0.getOperand(0) == Mask)
13732 Y = N0.getOperand(1);
13733 if (N0.getOperand(1) == Mask)
13734 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013735
Craig Topper1666cb62011-11-19 07:07:26 +000013736 // Check to see if the mask appeared in both the AND and ANDNP and
13737 if (!Y.getNode())
13738 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013739
Craig Topper1666cb62011-11-19 07:07:26 +000013740 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
13741 if (Mask.getOpcode() != ISD::BITCAST ||
13742 X.getOpcode() != ISD::BITCAST ||
13743 Y.getOpcode() != ISD::BITCAST)
13744 return SDValue();
Nate Begemanb65c1752010-12-17 22:55:37 +000013745
Craig Topper1666cb62011-11-19 07:07:26 +000013746 // Look through mask bitcast.
13747 Mask = Mask.getOperand(0);
13748 EVT MaskVT = Mask.getValueType();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013749
Craig Topper1666cb62011-11-19 07:07:26 +000013750 // Validate that the Mask operand is a vector sra node. The sra node
13751 // will be an intrinsic.
13752 if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
13753 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013754
Craig Topper1666cb62011-11-19 07:07:26 +000013755 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
13756 // there is no psrai.b
13757 switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
13758 case Intrinsic::x86_sse2_psrai_w:
13759 case Intrinsic::x86_sse2_psrai_d:
13760 case Intrinsic::x86_avx2_psrai_w:
13761 case Intrinsic::x86_avx2_psrai_d:
13762 break;
13763 default: return SDValue();
Nate Begemanb65c1752010-12-17 22:55:37 +000013764 }
Craig Topper1666cb62011-11-19 07:07:26 +000013765
13766 // Check that the SRA is all signbits.
13767 SDValue SraC = Mask.getOperand(2);
13768 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
13769 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
13770 if ((SraAmt + 1) != EltBits)
13771 return SDValue();
13772
13773 DebugLoc DL = N->getDebugLoc();
13774
13775 // Now we know we at least have a plendvb with the mask val. See if
13776 // we can form a psignb/w/d.
13777 // psign = x.type == y.type == mask.type && y = sub(0, x);
13778 X = X.getOperand(0);
13779 Y = Y.getOperand(0);
13780 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
13781 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
Craig Topper31133842011-11-19 07:33:10 +000013782 X.getValueType() == MaskVT && X.getValueType() == Y.getValueType() &&
13783 (EltBits == 8 || EltBits == 16 || EltBits == 32)) {
13784 SDValue Sign = DAG.getNode(X86ISD::PSIGN, DL, MaskVT, X,
13785 Mask.getOperand(1));
13786 return DAG.getNode(ISD::BITCAST, DL, VT, Sign);
Craig Topper1666cb62011-11-19 07:07:26 +000013787 }
13788 // PBLENDVB only available on SSE 4.1
Craig Topperc0d82852011-11-22 00:44:41 +000013789 if (!Subtarget->hasSSE41orAVX())
Craig Topper1666cb62011-11-19 07:07:26 +000013790 return SDValue();
13791
13792 EVT BlendVT = (VT == MVT::v4i64) ? MVT::v32i8 : MVT::v16i8;
13793
13794 X = DAG.getNode(ISD::BITCAST, DL, BlendVT, X);
13795 Y = DAG.getNode(ISD::BITCAST, DL, BlendVT, Y);
13796 Mask = DAG.getNode(ISD::BITCAST, DL, BlendVT, Mask);
Nadav Rotem18197d72011-11-30 10:13:37 +000013797 Mask = DAG.getNode(ISD::VSELECT, DL, BlendVT, Mask, Y, X);
Craig Topper1666cb62011-11-19 07:07:26 +000013798 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000013799 }
13800 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013801
Craig Topper1666cb62011-11-19 07:07:26 +000013802 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
13803 return SDValue();
13804
Nate Begemanb65c1752010-12-17 22:55:37 +000013805 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000013806 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
13807 std::swap(N0, N1);
13808 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
13809 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000013810 if (!N0.hasOneUse() || !N1.hasOneUse())
13811 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000013812
13813 SDValue ShAmt0 = N0.getOperand(1);
13814 if (ShAmt0.getValueType() != MVT::i8)
13815 return SDValue();
13816 SDValue ShAmt1 = N1.getOperand(1);
13817 if (ShAmt1.getValueType() != MVT::i8)
13818 return SDValue();
13819 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
13820 ShAmt0 = ShAmt0.getOperand(0);
13821 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
13822 ShAmt1 = ShAmt1.getOperand(0);
13823
13824 DebugLoc DL = N->getDebugLoc();
13825 unsigned Opc = X86ISD::SHLD;
13826 SDValue Op0 = N0.getOperand(0);
13827 SDValue Op1 = N1.getOperand(0);
13828 if (ShAmt0.getOpcode() == ISD::SUB) {
13829 Opc = X86ISD::SHRD;
13830 std::swap(Op0, Op1);
13831 std::swap(ShAmt0, ShAmt1);
13832 }
13833
Evan Cheng8b1190a2010-04-28 01:18:01 +000013834 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000013835 if (ShAmt1.getOpcode() == ISD::SUB) {
13836 SDValue Sum = ShAmt1.getOperand(0);
13837 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000013838 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
13839 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
13840 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
13841 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000013842 return DAG.getNode(Opc, DL, VT,
13843 Op0, Op1,
13844 DAG.getNode(ISD::TRUNCATE, DL,
13845 MVT::i8, ShAmt0));
13846 }
13847 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
13848 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
13849 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000013850 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000013851 return DAG.getNode(Opc, DL, VT,
13852 N0.getOperand(0), N1.getOperand(0),
13853 DAG.getNode(ISD::TRUNCATE, DL,
13854 MVT::i8, ShAmt0));
13855 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013856
Evan Cheng760d1942010-01-04 21:22:48 +000013857 return SDValue();
13858}
13859
Craig Topperb4c94572011-10-21 06:55:01 +000013860static SDValue PerformXorCombine(SDNode *N, SelectionDAG &DAG,
13861 TargetLowering::DAGCombinerInfo &DCI,
13862 const X86Subtarget *Subtarget) {
13863 if (DCI.isBeforeLegalizeOps())
13864 return SDValue();
13865
13866 EVT VT = N->getValueType(0);
13867
13868 if (VT != MVT::i32 && VT != MVT::i64)
13869 return SDValue();
13870
13871 // Create BLSMSK instructions by finding X ^ (X-1)
13872 SDValue N0 = N->getOperand(0);
13873 SDValue N1 = N->getOperand(1);
13874 DebugLoc DL = N->getDebugLoc();
13875
13876 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
13877 isAllOnes(N0.getOperand(1)))
13878 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N1);
13879
13880 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
13881 isAllOnes(N1.getOperand(1)))
13882 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N0);
13883
13884 return SDValue();
13885}
13886
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013887/// PerformLOADCombine - Do target-specific dag combines on LOAD nodes.
13888static SDValue PerformLOADCombine(SDNode *N, SelectionDAG &DAG,
13889 const X86Subtarget *Subtarget) {
13890 LoadSDNode *Ld = cast<LoadSDNode>(N);
13891 EVT RegVT = Ld->getValueType(0);
13892 EVT MemVT = Ld->getMemoryVT();
13893 DebugLoc dl = Ld->getDebugLoc();
13894 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13895
13896 ISD::LoadExtType Ext = Ld->getExtensionType();
13897
Nadav Rotemca6f2962011-09-18 19:00:23 +000013898 // If this is a vector EXT Load then attempt to optimize it using a
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013899 // shuffle. We need SSE4 for the shuffles.
13900 // TODO: It is possible to support ZExt by zeroing the undef values
13901 // during the shuffle phase or after the shuffle.
13902 if (RegVT.isVector() && Ext == ISD::EXTLOAD && Subtarget->hasSSE41()) {
13903 assert(MemVT != RegVT && "Cannot extend to the same type");
13904 assert(MemVT.isVector() && "Must load a vector from memory");
13905
13906 unsigned NumElems = RegVT.getVectorNumElements();
13907 unsigned RegSz = RegVT.getSizeInBits();
13908 unsigned MemSz = MemVT.getSizeInBits();
13909 assert(RegSz > MemSz && "Register size must be greater than the mem size");
Nadav Rotemca6f2962011-09-18 19:00:23 +000013910 // All sizes must be a power of two
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013911 if (!isPowerOf2_32(RegSz * MemSz * NumElems)) return SDValue();
13912
13913 // Attempt to load the original value using a single load op.
13914 // Find a scalar type which is equal to the loaded word size.
13915 MVT SclrLoadTy = MVT::i8;
13916 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
13917 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
13918 MVT Tp = (MVT::SimpleValueType)tp;
13919 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() == MemSz) {
13920 SclrLoadTy = Tp;
13921 break;
13922 }
13923 }
13924
13925 // Proceed if a load word is found.
13926 if (SclrLoadTy.getSizeInBits() != MemSz) return SDValue();
13927
13928 EVT LoadUnitVecVT = EVT::getVectorVT(*DAG.getContext(), SclrLoadTy,
13929 RegSz/SclrLoadTy.getSizeInBits());
13930
13931 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
13932 RegSz/MemVT.getScalarType().getSizeInBits());
13933 // Can't shuffle using an illegal type.
13934 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
13935
13936 // Perform a single load.
13937 SDValue ScalarLoad = DAG.getLoad(SclrLoadTy, dl, Ld->getChain(),
13938 Ld->getBasePtr(),
13939 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000013940 Ld->isNonTemporal(), Ld->isInvariant(),
13941 Ld->getAlignment());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013942
13943 // Insert the word loaded into a vector.
13944 SDValue ScalarInVector = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
13945 LoadUnitVecVT, ScalarLoad);
13946
13947 // Bitcast the loaded value to a vector of the original element type, in
13948 // the size of the target vector type.
13949 SDValue SlicedVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, ScalarInVector);
13950 unsigned SizeRatio = RegSz/MemSz;
13951
13952 // Redistribute the loaded elements into the different locations.
13953 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
13954 for (unsigned i = 0; i < NumElems; i++) ShuffleVec[i*SizeRatio] = i;
13955
13956 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
13957 DAG.getUNDEF(SlicedVec.getValueType()),
13958 ShuffleVec.data());
13959
13960 // Bitcast to the requested type.
13961 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
13962 // Replace the original load with the new sequence
13963 // and return the new chain.
13964 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Shuff);
13965 return SDValue(ScalarLoad.getNode(), 1);
13966 }
13967
13968 return SDValue();
13969}
13970
Chris Lattner149a4e52008-02-22 02:09:43 +000013971/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000013972static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000013973 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000013974 StoreSDNode *St = cast<StoreSDNode>(N);
13975 EVT VT = St->getValue().getValueType();
13976 EVT StVT = St->getMemoryVT();
13977 DebugLoc dl = St->getDebugLoc();
Nadav Rotem5e742a32011-08-11 16:41:21 +000013978 SDValue StoredVal = St->getOperand(1);
13979 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13980
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013981 // If we are saving a concatenation of two XMM registers, perform two stores.
Nadav Rotem6236f7f2011-08-11 17:05:47 +000013982 // This is better in Sandy Bridge cause one 256-bit mem op is done via two
13983 // 128-bit ones. If in the future the cost becomes only one memory access the
13984 // first version would be better.
Nadav Rotem5e742a32011-08-11 16:41:21 +000013985 if (VT.getSizeInBits() == 256 &&
13986 StoredVal.getNode()->getOpcode() == ISD::CONCAT_VECTORS &&
13987 StoredVal.getNumOperands() == 2) {
13988
13989 SDValue Value0 = StoredVal.getOperand(0);
13990 SDValue Value1 = StoredVal.getOperand(1);
13991
13992 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
13993 SDValue Ptr0 = St->getBasePtr();
13994 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
13995
13996 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
13997 St->getPointerInfo(), St->isVolatile(),
13998 St->isNonTemporal(), St->getAlignment());
13999 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
14000 St->getPointerInfo(), St->isVolatile(),
14001 St->isNonTemporal(), St->getAlignment());
14002 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
14003 }
Nadav Rotem614061b2011-08-10 19:30:14 +000014004
14005 // Optimize trunc store (of multiple scalars) to shuffle and store.
14006 // First, pack all of the elements in one place. Next, store to memory
14007 // in fewer chunks.
14008 if (St->isTruncatingStore() && VT.isVector()) {
14009 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14010 unsigned NumElems = VT.getVectorNumElements();
14011 assert(StVT != VT && "Cannot truncate to the same type");
14012 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
14013 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
14014
14015 // From, To sizes and ElemCount must be pow of two
14016 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000014017 // We are going to use the original vector elt for storing.
Nadav Rotem64ac73b2011-09-21 17:14:40 +000014018 // Accumulated smaller vector elements must be a multiple of the store size.
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000014019 if (0 != (NumElems * FromSz) % ToSz) return SDValue();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014020
Nadav Rotem614061b2011-08-10 19:30:14 +000014021 unsigned SizeRatio = FromSz / ToSz;
14022
14023 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
14024
14025 // Create a type on which we perform the shuffle
14026 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
14027 StVT.getScalarType(), NumElems*SizeRatio);
14028
14029 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
14030
14031 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
14032 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
14033 for (unsigned i = 0; i < NumElems; i++ ) ShuffleVec[i] = i * SizeRatio;
14034
14035 // Can't shuffle using an illegal type
14036 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
14037
14038 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
14039 DAG.getUNDEF(WideVec.getValueType()),
14040 ShuffleVec.data());
14041 // At this point all of the data is stored at the bottom of the
14042 // register. We now need to save it to mem.
14043
14044 // Find the largest store unit
14045 MVT StoreType = MVT::i8;
14046 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
14047 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
14048 MVT Tp = (MVT::SimpleValueType)tp;
14049 if (TLI.isTypeLegal(Tp) && StoreType.getSizeInBits() < NumElems * ToSz)
14050 StoreType = Tp;
14051 }
14052
14053 // Bitcast the original vector into a vector of store-size units
14054 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
14055 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
14056 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
14057 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
14058 SmallVector<SDValue, 8> Chains;
14059 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
14060 TLI.getPointerTy());
14061 SDValue Ptr = St->getBasePtr();
14062
14063 // Perform one or more big stores into memory.
14064 for (unsigned i = 0; i < (ToSz*NumElems)/StoreType.getSizeInBits() ; i++) {
14065 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
14066 StoreType, ShuffWide,
14067 DAG.getIntPtrConstant(i));
14068 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
14069 St->getPointerInfo(), St->isVolatile(),
14070 St->isNonTemporal(), St->getAlignment());
14071 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
14072 Chains.push_back(Ch);
14073 }
14074
14075 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
14076 Chains.size());
14077 }
14078
14079
Chris Lattner149a4e52008-02-22 02:09:43 +000014080 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
14081 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000014082 // A preferable solution to the general problem is to figure out the right
14083 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000014084
14085 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000014086 if (VT.getSizeInBits() != 64)
14087 return SDValue();
14088
Devang Patel578efa92009-06-05 21:57:13 +000014089 const Function *F = DAG.getMachineFunction().getFunction();
14090 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014091 bool F64IsLegal = !DAG.getTarget().Options.UseSoftFloat && !NoImplicitFloatOps
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000014092 && Subtarget->hasXMMInt();
Evan Cheng536e6672009-03-12 05:59:15 +000014093 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000014094 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000014095 isa<LoadSDNode>(St->getValue()) &&
14096 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
14097 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000014098 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014099 LoadSDNode *Ld = 0;
14100 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000014101 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000014102 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014103 // Must be a store of a load. We currently handle two cases: the load
14104 // is a direct child, and it's under an intervening TokenFactor. It is
14105 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000014106 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000014107 Ld = cast<LoadSDNode>(St->getChain());
14108 else if (St->getValue().hasOneUse() &&
14109 ChainVal->getOpcode() == ISD::TokenFactor) {
14110 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000014111 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000014112 TokenFactorIndex = i;
14113 Ld = cast<LoadSDNode>(St->getValue());
14114 } else
14115 Ops.push_back(ChainVal->getOperand(i));
14116 }
14117 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000014118
Evan Cheng536e6672009-03-12 05:59:15 +000014119 if (!Ld || !ISD::isNormalLoad(Ld))
14120 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014121
Evan Cheng536e6672009-03-12 05:59:15 +000014122 // If this is not the MMX case, i.e. we are just turning i64 load/store
14123 // into f64 load/store, avoid the transformation if there are multiple
14124 // uses of the loaded value.
14125 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
14126 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014127
Evan Cheng536e6672009-03-12 05:59:15 +000014128 DebugLoc LdDL = Ld->getDebugLoc();
14129 DebugLoc StDL = N->getDebugLoc();
14130 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
14131 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
14132 // pair instead.
14133 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000014134 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000014135 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
14136 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014137 Ld->isNonTemporal(), Ld->isInvariant(),
14138 Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000014139 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000014140 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000014141 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000014142 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000014143 Ops.size());
14144 }
Evan Cheng536e6672009-03-12 05:59:15 +000014145 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000014146 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000014147 St->isVolatile(), St->isNonTemporal(),
14148 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000014149 }
Evan Cheng536e6672009-03-12 05:59:15 +000014150
14151 // Otherwise, lower to two pairs of 32-bit loads / stores.
14152 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000014153 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
14154 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000014155
Owen Anderson825b72b2009-08-11 20:47:22 +000014156 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000014157 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000014158 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014159 Ld->isInvariant(), Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000014160 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000014161 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000014162 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014163 Ld->isInvariant(),
Evan Cheng536e6672009-03-12 05:59:15 +000014164 MinAlign(Ld->getAlignment(), 4));
14165
14166 SDValue NewChain = LoLd.getValue(1);
14167 if (TokenFactorIndex != -1) {
14168 Ops.push_back(LoLd);
14169 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000014170 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000014171 Ops.size());
14172 }
14173
14174 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000014175 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
14176 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000014177
14178 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000014179 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000014180 St->isVolatile(), St->isNonTemporal(),
14181 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000014182 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000014183 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000014184 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000014185 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000014186 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000014187 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000014188 }
Dan Gohman475871a2008-07-27 21:46:04 +000014189 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000014190}
14191
Duncan Sands17470be2011-09-22 20:15:48 +000014192/// isHorizontalBinOp - Return 'true' if this vector operation is "horizontal"
14193/// and return the operands for the horizontal operation in LHS and RHS. A
14194/// horizontal operation performs the binary operation on successive elements
14195/// of its first operand, then on successive elements of its second operand,
14196/// returning the resulting values in a vector. For example, if
14197/// A = < float a0, float a1, float a2, float a3 >
14198/// and
14199/// B = < float b0, float b1, float b2, float b3 >
14200/// then the result of doing a horizontal operation on A and B is
14201/// A horizontal-op B = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >.
14202/// In short, LHS and RHS are inspected to see if LHS op RHS is of the form
14203/// A horizontal-op B, for some already available A and B, and if so then LHS is
14204/// set to A, RHS to B, and the routine returns 'true'.
14205/// Note that the binary operation should have the property that if one of the
14206/// operands is UNDEF then the result is UNDEF.
Craig Topperbeabc6c2011-12-05 06:56:46 +000014207static bool isHorizontalBinOp(SDValue &LHS, SDValue &RHS, bool IsCommutative) {
Duncan Sands17470be2011-09-22 20:15:48 +000014208 // Look for the following pattern: if
14209 // A = < float a0, float a1, float a2, float a3 >
14210 // B = < float b0, float b1, float b2, float b3 >
14211 // and
14212 // LHS = VECTOR_SHUFFLE A, B, <0, 2, 4, 6>
14213 // RHS = VECTOR_SHUFFLE A, B, <1, 3, 5, 7>
14214 // then LHS op RHS = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >
14215 // which is A horizontal-op B.
14216
14217 // At least one of the operands should be a vector shuffle.
14218 if (LHS.getOpcode() != ISD::VECTOR_SHUFFLE &&
14219 RHS.getOpcode() != ISD::VECTOR_SHUFFLE)
14220 return false;
14221
14222 EVT VT = LHS.getValueType();
Craig Topperf8363302011-12-02 08:18:41 +000014223
14224 assert((VT.is128BitVector() || VT.is256BitVector()) &&
14225 "Unsupported vector type for horizontal add/sub");
14226
14227 // Handle 128 and 256-bit vector lengths. AVX defines horizontal add/sub to
14228 // operate independently on 128-bit lanes.
Craig Topperb72039c2011-11-30 09:10:50 +000014229 unsigned NumElts = VT.getVectorNumElements();
14230 unsigned NumLanes = VT.getSizeInBits()/128;
14231 unsigned NumLaneElts = NumElts / NumLanes;
Craig Topperf8363302011-12-02 08:18:41 +000014232 assert((NumLaneElts % 2 == 0) &&
14233 "Vector type should have an even number of elements in each lane");
14234 unsigned HalfLaneElts = NumLaneElts/2;
Duncan Sands17470be2011-09-22 20:15:48 +000014235
14236 // View LHS in the form
14237 // LHS = VECTOR_SHUFFLE A, B, LMask
14238 // If LHS is not a shuffle then pretend it is the shuffle
14239 // LHS = VECTOR_SHUFFLE LHS, undef, <0, 1, ..., N-1>
14240 // NOTE: in what follows a default initialized SDValue represents an UNDEF of
14241 // type VT.
14242 SDValue A, B;
Craig Topperb72039c2011-11-30 09:10:50 +000014243 SmallVector<int, 16> LMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000014244 if (LHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
14245 if (LHS.getOperand(0).getOpcode() != ISD::UNDEF)
14246 A = LHS.getOperand(0);
14247 if (LHS.getOperand(1).getOpcode() != ISD::UNDEF)
14248 B = LHS.getOperand(1);
14249 cast<ShuffleVectorSDNode>(LHS.getNode())->getMask(LMask);
14250 } else {
14251 if (LHS.getOpcode() != ISD::UNDEF)
14252 A = LHS;
Craig Topperb72039c2011-11-30 09:10:50 +000014253 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000014254 LMask[i] = i;
14255 }
14256
14257 // Likewise, view RHS in the form
14258 // RHS = VECTOR_SHUFFLE C, D, RMask
14259 SDValue C, D;
Craig Topperb72039c2011-11-30 09:10:50 +000014260 SmallVector<int, 16> RMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000014261 if (RHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
14262 if (RHS.getOperand(0).getOpcode() != ISD::UNDEF)
14263 C = RHS.getOperand(0);
14264 if (RHS.getOperand(1).getOpcode() != ISD::UNDEF)
14265 D = RHS.getOperand(1);
14266 cast<ShuffleVectorSDNode>(RHS.getNode())->getMask(RMask);
14267 } else {
14268 if (RHS.getOpcode() != ISD::UNDEF)
14269 C = RHS;
Craig Topperb72039c2011-11-30 09:10:50 +000014270 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000014271 RMask[i] = i;
14272 }
14273
14274 // Check that the shuffles are both shuffling the same vectors.
14275 if (!(A == C && B == D) && !(A == D && B == C))
14276 return false;
14277
14278 // If everything is UNDEF then bail out: it would be better to fold to UNDEF.
14279 if (!A.getNode() && !B.getNode())
14280 return false;
14281
14282 // If A and B occur in reverse order in RHS, then "swap" them (which means
14283 // rewriting the mask).
14284 if (A != C)
Craig Topperbeabc6c2011-12-05 06:56:46 +000014285 CommuteVectorShuffleMask(RMask, NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000014286
14287 // At this point LHS and RHS are equivalent to
14288 // LHS = VECTOR_SHUFFLE A, B, LMask
14289 // RHS = VECTOR_SHUFFLE A, B, RMask
14290 // Check that the masks correspond to performing a horizontal operation.
Craig Topperf8363302011-12-02 08:18:41 +000014291 for (unsigned i = 0; i != NumElts; ++i) {
Craig Topperbeabc6c2011-12-05 06:56:46 +000014292 int LIdx = LMask[i], RIdx = RMask[i];
Duncan Sands17470be2011-09-22 20:15:48 +000014293
Craig Topperf8363302011-12-02 08:18:41 +000014294 // Ignore any UNDEF components.
Craig Topperbeabc6c2011-12-05 06:56:46 +000014295 if (LIdx < 0 || RIdx < 0 ||
14296 (!A.getNode() && (LIdx < (int)NumElts || RIdx < (int)NumElts)) ||
14297 (!B.getNode() && (LIdx >= (int)NumElts || RIdx >= (int)NumElts)))
Craig Topperf8363302011-12-02 08:18:41 +000014298 continue;
Duncan Sands17470be2011-09-22 20:15:48 +000014299
Craig Topperf8363302011-12-02 08:18:41 +000014300 // Check that successive elements are being operated on. If not, this is
14301 // not a horizontal operation.
14302 unsigned Src = (i/HalfLaneElts) % 2; // each lane is split between srcs
14303 unsigned LaneStart = (i/NumLaneElts) * NumLaneElts;
Craig Topperbeabc6c2011-12-05 06:56:46 +000014304 int Index = 2*(i%HalfLaneElts) + NumElts*Src + LaneStart;
Craig Topperf8363302011-12-02 08:18:41 +000014305 if (!(LIdx == Index && RIdx == Index + 1) &&
Craig Topperbeabc6c2011-12-05 06:56:46 +000014306 !(IsCommutative && LIdx == Index + 1 && RIdx == Index))
Craig Topperf8363302011-12-02 08:18:41 +000014307 return false;
Duncan Sands17470be2011-09-22 20:15:48 +000014308 }
14309
14310 LHS = A.getNode() ? A : B; // If A is 'UNDEF', use B for it.
14311 RHS = B.getNode() ? B : A; // If B is 'UNDEF', use A for it.
14312 return true;
14313}
14314
14315/// PerformFADDCombine - Do target-specific dag combines on floating point adds.
14316static SDValue PerformFADDCombine(SDNode *N, SelectionDAG &DAG,
14317 const X86Subtarget *Subtarget) {
14318 EVT VT = N->getValueType(0);
14319 SDValue LHS = N->getOperand(0);
14320 SDValue RHS = N->getOperand(1);
14321
14322 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topper138a5c62011-12-02 07:16:01 +000014323 if (((Subtarget->hasSSE3orAVX() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
14324 (Subtarget->hasAVX() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000014325 isHorizontalBinOp(LHS, RHS, true))
14326 return DAG.getNode(X86ISD::FHADD, N->getDebugLoc(), VT, LHS, RHS);
14327 return SDValue();
14328}
14329
14330/// PerformFSUBCombine - Do target-specific dag combines on floating point subs.
14331static SDValue PerformFSUBCombine(SDNode *N, SelectionDAG &DAG,
14332 const X86Subtarget *Subtarget) {
14333 EVT VT = N->getValueType(0);
14334 SDValue LHS = N->getOperand(0);
14335 SDValue RHS = N->getOperand(1);
14336
14337 // Try to synthesize horizontal subs from subs of shuffles.
Craig Topper138a5c62011-12-02 07:16:01 +000014338 if (((Subtarget->hasSSE3orAVX() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
14339 (Subtarget->hasAVX() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000014340 isHorizontalBinOp(LHS, RHS, false))
14341 return DAG.getNode(X86ISD::FHSUB, N->getDebugLoc(), VT, LHS, RHS);
14342 return SDValue();
14343}
14344
Chris Lattner6cf73262008-01-25 06:14:17 +000014345/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
14346/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000014347static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000014348 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
14349 // F[X]OR(0.0, x) -> x
14350 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000014351 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
14352 if (C->getValueAPF().isPosZero())
14353 return N->getOperand(1);
14354 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
14355 if (C->getValueAPF().isPosZero())
14356 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000014357 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000014358}
14359
14360/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000014361static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000014362 // FAND(0.0, x) -> 0.0
14363 // FAND(x, 0.0) -> 0.0
14364 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
14365 if (C->getValueAPF().isPosZero())
14366 return N->getOperand(0);
14367 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
14368 if (C->getValueAPF().isPosZero())
14369 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000014370 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000014371}
14372
Dan Gohmane5af2d32009-01-29 01:59:02 +000014373static SDValue PerformBTCombine(SDNode *N,
14374 SelectionDAG &DAG,
14375 TargetLowering::DAGCombinerInfo &DCI) {
14376 // BT ignores high bits in the bit index operand.
14377 SDValue Op1 = N->getOperand(1);
14378 if (Op1.hasOneUse()) {
14379 unsigned BitWidth = Op1.getValueSizeInBits();
14380 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
14381 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014382 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
14383 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000014384 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000014385 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
14386 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
14387 DCI.CommitTargetLoweringOpt(TLO);
14388 }
14389 return SDValue();
14390}
Chris Lattner83e6c992006-10-04 06:57:07 +000014391
Eli Friedman7a5e5552009-06-07 06:52:44 +000014392static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
14393 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000014394 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000014395 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000014396 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000014397 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000014398 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000014399 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000014400 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000014401 }
14402 return SDValue();
14403}
14404
Evan Cheng2e489c42009-12-16 00:53:11 +000014405static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
14406 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
14407 // (and (i32 x86isd::setcc_carry), 1)
14408 // This eliminates the zext. This transformation is necessary because
14409 // ISD::SETCC is always legalized to i8.
14410 DebugLoc dl = N->getDebugLoc();
14411 SDValue N0 = N->getOperand(0);
14412 EVT VT = N->getValueType(0);
14413 if (N0.getOpcode() == ISD::AND &&
14414 N0.hasOneUse() &&
14415 N0.getOperand(0).hasOneUse()) {
14416 SDValue N00 = N0.getOperand(0);
14417 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
14418 return SDValue();
14419 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
14420 if (!C || C->getZExtValue() != 1)
14421 return SDValue();
14422 return DAG.getNode(ISD::AND, dl, VT,
14423 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
14424 N00.getOperand(0), N00.getOperand(1)),
14425 DAG.getConstant(1, VT));
14426 }
14427
14428 return SDValue();
14429}
14430
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014431// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
14432static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
14433 unsigned X86CC = N->getConstantOperandVal(0);
14434 SDValue EFLAG = N->getOperand(1);
14435 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014436
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014437 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
14438 // a zext and produces an all-ones bit which is more useful than 0/1 in some
14439 // cases.
14440 if (X86CC == X86::COND_B)
14441 return DAG.getNode(ISD::AND, DL, MVT::i8,
14442 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
14443 DAG.getConstant(X86CC, MVT::i8), EFLAG),
14444 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014445
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014446 return SDValue();
14447}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014448
Benjamin Kramer1396c402011-06-18 11:09:41 +000014449static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
14450 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000014451 SDValue Op0 = N->getOperand(0);
14452 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
14453 // a 32-bit target where SSE doesn't support i64->FP operations.
14454 if (Op0.getOpcode() == ISD::LOAD) {
14455 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
14456 EVT VT = Ld->getValueType(0);
14457 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
14458 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
14459 !XTLI->getSubtarget()->is64Bit() &&
14460 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000014461 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
14462 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000014463 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
14464 return FILDChain;
14465 }
14466 }
14467 return SDValue();
14468}
14469
Chris Lattner23a01992010-12-20 01:37:09 +000014470// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
14471static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
14472 X86TargetLowering::DAGCombinerInfo &DCI) {
14473 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
14474 // the result is either zero or one (depending on the input carry bit).
14475 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
14476 if (X86::isZeroNode(N->getOperand(0)) &&
14477 X86::isZeroNode(N->getOperand(1)) &&
14478 // We don't have a good way to replace an EFLAGS use, so only do this when
14479 // dead right now.
14480 SDValue(N, 1).use_empty()) {
14481 DebugLoc DL = N->getDebugLoc();
14482 EVT VT = N->getValueType(0);
14483 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
14484 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
14485 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
14486 DAG.getConstant(X86::COND_B,MVT::i8),
14487 N->getOperand(2)),
14488 DAG.getConstant(1, VT));
14489 return DCI.CombineTo(N, Res1, CarryOut);
14490 }
14491
14492 return SDValue();
14493}
14494
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014495// fold (add Y, (sete X, 0)) -> adc 0, Y
14496// (add Y, (setne X, 0)) -> sbb -1, Y
14497// (sub (sete X, 0), Y) -> sbb 0, Y
14498// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014499static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014500 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014501
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014502 // Look through ZExts.
14503 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
14504 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
14505 return SDValue();
14506
14507 SDValue SetCC = Ext.getOperand(0);
14508 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
14509 return SDValue();
14510
14511 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
14512 if (CC != X86::COND_E && CC != X86::COND_NE)
14513 return SDValue();
14514
14515 SDValue Cmp = SetCC.getOperand(1);
14516 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000014517 !X86::isZeroNode(Cmp.getOperand(1)) ||
14518 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014519 return SDValue();
14520
14521 SDValue CmpOp0 = Cmp.getOperand(0);
14522 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
14523 DAG.getConstant(1, CmpOp0.getValueType()));
14524
14525 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
14526 if (CC == X86::COND_NE)
14527 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
14528 DL, OtherVal.getValueType(), OtherVal,
14529 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
14530 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
14531 DL, OtherVal.getValueType(), OtherVal,
14532 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
14533}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014534
Craig Topper54f952a2011-11-19 09:02:40 +000014535/// PerformADDCombine - Do target-specific dag combines on integer adds.
14536static SDValue PerformAddCombine(SDNode *N, SelectionDAG &DAG,
14537 const X86Subtarget *Subtarget) {
14538 EVT VT = N->getValueType(0);
14539 SDValue Op0 = N->getOperand(0);
14540 SDValue Op1 = N->getOperand(1);
14541
14542 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperb72039c2011-11-30 09:10:50 +000014543 if (((Subtarget->hasSSSE3orAVX() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
14544 (Subtarget->hasAVX2() && (VT == MVT::v16i16 || MVT::v8i32))) &&
Craig Topper54f952a2011-11-19 09:02:40 +000014545 isHorizontalBinOp(Op0, Op1, true))
14546 return DAG.getNode(X86ISD::HADD, N->getDebugLoc(), VT, Op0, Op1);
14547
14548 return OptimizeConditionalInDecrement(N, DAG);
14549}
14550
14551static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG,
14552 const X86Subtarget *Subtarget) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014553 SDValue Op0 = N->getOperand(0);
14554 SDValue Op1 = N->getOperand(1);
14555
14556 // X86 can't encode an immediate LHS of a sub. See if we can push the
14557 // negation into a preceding instruction.
14558 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014559 // If the RHS of the sub is a XOR with one use and a constant, invert the
14560 // immediate. Then add one to the LHS of the sub so we can turn
14561 // X-Y -> X+~Y+1, saving one register.
14562 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
14563 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000014564 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014565 EVT VT = Op0.getValueType();
14566 SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
14567 Op1.getOperand(0),
14568 DAG.getConstant(~XorC, VT));
14569 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000014570 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014571 }
14572 }
14573
Craig Topper54f952a2011-11-19 09:02:40 +000014574 // Try to synthesize horizontal adds from adds of shuffles.
14575 EVT VT = N->getValueType(0);
Craig Topperb72039c2011-11-30 09:10:50 +000014576 if (((Subtarget->hasSSSE3orAVX() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
14577 (Subtarget->hasAVX2() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
14578 isHorizontalBinOp(Op0, Op1, true))
Craig Topper54f952a2011-11-19 09:02:40 +000014579 return DAG.getNode(X86ISD::HSUB, N->getDebugLoc(), VT, Op0, Op1);
14580
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014581 return OptimizeConditionalInDecrement(N, DAG);
14582}
14583
Dan Gohman475871a2008-07-27 21:46:04 +000014584SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000014585 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000014586 SelectionDAG &DAG = DCI.DAG;
14587 switch (N->getOpcode()) {
14588 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014589 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014590 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Duncan Sands6bcd2192011-09-17 16:49:39 +000014591 case ISD::VSELECT:
Chris Lattneraf723b92008-01-25 05:46:26 +000014592 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000014593 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Craig Topper54f952a2011-11-19 09:02:40 +000014594 case ISD::ADD: return PerformAddCombine(N, DAG, Subtarget);
14595 case ISD::SUB: return PerformSubCombine(N, DAG, Subtarget);
Chris Lattner23a01992010-12-20 01:37:09 +000014596 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000014597 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000014598 case ISD::SHL:
14599 case ISD::SRA:
14600 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000014601 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000014602 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Craig Topperb4c94572011-10-21 06:55:01 +000014603 case ISD::XOR: return PerformXorCombine(N, DAG, DCI, Subtarget);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014604 case ISD::LOAD: return PerformLOADCombine(N, DAG, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000014605 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000014606 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Duncan Sands17470be2011-09-22 20:15:48 +000014607 case ISD::FADD: return PerformFADDCombine(N, DAG, Subtarget);
14608 case ISD::FSUB: return PerformFSUBCombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000014609 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000014610 case X86ISD::FOR: return PerformFORCombine(N, DAG);
14611 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000014612 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000014613 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000014614 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014615 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014616 case X86ISD::SHUFPS: // Handle all target specific shuffles
14617 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000014618 case X86ISD::PALIGN:
Craig Topper06cb6802011-11-26 20:47:44 +000014619 case X86ISD::PUNPCKH:
14620 case X86ISD::UNPCKHP:
14621 case X86ISD::PUNPCKL:
14622 case X86ISD::UNPCKLP:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014623 case X86ISD::MOVHLPS:
14624 case X86ISD::MOVLHPS:
14625 case X86ISD::PSHUFD:
14626 case X86ISD::PSHUFHW:
14627 case X86ISD::PSHUFLW:
14628 case X86ISD::MOVSS:
14629 case X86ISD::MOVSD:
Craig Topper316cd2a2011-11-30 06:25:25 +000014630 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +000014631 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014632 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000014633 }
14634
Dan Gohman475871a2008-07-27 21:46:04 +000014635 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000014636}
14637
Evan Chenge5b51ac2010-04-17 06:13:15 +000014638/// isTypeDesirableForOp - Return true if the target has native support for
14639/// the specified value type and it is 'desirable' to use the type for the
14640/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
14641/// instruction encodings are longer and some i16 instructions are slow.
14642bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
14643 if (!isTypeLegal(VT))
14644 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014645 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000014646 return true;
14647
14648 switch (Opc) {
14649 default:
14650 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000014651 case ISD::LOAD:
14652 case ISD::SIGN_EXTEND:
14653 case ISD::ZERO_EXTEND:
14654 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000014655 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000014656 case ISD::SRL:
14657 case ISD::SUB:
14658 case ISD::ADD:
14659 case ISD::MUL:
14660 case ISD::AND:
14661 case ISD::OR:
14662 case ISD::XOR:
14663 return false;
14664 }
14665}
14666
14667/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000014668/// beneficial for dag combiner to promote the specified node. If true, it
14669/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000014670bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000014671 EVT VT = Op.getValueType();
14672 if (VT != MVT::i16)
14673 return false;
14674
Evan Cheng4c26e932010-04-19 19:29:22 +000014675 bool Promote = false;
14676 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014677 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000014678 default: break;
14679 case ISD::LOAD: {
14680 LoadSDNode *LD = cast<LoadSDNode>(Op);
14681 // If the non-extending load has a single use and it's not live out, then it
14682 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014683 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
14684 Op.hasOneUse()*/) {
14685 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
14686 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
14687 // The only case where we'd want to promote LOAD (rather then it being
14688 // promoted as an operand is when it's only use is liveout.
14689 if (UI->getOpcode() != ISD::CopyToReg)
14690 return false;
14691 }
14692 }
Evan Cheng4c26e932010-04-19 19:29:22 +000014693 Promote = true;
14694 break;
14695 }
14696 case ISD::SIGN_EXTEND:
14697 case ISD::ZERO_EXTEND:
14698 case ISD::ANY_EXTEND:
14699 Promote = true;
14700 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014701 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014702 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000014703 SDValue N0 = Op.getOperand(0);
14704 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000014705 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000014706 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000014707 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014708 break;
14709 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000014710 case ISD::ADD:
14711 case ISD::MUL:
14712 case ISD::AND:
14713 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000014714 case ISD::XOR:
14715 Commute = true;
14716 // fallthrough
14717 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000014718 SDValue N0 = Op.getOperand(0);
14719 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000014720 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014721 return false;
14722 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000014723 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014724 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000014725 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014726 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000014727 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014728 }
14729 }
14730
14731 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000014732 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014733}
14734
Evan Cheng60c07e12006-07-05 22:17:51 +000014735//===----------------------------------------------------------------------===//
14736// X86 Inline Assembly Support
14737//===----------------------------------------------------------------------===//
14738
Chris Lattnerb8105652009-07-20 17:51:36 +000014739bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
14740 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000014741
14742 std::string AsmStr = IA->getAsmString();
14743
14744 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000014745 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000014746 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000014747
14748 switch (AsmPieces.size()) {
14749 default: return false;
14750 case 1:
14751 AsmStr = AsmPieces[0];
14752 AsmPieces.clear();
14753 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
14754
Chris Lattner7a2bdde2011-04-15 05:18:47 +000014755 // FIXME: this should verify that we are targeting a 486 or better. If not,
Evan Cheng55d42002011-01-08 01:24:27 +000014756 // we will turn this bswap into something that will be lowered to logical ops
14757 // instead of emitting the bswap asm. For now, we don't support 486 or lower
14758 // so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000014759 // bswap $0
14760 if (AsmPieces.size() == 2 &&
14761 (AsmPieces[0] == "bswap" ||
14762 AsmPieces[0] == "bswapq" ||
14763 AsmPieces[0] == "bswapl") &&
14764 (AsmPieces[1] == "$0" ||
14765 AsmPieces[1] == "${0:q}")) {
14766 // No need to check constraints, nothing other than the equivalent of
14767 // "=r,0" would be valid here.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014768 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014769 if (!Ty || Ty->getBitWidth() % 16 != 0)
14770 return false;
14771 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000014772 }
14773 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000014774 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000014775 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000014776 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000014777 AsmPieces[1] == "$$8," &&
14778 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000014779 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
14780 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014781 const std::string &ConstraintsStr = IA->getConstraintString();
14782 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000014783 std::sort(AsmPieces.begin(), AsmPieces.end());
14784 if (AsmPieces.size() == 4 &&
14785 AsmPieces[0] == "~{cc}" &&
14786 AsmPieces[1] == "~{dirflag}" &&
14787 AsmPieces[2] == "~{flags}" &&
14788 AsmPieces[3] == "~{fpsr}") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014789 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014790 if (!Ty || Ty->getBitWidth() % 16 != 0)
14791 return false;
14792 return IntrinsicLowering::LowerToByteSwap(CI);
Dan Gohman0ef701e2010-03-04 19:58:08 +000014793 }
Chris Lattnerb8105652009-07-20 17:51:36 +000014794 }
14795 break;
14796 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000014797 if (CI->getType()->isIntegerTy(32) &&
14798 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
14799 SmallVector<StringRef, 4> Words;
14800 SplitString(AsmPieces[0], Words, " \t,");
14801 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
14802 Words[2] == "${0:w}") {
14803 Words.clear();
14804 SplitString(AsmPieces[1], Words, " \t,");
14805 if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
14806 Words[2] == "$0") {
14807 Words.clear();
14808 SplitString(AsmPieces[2], Words, " \t,");
14809 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
14810 Words[2] == "${0:w}") {
14811 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014812 const std::string &ConstraintsStr = IA->getConstraintString();
14813 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Peter Collingbourne948cf022010-11-13 19:54:30 +000014814 std::sort(AsmPieces.begin(), AsmPieces.end());
14815 if (AsmPieces.size() == 4 &&
14816 AsmPieces[0] == "~{cc}" &&
14817 AsmPieces[1] == "~{dirflag}" &&
14818 AsmPieces[2] == "~{flags}" &&
14819 AsmPieces[3] == "~{fpsr}") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014820 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014821 if (!Ty || Ty->getBitWidth() % 16 != 0)
14822 return false;
14823 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000014824 }
14825 }
14826 }
14827 }
14828 }
Evan Cheng55d42002011-01-08 01:24:27 +000014829
14830 if (CI->getType()->isIntegerTy(64)) {
14831 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
14832 if (Constraints.size() >= 2 &&
14833 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
14834 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
14835 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
14836 SmallVector<StringRef, 4> Words;
14837 SplitString(AsmPieces[0], Words, " \t");
14838 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
Chris Lattnerb8105652009-07-20 17:51:36 +000014839 Words.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014840 SplitString(AsmPieces[1], Words, " \t");
14841 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
14842 Words.clear();
14843 SplitString(AsmPieces[2], Words, " \t,");
14844 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
14845 Words[2] == "%edx") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014846 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014847 if (!Ty || Ty->getBitWidth() % 16 != 0)
14848 return false;
14849 return IntrinsicLowering::LowerToByteSwap(CI);
14850 }
Chris Lattnerb8105652009-07-20 17:51:36 +000014851 }
14852 }
14853 }
14854 }
14855 break;
14856 }
14857 return false;
14858}
14859
14860
14861
Chris Lattnerf4dff842006-07-11 02:54:03 +000014862/// getConstraintType - Given a constraint letter, return the type of
14863/// constraint it is for this target.
14864X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000014865X86TargetLowering::getConstraintType(const std::string &Constraint) const {
14866 if (Constraint.size() == 1) {
14867 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000014868 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000014869 case 'q':
14870 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000014871 case 'f':
14872 case 't':
14873 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000014874 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000014875 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000014876 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000014877 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000014878 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000014879 case 'a':
14880 case 'b':
14881 case 'c':
14882 case 'd':
14883 case 'S':
14884 case 'D':
14885 case 'A':
14886 return C_Register;
14887 case 'I':
14888 case 'J':
14889 case 'K':
14890 case 'L':
14891 case 'M':
14892 case 'N':
14893 case 'G':
14894 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000014895 case 'e':
14896 case 'Z':
14897 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000014898 default:
14899 break;
14900 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000014901 }
Chris Lattner4234f572007-03-25 02:14:49 +000014902 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000014903}
14904
John Thompson44ab89e2010-10-29 17:29:13 +000014905/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000014906/// This object must already have been set up with the operand type
14907/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000014908TargetLowering::ConstraintWeight
14909 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000014910 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000014911 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014912 Value *CallOperandVal = info.CallOperandVal;
14913 // If we don't have a value, we can't do a match,
14914 // but allow it at the lowest weight.
14915 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000014916 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014917 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000014918 // Look at the constraint type.
14919 switch (*constraint) {
14920 default:
John Thompson44ab89e2010-10-29 17:29:13 +000014921 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
14922 case 'R':
14923 case 'q':
14924 case 'Q':
14925 case 'a':
14926 case 'b':
14927 case 'c':
14928 case 'd':
14929 case 'S':
14930 case 'D':
14931 case 'A':
14932 if (CallOperandVal->getType()->isIntegerTy())
14933 weight = CW_SpecificReg;
14934 break;
14935 case 'f':
14936 case 't':
14937 case 'u':
14938 if (type->isFloatingPointTy())
14939 weight = CW_SpecificReg;
14940 break;
14941 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000014942 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000014943 weight = CW_SpecificReg;
14944 break;
14945 case 'x':
14946 case 'Y':
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014947 if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
John Thompson44ab89e2010-10-29 17:29:13 +000014948 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014949 break;
14950 case 'I':
14951 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
14952 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000014953 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014954 }
14955 break;
John Thompson44ab89e2010-10-29 17:29:13 +000014956 case 'J':
14957 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14958 if (C->getZExtValue() <= 63)
14959 weight = CW_Constant;
14960 }
14961 break;
14962 case 'K':
14963 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14964 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
14965 weight = CW_Constant;
14966 }
14967 break;
14968 case 'L':
14969 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14970 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
14971 weight = CW_Constant;
14972 }
14973 break;
14974 case 'M':
14975 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14976 if (C->getZExtValue() <= 3)
14977 weight = CW_Constant;
14978 }
14979 break;
14980 case 'N':
14981 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14982 if (C->getZExtValue() <= 0xff)
14983 weight = CW_Constant;
14984 }
14985 break;
14986 case 'G':
14987 case 'C':
14988 if (dyn_cast<ConstantFP>(CallOperandVal)) {
14989 weight = CW_Constant;
14990 }
14991 break;
14992 case 'e':
14993 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14994 if ((C->getSExtValue() >= -0x80000000LL) &&
14995 (C->getSExtValue() <= 0x7fffffffLL))
14996 weight = CW_Constant;
14997 }
14998 break;
14999 case 'Z':
15000 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15001 if (C->getZExtValue() <= 0xffffffff)
15002 weight = CW_Constant;
15003 }
15004 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000015005 }
15006 return weight;
15007}
15008
Dale Johannesenba2a0b92008-01-29 02:21:21 +000015009/// LowerXConstraint - try to replace an X constraint, which matches anything,
15010/// with another that has more specific requirements based on the type of the
15011/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000015012const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000015013LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000015014 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
15015 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000015016 if (ConstraintVT.isFloatingPoint()) {
Nate Begeman2ea8ee72010-12-10 00:26:57 +000015017 if (Subtarget->hasXMMInt())
Chris Lattner5e764232008-04-26 23:02:14 +000015018 return "Y";
Nate Begeman2ea8ee72010-12-10 00:26:57 +000015019 if (Subtarget->hasXMM())
Chris Lattner5e764232008-04-26 23:02:14 +000015020 return "x";
15021 }
Scott Michelfdc40a02009-02-17 22:15:04 +000015022
Chris Lattner5e764232008-04-26 23:02:14 +000015023 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000015024}
15025
Chris Lattner48884cd2007-08-25 00:47:38 +000015026/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
15027/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000015028void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000015029 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000015030 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000015031 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000015032 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000015033
Eric Christopher100c8332011-06-02 23:16:42 +000015034 // Only support length 1 constraints for now.
15035 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000015036
Eric Christopher100c8332011-06-02 23:16:42 +000015037 char ConstraintLetter = Constraint[0];
15038 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000015039 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000015040 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000015041 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000015042 if (C->getZExtValue() <= 31) {
15043 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000015044 break;
15045 }
Devang Patel84f7fd22007-03-17 00:13:28 +000015046 }
Chris Lattner48884cd2007-08-25 00:47:38 +000015047 return;
Evan Cheng364091e2008-09-22 23:57:37 +000015048 case 'J':
15049 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000015050 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000015051 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
15052 break;
15053 }
15054 }
15055 return;
15056 case 'K':
15057 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000015058 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000015059 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
15060 break;
15061 }
15062 }
15063 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000015064 case 'N':
15065 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000015066 if (C->getZExtValue() <= 255) {
15067 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000015068 break;
15069 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000015070 }
Chris Lattner48884cd2007-08-25 00:47:38 +000015071 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000015072 case 'e': {
15073 // 32-bit signed value
15074 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000015075 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
15076 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000015077 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000015078 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000015079 break;
15080 }
15081 // FIXME gcc accepts some relocatable values here too, but only in certain
15082 // memory models; it's complicated.
15083 }
15084 return;
15085 }
15086 case 'Z': {
15087 // 32-bit unsigned value
15088 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000015089 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
15090 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000015091 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
15092 break;
15093 }
15094 }
15095 // FIXME gcc accepts some relocatable values here too, but only in certain
15096 // memory models; it's complicated.
15097 return;
15098 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000015099 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000015100 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000015101 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000015102 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000015103 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000015104 break;
15105 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015106
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000015107 // In any sort of PIC mode addresses need to be computed at runtime by
15108 // adding in a register or some sort of table lookup. These can't
15109 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000015110 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000015111 return;
15112
Chris Lattnerdc43a882007-05-03 16:52:29 +000015113 // If we are in non-pic codegen mode, we allow the address of a global (with
15114 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000015115 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000015116 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000015117
Chris Lattner49921962009-05-08 18:23:14 +000015118 // Match either (GA), (GA+C), (GA+C1+C2), etc.
15119 while (1) {
15120 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
15121 Offset += GA->getOffset();
15122 break;
15123 } else if (Op.getOpcode() == ISD::ADD) {
15124 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
15125 Offset += C->getZExtValue();
15126 Op = Op.getOperand(0);
15127 continue;
15128 }
15129 } else if (Op.getOpcode() == ISD::SUB) {
15130 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
15131 Offset += -C->getZExtValue();
15132 Op = Op.getOperand(0);
15133 continue;
15134 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000015135 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000015136
Chris Lattner49921962009-05-08 18:23:14 +000015137 // Otherwise, this isn't something we can handle, reject it.
15138 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000015139 }
Eric Christopherfd179292009-08-27 18:07:15 +000015140
Dan Gohman46510a72010-04-15 01:51:59 +000015141 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000015142 // If we require an extra load to get this address, as in PIC mode, we
15143 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000015144 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
15145 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000015146 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000015147
Devang Patel0d881da2010-07-06 22:08:15 +000015148 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
15149 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000015150 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000015151 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000015152 }
Scott Michelfdc40a02009-02-17 22:15:04 +000015153
Gabor Greifba36cb52008-08-28 21:40:38 +000015154 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000015155 Ops.push_back(Result);
15156 return;
15157 }
Dale Johannesen1784d162010-06-25 21:55:36 +000015158 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000015159}
15160
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015161std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000015162X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000015163 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000015164 // First, see if this is a constraint that directly corresponds to an LLVM
15165 // register class.
15166 if (Constraint.size() == 1) {
15167 // GCC Constraint Letters
15168 switch (Constraint[0]) {
15169 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000015170 // TODO: Slight differences here in allocation order and leaving
15171 // RIP in the class. Do they matter any more here than they do
15172 // in the normal allocation?
15173 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
15174 if (Subtarget->is64Bit()) {
Nick Lewycky9bf45d02011-07-08 00:19:27 +000015175 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000015176 return std::make_pair(0U, X86::GR32RegisterClass);
15177 else if (VT == MVT::i16)
15178 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000015179 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000015180 return std::make_pair(0U, X86::GR8RegisterClass);
Nick Lewycky9bf45d02011-07-08 00:19:27 +000015181 else if (VT == MVT::i64 || VT == MVT::f64)
Eric Christopherd176af82011-06-29 17:23:50 +000015182 return std::make_pair(0U, X86::GR64RegisterClass);
15183 break;
15184 }
15185 // 32-bit fallthrough
15186 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000015187 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000015188 return std::make_pair(0U, X86::GR32_ABCDRegisterClass);
15189 else if (VT == MVT::i16)
15190 return std::make_pair(0U, X86::GR16_ABCDRegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000015191 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000015192 return std::make_pair(0U, X86::GR8_ABCD_LRegisterClass);
15193 else if (VT == MVT::i64)
15194 return std::make_pair(0U, X86::GR64_ABCDRegisterClass);
15195 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000015196 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000015197 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000015198 if (VT == MVT::i8 || VT == MVT::i1)
Chris Lattner0f65cad2007-04-09 05:49:22 +000015199 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000015200 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000015201 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000015202 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000015203 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000015204 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000015205 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000015206 if (VT == MVT::i8 || VT == MVT::i1)
Dale Johannesen5f3663e2009-10-07 22:47:20 +000015207 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
15208 if (VT == MVT::i16)
15209 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
15210 if (VT == MVT::i32 || !Subtarget->is64Bit())
15211 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
15212 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000015213 case 'f': // FP Stack registers.
15214 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
15215 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000015216 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000015217 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000015218 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000015219 return std::make_pair(0U, X86::RFP64RegisterClass);
15220 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000015221 case 'y': // MMX_REGS if MMX allowed.
15222 if (!Subtarget->hasMMX()) break;
15223 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000015224 case 'Y': // SSE_REGS if SSE2 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000015225 if (!Subtarget->hasXMMInt()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000015226 // FALL THROUGH.
15227 case 'x': // SSE_REGS if SSE1 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000015228 if (!Subtarget->hasXMM()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000015229
Owen Anderson825b72b2009-08-11 20:47:22 +000015230 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000015231 default: break;
15232 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000015233 case MVT::f32:
15234 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000015235 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000015236 case MVT::f64:
15237 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000015238 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000015239 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000015240 case MVT::v16i8:
15241 case MVT::v8i16:
15242 case MVT::v4i32:
15243 case MVT::v2i64:
15244 case MVT::v4f32:
15245 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000015246 return std::make_pair(0U, X86::VR128RegisterClass);
15247 }
Chris Lattnerad043e82007-04-09 05:11:28 +000015248 break;
15249 }
15250 }
Scott Michelfdc40a02009-02-17 22:15:04 +000015251
Chris Lattnerf76d1802006-07-31 23:26:50 +000015252 // Use the default implementation in TargetLowering to convert the register
15253 // constraint into a member of a register class.
15254 std::pair<unsigned, const TargetRegisterClass*> Res;
15255 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000015256
15257 // Not found as a standard register?
15258 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000015259 // Map st(0) -> st(7) -> ST0
15260 if (Constraint.size() == 7 && Constraint[0] == '{' &&
15261 tolower(Constraint[1]) == 's' &&
15262 tolower(Constraint[2]) == 't' &&
15263 Constraint[3] == '(' &&
15264 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
15265 Constraint[5] == ')' &&
15266 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000015267
Chris Lattner56d77c72009-09-13 22:41:48 +000015268 Res.first = X86::ST0+Constraint[4]-'0';
15269 Res.second = X86::RFP80RegisterClass;
15270 return Res;
15271 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000015272
Chris Lattner56d77c72009-09-13 22:41:48 +000015273 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000015274 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000015275 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000015276 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000015277 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000015278 }
Chris Lattner56d77c72009-09-13 22:41:48 +000015279
15280 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000015281 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000015282 Res.first = X86::EFLAGS;
15283 Res.second = X86::CCRRegisterClass;
15284 return Res;
15285 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000015286
Dale Johannesen330169f2008-11-13 21:52:36 +000015287 // 'A' means EAX + EDX.
15288 if (Constraint == "A") {
15289 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000015290 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000015291 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000015292 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000015293 return Res;
15294 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015295
Chris Lattnerf76d1802006-07-31 23:26:50 +000015296 // Otherwise, check to see if this is a register class of the wrong value
15297 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
15298 // turn into {ax},{dx}.
15299 if (Res.second->hasType(VT))
15300 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015301
Chris Lattnerf76d1802006-07-31 23:26:50 +000015302 // All of the single-register GCC register classes map their values onto
15303 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
15304 // really want an 8-bit or 32-bit register, map to the appropriate register
15305 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000015306 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015307 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000015308 unsigned DestReg = 0;
15309 switch (Res.first) {
15310 default: break;
15311 case X86::AX: DestReg = X86::AL; break;
15312 case X86::DX: DestReg = X86::DL; break;
15313 case X86::CX: DestReg = X86::CL; break;
15314 case X86::BX: DestReg = X86::BL; break;
15315 }
15316 if (DestReg) {
15317 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000015318 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000015319 }
Owen Anderson825b72b2009-08-11 20:47:22 +000015320 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000015321 unsigned DestReg = 0;
15322 switch (Res.first) {
15323 default: break;
15324 case X86::AX: DestReg = X86::EAX; break;
15325 case X86::DX: DestReg = X86::EDX; break;
15326 case X86::CX: DestReg = X86::ECX; break;
15327 case X86::BX: DestReg = X86::EBX; break;
15328 case X86::SI: DestReg = X86::ESI; break;
15329 case X86::DI: DestReg = X86::EDI; break;
15330 case X86::BP: DestReg = X86::EBP; break;
15331 case X86::SP: DestReg = X86::ESP; break;
15332 }
15333 if (DestReg) {
15334 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000015335 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000015336 }
Owen Anderson825b72b2009-08-11 20:47:22 +000015337 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000015338 unsigned DestReg = 0;
15339 switch (Res.first) {
15340 default: break;
15341 case X86::AX: DestReg = X86::RAX; break;
15342 case X86::DX: DestReg = X86::RDX; break;
15343 case X86::CX: DestReg = X86::RCX; break;
15344 case X86::BX: DestReg = X86::RBX; break;
15345 case X86::SI: DestReg = X86::RSI; break;
15346 case X86::DI: DestReg = X86::RDI; break;
15347 case X86::BP: DestReg = X86::RBP; break;
15348 case X86::SP: DestReg = X86::RSP; break;
15349 }
15350 if (DestReg) {
15351 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000015352 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000015353 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000015354 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000015355 } else if (Res.second == X86::FR32RegisterClass ||
15356 Res.second == X86::FR64RegisterClass ||
15357 Res.second == X86::VR128RegisterClass) {
15358 // Handle references to XMM physical registers that got mapped into the
15359 // wrong class. This can happen with constraints like {xmm0} where the
15360 // target independent register mapper will just pick the first match it can
15361 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000015362 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000015363 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000015364 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000015365 Res.second = X86::FR64RegisterClass;
15366 else if (X86::VR128RegisterClass->hasType(VT))
15367 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000015368 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015369
Chris Lattnerf76d1802006-07-31 23:26:50 +000015370 return Res;
15371}