blob: fa43ac398e5762f98582526443ccb19b324c60a3 [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 Topper34671b82011-12-06 08:21:25 +00002854 case X86ISD::UNPCKL:
2855 case X86ISD::UNPCKH:
Craig Topper316cd2a2011-11-30 06:25:25 +00002856 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +00002857 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002858 return true;
2859 }
2860 return false;
2861}
2862
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002863static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002864 SDValue V1, SelectionDAG &DAG) {
2865 switch(Opc) {
2866 default: llvm_unreachable("Unknown x86 shuffle node");
2867 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002868 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002869 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002870 return DAG.getNode(Opc, dl, VT, V1);
2871 }
2872
2873 return SDValue();
2874}
2875
2876static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002877 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002878 switch(Opc) {
2879 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002880 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002881 case X86ISD::PSHUFHW:
2882 case X86ISD::PSHUFLW:
Craig Topper316cd2a2011-11-30 06:25:25 +00002883 case X86ISD::VPERMILP:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002884 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2885 }
2886
2887 return SDValue();
2888}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002889
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002890static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2891 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2892 switch(Opc) {
2893 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002894 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002895 case X86ISD::SHUFPD:
2896 case X86ISD::SHUFPS:
Craig Topperec24e612011-11-30 07:47:51 +00002897 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002898 return DAG.getNode(Opc, dl, VT, V1, V2,
2899 DAG.getConstant(TargetMask, MVT::i8));
2900 }
2901 return SDValue();
2902}
2903
2904static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2905 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2906 switch(Opc) {
2907 default: llvm_unreachable("Unknown x86 shuffle node");
2908 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002909 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002910 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002911 case X86ISD::MOVLPS:
2912 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002913 case X86ISD::MOVSS:
2914 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00002915 case X86ISD::UNPCKL:
2916 case X86ISD::UNPCKH:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002917 return DAG.getNode(Opc, dl, VT, V1, V2);
2918 }
2919 return SDValue();
2920}
2921
Dan Gohmand858e902010-04-17 15:26:15 +00002922SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002923 MachineFunction &MF = DAG.getMachineFunction();
2924 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2925 int ReturnAddrIndex = FuncInfo->getRAIndex();
2926
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002927 if (ReturnAddrIndex == 0) {
2928 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002929 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002930 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002931 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002932 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002933 }
2934
Evan Cheng25ab6902006-09-08 06:48:29 +00002935 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002936}
2937
2938
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002939bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2940 bool hasSymbolicDisplacement) {
2941 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002942 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002943 return false;
2944
2945 // If we don't have a symbolic displacement - we don't have any extra
2946 // restrictions.
2947 if (!hasSymbolicDisplacement)
2948 return true;
2949
2950 // FIXME: Some tweaks might be needed for medium code model.
2951 if (M != CodeModel::Small && M != CodeModel::Kernel)
2952 return false;
2953
2954 // For small code model we assume that latest object is 16MB before end of 31
2955 // bits boundary. We may also accept pretty large negative constants knowing
2956 // that all objects are in the positive half of address space.
2957 if (M == CodeModel::Small && Offset < 16*1024*1024)
2958 return true;
2959
2960 // For kernel code model we know that all object resist in the negative half
2961 // of 32bits address space. We may not accept negative offsets, since they may
2962 // be just off and we may accept pretty large positive ones.
2963 if (M == CodeModel::Kernel && Offset > 0)
2964 return true;
2965
2966 return false;
2967}
2968
Evan Chengef41ff62011-06-23 17:54:54 +00002969/// isCalleePop - Determines whether the callee is required to pop its
2970/// own arguments. Callee pop is necessary to support tail calls.
2971bool X86::isCalleePop(CallingConv::ID CallingConv,
2972 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
2973 if (IsVarArg)
2974 return false;
2975
2976 switch (CallingConv) {
2977 default:
2978 return false;
2979 case CallingConv::X86_StdCall:
2980 return !is64Bit;
2981 case CallingConv::X86_FastCall:
2982 return !is64Bit;
2983 case CallingConv::X86_ThisCall:
2984 return !is64Bit;
2985 case CallingConv::Fast:
2986 return TailCallOpt;
2987 case CallingConv::GHC:
2988 return TailCallOpt;
2989 }
2990}
2991
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002992/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2993/// specific condition code, returning the condition code and the LHS/RHS of the
2994/// comparison to make.
2995static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2996 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002997 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002998 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2999 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
3000 // X > -1 -> X == 0, jump !sign.
3001 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003002 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003003 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
3004 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003005 return X86::COND_S;
Andrew Trickf6c39412011-03-23 23:11:02 +00003006 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00003007 // X < 1 -> X <= 0
3008 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003009 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003010 }
Chris Lattnerf9570512006-09-13 03:22:10 +00003011 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003012
Evan Chengd9558e02006-01-06 00:43:03 +00003013 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003014 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003015 case ISD::SETEQ: return X86::COND_E;
3016 case ISD::SETGT: return X86::COND_G;
3017 case ISD::SETGE: return X86::COND_GE;
3018 case ISD::SETLT: return X86::COND_L;
3019 case ISD::SETLE: return X86::COND_LE;
3020 case ISD::SETNE: return X86::COND_NE;
3021 case ISD::SETULT: return X86::COND_B;
3022 case ISD::SETUGT: return X86::COND_A;
3023 case ISD::SETULE: return X86::COND_BE;
3024 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00003025 }
Chris Lattner4c78e022008-12-23 23:42:27 +00003026 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003027
Chris Lattner4c78e022008-12-23 23:42:27 +00003028 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00003029
Chris Lattner4c78e022008-12-23 23:42:27 +00003030 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00003031 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
3032 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00003033 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
3034 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00003035 }
3036
Chris Lattner4c78e022008-12-23 23:42:27 +00003037 switch (SetCCOpcode) {
3038 default: break;
3039 case ISD::SETOLT:
3040 case ISD::SETOLE:
3041 case ISD::SETUGT:
3042 case ISD::SETUGE:
3043 std::swap(LHS, RHS);
3044 break;
3045 }
3046
3047 // On a floating point condition, the flags are set as follows:
3048 // ZF PF CF op
3049 // 0 | 0 | 0 | X > Y
3050 // 0 | 0 | 1 | X < Y
3051 // 1 | 0 | 0 | X == Y
3052 // 1 | 1 | 1 | unordered
3053 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003054 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00003055 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003056 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00003057 case ISD::SETOLT: // flipped
3058 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003059 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00003060 case ISD::SETOLE: // flipped
3061 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003062 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003063 case ISD::SETUGT: // flipped
3064 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003065 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00003066 case ISD::SETUGE: // flipped
3067 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003068 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003069 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003070 case ISD::SETNE: return X86::COND_NE;
3071 case ISD::SETUO: return X86::COND_P;
3072 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003073 case ISD::SETOEQ:
3074 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003075 }
Evan Chengd9558e02006-01-06 00:43:03 +00003076}
3077
Evan Cheng4a460802006-01-11 00:33:36 +00003078/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3079/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003080/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003081static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003082 switch (X86CC) {
3083 default:
3084 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003085 case X86::COND_B:
3086 case X86::COND_BE:
3087 case X86::COND_E:
3088 case X86::COND_P:
3089 case X86::COND_A:
3090 case X86::COND_AE:
3091 case X86::COND_NE:
3092 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003093 return true;
3094 }
3095}
3096
Evan Chengeb2f9692009-10-27 19:56:55 +00003097/// isFPImmLegal - Returns true if the target can instruction select the
3098/// specified FP immediate natively. If false, the legalizer will
3099/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003100bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003101 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3102 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3103 return true;
3104 }
3105 return false;
3106}
3107
Nate Begeman9008ca62009-04-27 18:41:29 +00003108/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3109/// the specified range (L, H].
3110static bool isUndefOrInRange(int Val, int Low, int Hi) {
3111 return (Val < 0) || (Val >= Low && Val < Hi);
3112}
3113
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00003114/// isUndefOrInRange - Return true if every element in Mask, begining
3115/// from position Pos and ending in Pos+Size, falls within the specified
3116/// range (L, L+Pos]. or is undef.
3117static bool isUndefOrInRange(const SmallVectorImpl<int> &Mask,
3118 int Pos, int Size, int Low, int Hi) {
3119 for (int i = Pos, e = Pos+Size; i != e; ++i)
3120 if (!isUndefOrInRange(Mask[i], Low, Hi))
3121 return false;
3122 return true;
3123}
3124
Nate Begeman9008ca62009-04-27 18:41:29 +00003125/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3126/// specified value.
3127static bool isUndefOrEqual(int Val, int CmpVal) {
3128 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003129 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003130 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00003131}
3132
Bruno Cardoso Lopes4002d7e2011-08-12 21:54:42 +00003133/// isSequentialOrUndefInRange - Return true if every element in Mask, begining
3134/// from position Pos and ending in Pos+Size, falls within the specified
3135/// sequential range (L, L+Pos]. or is undef.
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003136static bool isSequentialOrUndefInRange(const SmallVectorImpl<int> &Mask,
3137 int Pos, int Size, int Low) {
3138 for (int i = Pos, e = Pos+Size; i != e; ++i, ++Low)
3139 if (!isUndefOrEqual(Mask[i], Low))
3140 return false;
3141 return true;
3142}
3143
Nate Begeman9008ca62009-04-27 18:41:29 +00003144/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3145/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3146/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00003147static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003148 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003149 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003150 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003151 return (Mask[0] < 2 && Mask[1] < 2);
3152 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003153}
3154
Nate Begeman9008ca62009-04-27 18:41:29 +00003155bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003156 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003157 N->getMask(M);
3158 return ::isPSHUFDMask(M, N->getValueType(0));
3159}
Evan Cheng0188ecb2006-03-22 18:59:22 +00003160
Nate Begeman9008ca62009-04-27 18:41:29 +00003161/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3162/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00003163static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003164 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00003165 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003166
Nate Begeman9008ca62009-04-27 18:41:29 +00003167 // Lower quadword copied in order or undef.
3168 for (int i = 0; i != 4; ++i)
3169 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00003170 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003171
Evan Cheng506d3df2006-03-29 23:07:14 +00003172 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003173 for (int i = 4; i != 8; ++i)
3174 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00003175 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003176
Evan Cheng506d3df2006-03-29 23:07:14 +00003177 return true;
3178}
3179
Nate Begeman9008ca62009-04-27 18:41:29 +00003180bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003181 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003182 N->getMask(M);
3183 return ::isPSHUFHWMask(M, N->getValueType(0));
3184}
Evan Cheng506d3df2006-03-29 23:07:14 +00003185
Nate Begeman9008ca62009-04-27 18:41:29 +00003186/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3187/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00003188static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003189 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00003190 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003191
Rafael Espindola15684b22009-04-24 12:40:33 +00003192 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00003193 for (int i = 4; i != 8; ++i)
3194 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00003195 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003196
Rafael Espindola15684b22009-04-24 12:40:33 +00003197 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003198 for (int i = 0; i != 4; ++i)
3199 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00003200 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003201
Rafael Espindola15684b22009-04-24 12:40:33 +00003202 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003203}
3204
Nate Begeman9008ca62009-04-27 18:41:29 +00003205bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003206 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003207 N->getMask(M);
3208 return ::isPSHUFLWMask(M, N->getValueType(0));
3209}
3210
Nate Begemana09008b2009-10-19 02:17:23 +00003211/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3212/// is suitable for input to PALIGNR.
3213static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00003214 bool hasSSSE3OrAVX) {
Nate Begemana09008b2009-10-19 02:17:23 +00003215 int i, e = VT.getVectorNumElements();
Craig Topper1dc0fbc2011-12-05 07:27:14 +00003216 if (VT.getSizeInBits() != 128)
Bruno Cardoso Lopes9065d4b2011-07-29 01:30:59 +00003217 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003218
Nate Begemana09008b2009-10-19 02:17:23 +00003219 // Do not handle v2i64 / v2f64 shuffles with palignr.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00003220 if (e < 4 || !hasSSSE3OrAVX)
Nate Begemana09008b2009-10-19 02:17:23 +00003221 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003222
Nate Begemana09008b2009-10-19 02:17:23 +00003223 for (i = 0; i != e; ++i)
3224 if (Mask[i] >= 0)
3225 break;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003226
Nate Begemana09008b2009-10-19 02:17:23 +00003227 // All undef, not a palignr.
3228 if (i == e)
3229 return false;
3230
Eli Friedman63f8dde2011-07-25 21:36:45 +00003231 // Make sure we're shifting in the right direction.
3232 if (Mask[i] <= i)
3233 return false;
Nate Begemana09008b2009-10-19 02:17:23 +00003234
3235 int s = Mask[i] - i;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003236
Nate Begemana09008b2009-10-19 02:17:23 +00003237 // Check the rest of the elements to see if they are consecutive.
3238 for (++i; i != e; ++i) {
3239 int m = Mask[i];
Eli Friedman63f8dde2011-07-25 21:36:45 +00003240 if (m >= 0 && m != s+i)
Nate Begemana09008b2009-10-19 02:17:23 +00003241 return false;
3242 }
3243 return true;
3244}
3245
Craig Topper9d7025b2011-11-27 21:41:12 +00003246/// isVSHUFPYMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003247/// specifies a shuffle of elements that is suitable for input to 256-bit
3248/// VSHUFPSY.
Craig Topper9d7025b2011-11-27 21:41:12 +00003249static bool isVSHUFPYMask(const SmallVectorImpl<int> &Mask, EVT VT,
Craig Topper1ff73d72011-12-06 04:59:07 +00003250 bool HasAVX, bool Commuted = false) {
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003251 int NumElems = VT.getVectorNumElements();
3252
Craig Topper71c4c122011-11-28 01:14:24 +00003253 if (!HasAVX || VT.getSizeInBits() != 256)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003254 return false;
3255
Craig Topper9d7025b2011-11-27 21:41:12 +00003256 if (NumElems != 4 && NumElems != 8)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003257 return false;
3258
3259 // VSHUFPSY divides the resulting vector into 4 chunks.
3260 // The sources are also splitted into 4 chunks, and each destination
3261 // chunk must come from a different source chunk.
3262 //
3263 // SRC1 => X7 X6 X5 X4 X3 X2 X1 X0
3264 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3265 //
3266 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3267 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3268 //
Craig Topper9d7025b2011-11-27 21:41:12 +00003269 // VSHUFPDY divides the resulting vector into 4 chunks.
3270 // The sources are also splitted into 4 chunks, and each destination
3271 // chunk must come from a different source chunk.
3272 //
3273 // SRC1 => X3 X2 X1 X0
3274 // SRC2 => Y3 Y2 Y1 Y0
3275 //
3276 // DST => Y3..Y2, X3..X2, Y1..Y0, X1..X0
3277 //
Craig Topper1ff73d72011-12-06 04:59:07 +00003278 unsigned QuarterSize = NumElems/4;
3279 unsigned HalfSize = QuarterSize*2;
3280 for (unsigned l = 0; l != 2; ++l) {
3281 unsigned LaneStart = l*HalfSize;
3282 for (unsigned s = 0; s != 2; ++s) {
3283 unsigned QuarterStart = s*QuarterSize;
3284 unsigned Src = (Commuted) ? (1-s) : s;
3285 unsigned SrcStart = Src*NumElems + LaneStart;
3286 for (unsigned i = 0; i != QuarterSize; ++i) {
3287 int Idx = Mask[i+QuarterStart+LaneStart];
3288 if (!isUndefOrInRange(Idx, SrcStart, SrcStart+HalfSize))
3289 return false;
3290 // For VSHUFPSY, the mask of the second half must be the same as the first
3291 // but with the appropriate offsets. This works in the same way as
3292 // VPERMILPS works with masks.
3293 if (NumElems == 4 || l == 0 || Mask[i+QuarterStart] < 0)
3294 continue;
3295 if (!isUndefOrEqual(Idx, Mask[i+QuarterStart]+HalfSize))
3296 return false;
3297 }
3298 }
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003299 }
3300
3301 return true;
3302}
3303
Craig Topper9d7025b2011-11-27 21:41:12 +00003304/// getShuffleVSHUFPYImmediate - Return the appropriate immediate to shuffle
3305/// the specified VECTOR_MASK mask with VSHUFPSY/VSHUFPDY instructions.
3306static unsigned getShuffleVSHUFPYImmediate(SDNode *N) {
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003307 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3308 EVT VT = SVOp->getValueType(0);
3309 int NumElems = VT.getVectorNumElements();
3310
Craig Topper9d7025b2011-11-27 21:41:12 +00003311 assert(VT.getSizeInBits() == 256 && "Only supports 256-bit types");
3312 assert((NumElems == 4 || NumElems == 8) && "Only supports v4 and v8 types");
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003313
3314 int HalfSize = NumElems/2;
Craig Topper9d7025b2011-11-27 21:41:12 +00003315 unsigned Mul = (NumElems == 8) ? 2 : 1;
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003316 unsigned Mask = 0;
Craig Topper71c4c122011-11-28 01:14:24 +00003317 for (int i = 0; i != NumElems; ++i) {
Craig Topper9d7025b2011-11-27 21:41:12 +00003318 int Elt = SVOp->getMaskElt(i);
3319 if (Elt < 0)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003320 continue;
Craig Topper9d7025b2011-11-27 21:41:12 +00003321 Elt %= HalfSize;
3322 unsigned Shamt = i;
3323 // For VSHUFPSY, the mask of the first half must be equal to the second one.
3324 if (NumElems == 8) Shamt %= HalfSize;
3325 Mask |= Elt << (Shamt*Mul);
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003326 }
3327
3328 return Mask;
3329}
3330
Elena Demikhovsky52a35a82011-11-23 10:23:16 +00003331/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3332/// the two vector operands have swapped position.
Craig Topperbeabc6c2011-12-05 06:56:46 +00003333static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask,
3334 unsigned NumElems) {
Elena Demikhovsky52a35a82011-11-23 10:23:16 +00003335 for (unsigned i = 0; i != NumElems; ++i) {
3336 int idx = Mask[i];
3337 if (idx < 0)
3338 continue;
3339 else if (idx < (int)NumElems)
3340 Mask[i] = idx + NumElems;
3341 else
3342 Mask[i] = idx - NumElems;
3343 }
3344}
3345
Evan Cheng14aed5e2006-03-24 01:18:28 +00003346/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopesaf002d82011-08-24 23:17:55 +00003347/// specifies a shuffle of elements that is suitable for input to 128-bit
Craig Topper1ff73d72011-12-06 04:59:07 +00003348/// SHUFPS and SHUFPD. If Commuted is true, then it checks for sources to be
3349/// reverse of what x86 shuffles want.
3350static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT,
3351 bool Commuted = false) {
3352 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesaf002d82011-08-24 23:17:55 +00003353
3354 if (VT.getSizeInBits() != 128)
3355 return false;
3356
Nate Begeman9008ca62009-04-27 18:41:29 +00003357 if (NumElems != 2 && NumElems != 4)
3358 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003359
Craig Topper1ff73d72011-12-06 04:59:07 +00003360 unsigned Half = NumElems / 2;
3361 unsigned SrcStart = Commuted ? NumElems : 0;
3362 for (unsigned i = 0; i != Half; ++i)
3363 if (!isUndefOrInRange(Mask[i], SrcStart, SrcStart+NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003364 return false;
Craig Topper1ff73d72011-12-06 04:59:07 +00003365 SrcStart = Commuted ? 0 : NumElems;
3366 for (unsigned i = Half; i != NumElems; ++i)
3367 if (!isUndefOrInRange(Mask[i], SrcStart, SrcStart+NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003368 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003369
Evan Cheng14aed5e2006-03-24 01:18:28 +00003370 return true;
3371}
3372
Nate Begeman9008ca62009-04-27 18:41:29 +00003373bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
3374 SmallVector<int, 8> M;
3375 N->getMask(M);
3376 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003377}
3378
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003379/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3380/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00003381bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003382 EVT VT = N->getValueType(0);
3383 unsigned NumElems = VT.getVectorNumElements();
3384
3385 if (VT.getSizeInBits() != 128)
3386 return false;
3387
3388 if (NumElems != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003389 return false;
3390
Evan Cheng2064a2b2006-03-28 06:50:32 +00003391 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00003392 return isUndefOrEqual(N->getMaskElt(0), 6) &&
3393 isUndefOrEqual(N->getMaskElt(1), 7) &&
3394 isUndefOrEqual(N->getMaskElt(2), 2) &&
3395 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003396}
3397
Nate Begeman0b10b912009-11-07 23:17:15 +00003398/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3399/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3400/// <2, 3, 2, 3>
3401bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003402 EVT VT = N->getValueType(0);
3403 unsigned NumElems = VT.getVectorNumElements();
3404
3405 if (VT.getSizeInBits() != 128)
3406 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003407
Nate Begeman0b10b912009-11-07 23:17:15 +00003408 if (NumElems != 4)
3409 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003410
Nate Begeman0b10b912009-11-07 23:17:15 +00003411 return isUndefOrEqual(N->getMaskElt(0), 2) &&
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003412 isUndefOrEqual(N->getMaskElt(1), 3) &&
3413 isUndefOrEqual(N->getMaskElt(2), 2) &&
3414 isUndefOrEqual(N->getMaskElt(3), 3);
Nate Begeman0b10b912009-11-07 23:17:15 +00003415}
3416
Evan Cheng5ced1d82006-04-06 23:23:56 +00003417/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3418/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003419bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3420 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003421
Evan Cheng5ced1d82006-04-06 23:23:56 +00003422 if (NumElems != 2 && NumElems != 4)
3423 return false;
3424
Evan Chengc5cdff22006-04-07 21:53:05 +00003425 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003426 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003427 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003428
Evan Chengc5cdff22006-04-07 21:53:05 +00003429 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003430 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003431 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003432
3433 return true;
3434}
3435
Nate Begeman0b10b912009-11-07 23:17:15 +00003436/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3437/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3438bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003439 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003440
David Greenea20244d2011-03-02 17:23:43 +00003441 if ((NumElems != 2 && NumElems != 4)
3442 || N->getValueType(0).getSizeInBits() > 128)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003443 return false;
3444
Evan Chengc5cdff22006-04-07 21:53:05 +00003445 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003446 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003447 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003448
Nate Begeman9008ca62009-04-27 18:41:29 +00003449 for (unsigned i = 0; i < NumElems/2; ++i)
3450 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003451 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003452
3453 return true;
3454}
3455
Evan Cheng0038e592006-03-28 00:39:58 +00003456/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3457/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003458static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Craig Topper6347e862011-11-21 06:57:39 +00003459 bool HasAVX2, bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003460 int NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003461
3462 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3463 "Unsupported vector type for unpckh");
3464
Craig Topper6347e862011-11-21 06:57:39 +00003465 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Craig Topper6fa583d2011-11-21 08:26:50 +00003466 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng0038e592006-03-28 00:39:58 +00003467 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003468
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003469 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3470 // independently on 128-bit lanes.
3471 unsigned NumLanes = VT.getSizeInBits()/128;
3472 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003473
3474 unsigned Start = 0;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003475 unsigned End = NumLaneElts;
3476 for (unsigned s = 0; s < NumLanes; ++s) {
3477 for (unsigned i = Start, j = s * NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003478 i != End;
3479 i += 2, ++j) {
3480 int BitI = Mask[i];
3481 int BitI1 = Mask[i+1];
3482 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003483 return false;
David Greenea20244d2011-03-02 17:23:43 +00003484 if (V2IsSplat) {
3485 if (!isUndefOrEqual(BitI1, NumElts))
3486 return false;
3487 } else {
3488 if (!isUndefOrEqual(BitI1, j + NumElts))
3489 return false;
3490 }
Evan Cheng39623da2006-04-20 08:58:49 +00003491 }
David Greenea20244d2011-03-02 17:23:43 +00003492 // Process the next 128 bits.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003493 Start += NumLaneElts;
3494 End += NumLaneElts;
Evan Cheng0038e592006-03-28 00:39:58 +00003495 }
David Greenea20244d2011-03-02 17:23:43 +00003496
Evan Cheng0038e592006-03-28 00:39:58 +00003497 return true;
3498}
3499
Craig Topper6347e862011-11-21 06:57:39 +00003500bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool HasAVX2, bool V2IsSplat) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003501 SmallVector<int, 8> M;
3502 N->getMask(M);
Craig Topper6347e862011-11-21 06:57:39 +00003503 return ::isUNPCKLMask(M, N->getValueType(0), HasAVX2, V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003504}
3505
Evan Cheng4fcb9222006-03-28 02:43:26 +00003506/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3507/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003508static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Craig Topper6347e862011-11-21 06:57:39 +00003509 bool HasAVX2, bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003510 int NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003511
3512 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3513 "Unsupported vector type for unpckh");
3514
Craig Topper6347e862011-11-21 06:57:39 +00003515 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Craig Topper6fa583d2011-11-21 08:26:50 +00003516 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng4fcb9222006-03-28 02:43:26 +00003517 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003518
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003519 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3520 // independently on 128-bit lanes.
3521 unsigned NumLanes = VT.getSizeInBits()/128;
3522 unsigned NumLaneElts = NumElts/NumLanes;
3523
3524 unsigned Start = 0;
3525 unsigned End = NumLaneElts;
3526 for (unsigned l = 0; l != NumLanes; ++l) {
3527 for (unsigned i = Start, j = (l*NumLaneElts)+NumLaneElts/2;
3528 i != End; i += 2, ++j) {
3529 int BitI = Mask[i];
3530 int BitI1 = Mask[i+1];
3531 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003532 return false;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003533 if (V2IsSplat) {
3534 if (isUndefOrEqual(BitI1, NumElts))
3535 return false;
3536 } else {
3537 if (!isUndefOrEqual(BitI1, j+NumElts))
3538 return false;
3539 }
Evan Cheng39623da2006-04-20 08:58:49 +00003540 }
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003541 // Process the next 128 bits.
3542 Start += NumLaneElts;
3543 End += NumLaneElts;
Evan Cheng4fcb9222006-03-28 02:43:26 +00003544 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003545 return true;
3546}
3547
Craig Topper6347e862011-11-21 06:57:39 +00003548bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool HasAVX2, bool V2IsSplat) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003549 SmallVector<int, 8> M;
3550 N->getMask(M);
Craig Topper6347e862011-11-21 06:57:39 +00003551 return ::isUNPCKHMask(M, N->getValueType(0), HasAVX2, V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003552}
3553
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003554/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3555/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3556/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003557static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003558 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003559 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003560 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003561
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003562 // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern
3563 // FIXME: Need a better way to get rid of this, there's no latency difference
3564 // between UNPCKLPD and MOVDDUP, the later should always be checked first and
3565 // the former later. We should also remove the "_undef" special mask.
3566 if (NumElems == 4 && VT.getSizeInBits() == 256)
3567 return false;
3568
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003569 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3570 // independently on 128-bit lanes.
3571 unsigned NumLanes = VT.getSizeInBits() / 128;
3572 unsigned NumLaneElts = NumElems / NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003573
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003574 for (unsigned s = 0; s < NumLanes; ++s) {
3575 for (unsigned i = s * NumLaneElts, j = s * NumLaneElts;
3576 i != NumLaneElts * (s + 1);
David Greenea20244d2011-03-02 17:23:43 +00003577 i += 2, ++j) {
3578 int BitI = Mask[i];
3579 int BitI1 = Mask[i+1];
3580
3581 if (!isUndefOrEqual(BitI, j))
3582 return false;
3583 if (!isUndefOrEqual(BitI1, j))
3584 return false;
3585 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003586 }
David Greenea20244d2011-03-02 17:23:43 +00003587
Rafael Espindola15684b22009-04-24 12:40:33 +00003588 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003589}
3590
Nate Begeman9008ca62009-04-27 18:41:29 +00003591bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3592 SmallVector<int, 8> M;
3593 N->getMask(M);
3594 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3595}
3596
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003597/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3598/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3599/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003600static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003601 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003602 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3603 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003604
Nate Begeman9008ca62009-04-27 18:41:29 +00003605 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3606 int BitI = Mask[i];
3607 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003608 if (!isUndefOrEqual(BitI, j))
3609 return false;
3610 if (!isUndefOrEqual(BitI1, j))
3611 return false;
3612 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003613 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003614}
3615
Nate Begeman9008ca62009-04-27 18:41:29 +00003616bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3617 SmallVector<int, 8> M;
3618 N->getMask(M);
3619 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3620}
3621
Evan Cheng017dcc62006-04-21 01:05:10 +00003622/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3623/// specifies a shuffle of elements that is suitable for input to MOVSS,
3624/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003625static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003626 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003627 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003628
3629 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003630
Nate Begeman9008ca62009-04-27 18:41:29 +00003631 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003632 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003633
Nate Begeman9008ca62009-04-27 18:41:29 +00003634 for (int i = 1; i < NumElts; ++i)
3635 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003636 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003637
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003638 return true;
3639}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003640
Nate Begeman9008ca62009-04-27 18:41:29 +00003641bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3642 SmallVector<int, 8> M;
3643 N->getMask(M);
3644 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003645}
3646
Craig Topper70b883b2011-11-28 10:14:51 +00003647/// isVPERM2X128Mask - Match 256-bit shuffles where the elements are considered
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003648/// as permutations between 128-bit chunks or halves. As an example: this
3649/// shuffle bellow:
3650/// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
3651/// The first half comes from the second half of V1 and the second half from the
3652/// the second half of V2.
Craig Topper70b883b2011-11-28 10:14:51 +00003653static bool isVPERM2X128Mask(const SmallVectorImpl<int> &Mask, EVT VT,
3654 bool HasAVX) {
3655 if (!HasAVX || VT.getSizeInBits() != 256)
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003656 return false;
3657
3658 // The shuffle result is divided into half A and half B. In total the two
3659 // sources have 4 halves, namely: C, D, E, F. The final values of A and
3660 // B must come from C, D, E or F.
3661 int HalfSize = VT.getVectorNumElements()/2;
3662 bool MatchA = false, MatchB = false;
3663
3664 // Check if A comes from one of C, D, E, F.
3665 for (int Half = 0; Half < 4; ++Half) {
3666 if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
3667 MatchA = true;
3668 break;
3669 }
3670 }
3671
3672 // Check if B comes from one of C, D, E, F.
3673 for (int Half = 0; Half < 4; ++Half) {
3674 if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) {
3675 MatchB = true;
3676 break;
3677 }
3678 }
3679
3680 return MatchA && MatchB;
3681}
3682
Craig Topper70b883b2011-11-28 10:14:51 +00003683/// getShuffleVPERM2X128Immediate - Return the appropriate immediate to shuffle
3684/// the specified VECTOR_MASK mask with VPERM2F128/VPERM2I128 instructions.
3685static unsigned getShuffleVPERM2X128Immediate(SDNode *N) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003686 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3687 EVT VT = SVOp->getValueType(0);
3688
3689 int HalfSize = VT.getVectorNumElements()/2;
3690
3691 int FstHalf = 0, SndHalf = 0;
3692 for (int i = 0; i < HalfSize; ++i) {
3693 if (SVOp->getMaskElt(i) > 0) {
3694 FstHalf = SVOp->getMaskElt(i)/HalfSize;
3695 break;
3696 }
3697 }
3698 for (int i = HalfSize; i < HalfSize*2; ++i) {
3699 if (SVOp->getMaskElt(i) > 0) {
3700 SndHalf = SVOp->getMaskElt(i)/HalfSize;
3701 break;
3702 }
3703 }
3704
3705 return (FstHalf | (SndHalf << 4));
3706}
3707
Craig Topper70b883b2011-11-28 10:14:51 +00003708/// isVPERMILPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003709/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
3710/// Note that VPERMIL mask matching is different depending whether theunderlying
3711/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3712/// to the same elements of the low, but to the higher half of the source.
3713/// In VPERMILPD the two lanes could be shuffled independently of each other
3714/// with the same restriction that lanes can't be crossed.
Craig Topper70b883b2011-11-28 10:14:51 +00003715static bool isVPERMILPMask(const SmallVectorImpl<int> &Mask, EVT VT,
3716 bool HasAVX) {
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003717 int NumElts = VT.getVectorNumElements();
3718 int NumLanes = VT.getSizeInBits()/128;
3719
Craig Topper70b883b2011-11-28 10:14:51 +00003720 if (!HasAVX)
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003721 return false;
3722
Craig Topper70b883b2011-11-28 10:14:51 +00003723 // Only match 256-bit with 32/64-bit types
3724 if (VT.getSizeInBits() != 256 || (NumElts != 4 && NumElts != 8))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003725 return false;
3726
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003727 int LaneSize = NumElts/NumLanes;
Craig Topper70b883b2011-11-28 10:14:51 +00003728 for (int l = 0; l != NumLanes; ++l) {
3729 int LaneStart = l*LaneSize;
3730 for (int i = 0; i != LaneSize; ++i) {
3731 if (!isUndefOrInRange(Mask[i+LaneStart], LaneStart, LaneStart+LaneSize))
3732 return false;
3733 if (NumElts == 4 || l == 0)
3734 continue;
3735 // VPERMILPS handling
3736 if (Mask[i] < 0)
3737 continue;
3738 if (!isUndefOrEqual(Mask[i+LaneStart], Mask[i]+LaneSize))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003739 return false;
3740 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003741 }
3742
3743 return true;
3744}
3745
Craig Topper70b883b2011-11-28 10:14:51 +00003746/// getShuffleVPERMILPImmediate - Return the appropriate immediate to shuffle
3747/// the specified VECTOR_MASK mask with VPERMILPS/D* instructions.
3748static unsigned getShuffleVPERMILPImmediate(SDNode *N) {
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003749 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3750 EVT VT = SVOp->getValueType(0);
3751
3752 int NumElts = VT.getVectorNumElements();
3753 int NumLanes = VT.getSizeInBits()/128;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003754 int LaneSize = NumElts/NumLanes;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003755
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003756 // Although the mask is equal for both lanes do it twice to get the cases
3757 // where a mask will match because the same mask element is undef on the
3758 // first half but valid on the second. This would get pathological cases
3759 // such as: shuffle <u, 0, 1, 2, 4, 4, 5, 6>, which is completely valid.
Craig Topper70b883b2011-11-28 10:14:51 +00003760 unsigned Shift = (LaneSize == 4) ? 2 : 1;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003761 unsigned Mask = 0;
Craig Topper70b883b2011-11-28 10:14:51 +00003762 for (int i = 0; i != NumElts; ++i) {
3763 int MaskElt = SVOp->getMaskElt(i);
3764 if (MaskElt < 0)
3765 continue;
3766 MaskElt %= LaneSize;
3767 unsigned Shamt = i;
3768 // VPERMILPSY, the mask of the first half must be equal to the second one
3769 if (NumElts == 8) Shamt %= LaneSize;
3770 Mask |= MaskElt << (Shamt*Shift);
Bruno Cardoso Lopes377baa52011-07-29 01:31:04 +00003771 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003772
3773 return Mask;
3774}
3775
Evan Cheng017dcc62006-04-21 01:05:10 +00003776/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3777/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003778/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003779static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003780 bool V2IsSplat = false, bool V2IsUndef = false) {
3781 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003782 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003783 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003784
Nate Begeman9008ca62009-04-27 18:41:29 +00003785 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003786 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003787
Nate Begeman9008ca62009-04-27 18:41:29 +00003788 for (int i = 1; i < NumOps; ++i)
3789 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3790 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3791 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003792 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003793
Evan Cheng39623da2006-04-20 08:58:49 +00003794 return true;
3795}
3796
Nate Begeman9008ca62009-04-27 18:41:29 +00003797static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003798 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003799 SmallVector<int, 8> M;
3800 N->getMask(M);
3801 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003802}
3803
Evan Chengd9539472006-04-14 21:59:03 +00003804/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3805/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003806/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
3807bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N,
3808 const X86Subtarget *Subtarget) {
Craig Topperc0d82852011-11-22 00:44:41 +00003809 if (!Subtarget->hasSSE3orAVX())
Evan Chengd9539472006-04-14 21:59:03 +00003810 return false;
3811
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003812 // The second vector must be undef
3813 if (N->getOperand(1).getOpcode() != ISD::UNDEF)
3814 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003815
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003816 EVT VT = N->getValueType(0);
3817 unsigned NumElems = VT.getVectorNumElements();
3818
3819 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3820 (VT.getSizeInBits() == 256 && NumElems != 8))
3821 return false;
3822
3823 // "i+1" is the value the indexed mask element must have
3824 for (unsigned i = 0; i < NumElems; i += 2)
3825 if (!isUndefOrEqual(N->getMaskElt(i), i+1) ||
3826 !isUndefOrEqual(N->getMaskElt(i+1), i+1))
Nate Begeman9008ca62009-04-27 18:41:29 +00003827 return false;
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003828
3829 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003830}
3831
3832/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3833/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003834/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
3835bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N,
3836 const X86Subtarget *Subtarget) {
Craig Topperc0d82852011-11-22 00:44:41 +00003837 if (!Subtarget->hasSSE3orAVX())
Evan Chengd9539472006-04-14 21:59:03 +00003838 return false;
3839
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003840 // The second vector must be undef
3841 if (N->getOperand(1).getOpcode() != ISD::UNDEF)
3842 return false;
3843
3844 EVT VT = N->getValueType(0);
3845 unsigned NumElems = VT.getVectorNumElements();
3846
3847 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3848 (VT.getSizeInBits() == 256 && NumElems != 8))
3849 return false;
3850
3851 // "i" is the value the indexed mask element must have
3852 for (unsigned i = 0; i < NumElems; i += 2)
3853 if (!isUndefOrEqual(N->getMaskElt(i), i) ||
3854 !isUndefOrEqual(N->getMaskElt(i+1), i))
Nate Begeman9008ca62009-04-27 18:41:29 +00003855 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003856
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003857 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003858}
3859
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003860/// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand
3861/// specifies a shuffle of elements that is suitable for input to 256-bit
3862/// version of MOVDDUP.
Craig Topperbeabc6c2011-12-05 06:56:46 +00003863static bool isMOVDDUPYMask(const SmallVectorImpl<int> &Mask, EVT VT,
3864 bool HasAVX) {
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003865 int NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003866
Craig Topperbeabc6c2011-12-05 06:56:46 +00003867 if (!HasAVX || VT.getSizeInBits() != 256 || NumElts != 4)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003868 return false;
3869
3870 for (int i = 0; i != NumElts/2; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003871 if (!isUndefOrEqual(Mask[i], 0))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003872 return false;
3873 for (int i = NumElts/2; i != NumElts; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003874 if (!isUndefOrEqual(Mask[i], NumElts/2))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003875 return false;
3876 return true;
3877}
3878
Evan Cheng0b457f02008-09-25 20:50:48 +00003879/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003880/// specifies a shuffle of elements that is suitable for input to 128-bit
3881/// version of MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003882bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003883 EVT VT = N->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00003884
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003885 if (VT.getSizeInBits() != 128)
3886 return false;
3887
3888 int e = VT.getVectorNumElements() / 2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003889 for (int i = 0; i < e; ++i)
3890 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003891 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003892 for (int i = 0; i < e; ++i)
3893 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003894 return false;
3895 return true;
3896}
3897
David Greenec38a03e2011-02-03 15:50:00 +00003898/// isVEXTRACTF128Index - Return true if the specified
3899/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3900/// suitable for input to VEXTRACTF128.
3901bool X86::isVEXTRACTF128Index(SDNode *N) {
3902 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3903 return false;
3904
3905 // The index should be aligned on a 128-bit boundary.
3906 uint64_t Index =
3907 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3908
3909 unsigned VL = N->getValueType(0).getVectorNumElements();
3910 unsigned VBits = N->getValueType(0).getSizeInBits();
3911 unsigned ElSize = VBits / VL;
3912 bool Result = (Index * ElSize) % 128 == 0;
3913
3914 return Result;
3915}
3916
David Greeneccacdc12011-02-04 16:08:29 +00003917/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
3918/// operand specifies a subvector insert that is suitable for input to
3919/// VINSERTF128.
3920bool X86::isVINSERTF128Index(SDNode *N) {
3921 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3922 return false;
3923
3924 // The index should be aligned on a 128-bit boundary.
3925 uint64_t Index =
3926 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
3927
3928 unsigned VL = N->getValueType(0).getVectorNumElements();
3929 unsigned VBits = N->getValueType(0).getSizeInBits();
3930 unsigned ElSize = VBits / VL;
3931 bool Result = (Index * ElSize) % 128 == 0;
3932
3933 return Result;
3934}
3935
Evan Cheng63d33002006-03-22 08:01:21 +00003936/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003937/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00003938unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003939 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3940 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
3941
Evan Chengb9df0ca2006-03-22 02:53:00 +00003942 unsigned Shift = (NumOperands == 4) ? 2 : 1;
3943 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003944 for (int i = 0; i < NumOperands; ++i) {
3945 int Val = SVOp->getMaskElt(NumOperands-i-1);
3946 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00003947 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00003948 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00003949 if (i != NumOperands - 1)
3950 Mask <<= Shift;
3951 }
Evan Cheng63d33002006-03-22 08:01:21 +00003952 return Mask;
3953}
3954
Evan Cheng506d3df2006-03-29 23:07:14 +00003955/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003956/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003957unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003958 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003959 unsigned Mask = 0;
3960 // 8 nodes, but we only care about the last 4.
3961 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003962 int Val = SVOp->getMaskElt(i);
3963 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003964 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00003965 if (i != 4)
3966 Mask <<= 2;
3967 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003968 return Mask;
3969}
3970
3971/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003972/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003973unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003974 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003975 unsigned Mask = 0;
3976 // 8 nodes, but we only care about the first 4.
3977 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003978 int Val = SVOp->getMaskElt(i);
3979 if (Val >= 0)
3980 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00003981 if (i != 0)
3982 Mask <<= 2;
3983 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003984 return Mask;
3985}
3986
Nate Begemana09008b2009-10-19 02:17:23 +00003987/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3988/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
3989unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
3990 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3991 EVT VVT = N->getValueType(0);
3992 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
3993 int Val = 0;
3994
3995 unsigned i, e;
3996 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
3997 Val = SVOp->getMaskElt(i);
3998 if (Val >= 0)
3999 break;
4000 }
Eli Friedman63f8dde2011-07-25 21:36:45 +00004001 assert(Val - i > 0 && "PALIGNR imm should be positive");
Nate Begemana09008b2009-10-19 02:17:23 +00004002 return (Val - i) * EltSize;
4003}
4004
David Greenec38a03e2011-02-03 15:50:00 +00004005/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
4006/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
4007/// instructions.
4008unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
4009 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4010 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
4011
4012 uint64_t Index =
4013 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4014
4015 EVT VecVT = N->getOperand(0).getValueType();
4016 EVT ElVT = VecVT.getVectorElementType();
4017
4018 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004019 return Index / NumElemsPerChunk;
4020}
4021
David Greeneccacdc12011-02-04 16:08:29 +00004022/// getInsertVINSERTF128Immediate - Return the appropriate immediate
4023/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
4024/// instructions.
4025unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
4026 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4027 llvm_unreachable("Illegal insert subvector for VINSERTF128");
4028
4029 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00004030 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00004031
4032 EVT VecVT = N->getValueType(0);
4033 EVT ElVT = VecVT.getVectorElementType();
4034
4035 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004036 return Index / NumElemsPerChunk;
4037}
4038
Evan Cheng37b73872009-07-30 08:33:02 +00004039/// isZeroNode - Returns true if Elt is a constant zero or a floating point
4040/// constant +0.0.
4041bool X86::isZeroNode(SDValue Elt) {
4042 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00004043 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00004044 (isa<ConstantFPSDNode>(Elt) &&
4045 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
4046}
4047
Nate Begeman9008ca62009-04-27 18:41:29 +00004048/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
4049/// their permute mask.
4050static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
4051 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004052 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004053 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004054 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00004055
Nate Begeman5a5ca152009-04-29 05:20:52 +00004056 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004057 int idx = SVOp->getMaskElt(i);
4058 if (idx < 0)
4059 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004060 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00004061 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004062 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004063 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004064 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004065 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
4066 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004067}
4068
Evan Cheng533a0aa2006-04-19 20:35:22 +00004069/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
4070/// match movhlps. The lower half elements should come from upper half of
4071/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004072/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00004073static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004074 EVT VT = Op->getValueType(0);
4075 if (VT.getSizeInBits() != 128)
4076 return false;
4077 if (VT.getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00004078 return false;
4079 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004080 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004081 return false;
4082 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004083 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004084 return false;
4085 return true;
4086}
4087
Evan Cheng5ced1d82006-04-06 23:23:56 +00004088/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00004089/// is promoted to a vector. It also returns the LoadSDNode by reference if
4090/// required.
4091static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00004092 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
4093 return false;
4094 N = N->getOperand(0).getNode();
4095 if (!ISD::isNON_EXTLoad(N))
4096 return false;
4097 if (LD)
4098 *LD = cast<LoadSDNode>(N);
4099 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004100}
4101
Dan Gohman65fd6562011-11-03 21:49:52 +00004102// Test whether the given value is a vector value which will be legalized
4103// into a load.
4104static bool WillBeConstantPoolLoad(SDNode *N) {
4105 if (N->getOpcode() != ISD::BUILD_VECTOR)
4106 return false;
4107
4108 // Check for any non-constant elements.
4109 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4110 switch (N->getOperand(i).getNode()->getOpcode()) {
4111 case ISD::UNDEF:
4112 case ISD::ConstantFP:
4113 case ISD::Constant:
4114 break;
4115 default:
4116 return false;
4117 }
4118
4119 // Vectors of all-zeros and all-ones are materialized with special
4120 // instructions rather than being loaded.
4121 return !ISD::isBuildVectorAllZeros(N) &&
4122 !ISD::isBuildVectorAllOnes(N);
4123}
4124
Evan Cheng533a0aa2006-04-19 20:35:22 +00004125/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4126/// match movlp{s|d}. The lower half elements should come from lower half of
4127/// V1 (and in order), and the upper half elements should come from the upper
4128/// half of V2 (and in order). And since V1 will become the source of the
4129/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004130static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
4131 ShuffleVectorSDNode *Op) {
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004132 EVT VT = Op->getValueType(0);
4133 if (VT.getSizeInBits() != 128)
4134 return false;
4135
Evan Cheng466685d2006-10-09 20:57:25 +00004136 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004137 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00004138 // Is V2 is a vector load, don't do this transformation. We will try to use
4139 // load folding shufps op.
Dan Gohman65fd6562011-11-03 21:49:52 +00004140 if (ISD::isNON_EXTLoad(V2) || WillBeConstantPoolLoad(V2))
Evan Cheng23425f52006-10-09 21:39:25 +00004141 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004142
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004143 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004144
Evan Cheng533a0aa2006-04-19 20:35:22 +00004145 if (NumElems != 2 && NumElems != 4)
4146 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004147 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004148 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004149 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004150 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004151 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004152 return false;
4153 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004154}
4155
Evan Cheng39623da2006-04-20 08:58:49 +00004156/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4157/// all the same.
4158static bool isSplatVector(SDNode *N) {
4159 if (N->getOpcode() != ISD::BUILD_VECTOR)
4160 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004161
Dan Gohman475871a2008-07-27 21:46:04 +00004162 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00004163 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4164 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00004165 return false;
4166 return true;
4167}
4168
Evan Cheng213d2cf2007-05-17 18:45:50 +00004169/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00004170/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00004171/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00004172static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00004173 SDValue V1 = N->getOperand(0);
4174 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004175 unsigned NumElems = N->getValueType(0).getVectorNumElements();
4176 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004177 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004178 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004179 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00004180 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4181 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004182 if (Opc != ISD::BUILD_VECTOR ||
4183 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00004184 return false;
4185 } else if (Idx >= 0) {
4186 unsigned Opc = V1.getOpcode();
4187 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4188 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004189 if (Opc != ISD::BUILD_VECTOR ||
4190 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00004191 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00004192 }
4193 }
4194 return true;
4195}
4196
4197/// getZeroVector - Returns a vector of specified type with all zero elements.
4198///
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004199static SDValue getZeroVector(EVT VT, bool HasXMMInt, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00004200 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004201 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004202
Dale Johannesen0488fb62010-09-30 23:57:10 +00004203 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004204 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00004205 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00004206 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004207 if (HasXMMInt) { // SSE2
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004208 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4209 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4210 } else { // SSE1
4211 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4212 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4213 }
4214 } else if (VT.getSizeInBits() == 256) { // AVX
4215 // 256-bit logic and arithmetic instructions in AVX are
4216 // all floating-point, no support for integer ops. Default
4217 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00004218 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004219 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4220 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00004221 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004222 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004223}
4224
Chris Lattner8a594482007-11-25 00:24:49 +00004225/// getOnesVector - Returns a vector of specified type with all bits set.
Craig Topper745a86b2011-11-19 22:34:59 +00004226/// Always build ones vectors as <4 x i32> or <8 x i32>. For 256-bit types with
4227/// no AVX2 supprt, use two <4 x i32> inserted in a <8 x i32> appropriately.
4228/// Then bitcast to their original type, ensuring they get CSE'd.
4229static SDValue getOnesVector(EVT VT, bool HasAVX2, SelectionDAG &DAG,
4230 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004231 assert(VT.isVector() && "Expected a vector type");
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004232 assert((VT.is128BitVector() || VT.is256BitVector())
4233 && "Expected a 128-bit or 256-bit vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004234
Owen Anderson825b72b2009-08-11 20:47:22 +00004235 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Craig Topper745a86b2011-11-19 22:34:59 +00004236 SDValue Vec;
4237 if (VT.getSizeInBits() == 256) {
4238 if (HasAVX2) { // AVX2
4239 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4240 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4241 } else { // AVX
4242 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4243 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, MVT::v8i32),
4244 Vec, DAG.getConstant(0, MVT::i32), DAG, dl);
4245 Vec = Insert128BitVector(InsV, Vec,
4246 DAG.getConstant(4 /* NumElems/2 */, MVT::i32), DAG, dl);
4247 }
4248 } else {
4249 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004250 }
4251
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004252 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00004253}
4254
Evan Cheng39623da2006-04-20 08:58:49 +00004255/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4256/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00004257static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004258 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004259 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004260
Evan Cheng39623da2006-04-20 08:58:49 +00004261 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00004262 SmallVector<int, 8> MaskVec;
4263 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00004264
Nate Begeman5a5ca152009-04-29 05:20:52 +00004265 for (unsigned i = 0; i != NumElems; ++i) {
4266 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004267 MaskVec[i] = NumElems;
4268 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00004269 }
Evan Cheng39623da2006-04-20 08:58:49 +00004270 }
Evan Cheng39623da2006-04-20 08:58:49 +00004271 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00004272 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
4273 SVOp->getOperand(1), &MaskVec[0]);
4274 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00004275}
4276
Evan Cheng017dcc62006-04-21 01:05:10 +00004277/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4278/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00004279static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004280 SDValue V2) {
4281 unsigned NumElems = VT.getVectorNumElements();
4282 SmallVector<int, 8> Mask;
4283 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00004284 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004285 Mask.push_back(i);
4286 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00004287}
4288
Nate Begeman9008ca62009-04-27 18:41:29 +00004289/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004290static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004291 SDValue V2) {
4292 unsigned NumElems = VT.getVectorNumElements();
4293 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00004294 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004295 Mask.push_back(i);
4296 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00004297 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004298 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00004299}
4300
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004301/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004302static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004303 SDValue V2) {
4304 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00004305 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00004306 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00004307 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004308 Mask.push_back(i + Half);
4309 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00004310 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004311 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004312}
4313
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004314// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004315// a generic shuffle instruction because the target has no such instructions.
4316// Generate shuffles which repeat i16 and i8 several times until they can be
4317// represented by v4f32 and then be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004318static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004319 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004320 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004321 DebugLoc dl = V.getDebugLoc();
Rafael Espindola15684b22009-04-24 12:40:33 +00004322
Nate Begeman9008ca62009-04-27 18:41:29 +00004323 while (NumElems > 4) {
4324 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004325 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004326 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004327 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004328 EltNo -= NumElems/2;
4329 }
4330 NumElems >>= 1;
4331 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004332 return V;
4333}
Eric Christopherfd179292009-08-27 18:07:15 +00004334
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004335/// getLegalSplat - Generate a legal splat with supported x86 shuffles
4336static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
4337 EVT VT = V.getValueType();
4338 DebugLoc dl = V.getDebugLoc();
4339 assert((VT.getSizeInBits() == 128 || VT.getSizeInBits() == 256)
4340 && "Vector size not supported");
4341
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004342 if (VT.getSizeInBits() == 128) {
4343 V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004344 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004345 V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
4346 &SplatMask[0]);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004347 } else {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004348 // To use VPERMILPS to splat scalars, the second half of indicies must
4349 // refer to the higher part, which is a duplication of the lower one,
4350 // because VPERMILPS can only handle in-lane permutations.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004351 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
4352 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004353
4354 V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
4355 V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
4356 &SplatMask[0]);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004357 }
4358
4359 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4360}
4361
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004362/// PromoteSplat - Splat is promoted to target supported vector shuffles.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004363static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4364 EVT SrcVT = SV->getValueType(0);
4365 SDValue V1 = SV->getOperand(0);
4366 DebugLoc dl = SV->getDebugLoc();
4367
4368 int EltNo = SV->getSplatIndex();
4369 int NumElems = SrcVT.getVectorNumElements();
4370 unsigned Size = SrcVT.getSizeInBits();
4371
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004372 assert(((Size == 128 && NumElems > 4) || Size == 256) &&
4373 "Unknown how to promote splat for type");
4374
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004375 // Extract the 128-bit part containing the splat element and update
4376 // the splat element index when it refers to the higher register.
4377 if (Size == 256) {
4378 unsigned Idx = (EltNo > NumElems/2) ? NumElems/2 : 0;
4379 V1 = Extract128BitVector(V1, DAG.getConstant(Idx, MVT::i32), DAG, dl);
4380 if (Idx > 0)
4381 EltNo -= NumElems/2;
4382 }
4383
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004384 // All i16 and i8 vector types can't be used directly by a generic shuffle
4385 // instruction because the target has no such instruction. Generate shuffles
4386 // which repeat i16 and i8 several times until they fit in i32, and then can
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004387 // be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004388 EVT EltVT = SrcVT.getVectorElementType();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004389 if (EltVT == MVT::i8 || EltVT == MVT::i16)
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004390 V1 = PromoteSplati8i16(V1, DAG, EltNo);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004391
4392 // Recreate the 256-bit vector and place the same 128-bit vector
4393 // into the low and high part. This is necessary because we want
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004394 // to use VPERM* to shuffle the vectors
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004395 if (Size == 256) {
4396 SDValue InsV = Insert128BitVector(DAG.getUNDEF(SrcVT), V1,
4397 DAG.getConstant(0, MVT::i32), DAG, dl);
4398 V1 = Insert128BitVector(InsV, V1,
4399 DAG.getConstant(NumElems/2, MVT::i32), DAG, dl);
4400 }
4401
4402 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004403}
4404
Evan Chengba05f722006-04-21 23:03:30 +00004405/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004406/// vector of zero or undef vector. This produces a shuffle where the low
4407/// element of V2 is swizzled into the zero/undef vector, landing at element
4408/// 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 +00004409static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004410 bool isZero, bool HasXMMInt,
4411 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004412 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00004413 SDValue V1 = isZero
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004414 ? getZeroVector(VT, HasXMMInt, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00004415 unsigned NumElems = VT.getVectorNumElements();
4416 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004417 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004418 // If this is the insertion idx, put the low elt of V2 here.
4419 MaskVec.push_back(i == Idx ? NumElems : i);
4420 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004421}
4422
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004423/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4424/// element of the result of the vector shuffle.
Benjamin Kramer050db522011-03-26 12:38:19 +00004425static SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
4426 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004427 if (Depth == 6)
4428 return SDValue(); // Limit search depth.
4429
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004430 SDValue V = SDValue(N, 0);
4431 EVT VT = V.getValueType();
4432 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004433
4434 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4435 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
4436 Index = SV->getMaskElt(Index);
4437
4438 if (Index < 0)
4439 return DAG.getUNDEF(VT.getVectorElementType());
4440
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004441 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004442 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004443 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004444 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004445
4446 // Recurse into target specific vector shuffles to find scalars.
4447 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004448 int NumElems = VT.getVectorNumElements();
4449 SmallVector<unsigned, 16> ShuffleMask;
4450 SDValue ImmN;
4451
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004452 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004453 case X86ISD::SHUFPS:
4454 case X86ISD::SHUFPD:
4455 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Topper36e36ac2011-11-29 07:49:05 +00004456 DecodeSHUFPMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4457 ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004458 break;
Craig Topper34671b82011-12-06 08:21:25 +00004459 case X86ISD::UNPCKH:
Craig Topper3d8c2ce2011-12-06 05:31:16 +00004460 DecodeUNPCKHMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004461 break;
Craig Topper34671b82011-12-06 08:21:25 +00004462 case X86ISD::UNPCKL:
Craig Topper3d8c2ce2011-12-06 05:31:16 +00004463 DecodeUNPCKLMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004464 break;
4465 case X86ISD::MOVHLPS:
4466 DecodeMOVHLPSMask(NumElems, ShuffleMask);
4467 break;
4468 case X86ISD::MOVLHPS:
4469 DecodeMOVLHPSMask(NumElems, ShuffleMask);
4470 break;
4471 case X86ISD::PSHUFD:
4472 ImmN = N->getOperand(N->getNumOperands()-1);
4473 DecodePSHUFMask(NumElems,
4474 cast<ConstantSDNode>(ImmN)->getZExtValue(),
4475 ShuffleMask);
4476 break;
4477 case X86ISD::PSHUFHW:
4478 ImmN = N->getOperand(N->getNumOperands()-1);
4479 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4480 ShuffleMask);
4481 break;
4482 case X86ISD::PSHUFLW:
4483 ImmN = N->getOperand(N->getNumOperands()-1);
4484 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4485 ShuffleMask);
4486 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004487 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004488 case X86ISD::MOVSD: {
4489 // The index 0 always comes from the first element of the second source,
4490 // this is why MOVSS and MOVSD are used in the first place. The other
4491 // elements come from the other positions of the first source vector.
4492 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004493 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
4494 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004495 }
Craig Topper316cd2a2011-11-30 06:25:25 +00004496 case X86ISD::VPERMILP:
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00004497 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Topper316cd2a2011-11-30 06:25:25 +00004498 DecodeVPERMILPMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(),
Bruno Cardoso Lopes2eb4c2b2011-07-29 01:31:11 +00004499 ShuffleMask);
4500 break;
Craig Topperec24e612011-11-30 07:47:51 +00004501 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00004502 ImmN = N->getOperand(N->getNumOperands()-1);
4503 DecodeVPERM2F128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4504 ShuffleMask);
4505 break;
Eli Friedman106f6e72011-09-10 02:01:42 +00004506 case X86ISD::MOVDDUP:
4507 case X86ISD::MOVLHPD:
4508 case X86ISD::MOVLPD:
4509 case X86ISD::MOVLPS:
4510 case X86ISD::MOVSHDUP:
4511 case X86ISD::MOVSLDUP:
4512 case X86ISD::PALIGN:
4513 return SDValue(); // Not yet implemented.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004514 default:
Eli Friedman106f6e72011-09-10 02:01:42 +00004515 assert(0 && "unknown target shuffle node");
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004516 return SDValue();
4517 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004518
4519 Index = ShuffleMask[Index];
4520 if (Index < 0)
4521 return DAG.getUNDEF(VT.getVectorElementType());
4522
4523 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
4524 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
4525 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004526 }
4527
4528 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004529 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004530 V = V.getOperand(0);
4531 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004532 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004533
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004534 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004535 return SDValue();
4536 }
4537
4538 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4539 return (Index == 0) ? V.getOperand(0)
4540 : DAG.getUNDEF(VT.getVectorElementType());
4541
4542 if (V.getOpcode() == ISD::BUILD_VECTOR)
4543 return V.getOperand(Index);
4544
4545 return SDValue();
4546}
4547
4548/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4549/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004550/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004551static
4552unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
4553 bool ZerosFromLeft, SelectionDAG &DAG) {
4554 int i = 0;
4555
4556 while (i < NumElems) {
4557 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004558 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004559 if (!(Elt.getNode() &&
4560 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4561 break;
4562 ++i;
4563 }
4564
4565 return i;
4566}
4567
4568/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
4569/// MaskE correspond consecutively to elements from one of the vector operands,
4570/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4571static
4572bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
4573 int OpIdx, int NumElems, unsigned &OpNum) {
4574 bool SeenV1 = false;
4575 bool SeenV2 = false;
4576
4577 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
4578 int Idx = SVOp->getMaskElt(i);
4579 // Ignore undef indicies
4580 if (Idx < 0)
4581 continue;
4582
4583 if (Idx < NumElems)
4584 SeenV1 = true;
4585 else
4586 SeenV2 = true;
4587
4588 // Only accept consecutive elements from the same vector
4589 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4590 return false;
4591 }
4592
4593 OpNum = SeenV1 ? 0 : 1;
4594 return true;
4595}
4596
4597/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4598/// logical left shift of a vector.
4599static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4600 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4601 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4602 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4603 false /* check zeros from right */, DAG);
4604 unsigned OpSrc;
4605
4606 if (!NumZeros)
4607 return false;
4608
4609 // Considering the elements in the mask that are not consecutive zeros,
4610 // check if they consecutively come from only one of the source vectors.
4611 //
4612 // V1 = {X, A, B, C} 0
4613 // \ \ \ /
4614 // vector_shuffle V1, V2 <1, 2, 3, X>
4615 //
4616 if (!isShuffleMaskConsecutive(SVOp,
4617 0, // Mask Start Index
4618 NumElems-NumZeros-1, // Mask End Index
4619 NumZeros, // Where to start looking in the src vector
4620 NumElems, // Number of elements in vector
4621 OpSrc)) // Which source operand ?
4622 return false;
4623
4624 isLeft = false;
4625 ShAmt = NumZeros;
4626 ShVal = SVOp->getOperand(OpSrc);
4627 return true;
4628}
4629
4630/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4631/// logical left shift of a vector.
4632static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4633 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4634 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4635 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4636 true /* check zeros from left */, DAG);
4637 unsigned OpSrc;
4638
4639 if (!NumZeros)
4640 return false;
4641
4642 // Considering the elements in the mask that are not consecutive zeros,
4643 // check if they consecutively come from only one of the source vectors.
4644 //
4645 // 0 { A, B, X, X } = V2
4646 // / \ / /
4647 // vector_shuffle V1, V2 <X, X, 4, 5>
4648 //
4649 if (!isShuffleMaskConsecutive(SVOp,
4650 NumZeros, // Mask Start Index
4651 NumElems-1, // Mask End Index
4652 0, // Where to start looking in the src vector
4653 NumElems, // Number of elements in vector
4654 OpSrc)) // Which source operand ?
4655 return false;
4656
4657 isLeft = true;
4658 ShAmt = NumZeros;
4659 ShVal = SVOp->getOperand(OpSrc);
4660 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004661}
4662
4663/// isVectorShift - Returns true if the shuffle can be implemented as a
4664/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004665static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004666 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004667 // Although the logic below support any bitwidth size, there are no
4668 // shift instructions which handle more than 128-bit vectors.
4669 if (SVOp->getValueType(0).getSizeInBits() > 128)
4670 return false;
4671
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004672 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4673 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4674 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004675
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004676 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004677}
4678
Evan Chengc78d3b42006-04-24 18:01:45 +00004679/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4680///
Dan Gohman475871a2008-07-27 21:46:04 +00004681static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004682 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004683 SelectionDAG &DAG,
4684 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004685 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004686 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004687
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004688 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004689 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004690 bool First = true;
4691 for (unsigned i = 0; i < 16; ++i) {
4692 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4693 if (ThisIsNonZero && First) {
4694 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004695 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004696 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004697 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004698 First = false;
4699 }
4700
4701 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004702 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004703 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4704 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004705 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004706 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004707 }
4708 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004709 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4710 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4711 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004712 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004713 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004714 } else
4715 ThisElt = LastElt;
4716
Gabor Greifba36cb52008-08-28 21:40:38 +00004717 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004718 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004719 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004720 }
4721 }
4722
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004723 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004724}
4725
Bill Wendlinga348c562007-03-22 18:42:45 +00004726/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004727///
Dan Gohman475871a2008-07-27 21:46:04 +00004728static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004729 unsigned NumNonZero, unsigned NumZero,
4730 SelectionDAG &DAG,
4731 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004732 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004733 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004734
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004735 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004736 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004737 bool First = true;
4738 for (unsigned i = 0; i < 8; ++i) {
4739 bool isNonZero = (NonZeros & (1 << i)) != 0;
4740 if (isNonZero) {
4741 if (First) {
4742 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004743 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004744 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004745 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004746 First = false;
4747 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004748 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004749 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004750 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004751 }
4752 }
4753
4754 return V;
4755}
4756
Evan Chengf26ffe92008-05-29 08:22:04 +00004757/// getVShift - Return a vector logical shift node.
4758///
Owen Andersone50ed302009-08-10 22:56:29 +00004759static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004760 unsigned NumBits, SelectionDAG &DAG,
4761 const TargetLowering &TLI, DebugLoc dl) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004762 assert(VT.getSizeInBits() == 128 && "Unknown type for VShift");
Dale Johannesen0488fb62010-09-30 23:57:10 +00004763 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00004764 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004765 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4766 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004767 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004768 DAG.getConstant(NumBits,
4769 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004770}
4771
Dan Gohman475871a2008-07-27 21:46:04 +00004772SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004773X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004774 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004775
Evan Chengc3630942009-12-09 21:00:30 +00004776 // Check if the scalar load can be widened into a vector load. And if
4777 // the address is "base + cst" see if the cst can be "absorbed" into
4778 // the shuffle mask.
4779 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4780 SDValue Ptr = LD->getBasePtr();
4781 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4782 return SDValue();
4783 EVT PVT = LD->getValueType(0);
4784 if (PVT != MVT::i32 && PVT != MVT::f32)
4785 return SDValue();
4786
4787 int FI = -1;
4788 int64_t Offset = 0;
4789 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4790 FI = FINode->getIndex();
4791 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004792 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004793 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4794 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4795 Offset = Ptr.getConstantOperandVal(1);
4796 Ptr = Ptr.getOperand(0);
4797 } else {
4798 return SDValue();
4799 }
4800
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004801 // FIXME: 256-bit vector instructions don't require a strict alignment,
4802 // improve this code to support it better.
4803 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00004804 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004805 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00004806 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004807 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00004808 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004809 // Can't change the alignment. FIXME: It's possible to compute
4810 // the exact stack offset and reference FI + adjust offset instead.
4811 // If someone *really* cares about this. That's the way to implement it.
4812 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004813 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004814 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00004815 }
4816 }
4817
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004818 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00004819 // Ptr + (Offset & ~15).
4820 if (Offset < 0)
4821 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004822 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00004823 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004824 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00004825 if (StartOffset)
4826 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4827 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4828
4829 int EltNo = (Offset - StartOffset) >> 2;
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004830 int NumElems = VT.getVectorNumElements();
4831
4832 EVT CanonVT = VT.getSizeInBits() == 128 ? MVT::v4i32 : MVT::v8i32;
4833 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
4834 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00004835 LD->getPointerInfo().getWithOffset(StartOffset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004836 false, false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004837
4838 // Canonicalize it to a v4i32 or v8i32 shuffle.
4839 SmallVector<int, 8> Mask;
4840 for (int i = 0; i < NumElems; ++i)
4841 Mask.push_back(EltNo);
4842
4843 V1 = DAG.getNode(ISD::BITCAST, dl, CanonVT, V1);
4844 return DAG.getNode(ISD::BITCAST, dl, NVT,
4845 DAG.getVectorShuffle(CanonVT, dl, V1,
4846 DAG.getUNDEF(CanonVT),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00004847 }
4848
4849 return SDValue();
4850}
4851
Michael J. Spencerec38de22010-10-10 22:04:20 +00004852/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4853/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004854/// load which has the same value as a build_vector whose operands are 'elts'.
4855///
4856/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004857///
Nate Begeman1449f292010-03-24 22:19:06 +00004858/// FIXME: we'd also like to handle the case where the last elements are zero
4859/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4860/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004861static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004862 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004863 EVT EltVT = VT.getVectorElementType();
4864 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004865
Nate Begemanfdea31a2010-03-24 20:49:50 +00004866 LoadSDNode *LDBase = NULL;
4867 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004868
Nate Begeman1449f292010-03-24 22:19:06 +00004869 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004870 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004871 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004872 for (unsigned i = 0; i < NumElems; ++i) {
4873 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004874
Nate Begemanfdea31a2010-03-24 20:49:50 +00004875 if (!Elt.getNode() ||
4876 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4877 return SDValue();
4878 if (!LDBase) {
4879 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4880 return SDValue();
4881 LDBase = cast<LoadSDNode>(Elt.getNode());
4882 LastLoadedElt = i;
4883 continue;
4884 }
4885 if (Elt.getOpcode() == ISD::UNDEF)
4886 continue;
4887
4888 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4889 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4890 return SDValue();
4891 LastLoadedElt = i;
4892 }
Nate Begeman1449f292010-03-24 22:19:06 +00004893
4894 // If we have found an entire vector of loads and undefs, then return a large
4895 // load of the entire vector width starting at the base pointer. If we found
4896 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004897 if (LastLoadedElt == NumElems - 1) {
4898 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00004899 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004900 LDBase->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004901 LDBase->isVolatile(), LDBase->isNonTemporal(),
4902 LDBase->isInvariant(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00004903 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004904 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004905 LDBase->isVolatile(), LDBase->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004906 LDBase->isInvariant(), LDBase->getAlignment());
Eli Friedman61cc47e2011-07-26 21:02:58 +00004907 } else if (NumElems == 4 && LastLoadedElt == 1 &&
4908 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004909 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
4910 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Eli Friedman322ea082011-09-14 23:42:45 +00004911 SDValue ResNode =
4912 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys, Ops, 2, MVT::i64,
4913 LDBase->getPointerInfo(),
4914 LDBase->getAlignment(),
4915 false/*isVolatile*/, true/*ReadMem*/,
4916 false/*WriteMem*/);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004917 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00004918 }
4919 return SDValue();
4920}
4921
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004922/// isVectorBroadcast - Check if the node chain is suitable to be xformed to
4923/// a vbroadcast node. We support two patterns:
4924/// 1. A splat BUILD_VECTOR which uses a single scalar load.
4925/// 2. A splat shuffle which uses a scalar_to_vector node which comes from
4926/// a scalar load.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004927/// The scalar load node is returned when a pattern is found,
4928/// or SDValue() otherwise.
4929static SDValue isVectorBroadcast(SDValue &Op, bool hasAVX2) {
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004930 EVT VT = Op.getValueType();
4931 SDValue V = Op;
4932
4933 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
4934 V = V.getOperand(0);
4935
4936 //A suspected load to be broadcasted.
4937 SDValue Ld;
4938
4939 switch (V.getOpcode()) {
4940 default:
4941 // Unknown pattern found.
4942 return SDValue();
4943
4944 case ISD::BUILD_VECTOR: {
4945 // The BUILD_VECTOR node must be a splat.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004946 if (!isSplatVector(V.getNode()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004947 return SDValue();
4948
4949 Ld = V.getOperand(0);
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004950
4951 // The suspected load node has several users. Make sure that all
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004952 // of its users are from the BUILD_VECTOR node.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004953 if (!Ld->hasNUsesOfValue(VT.getVectorNumElements(), 0))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004954 return SDValue();
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004955 break;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004956 }
4957
4958 case ISD::VECTOR_SHUFFLE: {
4959 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
4960
4961 // Shuffles must have a splat mask where the first element is
4962 // broadcasted.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004963 if ((!SVOp->isSplat()) || SVOp->getMaskElt(0) != 0)
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004964 return SDValue();
4965
4966 SDValue Sc = Op.getOperand(0);
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004967 if (Sc.getOpcode() != ISD::SCALAR_TO_VECTOR)
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004968 return SDValue();
4969
4970 Ld = Sc.getOperand(0);
4971
4972 // The scalar_to_vector node and the suspected
4973 // load node must have exactly one user.
4974 if (!Sc.hasOneUse() || !Ld.hasOneUse())
4975 return SDValue();
4976 break;
4977 }
4978 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004979
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004980 // The scalar source must be a normal load.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004981 if (!ISD::isNormalLoad(Ld.getNode()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004982 return SDValue();
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004983
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004984 bool Is256 = VT.getSizeInBits() == 256;
4985 bool Is128 = VT.getSizeInBits() == 128;
4986 unsigned ScalarSize = Ld.getValueType().getSizeInBits();
4987
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004988 if (hasAVX2) {
4989 // VBroadcast to YMM
4990 if (Is256 && (ScalarSize == 8 || ScalarSize == 16 ||
4991 ScalarSize == 32 || ScalarSize == 64 ))
4992 return Ld;
4993
4994 // VBroadcast to XMM
4995 if (Is128 && (ScalarSize == 8 || ScalarSize == 32 ||
4996 ScalarSize == 16 || ScalarSize == 64 ))
4997 return Ld;
4998 }
4999
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005000 // VBroadcast to YMM
5001 if (Is256 && (ScalarSize == 32 || ScalarSize == 64))
5002 return Ld;
5003
5004 // VBroadcast to XMM
5005 if (Is128 && (ScalarSize == 32))
5006 return Ld;
5007
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005008
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005009 // Unsupported broadcast.
5010 return SDValue();
5011}
5012
Evan Chengc3630942009-12-09 21:00:30 +00005013SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005014X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005015 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00005016
David Greenef125a292011-02-08 19:04:41 +00005017 EVT VT = Op.getValueType();
5018 EVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00005019 unsigned NumElems = Op.getNumOperands();
5020
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005021 // Vectors containing all zeros can be matched by pxor and xorps later
5022 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5023 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
5024 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00005025 if (Op.getValueType() == MVT::v4i32 ||
5026 Op.getValueType() == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00005027 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005028
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005029 return getZeroVector(Op.getValueType(), Subtarget->hasXMMInt(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00005030 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005031
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005032 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
Craig Topper745a86b2011-11-19 22:34:59 +00005033 // vectors or broken into v4i32 operations on 256-bit vectors. AVX2 can use
5034 // vpcmpeqd on 256-bit vectors.
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005035 if (ISD::isBuildVectorAllOnes(Op.getNode())) {
Craig Topper745a86b2011-11-19 22:34:59 +00005036 if (Op.getValueType() == MVT::v4i32 ||
5037 (Op.getValueType() == MVT::v8i32 && Subtarget->hasAVX2()))
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005038 return Op;
5039
Craig Topper745a86b2011-11-19 22:34:59 +00005040 return getOnesVector(Op.getValueType(), Subtarget->hasAVX2(), DAG, dl);
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005041 }
5042
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005043 SDValue LD = isVectorBroadcast(Op, Subtarget->hasAVX2());
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005044 if (Subtarget->hasAVX() && LD.getNode())
5045 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, LD);
5046
Owen Andersone50ed302009-08-10 22:56:29 +00005047 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005048
Evan Cheng0db9fe62006-04-25 20:13:52 +00005049 unsigned NumZero = 0;
5050 unsigned NumNonZero = 0;
5051 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005052 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005053 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005054 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005055 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00005056 if (Elt.getOpcode() == ISD::UNDEF)
5057 continue;
5058 Values.insert(Elt);
5059 if (Elt.getOpcode() != ISD::Constant &&
5060 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005061 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00005062 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00005063 NumZero++;
5064 else {
5065 NonZeros |= (1 << i);
5066 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005067 }
5068 }
5069
Chris Lattner97a2a562010-08-26 05:24:29 +00005070 // All undef vector. Return an UNDEF. All zero vectors were handled above.
5071 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00005072 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005073
Chris Lattner67f453a2008-03-09 05:42:06 +00005074 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00005075 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005076 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00005077 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005078
Chris Lattner62098042008-03-09 01:05:04 +00005079 // If this is an insertion of an i64 value on x86-32, and if the top bits of
5080 // the value are obviously zero, truncate the value to i32 and do the
5081 // insertion that way. Only do this if the value is non-constant or if the
5082 // value is a constant being inserted into element 0. It is cheaper to do
5083 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00005084 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00005085 (!IsAllConstants || Idx == 0)) {
5086 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00005087 // Handle SSE only.
5088 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
5089 EVT VecVT = MVT::v4i32;
5090 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00005091
Chris Lattner62098042008-03-09 01:05:04 +00005092 // Truncate the value (which may itself be a constant) to i32, and
5093 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00005094 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00005095 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00005096 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005097 Subtarget->hasXMMInt(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005098
Chris Lattner62098042008-03-09 01:05:04 +00005099 // Now we have our 32-bit value zero extended in the low element of
5100 // a vector. If Idx != 0, swizzle it into place.
5101 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005102 SmallVector<int, 4> Mask;
5103 Mask.push_back(Idx);
5104 for (unsigned i = 1; i != VecElts; ++i)
5105 Mask.push_back(i);
5106 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00005107 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00005108 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00005109 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005110 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00005111 }
5112 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005113
Chris Lattner19f79692008-03-08 22:59:52 +00005114 // If we have a constant or non-constant insertion into the low element of
5115 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5116 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00005117 // depending on what the source datatype is.
5118 if (Idx == 0) {
5119 if (NumZero == 0) {
5120 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00005121 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
5122 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00005123 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5124 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005125 return getShuffleVectorZeroOrUndef(Item, 0, true,Subtarget->hasXMMInt(),
Eli Friedman10415532009-06-06 06:05:10 +00005126 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00005127 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
5128 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005129 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
5130 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00005131 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
5132 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005133 Subtarget->hasXMMInt(), DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005134 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00005135 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005136 }
Evan Chengf26ffe92008-05-29 08:22:04 +00005137
5138 // Is it a vector logical left shift?
5139 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00005140 X86::isZeroNode(Op.getOperand(0)) &&
5141 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005142 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00005143 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00005144 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005145 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00005146 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005147 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005148
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005149 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00005150 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005151
Chris Lattner19f79692008-03-08 22:59:52 +00005152 // Otherwise, if this is a vector with i32 or f32 elements, and the element
5153 // is a non-constant being inserted into an element other than the low one,
5154 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
5155 // movd/movss) to move this into the low element, then shuffle it into
5156 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005157 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00005158 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00005159
Evan Cheng0db9fe62006-04-25 20:13:52 +00005160 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00005161 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005162 Subtarget->hasXMMInt(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00005163 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005164 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00005165 MaskVec.push_back(i == Idx ? 0 : 1);
5166 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005167 }
5168 }
5169
Chris Lattner67f453a2008-03-09 05:42:06 +00005170 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00005171 if (Values.size() == 1) {
5172 if (EVTBits == 32) {
5173 // Instead of a shuffle like this:
5174 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5175 // Check if it's possible to issue this instead.
5176 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
5177 unsigned Idx = CountTrailingZeros_32(NonZeros);
5178 SDValue Item = Op.getOperand(Idx);
5179 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5180 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5181 }
Dan Gohman475871a2008-07-27 21:46:04 +00005182 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005183 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005184
Dan Gohmana3941172007-07-24 22:55:08 +00005185 // A vector full of immediates; various special cases are already
5186 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005187 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00005188 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00005189
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005190 // For AVX-length vectors, build the individual 128-bit pieces and use
5191 // shuffles to put them in place.
5192 if (VT.getSizeInBits() == 256 && !ISD::isBuildVectorAllZeros(Op.getNode())) {
5193 SmallVector<SDValue, 32> V;
5194 for (unsigned i = 0; i < NumElems; ++i)
5195 V.push_back(Op.getOperand(i));
5196
5197 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
5198
5199 // Build both the lower and upper subvector.
5200 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
5201 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
5202 NumElems/2);
5203
5204 // Recreate the wider vector with the lower and upper part.
Bruno Cardoso Lopes15d03fb2011-07-28 01:26:53 +00005205 SDValue Vec = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), Lower,
5206 DAG.getConstant(0, MVT::i32), DAG, dl);
5207 return Insert128BitVector(Vec, Upper, DAG.getConstant(NumElems/2, MVT::i32),
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005208 DAG, dl);
5209 }
5210
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005211 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005212 if (EVTBits == 64) {
5213 if (NumNonZero == 1) {
5214 // One half is zero or undef.
5215 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005216 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005217 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00005218 return getShuffleVectorZeroOrUndef(V2, Idx, true,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005219 Subtarget->hasXMMInt(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005220 }
Dan Gohman475871a2008-07-27 21:46:04 +00005221 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005222 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005223
5224 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005225 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005226 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00005227 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005228 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005229 }
5230
Bill Wendling826f36f2007-03-28 00:57:11 +00005231 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005232 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24: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
5237 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00005238 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00005239 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005240 if (NumElems == 4 && NumZero > 0) {
5241 for (unsigned i = 0; i < 4; ++i) {
5242 bool isZero = !(NonZeros & (1 << i));
5243 if (isZero)
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005244 V[i] = getZeroVector(VT, Subtarget->hasXMMInt(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005245 else
Dale Johannesenace16102009-02-03 19:33:06 +00005246 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005247 }
5248
5249 for (unsigned i = 0; i < 2; ++i) {
5250 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5251 default: break;
5252 case 0:
5253 V[i] = V[i*2]; // Must be a zero vector.
5254 break;
5255 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005256 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005257 break;
5258 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005259 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005260 break;
5261 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005262 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005263 break;
5264 }
5265 }
5266
Nate Begeman9008ca62009-04-27 18:41:29 +00005267 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005268 bool Reverse = (NonZeros & 0x3) == 2;
5269 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005270 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005271 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5272 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005273 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
5274 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005275 }
5276
Nate Begemanfdea31a2010-03-24 20:49:50 +00005277 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
5278 // Check for a build vector of consecutive loads.
5279 for (unsigned i = 0; i < NumElems; ++i)
5280 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005281
Nate Begemanfdea31a2010-03-24 20:49:50 +00005282 // Check for elements which are consecutive loads.
5283 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5284 if (LD.getNode())
5285 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005286
5287 // For SSE 4.1, use insertps to put the high elements into the low element.
Craig Topperc0d82852011-11-22 00:44:41 +00005288 if (getSubtarget()->hasSSE41orAVX()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005289 SDValue Result;
5290 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5291 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5292 else
5293 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005294
Chris Lattner24faf612010-08-28 17:59:08 +00005295 for (unsigned i = 1; i < NumElems; ++i) {
5296 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5297 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005298 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005299 }
5300 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005301 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005302
Chris Lattner6e80e442010-08-28 17:15:43 +00005303 // Otherwise, expand into a number of unpckl*, start by extending each of
5304 // our (non-undef) elements to the full vector width with the element in the
5305 // bottom slot of the vector (which generates no code for SSE).
5306 for (unsigned i = 0; i < NumElems; ++i) {
5307 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5308 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5309 else
5310 V[i] = DAG.getUNDEF(VT);
5311 }
5312
5313 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005314 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5315 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5316 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005317 unsigned EltStride = NumElems >> 1;
5318 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005319 for (unsigned i = 0; i < EltStride; ++i) {
5320 // If V[i+EltStride] is undef and this is the first round of mixing,
5321 // then it is safe to just drop this shuffle: V[i] is already in the
5322 // right place, the one element (since it's the first round) being
5323 // inserted as undef can be dropped. This isn't safe for successive
5324 // rounds because they will permute elements within both vectors.
5325 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5326 EltStride == NumElems/2)
5327 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005328
Chris Lattner6e80e442010-08-28 17:15:43 +00005329 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005330 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005331 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005332 }
5333 return V[0];
5334 }
Dan Gohman475871a2008-07-27 21:46:04 +00005335 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005336}
5337
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005338// LowerMMXCONCAT_VECTORS - We support concatenate two MMX registers and place
5339// them in a MMX register. This is better than doing a stack convert.
5340static SDValue LowerMMXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005341 DebugLoc dl = Op.getDebugLoc();
5342 EVT ResVT = Op.getValueType();
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005343
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005344 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
5345 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
5346 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005347 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005348 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
5349 InVec = Op.getOperand(1);
5350 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5351 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005352 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005353 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
5354 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
5355 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005356 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005357 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
5358 Mask[0] = 0; Mask[1] = 2;
5359 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
5360 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005361 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005362}
5363
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005364// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5365// to create 256-bit vectors from two other 128-bit ones.
5366static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5367 DebugLoc dl = Op.getDebugLoc();
5368 EVT ResVT = Op.getValueType();
5369
5370 assert(ResVT.getSizeInBits() == 256 && "Value type must be 256-bit wide");
5371
5372 SDValue V1 = Op.getOperand(0);
5373 SDValue V2 = Op.getOperand(1);
5374 unsigned NumElems = ResVT.getVectorNumElements();
5375
5376 SDValue V = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, ResVT), V1,
5377 DAG.getConstant(0, MVT::i32), DAG, dl);
5378 return Insert128BitVector(V, V2, DAG.getConstant(NumElems/2, MVT::i32),
5379 DAG, dl);
5380}
5381
5382SDValue
5383X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005384 EVT ResVT = Op.getValueType();
5385
5386 assert(Op.getNumOperands() == 2);
5387 assert((ResVT.getSizeInBits() == 128 || ResVT.getSizeInBits() == 256) &&
5388 "Unsupported CONCAT_VECTORS for value type");
5389
5390 // We support concatenate two MMX registers and place them in a MMX register.
5391 // This is better than doing a stack convert.
5392 if (ResVT.is128BitVector())
5393 return LowerMMXCONCAT_VECTORS(Op, DAG);
5394
5395 // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
5396 // from two other 128-bit ones.
5397 return LowerAVXCONCAT_VECTORS(Op, DAG);
5398}
5399
Nate Begemanb9a47b82009-02-23 08:49:38 +00005400// v8i16 shuffles - Prefer shuffles in the following order:
5401// 1. [all] pshuflw, pshufhw, optional move
5402// 2. [ssse3] 1 x pshufb
5403// 3. [ssse3] 2 x pshufb + 1 x por
5404// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005405SDValue
5406X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
5407 SelectionDAG &DAG) const {
5408 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00005409 SDValue V1 = SVOp->getOperand(0);
5410 SDValue V2 = SVOp->getOperand(1);
5411 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005412 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00005413
Nate Begemanb9a47b82009-02-23 08:49:38 +00005414 // Determine if more than 1 of the words in each of the low and high quadwords
5415 // of the result come from the same quadword of one of the two inputs. Undef
5416 // mask values count as coming from any quadword, for better codegen.
Benjamin Kramer003fad92011-10-15 13:28:31 +00005417 unsigned LoQuad[] = { 0, 0, 0, 0 };
5418 unsigned HiQuad[] = { 0, 0, 0, 0 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005419 BitVector InputQuads(4);
5420 for (unsigned i = 0; i < 8; ++i) {
Benjamin Kramer003fad92011-10-15 13:28:31 +00005421 unsigned *Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00005422 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005423 MaskVals.push_back(EltIdx);
5424 if (EltIdx < 0) {
5425 ++Quad[0];
5426 ++Quad[1];
5427 ++Quad[2];
5428 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00005429 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005430 }
5431 ++Quad[EltIdx / 4];
5432 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00005433 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005434
Nate Begemanb9a47b82009-02-23 08:49:38 +00005435 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005436 unsigned MaxQuad = 1;
5437 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005438 if (LoQuad[i] > MaxQuad) {
5439 BestLoQuad = i;
5440 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005441 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005442 }
5443
Nate Begemanb9a47b82009-02-23 08:49:38 +00005444 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005445 MaxQuad = 1;
5446 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005447 if (HiQuad[i] > MaxQuad) {
5448 BestHiQuad = i;
5449 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005450 }
5451 }
5452
Nate Begemanb9a47b82009-02-23 08:49:38 +00005453 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00005454 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00005455 // single pshufb instruction is necessary. If There are more than 2 input
5456 // quads, disable the next transformation since it does not help SSSE3.
5457 bool V1Used = InputQuads[0] || InputQuads[1];
5458 bool V2Used = InputQuads[2] || InputQuads[3];
Craig Topperc0d82852011-11-22 00:44:41 +00005459 if (Subtarget->hasSSSE3orAVX()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005460 if (InputQuads.count() == 2 && V1Used && V2Used) {
5461 BestLoQuad = InputQuads.find_first();
5462 BestHiQuad = InputQuads.find_next(BestLoQuad);
5463 }
5464 if (InputQuads.count() > 2) {
5465 BestLoQuad = -1;
5466 BestHiQuad = -1;
5467 }
5468 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005469
Nate Begemanb9a47b82009-02-23 08:49:38 +00005470 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5471 // the shuffle mask. If a quad is scored as -1, that means that it contains
5472 // words from all 4 input quadwords.
5473 SDValue NewV;
5474 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005475 SmallVector<int, 8> MaskV;
5476 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
5477 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00005478 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005479 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
5480 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
5481 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005482
Nate Begemanb9a47b82009-02-23 08:49:38 +00005483 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5484 // source words for the shuffle, to aid later transformations.
5485 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00005486 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00005487 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005488 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00005489 if (idx != (int)i)
5490 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005491 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00005492 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005493 AllWordsInNewV = false;
5494 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00005495 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005496
Nate Begemanb9a47b82009-02-23 08:49:38 +00005497 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5498 if (AllWordsInNewV) {
5499 for (int i = 0; i != 8; ++i) {
5500 int idx = MaskVals[i];
5501 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005502 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005503 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005504 if ((idx != i) && idx < 4)
5505 pshufhw = false;
5506 if ((idx != i) && idx > 3)
5507 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00005508 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005509 V1 = NewV;
5510 V2Used = false;
5511 BestLoQuad = 0;
5512 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005513 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005514
Nate Begemanb9a47b82009-02-23 08:49:38 +00005515 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5516 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005517 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005518 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5519 unsigned TargetMask = 0;
5520 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005521 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005522 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
5523 X86::getShufflePSHUFLWImmediate(NewV.getNode());
5524 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005525 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005526 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005527 }
Eric Christopherfd179292009-08-27 18:07:15 +00005528
Nate Begemanb9a47b82009-02-23 08:49:38 +00005529 // If we have SSSE3, and all words of the result are from 1 input vector,
5530 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5531 // is present, fall back to case 4.
Craig Topperc0d82852011-11-22 00:44:41 +00005532 if (Subtarget->hasSSSE3orAVX()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005533 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005534
Nate Begemanb9a47b82009-02-23 08:49:38 +00005535 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005536 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005537 // mask, and elements that come from V1 in the V2 mask, so that the two
5538 // results can be OR'd together.
5539 bool TwoInputs = V1Used && V2Used;
5540 for (unsigned i = 0; i != 8; ++i) {
5541 int EltIdx = MaskVals[i] * 2;
5542 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005543 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5544 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005545 continue;
5546 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005547 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
5548 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005549 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005550 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005551 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005552 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005553 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005554 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005555 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005556
Nate Begemanb9a47b82009-02-23 08:49:38 +00005557 // Calculate the shuffle mask for the second input, shuffle it, and
5558 // OR it with the first shuffled input.
5559 pshufbMask.clear();
5560 for (unsigned i = 0; i != 8; ++i) {
5561 int EltIdx = MaskVals[i] * 2;
5562 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005563 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5564 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005565 continue;
5566 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005567 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
5568 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005569 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005570 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005571 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005572 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005573 MVT::v16i8, &pshufbMask[0], 16));
5574 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005575 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005576 }
5577
5578 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5579 // and update MaskVals with new element order.
5580 BitVector InOrder(8);
5581 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005582 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005583 for (int i = 0; i != 4; ++i) {
5584 int idx = MaskVals[i];
5585 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005586 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005587 InOrder.set(i);
5588 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005589 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005590 InOrder.set(i);
5591 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005592 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005593 }
5594 }
5595 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005596 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00005597 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005598 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005599
Craig Topperc0d82852011-11-22 00:44:41 +00005600 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3orAVX())
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005601 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
5602 NewV.getOperand(0),
5603 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
5604 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005605 }
Eric Christopherfd179292009-08-27 18:07:15 +00005606
Nate Begemanb9a47b82009-02-23 08:49:38 +00005607 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5608 // and update MaskVals with the new element order.
5609 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005610 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005611 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005612 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005613 for (unsigned i = 4; i != 8; ++i) {
5614 int idx = MaskVals[i];
5615 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005616 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005617 InOrder.set(i);
5618 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005619 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005620 InOrder.set(i);
5621 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005622 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005623 }
5624 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005625 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005626 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005627
Craig Topperc0d82852011-11-22 00:44:41 +00005628 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3orAVX())
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005629 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
5630 NewV.getOperand(0),
5631 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
5632 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005633 }
Eric Christopherfd179292009-08-27 18:07:15 +00005634
Nate Begemanb9a47b82009-02-23 08:49:38 +00005635 // In case BestHi & BestLo were both -1, which means each quadword has a word
5636 // from each of the four input quadwords, calculate the InOrder bitvector now
5637 // before falling through to the insert/extract cleanup.
5638 if (BestLoQuad == -1 && BestHiQuad == -1) {
5639 NewV = V1;
5640 for (int i = 0; i != 8; ++i)
5641 if (MaskVals[i] < 0 || MaskVals[i] == i)
5642 InOrder.set(i);
5643 }
Eric Christopherfd179292009-08-27 18:07:15 +00005644
Nate Begemanb9a47b82009-02-23 08:49:38 +00005645 // The other elements are put in the right place using pextrw and pinsrw.
5646 for (unsigned i = 0; i != 8; ++i) {
5647 if (InOrder[i])
5648 continue;
5649 int EltIdx = MaskVals[i];
5650 if (EltIdx < 0)
5651 continue;
5652 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00005653 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005654 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00005655 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005656 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005657 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005658 DAG.getIntPtrConstant(i));
5659 }
5660 return NewV;
5661}
5662
5663// v16i8 shuffles - Prefer shuffles in the following order:
5664// 1. [ssse3] 1 x pshufb
5665// 2. [ssse3] 2 x pshufb + 1 x por
5666// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5667static
Nate Begeman9008ca62009-04-27 18:41:29 +00005668SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005669 SelectionDAG &DAG,
5670 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005671 SDValue V1 = SVOp->getOperand(0);
5672 SDValue V2 = SVOp->getOperand(1);
5673 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005674 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00005675 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00005676
Nate Begemanb9a47b82009-02-23 08:49:38 +00005677 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005678 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005679 // present, fall back to case 3.
5680 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
5681 bool V1Only = true;
5682 bool V2Only = true;
5683 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005684 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00005685 if (EltIdx < 0)
5686 continue;
5687 if (EltIdx < 16)
5688 V2Only = false;
5689 else
5690 V1Only = false;
5691 }
Eric Christopherfd179292009-08-27 18:07:15 +00005692
Nate Begemanb9a47b82009-02-23 08:49:38 +00005693 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
Craig Topperc0d82852011-11-22 00:44:41 +00005694 if (TLI.getSubtarget()->hasSSSE3orAVX()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005695 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005696
Nate Begemanb9a47b82009-02-23 08:49:38 +00005697 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005698 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005699 //
5700 // Otherwise, we have elements from both input vectors, and must zero out
5701 // elements that come from V2 in the first mask, and V1 in the second mask
5702 // so that we can OR them together.
5703 bool TwoInputs = !(V1Only || V2Only);
5704 for (unsigned i = 0; i != 16; ++i) {
5705 int EltIdx = MaskVals[i];
5706 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005707 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005708 continue;
5709 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005710 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005711 }
5712 // If all the elements are from V2, assign it to V1 and return after
5713 // building the first pshufb.
5714 if (V2Only)
5715 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00005716 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005717 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005718 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005719 if (!TwoInputs)
5720 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005721
Nate Begemanb9a47b82009-02-23 08:49:38 +00005722 // Calculate the shuffle mask for the second input, shuffle it, and
5723 // OR it with the first shuffled input.
5724 pshufbMask.clear();
5725 for (unsigned i = 0; i != 16; ++i) {
5726 int EltIdx = MaskVals[i];
5727 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005728 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005729 continue;
5730 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005731 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005732 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005733 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005734 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005735 MVT::v16i8, &pshufbMask[0], 16));
5736 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005737 }
Eric Christopherfd179292009-08-27 18:07:15 +00005738
Nate Begemanb9a47b82009-02-23 08:49:38 +00005739 // No SSSE3 - Calculate in place words and then fix all out of place words
5740 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5741 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005742 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5743 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005744 SDValue NewV = V2Only ? V2 : V1;
5745 for (int i = 0; i != 8; ++i) {
5746 int Elt0 = MaskVals[i*2];
5747 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005748
Nate Begemanb9a47b82009-02-23 08:49:38 +00005749 // This word of the result is all undef, skip it.
5750 if (Elt0 < 0 && Elt1 < 0)
5751 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005752
Nate Begemanb9a47b82009-02-23 08:49:38 +00005753 // This word of the result is already in the correct place, skip it.
5754 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
5755 continue;
5756 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
5757 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005758
Nate Begemanb9a47b82009-02-23 08:49:38 +00005759 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5760 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5761 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005762
5763 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5764 // using a single extract together, load it and store it.
5765 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005766 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005767 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005768 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005769 DAG.getIntPtrConstant(i));
5770 continue;
5771 }
5772
Nate Begemanb9a47b82009-02-23 08:49:38 +00005773 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00005774 // source byte is not also odd, shift the extracted word left 8 bits
5775 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005776 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005777 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005778 DAG.getIntPtrConstant(Elt1 / 2));
5779 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005780 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00005781 DAG.getConstant(8,
5782 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005783 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005784 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
5785 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005786 }
5787 // If Elt0 is defined, extract it from the appropriate source. If the
5788 // source byte is not also even, shift the extracted word right 8 bits. If
5789 // Elt1 was also defined, OR the extracted values together before
5790 // inserting them in the result.
5791 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005792 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005793 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
5794 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005795 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00005796 DAG.getConstant(8,
5797 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005798 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005799 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
5800 DAG.getConstant(0x00FF, MVT::i16));
5801 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00005802 : InsElt0;
5803 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005804 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005805 DAG.getIntPtrConstant(i));
5806 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005807 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005808}
5809
Evan Cheng7a831ce2007-12-15 03:00:47 +00005810/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005811/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00005812/// done when every pair / quad of shuffle mask elements point to elements in
5813/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005814/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00005815static
Nate Begeman9008ca62009-04-27 18:41:29 +00005816SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005817 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00005818 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00005819 SDValue V1 = SVOp->getOperand(0);
5820 SDValue V2 = SVOp->getOperand(1);
5821 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00005822 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005823 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00005824 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005825 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005826 case MVT::v4f32: NewVT = MVT::v2f64; break;
5827 case MVT::v4i32: NewVT = MVT::v2i64; break;
5828 case MVT::v8i16: NewVT = MVT::v4i32; break;
5829 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00005830 }
5831
Nate Begeman9008ca62009-04-27 18:41:29 +00005832 int Scale = NumElems / NewWidth;
5833 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00005834 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005835 int StartIdx = -1;
5836 for (int j = 0; j < Scale; ++j) {
5837 int EltIdx = SVOp->getMaskElt(i+j);
5838 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005839 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00005840 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00005841 StartIdx = EltIdx - (EltIdx % Scale);
5842 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00005843 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005844 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005845 if (StartIdx == -1)
5846 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00005847 else
Nate Begeman9008ca62009-04-27 18:41:29 +00005848 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005849 }
5850
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005851 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
5852 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00005853 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005854}
5855
Evan Chengd880b972008-05-09 21:53:03 +00005856/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005857///
Owen Andersone50ed302009-08-10 22:56:29 +00005858static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00005859 SDValue SrcOp, SelectionDAG &DAG,
5860 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005861 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005862 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00005863 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00005864 LD = dyn_cast<LoadSDNode>(SrcOp);
5865 if (!LD) {
5866 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
5867 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00005868 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00005869 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00005870 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005871 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00005872 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005873 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00005874 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005875 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005876 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
5877 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
5878 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00005879 SrcOp.getOperand(0)
5880 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005881 }
5882 }
5883 }
5884
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005885 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005886 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005887 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00005888 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005889}
5890
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00005891/// areShuffleHalvesWithinDisjointLanes - Check whether each half of a vector
5892/// shuffle node referes to only one lane in the sources.
5893static bool areShuffleHalvesWithinDisjointLanes(ShuffleVectorSDNode *SVOp) {
5894 EVT VT = SVOp->getValueType(0);
5895 int NumElems = VT.getVectorNumElements();
5896 int HalfSize = NumElems/2;
5897 SmallVector<int, 16> M;
5898 SVOp->getMask(M);
5899 bool MatchA = false, MatchB = false;
5900
5901 for (int l = 0; l < NumElems*2; l += HalfSize) {
5902 if (isUndefOrInRange(M, 0, HalfSize, l, l+HalfSize)) {
5903 MatchA = true;
5904 break;
5905 }
5906 }
5907
5908 for (int l = 0; l < NumElems*2; l += HalfSize) {
5909 if (isUndefOrInRange(M, HalfSize, HalfSize, l, l+HalfSize)) {
5910 MatchB = true;
5911 break;
5912 }
5913 }
5914
5915 return MatchA && MatchB;
5916}
5917
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00005918/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
5919/// which could not be matched by any known target speficic shuffle
5920static SDValue
5921LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00005922 if (areShuffleHalvesWithinDisjointLanes(SVOp)) {
5923 // If each half of a vector shuffle node referes to only one lane in the
5924 // source vectors, extract each used 128-bit lane and shuffle them using
5925 // 128-bit shuffles. Then, concatenate the results. Otherwise leave
5926 // the work to the legalizer.
5927 DebugLoc dl = SVOp->getDebugLoc();
5928 EVT VT = SVOp->getValueType(0);
5929 int NumElems = VT.getVectorNumElements();
5930 int HalfSize = NumElems/2;
5931
5932 // Extract the reference for each half
5933 int FstVecExtractIdx = 0, SndVecExtractIdx = 0;
5934 int FstVecOpNum = 0, SndVecOpNum = 0;
5935 for (int i = 0; i < HalfSize; ++i) {
5936 int Elt = SVOp->getMaskElt(i);
5937 if (SVOp->getMaskElt(i) < 0)
5938 continue;
5939 FstVecOpNum = Elt/NumElems;
5940 FstVecExtractIdx = Elt % NumElems < HalfSize ? 0 : HalfSize;
5941 break;
5942 }
5943 for (int i = HalfSize; i < NumElems; ++i) {
5944 int Elt = SVOp->getMaskElt(i);
5945 if (SVOp->getMaskElt(i) < 0)
5946 continue;
5947 SndVecOpNum = Elt/NumElems;
5948 SndVecExtractIdx = Elt % NumElems < HalfSize ? 0 : HalfSize;
5949 break;
5950 }
5951
5952 // Extract the subvectors
5953 SDValue V1 = Extract128BitVector(SVOp->getOperand(FstVecOpNum),
5954 DAG.getConstant(FstVecExtractIdx, MVT::i32), DAG, dl);
5955 SDValue V2 = Extract128BitVector(SVOp->getOperand(SndVecOpNum),
5956 DAG.getConstant(SndVecExtractIdx, MVT::i32), DAG, dl);
5957
5958 // Generate 128-bit shuffles
5959 SmallVector<int, 16> MaskV1, MaskV2;
5960 for (int i = 0; i < HalfSize; ++i) {
5961 int Elt = SVOp->getMaskElt(i);
5962 MaskV1.push_back(Elt < 0 ? Elt : Elt % HalfSize);
5963 }
5964 for (int i = HalfSize; i < NumElems; ++i) {
5965 int Elt = SVOp->getMaskElt(i);
5966 MaskV2.push_back(Elt < 0 ? Elt : Elt % HalfSize);
5967 }
5968
5969 EVT NVT = V1.getValueType();
5970 V1 = DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &MaskV1[0]);
5971 V2 = DAG.getVectorShuffle(NVT, dl, V2, DAG.getUNDEF(NVT), &MaskV2[0]);
5972
5973 // Concatenate the result back
5974 SDValue V = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), V1,
5975 DAG.getConstant(0, MVT::i32), DAG, dl);
5976 return Insert128BitVector(V, V2, DAG.getConstant(NumElems/2, MVT::i32),
5977 DAG, dl);
5978 }
5979
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00005980 return SDValue();
5981}
5982
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00005983/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
5984/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00005985static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00005986LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005987 SDValue V1 = SVOp->getOperand(0);
5988 SDValue V2 = SVOp->getOperand(1);
5989 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005990 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00005991
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00005992 assert(VT.getSizeInBits() == 128 && "Unsupported vector size");
5993
Evan Chengace3c172008-07-22 21:13:36 +00005994 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00005995 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00005996 SmallVector<int, 8> Mask1(4U, -1);
5997 SmallVector<int, 8> PermMask;
5998 SVOp->getMask(PermMask);
5999
Evan Chengace3c172008-07-22 21:13:36 +00006000 unsigned NumHi = 0;
6001 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00006002 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006003 int Idx = PermMask[i];
6004 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006005 Locs[i] = std::make_pair(-1, -1);
6006 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006007 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
6008 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006009 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00006010 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006011 NumLo++;
6012 } else {
6013 Locs[i] = std::make_pair(1, NumHi);
6014 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00006015 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006016 NumHi++;
6017 }
6018 }
6019 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006020
Evan Chengace3c172008-07-22 21:13:36 +00006021 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006022 // If no more than two elements come from either vector. This can be
6023 // implemented with two shuffles. First shuffle gather the elements.
6024 // The second shuffle, which takes the first shuffle as both of its
6025 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00006026 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006027
Nate Begeman9008ca62009-04-27 18:41:29 +00006028 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00006029
Evan Chengace3c172008-07-22 21:13:36 +00006030 for (unsigned i = 0; i != 4; ++i) {
6031 if (Locs[i].first == -1)
6032 continue;
6033 else {
6034 unsigned Idx = (i < 2) ? 0 : 4;
6035 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006036 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006037 }
6038 }
6039
Nate Begeman9008ca62009-04-27 18:41:29 +00006040 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006041 } else if (NumLo == 3 || NumHi == 3) {
6042 // Otherwise, we must have three elements from one vector, call it X, and
6043 // one element from the other, call it Y. First, use a shufps to build an
6044 // intermediate vector with the one element from Y and the element from X
6045 // that will be in the same half in the final destination (the indexes don't
6046 // matter). Then, use a shufps to build the final vector, taking the half
6047 // containing the element from Y from the intermediate, and the other half
6048 // from X.
6049 if (NumHi == 3) {
6050 // Normalize it so the 3 elements come from V1.
Craig Topperbeabc6c2011-12-05 06:56:46 +00006051 CommuteVectorShuffleMask(PermMask, 4);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006052 std::swap(V1, V2);
6053 }
6054
6055 // Find the element from V2.
6056 unsigned HiIndex;
6057 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006058 int Val = PermMask[HiIndex];
6059 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006060 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006061 if (Val >= 4)
6062 break;
6063 }
6064
Nate Begeman9008ca62009-04-27 18:41:29 +00006065 Mask1[0] = PermMask[HiIndex];
6066 Mask1[1] = -1;
6067 Mask1[2] = PermMask[HiIndex^1];
6068 Mask1[3] = -1;
6069 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006070
6071 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006072 Mask1[0] = PermMask[0];
6073 Mask1[1] = PermMask[1];
6074 Mask1[2] = HiIndex & 1 ? 6 : 4;
6075 Mask1[3] = HiIndex & 1 ? 4 : 6;
6076 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006077 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006078 Mask1[0] = HiIndex & 1 ? 2 : 0;
6079 Mask1[1] = HiIndex & 1 ? 0 : 2;
6080 Mask1[2] = PermMask[2];
6081 Mask1[3] = PermMask[3];
6082 if (Mask1[2] >= 0)
6083 Mask1[2] += 4;
6084 if (Mask1[3] >= 0)
6085 Mask1[3] += 4;
6086 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006087 }
Evan Chengace3c172008-07-22 21:13:36 +00006088 }
6089
6090 // Break it into (shuffle shuffle_hi, shuffle_lo).
6091 Locs.clear();
David Greenec4db4e52011-02-28 19:06:56 +00006092 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00006093 SmallVector<int,8> LoMask(4U, -1);
6094 SmallVector<int,8> HiMask(4U, -1);
6095
6096 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00006097 unsigned MaskIdx = 0;
6098 unsigned LoIdx = 0;
6099 unsigned HiIdx = 2;
6100 for (unsigned i = 0; i != 4; ++i) {
6101 if (i == 2) {
6102 MaskPtr = &HiMask;
6103 MaskIdx = 1;
6104 LoIdx = 0;
6105 HiIdx = 2;
6106 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006107 int Idx = PermMask[i];
6108 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006109 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00006110 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006111 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00006112 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006113 LoIdx++;
6114 } else {
6115 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00006116 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006117 HiIdx++;
6118 }
6119 }
6120
Nate Begeman9008ca62009-04-27 18:41:29 +00006121 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6122 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
6123 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00006124 for (unsigned i = 0; i != 4; ++i) {
6125 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006126 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00006127 } else {
6128 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006129 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00006130 }
6131 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006132 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006133}
6134
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006135static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006136 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006137 V = V.getOperand(0);
6138 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6139 V = V.getOperand(0);
Evan Cheng7bc389b2011-11-08 00:31:58 +00006140 if (V.hasOneUse() && V.getOpcode() == ISD::BUILD_VECTOR &&
6141 V.getNumOperands() == 2 && V.getOperand(1).getOpcode() == ISD::UNDEF)
6142 // BUILD_VECTOR (load), undef
6143 V = V.getOperand(0);
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006144 if (MayFoldLoad(V))
6145 return true;
6146 return false;
6147}
6148
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006149// FIXME: the version above should always be used. Since there's
6150// a bug where several vector shuffles can't be folded because the
6151// DAG is not updated during lowering and a node claims to have two
6152// uses while it only has one, use this version, and let isel match
6153// another instruction if the load really happens to have more than
6154// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00006155// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006156static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006157 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006158 V = V.getOperand(0);
6159 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6160 V = V.getOperand(0);
6161 if (ISD::isNormalLoad(V.getNode()))
6162 return true;
6163 return false;
6164}
6165
6166/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
6167/// a vector extract, and if both can be later optimized into a single load.
6168/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
6169/// here because otherwise a target specific shuffle node is going to be
6170/// emitted for this shuffle, and the optimization not done.
6171/// FIXME: This is probably not the best approach, but fix the problem
6172/// until the right path is decided.
6173static
6174bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
6175 const TargetLowering &TLI) {
6176 EVT VT = V.getValueType();
6177 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
6178
6179 // Be sure that the vector shuffle is present in a pattern like this:
6180 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
6181 if (!V.hasOneUse())
6182 return false;
6183
6184 SDNode *N = *V.getNode()->use_begin();
6185 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
6186 return false;
6187
6188 SDValue EltNo = N->getOperand(1);
6189 if (!isa<ConstantSDNode>(EltNo))
6190 return false;
6191
6192 // If the bit convert changed the number of elements, it is unsafe
6193 // to examine the mask.
6194 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006195 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006196 EVT SrcVT = V.getOperand(0).getValueType();
6197 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
6198 return false;
6199 V = V.getOperand(0);
6200 HasShuffleIntoBitcast = true;
6201 }
6202
6203 // Select the input vector, guarding against out of range extract vector.
6204 unsigned NumElems = VT.getVectorNumElements();
6205 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
6206 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
6207 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
6208
6209 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006210 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006211 V = V.getOperand(0);
6212
6213 if (ISD::isNormalLoad(V.getNode())) {
6214 // Is the original load suitable?
6215 LoadSDNode *LN0 = cast<LoadSDNode>(V);
6216
6217 // FIXME: avoid the multi-use bug that is preventing lots of
6218 // of foldings to be detected, this is still wrong of course, but
6219 // give the temporary desired behavior, and if it happens that
6220 // the load has real more uses, during isel it will not fold, and
6221 // will generate poor code.
6222 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
6223 return false;
6224
6225 if (!HasShuffleIntoBitcast)
6226 return true;
6227
6228 // If there's a bitcast before the shuffle, check if the load type and
6229 // alignment is valid.
6230 unsigned Align = LN0->getAlignment();
6231 unsigned NewAlign =
6232 TLI.getTargetData()->getABITypeAlignment(
6233 VT.getTypeForEVT(*DAG.getContext()));
6234
6235 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
6236 return false;
6237 }
6238
6239 return true;
6240}
6241
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006242static
Evan Cheng835580f2010-10-07 20:50:20 +00006243SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
6244 EVT VT = Op.getValueType();
6245
6246 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006247 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6248 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006249 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6250 V1, DAG));
6251}
6252
6253static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006254SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006255 bool HasXMMInt) {
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006256 SDValue V1 = Op.getOperand(0);
6257 SDValue V2 = Op.getOperand(1);
6258 EVT VT = Op.getValueType();
6259
6260 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6261
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006262 if (HasXMMInt && VT == MVT::v2f64)
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006263 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6264
Evan Cheng0899f5c2011-08-31 02:05:24 +00006265 // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
6266 return DAG.getNode(ISD::BITCAST, dl, VT,
6267 getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
6268 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
6269 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006270}
6271
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006272static
6273SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
6274 SDValue V1 = Op.getOperand(0);
6275 SDValue V2 = Op.getOperand(1);
6276 EVT VT = Op.getValueType();
6277
6278 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6279 "unsupported shuffle type");
6280
6281 if (V2.getOpcode() == ISD::UNDEF)
6282 V2 = V1;
6283
6284 // v4i32 or v4f32
6285 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6286}
6287
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006288static inline unsigned getSHUFPOpcode(EVT VT) {
6289 switch(VT.getSimpleVT().SimpleTy) {
6290 case MVT::v8i32: // Use fp unit for int unpack.
6291 case MVT::v8f32:
6292 case MVT::v4i32: // Use fp unit for int unpack.
6293 case MVT::v4f32: return X86ISD::SHUFPS;
6294 case MVT::v4i64: // Use fp unit for int unpack.
6295 case MVT::v4f64:
6296 case MVT::v2i64: // Use fp unit for int unpack.
6297 case MVT::v2f64: return X86ISD::SHUFPD;
6298 default:
6299 llvm_unreachable("Unknown type for shufp*");
6300 }
6301 return 0;
6302}
6303
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006304static
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006305SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasXMMInt) {
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006306 SDValue V1 = Op.getOperand(0);
6307 SDValue V2 = Op.getOperand(1);
6308 EVT VT = Op.getValueType();
6309 unsigned NumElems = VT.getVectorNumElements();
6310
6311 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6312 // operand of these instructions is only memory, so check if there's a
6313 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6314 // same masks.
6315 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006316
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006317 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006318 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006319 CanFoldLoad = true;
6320
6321 // When V1 is a load, it can be folded later into a store in isel, example:
6322 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6323 // turns into:
6324 // (MOVLPSmr addr:$src1, VR128:$src2)
6325 // So, recognize this potential and also use MOVLPS or MOVLPD
Evan Cheng7bc389b2011-11-08 00:31:58 +00006326 else if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006327 CanFoldLoad = true;
6328
Dan Gohman65fd6562011-11-03 21:49:52 +00006329 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006330 if (CanFoldLoad) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006331 if (HasXMMInt && NumElems == 2)
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006332 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6333
6334 if (NumElems == 4)
Dan Gohman65fd6562011-11-03 21:49:52 +00006335 // If we don't care about the second element, procede to use movss.
6336 if (SVOp->getMaskElt(1) != -1)
6337 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006338 }
6339
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006340 // movl and movlp will both match v2i64, but v2i64 is never matched by
6341 // movl earlier because we make it strict to avoid messing with the movlp load
6342 // folding logic (see the code above getMOVLP call). Match it here then,
6343 // this is horrible, but will stay like this until we move all shuffle
6344 // matching to x86 specific nodes. Note that for the 1st condition all
6345 // types are matched with movsd.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006346 if (HasXMMInt) {
Bruno Cardoso Lopes5ca0d142011-09-14 02:36:14 +00006347 // FIXME: isMOVLMask should be checked and matched before getMOVLP,
6348 // as to remove this logic from here, as much as possible
6349 if (NumElems == 2 || !X86::isMOVLMask(SVOp))
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006350 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006351 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006352 }
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006353
6354 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6355
6356 // Invert the operand order and use SHUFPS to match it.
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006357 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V2, V1,
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006358 X86::getShuffleSHUFImmediate(SVOp), DAG);
6359}
6360
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006361static
6362SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006363 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006364 const X86Subtarget *Subtarget) {
6365 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6366 EVT VT = Op.getValueType();
6367 DebugLoc dl = Op.getDebugLoc();
6368 SDValue V1 = Op.getOperand(0);
6369 SDValue V2 = Op.getOperand(1);
6370
6371 if (isZeroShuffle(SVOp))
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006372 return getZeroVector(VT, Subtarget->hasXMMInt(), DAG, dl);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006373
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006374 // Handle splat operations
6375 if (SVOp->isSplat()) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006376 unsigned NumElem = VT.getVectorNumElements();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006377 int Size = VT.getSizeInBits();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006378 // Special case, this is the only place now where it's allowed to return
6379 // a vector_shuffle operation without using a target specific node, because
6380 // *hopefully* it will be optimized away by the dag combiner. FIXME: should
6381 // this be moved to DAGCombine instead?
6382 if (NumElem <= 4 && CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006383 return Op;
6384
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006385 // Use vbroadcast whenever the splat comes from a foldable load
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00006386 SDValue LD = isVectorBroadcast(Op, Subtarget->hasAVX2());
Nadav Rotemf8c10e52011-11-15 22:50:37 +00006387 if (Subtarget->hasAVX() && LD.getNode())
6388 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, LD);
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006389
Bruno Cardoso Lopes6a32adc2011-07-25 23:05:25 +00006390 // Handle splats by matching through known shuffle masks
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006391 if ((Size == 128 && NumElem <= 4) ||
6392 (Size == 256 && NumElem < 8))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006393 return SDValue();
6394
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00006395 // All remaning splats are promoted to target supported vector shuffles.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006396 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006397 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006398
6399 // If the shuffle can be profitably rewritten as a narrower shuffle, then
6400 // do it!
6401 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
6402 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6403 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006404 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006405 } else if ((VT == MVT::v4i32 ||
6406 (VT == MVT::v4f32 && Subtarget->hasXMMInt()))) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006407 // FIXME: Figure out a cleaner way to do this.
6408 // Try to make use of movq to zero out the top part.
6409 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
6410 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6411 if (NewOp.getNode()) {
6412 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
6413 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
6414 DAG, Subtarget, dl);
6415 }
6416 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
6417 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6418 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
6419 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
6420 DAG, Subtarget, dl);
6421 }
6422 }
6423 return SDValue();
6424}
6425
Dan Gohman475871a2008-07-27 21:46:04 +00006426SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006427X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00006428 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00006429 SDValue V1 = Op.getOperand(0);
6430 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006431 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006432 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006433 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006434 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00006435 bool V1IsSplat = false;
6436 bool V2IsSplat = false;
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006437 bool HasXMMInt = Subtarget->hasXMMInt();
Craig Topperbeabc6c2011-12-05 06:56:46 +00006438 bool HasAVX = Subtarget->hasAVX();
Craig Topper6347e862011-11-21 06:57:39 +00006439 bool HasAVX2 = Subtarget->hasAVX2();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006440 MachineFunction &MF = DAG.getMachineFunction();
6441 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006442
Craig Topper3426a3e2011-11-14 06:46:21 +00006443 assert(VT.getSizeInBits() != 64 && "Can't lower MMX shuffles");
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00006444
Craig Topper38034c52011-11-26 22:55:48 +00006445 assert(V1.getOpcode() != ISD::UNDEF && "Op 1 of shuffle should not be undef");
6446
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006447 // Vector shuffle lowering takes 3 steps:
6448 //
6449 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
6450 // narrowing and commutation of operands should be handled.
6451 // 2) Matching of shuffles with known shuffle masks to x86 target specific
6452 // shuffle nodes.
6453 // 3) Rewriting of unmatched masks into new generic shuffle operations,
6454 // so the shuffle can be broken into other shuffles and the legalizer can
6455 // try the lowering again.
6456 //
Craig Topper3426a3e2011-11-14 06:46:21 +00006457 // The general idea is that no vector_shuffle operation should be left to
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006458 // be matched during isel, all of them must be converted to a target specific
6459 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00006460
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006461 // Normalize the input vectors. Here splats, zeroed vectors, profitable
6462 // narrowing and commutation of operands should be handled. The actual code
6463 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006464 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006465 if (NewOp.getNode())
6466 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00006467
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006468 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
6469 // unpckh_undef). Only use pshufd if speed is more important than size.
6470 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
Craig Topper34671b82011-12-06 08:21:25 +00006471 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006472 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
Craig Topper34671b82011-12-06 08:21:25 +00006473 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00006474
Craig Topperc0d82852011-11-22 00:44:41 +00006475 if (X86::isMOVDDUPMask(SVOp) && Subtarget->hasSSE3orAVX() &&
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006476 V2IsUndef && RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00006477 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006478
Dale Johannesen0488fb62010-09-30 23:57:10 +00006479 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006480 return getMOVHighToLow(Op, dl, DAG);
6481
6482 // Use to match splats
Craig Topperc0d82852011-11-22 00:44:41 +00006483 if (HasXMMInt && X86::isUNPCKHMask(SVOp, HasAVX2) && V2IsUndef &&
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006484 (VT == MVT::v2f64 || VT == MVT::v2i64))
Craig Topper34671b82011-12-06 08:21:25 +00006485 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006486
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006487 if (X86::isPSHUFDMask(SVOp)) {
6488 // The actual implementation will match the mask in the if above and then
6489 // during isel it can match several different instructions, not only pshufd
6490 // as its name says, sad but true, emulate the behavior for now...
6491 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
6492 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
6493
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006494 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
6495
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006496 if (HasXMMInt && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006497 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
6498
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006499 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V1,
6500 TargetMask, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006501 }
Eric Christopherfd179292009-08-27 18:07:15 +00006502
Evan Chengf26ffe92008-05-29 08:22:04 +00006503 // Check if this can be converted into a logical shift.
6504 bool isLeft = false;
6505 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00006506 SDValue ShVal;
Craig Topperc0d82852011-11-22 00:44:41 +00006507 bool isShift = HasXMMInt && isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00006508 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006509 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00006510 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006511 EVT EltVT = VT.getVectorElementType();
6512 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006513 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006514 }
Eric Christopherfd179292009-08-27 18:07:15 +00006515
Nate Begeman9008ca62009-04-27 18:41:29 +00006516 if (X86::isMOVLMask(SVOp)) {
Gabor Greifba36cb52008-08-28 21:40:38 +00006517 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00006518 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00006519 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006520 if (HasXMMInt && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006521 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6522
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006523 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006524 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6525 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00006526 }
Eric Christopherfd179292009-08-27 18:07:15 +00006527
Nate Begeman9008ca62009-04-27 18:41:29 +00006528 // FIXME: fold these into legal mask.
Craig Topperc0d82852011-11-22 00:44:41 +00006529 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp, HasAVX2))
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006530 return getMOVLowToHigh(Op, dl, DAG, HasXMMInt);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006531
Dale Johannesen0488fb62010-09-30 23:57:10 +00006532 if (X86::isMOVHLPSMask(SVOp))
6533 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006534
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00006535 if (X86::isMOVSHDUPMask(SVOp, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006536 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006537
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00006538 if (X86::isMOVSLDUPMask(SVOp, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006539 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00006540
Dale Johannesen0488fb62010-09-30 23:57:10 +00006541 if (X86::isMOVLPMask(SVOp))
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006542 return getMOVLP(Op, dl, DAG, HasXMMInt);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006543
Nate Begeman9008ca62009-04-27 18:41:29 +00006544 if (ShouldXformToMOVHLPS(SVOp) ||
6545 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
6546 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006547
Evan Chengf26ffe92008-05-29 08:22:04 +00006548 if (isShift) {
6549 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006550 EVT EltVT = VT.getVectorElementType();
6551 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006552 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006553 }
Eric Christopherfd179292009-08-27 18:07:15 +00006554
Evan Cheng9eca5e82006-10-25 21:49:50 +00006555 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00006556 // FIXME: This should also accept a bitcast of a splat? Be careful, not
6557 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00006558 V1IsSplat = isSplatVector(V1.getNode());
6559 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006560
Chris Lattner8a594482007-11-25 00:24:49 +00006561 // Canonicalize the splat or undef, if present, to be on the RHS.
Craig Topper38034c52011-11-26 22:55:48 +00006562 if (V1IsSplat && !V2IsSplat) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006563 Op = CommuteVectorShuffle(SVOp, DAG);
6564 SVOp = cast<ShuffleVectorSDNode>(Op);
6565 V1 = SVOp->getOperand(0);
6566 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00006567 std::swap(V1IsSplat, V2IsSplat);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006568 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00006569 }
6570
Craig Topperbeabc6c2011-12-05 06:56:46 +00006571 SmallVector<int, 32> M;
6572 SVOp->getMask(M);
6573
6574 if (isCommutedMOVLMask(M, VT, V2IsSplat, V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006575 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00006576 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00006577 return V1;
6578 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6579 // the instruction selector will not match, so get a canonical MOVL with
6580 // swapped operands to undo the commute.
6581 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006582 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006583
Craig Topperbeabc6c2011-12-05 06:56:46 +00006584 if (isUNPCKLMask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006585 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006586
Craig Topperbeabc6c2011-12-05 06:56:46 +00006587 if (isUNPCKHMask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006588 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006589
Evan Cheng9bbbb982006-10-25 20:48:19 +00006590 if (V2IsSplat) {
6591 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006592 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00006593 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00006594 SDValue NewMask = NormalizeMask(SVOp, DAG);
6595 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
6596 if (NSVOp != SVOp) {
Craig Topperc0d82852011-11-22 00:44:41 +00006597 if (X86::isUNPCKLMask(NSVOp, HasAVX2, true)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006598 return NewMask;
Craig Topperc0d82852011-11-22 00:44:41 +00006599 } else if (X86::isUNPCKHMask(NSVOp, HasAVX2, true)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006600 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006601 }
6602 }
6603 }
6604
Evan Cheng9eca5e82006-10-25 21:49:50 +00006605 if (Commuted) {
6606 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006607 // FIXME: this seems wrong.
6608 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
6609 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006610
Craig Topperc0d82852011-11-22 00:44:41 +00006611 if (X86::isUNPCKLMask(NewSVOp, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006612 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V2, V1, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006613
Craig Topperc0d82852011-11-22 00:44:41 +00006614 if (X86::isUNPCKHMask(NewSVOp, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006615 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V2, V1, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006616 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006617
Nate Begeman9008ca62009-04-27 18:41:29 +00006618 // Normalize the node to match x86 shuffle ops if needed
Craig Topper1ff73d72011-12-06 04:59:07 +00006619 if (!V2IsUndef && (isSHUFPMask(M, VT, /* Commuted */ true) ||
6620 isVSHUFPYMask(M, VT, HasAVX, /* Commuted */ true)))
Nate Begeman9008ca62009-04-27 18:41:29 +00006621 return CommuteVectorShuffle(SVOp, DAG);
6622
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006623 // The checks below are all present in isShuffleMaskLegal, but they are
6624 // inlined here right now to enable us to directly emit target specific
6625 // nodes, and remove one by one until they don't return Op anymore.
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006626
Craig Topperc0d82852011-11-22 00:44:41 +00006627 if (isPALIGNRMask(M, VT, Subtarget->hasSSSE3orAVX()))
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006628 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
6629 X86::getShufflePALIGNRImmediate(SVOp),
6630 DAG);
6631
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006632 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6633 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Craig Topperbeabc6c2011-12-05 06:56:46 +00006634 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Craig Topper34671b82011-12-06 08:21:25 +00006635 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006636 }
6637
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006638 if (isPSHUFHWMask(M, VT))
6639 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
6640 X86::getShufflePSHUFHWImmediate(SVOp),
6641 DAG);
6642
6643 if (isPSHUFLWMask(M, VT))
6644 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
6645 X86::getShufflePSHUFLWImmediate(SVOp),
6646 DAG);
6647
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006648 if (isSHUFPMask(M, VT))
6649 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6650 X86::getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00006651
Craig Topperbeabc6c2011-12-05 06:56:46 +00006652 if (isUNPCKL_v_undef_Mask(M, VT))
Craig Topper34671b82011-12-06 08:21:25 +00006653 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Craig Topperbeabc6c2011-12-05 06:56:46 +00006654 if (isUNPCKH_v_undef_Mask(M, VT))
Craig Topper34671b82011-12-06 08:21:25 +00006655 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006656
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006657 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006658 // Generate target specific nodes for 128 or 256-bit shuffles only
6659 // supported in the AVX instruction set.
6660 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006661
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006662 // Handle VMOVDDUPY permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006663 if (V2IsUndef && isMOVDDUPYMask(M, VT, HasAVX))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006664 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
6665
Craig Topper70b883b2011-11-28 10:14:51 +00006666 // Handle VPERMILPS/D* permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006667 if (isVPERMILPMask(M, VT, HasAVX))
Craig Topper316cd2a2011-11-30 06:25:25 +00006668 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1,
Craig Topper70b883b2011-11-28 10:14:51 +00006669 getShuffleVPERMILPImmediate(SVOp), DAG);
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006670
Craig Topper70b883b2011-11-28 10:14:51 +00006671 // Handle VPERM2F128/VPERM2I128 permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006672 if (isVPERM2X128Mask(M, VT, HasAVX))
Craig Topperec24e612011-11-30 07:47:51 +00006673 return getTargetShuffleNode(X86ISD::VPERM2X128, dl, VT, V1,
Craig Topper70b883b2011-11-28 10:14:51 +00006674 V2, getShuffleVPERM2X128Immediate(SVOp), DAG);
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006675
Craig Topper70b883b2011-11-28 10:14:51 +00006676 // Handle VSHUFPS/DY permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006677 if (isVSHUFPYMask(M, VT, HasAVX))
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006678 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
Craig Topper9d7025b2011-11-27 21:41:12 +00006679 getShuffleVSHUFPYImmediate(SVOp), DAG);
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006680
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006681 //===--------------------------------------------------------------------===//
6682 // Since no target specific shuffle was selected for this generic one,
6683 // lower it into other known shuffles. FIXME: this isn't true yet, but
6684 // this is the plan.
6685 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00006686
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006687 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
6688 if (VT == MVT::v8i16) {
6689 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
6690 if (NewOp.getNode())
6691 return NewOp;
6692 }
6693
6694 if (VT == MVT::v16i8) {
6695 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
6696 if (NewOp.getNode())
6697 return NewOp;
6698 }
6699
6700 // Handle all 128-bit wide vectors with 4 elements, and match them with
6701 // several different shuffle types.
6702 if (NumElems == 4 && VT.getSizeInBits() == 128)
6703 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
6704
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006705 // Handle general 256-bit shuffles
6706 if (VT.is256BitVector())
6707 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
6708
Dan Gohman475871a2008-07-27 21:46:04 +00006709 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006710}
6711
Dan Gohman475871a2008-07-27 21:46:04 +00006712SDValue
6713X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00006714 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006715 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006716 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006717
6718 if (Op.getOperand(0).getValueType().getSizeInBits() != 128)
6719 return SDValue();
6720
Duncan Sands83ec4b62008-06-06 12:08:01 +00006721 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006722 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006723 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006724 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006725 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006726 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006727 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00006728 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
6729 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
6730 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006731 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6732 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006733 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006734 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00006735 Op.getOperand(0)),
6736 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006737 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006738 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006739 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006740 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006741 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00006742 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00006743 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
6744 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006745 // result has a single use which is a store or a bitcast to i32. And in
6746 // the case of a store, it's not worth it if the index is a constant 0,
6747 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00006748 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00006749 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00006750 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006751 if ((User->getOpcode() != ISD::STORE ||
6752 (isa<ConstantSDNode>(Op.getOperand(1)) &&
6753 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006754 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00006755 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00006756 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00006757 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006758 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00006759 Op.getOperand(0)),
6760 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006761 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Pete Coopera77214a2011-11-14 19:38:42 +00006762 } else if (VT == MVT::i32 || VT == MVT::i64) {
6763 // ExtractPS/pextrq works with constant index.
Mon P Wangf0fcdd82009-01-15 21:10:20 +00006764 if (isa<ConstantSDNode>(Op.getOperand(1)))
6765 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006766 }
Dan Gohman475871a2008-07-27 21:46:04 +00006767 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006768}
6769
6770
Dan Gohman475871a2008-07-27 21:46:04 +00006771SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006772X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
6773 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006774 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00006775 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006776
David Greene74a579d2011-02-10 16:57:36 +00006777 SDValue Vec = Op.getOperand(0);
6778 EVT VecVT = Vec.getValueType();
6779
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006780 // If this is a 256-bit vector result, first extract the 128-bit vector and
6781 // then extract the element from the 128-bit vector.
6782 if (VecVT.getSizeInBits() == 256) {
David Greene74a579d2011-02-10 16:57:36 +00006783 DebugLoc dl = Op.getNode()->getDebugLoc();
6784 unsigned NumElems = VecVT.getVectorNumElements();
6785 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00006786 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
6787
6788 // Get the 128-bit vector.
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006789 bool Upper = IdxVal >= NumElems/2;
6790 Vec = Extract128BitVector(Vec,
6791 DAG.getConstant(Upper ? NumElems/2 : 0, MVT::i32), DAG, dl);
David Greene74a579d2011-02-10 16:57:36 +00006792
David Greene74a579d2011-02-10 16:57:36 +00006793 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006794 Upper ? DAG.getConstant(IdxVal-NumElems/2, MVT::i32) : Idx);
David Greene74a579d2011-02-10 16:57:36 +00006795 }
6796
6797 assert(Vec.getValueSizeInBits() <= 128 && "Unexpected vector length");
6798
Craig Topperc0d82852011-11-22 00:44:41 +00006799 if (Subtarget->hasSSE41orAVX()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006800 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006801 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00006802 return Res;
6803 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00006804
Owen Andersone50ed302009-08-10 22:56:29 +00006805 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006806 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006807 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006808 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00006809 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006810 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006811 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006812 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6813 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006814 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006815 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00006816 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006817 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00006818 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00006819 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006820 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00006821 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006822 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006823 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006824 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006825 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006826 if (Idx == 0)
6827 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00006828
Evan Cheng0db9fe62006-04-25 20:13:52 +00006829 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00006830 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006831 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006832 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006833 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006834 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006835 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00006836 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006837 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
6838 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
6839 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006840 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006841 if (Idx == 0)
6842 return Op;
6843
6844 // UNPCKHPD the element to the lowest double word, then movsd.
6845 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
6846 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00006847 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006848 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006849 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006850 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006851 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006852 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006853 }
6854
Dan Gohman475871a2008-07-27 21:46:04 +00006855 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006856}
6857
Dan Gohman475871a2008-07-27 21:46:04 +00006858SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006859X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
6860 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006861 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006862 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006863 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006864
Dan Gohman475871a2008-07-27 21:46:04 +00006865 SDValue N0 = Op.getOperand(0);
6866 SDValue N1 = Op.getOperand(1);
6867 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00006868
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006869 if (VT.getSizeInBits() == 256)
6870 return SDValue();
6871
Dan Gohman8a55ce42009-09-23 21:02:20 +00006872 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00006873 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006874 unsigned Opc;
6875 if (VT == MVT::v8i16)
6876 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006877 else if (VT == MVT::v16i8)
6878 Opc = X86ISD::PINSRB;
6879 else
6880 Opc = X86ISD::PINSRB;
6881
Nate Begeman14d12ca2008-02-11 04:19:36 +00006882 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
6883 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006884 if (N1.getValueType() != MVT::i32)
6885 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6886 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006887 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00006888 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006889 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006890 // Bits [7:6] of the constant are the source select. This will always be
6891 // zero here. The DAG Combiner may combine an extract_elt index into these
6892 // bits. For example (insert (extract, 3), 2) could be matched by putting
6893 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00006894 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00006895 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00006896 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00006897 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006898 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00006899 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00006900 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00006901 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Pete Coopera77214a2011-11-14 19:38:42 +00006902 } else if ((EltVT == MVT::i32 || EltVT == MVT::i64) &&
6903 isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00006904 // PINSR* works with constant index.
6905 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006906 }
Dan Gohman475871a2008-07-27 21:46:04 +00006907 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006908}
6909
Dan Gohman475871a2008-07-27 21:46:04 +00006910SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006911X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006912 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006913 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006914
David Greene6b381262011-02-09 15:32:06 +00006915 DebugLoc dl = Op.getDebugLoc();
6916 SDValue N0 = Op.getOperand(0);
6917 SDValue N1 = Op.getOperand(1);
6918 SDValue N2 = Op.getOperand(2);
6919
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006920 // If this is a 256-bit vector result, first extract the 128-bit vector,
6921 // insert the element into the extracted half and then place it back.
6922 if (VT.getSizeInBits() == 256) {
David Greene6b381262011-02-09 15:32:06 +00006923 if (!isa<ConstantSDNode>(N2))
6924 return SDValue();
6925
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006926 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00006927 unsigned NumElems = VT.getVectorNumElements();
6928 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006929 bool Upper = IdxVal >= NumElems/2;
6930 SDValue Ins128Idx = DAG.getConstant(Upper ? NumElems/2 : 0, MVT::i32);
6931 SDValue V = Extract128BitVector(N0, Ins128Idx, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00006932
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006933 // Insert the element into the desired half.
6934 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V,
6935 N1, Upper ? DAG.getConstant(IdxVal-NumElems/2, MVT::i32) : N2);
David Greene6b381262011-02-09 15:32:06 +00006936
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006937 // Insert the changed part back to the 256-bit vector
6938 return Insert128BitVector(N0, V, Ins128Idx, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00006939 }
6940
Craig Topperc0d82852011-11-22 00:44:41 +00006941 if (Subtarget->hasSSE41orAVX())
Nate Begeman14d12ca2008-02-11 04:19:36 +00006942 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
6943
Dan Gohman8a55ce42009-09-23 21:02:20 +00006944 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00006945 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00006946
Dan Gohman8a55ce42009-09-23 21:02:20 +00006947 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00006948 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
6949 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006950 if (N1.getValueType() != MVT::i32)
6951 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6952 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006953 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00006954 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006955 }
Dan Gohman475871a2008-07-27 21:46:04 +00006956 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006957}
6958
Dan Gohman475871a2008-07-27 21:46:04 +00006959SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006960X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00006961 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006962 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00006963 EVT OpVT = Op.getValueType();
6964
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00006965 // If this is a 256-bit vector result, first insert into a 128-bit
6966 // vector and then insert into the 256-bit vector.
6967 if (OpVT.getSizeInBits() > 128) {
6968 // Insert into a 128-bit vector.
6969 EVT VT128 = EVT::getVectorVT(*Context,
6970 OpVT.getVectorElementType(),
6971 OpVT.getVectorNumElements() / 2);
6972
6973 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
6974
6975 // Insert the 128-bit vector.
6976 return Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, OpVT), Op,
6977 DAG.getConstant(0, MVT::i32),
6978 DAG, dl);
6979 }
6980
Chris Lattnerf172ecd2010-07-04 23:07:25 +00006981 if (Op.getValueType() == MVT::v1i64 &&
6982 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00006983 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00006984
Owen Anderson825b72b2009-08-11 20:47:22 +00006985 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00006986 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
6987 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006988 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00006989 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006990}
6991
David Greene91585092011-01-26 15:38:49 +00006992// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
6993// a simple subregister reference or explicit instructions to grab
6994// upper bits of a vector.
6995SDValue
6996X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
6997 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00006998 DebugLoc dl = Op.getNode()->getDebugLoc();
6999 SDValue Vec = Op.getNode()->getOperand(0);
7000 SDValue Idx = Op.getNode()->getOperand(1);
7001
7002 if (Op.getNode()->getValueType(0).getSizeInBits() == 128
7003 && Vec.getNode()->getValueType(0).getSizeInBits() == 256) {
7004 return Extract128BitVector(Vec, Idx, DAG, dl);
7005 }
David Greene91585092011-01-26 15:38:49 +00007006 }
7007 return SDValue();
7008}
7009
David Greenecfe33c42011-01-26 19:13:22 +00007010// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
7011// simple superregister reference or explicit instructions to insert
7012// the upper bits of a vector.
7013SDValue
7014X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7015 if (Subtarget->hasAVX()) {
7016 DebugLoc dl = Op.getNode()->getDebugLoc();
7017 SDValue Vec = Op.getNode()->getOperand(0);
7018 SDValue SubVec = Op.getNode()->getOperand(1);
7019 SDValue Idx = Op.getNode()->getOperand(2);
7020
7021 if (Op.getNode()->getValueType(0).getSizeInBits() == 256
7022 && SubVec.getNode()->getValueType(0).getSizeInBits() == 128) {
David Greenea5f26012011-02-07 19:36:54 +00007023 return Insert128BitVector(Vec, SubVec, Idx, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00007024 }
7025 }
7026 return SDValue();
7027}
7028
Bill Wendling056292f2008-09-16 21:48:12 +00007029// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7030// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7031// one of the above mentioned nodes. It has to be wrapped because otherwise
7032// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7033// be used to form addressing mode. These wrapped nodes will be selected
7034// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00007035SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007036X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007037 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007038
Chris Lattner41621a22009-06-26 19:22:52 +00007039 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7040 // global base reg.
7041 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007042 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007043 CodeModel::Model M = getTargetMachine().getCodeModel();
7044
Chris Lattner4f066492009-07-11 20:29:19 +00007045 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007046 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007047 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007048 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007049 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007050 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007051 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007052
Evan Cheng1606e8e2009-03-13 07:51:59 +00007053 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00007054 CP->getAlignment(),
7055 CP->getOffset(), OpFlag);
7056 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00007057 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007058 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00007059 if (OpFlag) {
7060 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007061 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007062 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007063 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007064 }
7065
7066 return Result;
7067}
7068
Dan Gohmand858e902010-04-17 15:26:15 +00007069SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007070 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007071
Chris Lattner18c59872009-06-27 04:16:01 +00007072 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7073 // global base reg.
7074 unsigned char OpFlag = 0;
7075 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007076 CodeModel::Model M = getTargetMachine().getCodeModel();
7077
Chris Lattner4f066492009-07-11 20:29:19 +00007078 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007079 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007080 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007081 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007082 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007083 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007084 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007085
Chris Lattner18c59872009-06-27 04:16:01 +00007086 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7087 OpFlag);
7088 DebugLoc DL = JT->getDebugLoc();
7089 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007090
Chris Lattner18c59872009-06-27 04:16:01 +00007091 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00007092 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00007093 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7094 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007095 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007096 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007097
Chris Lattner18c59872009-06-27 04:16:01 +00007098 return Result;
7099}
7100
7101SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007102X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007103 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00007104
Chris Lattner18c59872009-06-27 04:16:01 +00007105 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7106 // global base reg.
7107 unsigned char OpFlag = 0;
7108 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007109 CodeModel::Model M = getTargetMachine().getCodeModel();
7110
Chris Lattner4f066492009-07-11 20:29:19 +00007111 if (Subtarget->isPICStyleRIPRel() &&
Eli Friedman586272d2011-08-11 01:48:05 +00007112 (M == CodeModel::Small || M == CodeModel::Kernel)) {
7113 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
7114 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00007115 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00007116 } else if (Subtarget->isPICStyleGOT()) {
7117 OpFlag = X86II::MO_GOT;
7118 } else if (Subtarget->isPICStyleStubPIC()) {
7119 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
7120 } else if (Subtarget->isPICStyleStubNoDynamic()) {
7121 OpFlag = X86II::MO_DARWIN_NONLAZY;
7122 }
Eric Christopherfd179292009-08-27 18:07:15 +00007123
Chris Lattner18c59872009-06-27 04:16:01 +00007124 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00007125
Chris Lattner18c59872009-06-27 04:16:01 +00007126 DebugLoc DL = Op.getDebugLoc();
7127 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007128
7129
Chris Lattner18c59872009-06-27 04:16:01 +00007130 // With PIC, the address is actually $g + Offset.
7131 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007132 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007133 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7134 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007135 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007136 Result);
7137 }
Eric Christopherfd179292009-08-27 18:07:15 +00007138
Eli Friedman586272d2011-08-11 01:48:05 +00007139 // For symbols that require a load from a stub to get the address, emit the
7140 // load.
7141 if (isGlobalStubReference(OpFlag))
7142 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007143 MachinePointerInfo::getGOT(), false, false, false, 0);
Eli Friedman586272d2011-08-11 01:48:05 +00007144
Chris Lattner18c59872009-06-27 04:16:01 +00007145 return Result;
7146}
7147
Dan Gohman475871a2008-07-27 21:46:04 +00007148SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007149X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007150 // Create the TargetBlockAddressAddress node.
7151 unsigned char OpFlags =
7152 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007153 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007154 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00007155 DebugLoc dl = Op.getDebugLoc();
7156 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
7157 /*isTarget=*/true, OpFlags);
7158
Dan Gohmanf705adb2009-10-30 01:28:02 +00007159 if (Subtarget->isPICStyleRIPRel() &&
7160 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007161 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7162 else
7163 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007164
Dan Gohman29cbade2009-11-20 23:18:13 +00007165 // With PIC, the address is actually $g + Offset.
7166 if (isGlobalRelativeToPICBase(OpFlags)) {
7167 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7168 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7169 Result);
7170 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007171
7172 return Result;
7173}
7174
7175SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00007176X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00007177 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00007178 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007179 // Create the TargetGlobalAddress node, folding in the constant
7180 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007181 unsigned char OpFlags =
7182 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007183 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007184 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007185 if (OpFlags == X86II::MO_NO_FLAG &&
7186 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007187 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007188 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007189 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007190 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007191 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007192 }
Eric Christopherfd179292009-08-27 18:07:15 +00007193
Chris Lattner4f066492009-07-11 20:29:19 +00007194 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007195 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007196 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7197 else
7198 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007199
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007200 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007201 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007202 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7203 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007204 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007205 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007206
Chris Lattner36c25012009-07-10 07:34:39 +00007207 // For globals that require a load from a stub to get the address, emit the
7208 // load.
7209 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007210 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007211 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007212
Dan Gohman6520e202008-10-18 02:06:02 +00007213 // If there was a non-zero offset that we didn't fold, create an explicit
7214 // addition for it.
7215 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007216 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007217 DAG.getConstant(Offset, getPointerTy()));
7218
Evan Cheng0db9fe62006-04-25 20:13:52 +00007219 return Result;
7220}
7221
Evan Chengda43bcf2008-09-24 00:05:32 +00007222SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007223X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007224 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007225 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007226 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007227}
7228
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007229static SDValue
7230GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007231 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00007232 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007233 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007234 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007235 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007236 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007237 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007238 GA->getOffset(),
7239 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007240 if (InFlag) {
7241 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00007242 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007243 } else {
7244 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00007245 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007246 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007247
7248 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007249 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007250
Rafael Espindola15f1b662009-04-24 12:59:40 +00007251 SDValue Flag = Chain.getValue(1);
7252 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007253}
7254
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007255// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007256static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007257LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007258 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007259 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00007260 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
7261 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007262 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007263 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007264 InFlag = Chain.getValue(1);
7265
Chris Lattnerb903bed2009-06-26 21:20:29 +00007266 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007267}
7268
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007269// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007270static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007271LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007272 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007273 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7274 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007275}
7276
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007277// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
7278// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007279static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007280 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00007281 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007282 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00007283
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007284 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
7285 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
7286 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00007287
Michael J. Spencerec38de22010-10-10 22:04:20 +00007288 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007289 DAG.getIntPtrConstant(0),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007290 MachinePointerInfo(Ptr),
7291 false, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00007292
Chris Lattnerb903bed2009-06-26 21:20:29 +00007293 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007294 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
7295 // initialexec.
7296 unsigned WrapperKind = X86ISD::Wrapper;
7297 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007298 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00007299 } else if (is64Bit) {
7300 assert(model == TLSModel::InitialExec);
7301 OperandFlags = X86II::MO_GOTTPOFF;
7302 WrapperKind = X86ISD::WrapperRIP;
7303 } else {
7304 assert(model == TLSModel::InitialExec);
7305 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00007306 }
Eric Christopherfd179292009-08-27 18:07:15 +00007307
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007308 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
7309 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00007310 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00007311 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007312 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007313 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007314
Rafael Espindola9a580232009-02-27 13:37:18 +00007315 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007316 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007317 MachinePointerInfo::getGOT(), false, false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007318
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007319 // The address of the thread local variable is the add of the thread
7320 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00007321 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007322}
7323
Dan Gohman475871a2008-07-27 21:46:04 +00007324SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007325X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00007326
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007327 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00007328 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00007329
Eric Christopher30ef0e52010-06-03 04:07:48 +00007330 if (Subtarget->isTargetELF()) {
7331 // TODO: implement the "local dynamic" model
7332 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00007333
Eric Christopher30ef0e52010-06-03 04:07:48 +00007334 // If GV is an alias then use the aliasee for determining
7335 // thread-localness.
7336 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
7337 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007338
7339 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00007340 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00007341
Eric Christopher30ef0e52010-06-03 04:07:48 +00007342 switch (model) {
7343 case TLSModel::GeneralDynamic:
7344 case TLSModel::LocalDynamic: // not implemented
7345 if (Subtarget->is64Bit())
7346 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
7347 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00007348
Eric Christopher30ef0e52010-06-03 04:07:48 +00007349 case TLSModel::InitialExec:
7350 case TLSModel::LocalExec:
7351 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
7352 Subtarget->is64Bit());
7353 }
7354 } else if (Subtarget->isTargetDarwin()) {
7355 // Darwin only has one model of TLS. Lower to that.
7356 unsigned char OpFlag = 0;
7357 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7358 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007359
Eric Christopher30ef0e52010-06-03 04:07:48 +00007360 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7361 // global base reg.
7362 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7363 !Subtarget->is64Bit();
7364 if (PIC32)
7365 OpFlag = X86II::MO_TLVP_PIC_BASE;
7366 else
7367 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007368 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007369 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00007370 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007371 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007372 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007373
Eric Christopher30ef0e52010-06-03 04:07:48 +00007374 // With PIC32, the address is actually $g + Offset.
7375 if (PIC32)
7376 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7377 DAG.getNode(X86ISD::GlobalBaseReg,
7378 DebugLoc(), getPointerTy()),
7379 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007380
Eric Christopher30ef0e52010-06-03 04:07:48 +00007381 // Lowering the machine isd will make sure everything is in the right
7382 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007383 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007384 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00007385 SDValue Args[] = { Chain, Offset };
7386 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007387
Eric Christopher30ef0e52010-06-03 04:07:48 +00007388 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7389 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7390 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007391
Eric Christopher30ef0e52010-06-03 04:07:48 +00007392 // And our return value (tls address) is in the standard call return value
7393 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007394 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Evan Chengfd230df2011-10-19 22:22:54 +00007395 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(),
7396 Chain.getValue(1));
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007397 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007398
Eric Christopher30ef0e52010-06-03 04:07:48 +00007399 assert(false &&
7400 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00007401
Torok Edwinc23197a2009-07-14 16:55:14 +00007402 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00007403 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007404}
7405
Evan Cheng0db9fe62006-04-25 20:13:52 +00007406
Nadav Rotem43012222011-05-11 08:12:09 +00007407/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00007408/// take a 2 x i32 value to shift plus a shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +00007409SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00007410 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00007411 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007412 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007413 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007414 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00007415 SDValue ShOpLo = Op.getOperand(0);
7416 SDValue ShOpHi = Op.getOperand(1);
7417 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00007418 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00007419 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00007420 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00007421
Dan Gohman475871a2008-07-27 21:46:04 +00007422 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007423 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007424 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
7425 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007426 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007427 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
7428 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007429 }
Evan Chenge3413162006-01-09 18:33:28 +00007430
Owen Anderson825b72b2009-08-11 20:47:22 +00007431 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
7432 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00007433 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00007434 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00007435
Dan Gohman475871a2008-07-27 21:46:04 +00007436 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00007437 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00007438 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
7439 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00007440
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007441 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007442 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7443 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007444 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007445 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7446 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007447 }
7448
Dan Gohman475871a2008-07-27 21:46:04 +00007449 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00007450 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007451}
Evan Chenga3195e82006-01-12 22:54:21 +00007452
Dan Gohmand858e902010-04-17 15:26:15 +00007453SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
7454 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007455 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00007456
Dale Johannesen0488fb62010-09-30 23:57:10 +00007457 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007458 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007459
Owen Anderson825b72b2009-08-11 20:47:22 +00007460 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00007461 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007462
Eli Friedman36df4992009-05-27 00:47:34 +00007463 // These are really Legal; return the operand so the caller accepts it as
7464 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007465 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00007466 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00007467 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00007468 Subtarget->is64Bit()) {
7469 return Op;
7470 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007471
Bruno Cardoso Lopesa511b8e2011-08-09 17:39:01 +00007472 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007473 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007474 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00007475 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007476 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00007477 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00007478 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007479 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007480 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007481 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
7482}
Evan Cheng0db9fe62006-04-25 20:13:52 +00007483
Owen Andersone50ed302009-08-10 22:56:29 +00007484SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007485 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00007486 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007487 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00007488 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00007489 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00007490 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007491 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007492 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00007493 else
Owen Anderson825b72b2009-08-11 20:47:22 +00007494 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007495
Chris Lattner492a43e2010-09-22 01:28:21 +00007496 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007497
Stuart Hastings84be9582011-06-02 15:57:11 +00007498 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
7499 MachineMemOperand *MMO;
7500 if (FI) {
7501 int SSFI = FI->getIndex();
7502 MMO =
7503 DAG.getMachineFunction()
7504 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7505 MachineMemOperand::MOLoad, ByteSize, ByteSize);
7506 } else {
7507 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
7508 StackSlot = StackSlot.getOperand(1);
7509 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007510 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007511 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
7512 X86ISD::FILD, DL,
7513 Tys, Ops, array_lengthof(Ops),
7514 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007515
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007516 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007517 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00007518 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007519
7520 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
7521 // shouldn't be necessary except that RFP cannot be live across
7522 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007523 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007524 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
7525 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007526 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00007527 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007528 SDValue Ops[] = {
7529 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
7530 };
Chris Lattner492a43e2010-09-22 01:28:21 +00007531 MachineMemOperand *MMO =
7532 DAG.getMachineFunction()
7533 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007534 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007535
Chris Lattner492a43e2010-09-22 01:28:21 +00007536 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
7537 Ops, array_lengthof(Ops),
7538 Op.getValueType(), MMO);
7539 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007540 MachinePointerInfo::getFixedStack(SSFI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007541 false, false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007542 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007543
Evan Cheng0db9fe62006-04-25 20:13:52 +00007544 return Result;
7545}
7546
Bill Wendling8b8a6362009-01-17 03:56:04 +00007547// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007548SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
7549 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00007550 // This algorithm is not obvious. Here it is in C code, more or less:
7551 /*
7552 double uint64_to_double( uint32_t hi, uint32_t lo ) {
7553 static const __m128i exp = { 0x4330000045300000ULL, 0 };
7554 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00007555
Bill Wendling8b8a6362009-01-17 03:56:04 +00007556 // Copy ints to xmm registers.
7557 __m128i xh = _mm_cvtsi32_si128( hi );
7558 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00007559
Bill Wendling8b8a6362009-01-17 03:56:04 +00007560 // Combine into low half of a single xmm register.
7561 __m128i x = _mm_unpacklo_epi32( xh, xl );
7562 __m128d d;
7563 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00007564
Bill Wendling8b8a6362009-01-17 03:56:04 +00007565 // Merge in appropriate exponents to give the integer bits the right
7566 // magnitude.
7567 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00007568
Bill Wendling8b8a6362009-01-17 03:56:04 +00007569 // Subtract away the biases to deal with the IEEE-754 double precision
7570 // implicit 1.
7571 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00007572
Bill Wendling8b8a6362009-01-17 03:56:04 +00007573 // All conversions up to here are exact. The correctly rounded result is
7574 // calculated using the current rounding mode using the following
7575 // horizontal add.
7576 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
7577 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
7578 // store doesn't really need to be here (except
7579 // maybe to zero the other double)
7580 return sd;
7581 }
7582 */
Dale Johannesen040225f2008-10-21 23:07:49 +00007583
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007584 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00007585 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00007586
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007587 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00007588 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00007589 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
7590 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
7591 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
7592 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00007593 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007594 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007595
Bill Wendling8b8a6362009-01-17 03:56:04 +00007596 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00007597 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007598 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00007599 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007600 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00007601 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007602 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007603
Owen Anderson825b72b2009-08-11 20:47:22 +00007604 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7605 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00007606 Op.getOperand(0),
7607 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007608 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7609 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00007610 Op.getOperand(0),
7611 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007612 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
7613 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00007614 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007615 false, false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00007616 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007617 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
Owen Anderson825b72b2009-08-11 20:47:22 +00007618 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00007619 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007620 false, false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00007621 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007622
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007623 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00007624 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00007625 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
7626 DAG.getUNDEF(MVT::v2f64), ShufMask);
7627 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
7628 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007629 DAG.getIntPtrConstant(0));
7630}
7631
Bill Wendling8b8a6362009-01-17 03:56:04 +00007632// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007633SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
7634 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007635 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007636 // FP constant to bias correct the final result.
7637 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00007638 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007639
7640 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00007641 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00007642 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007643
Eli Friedmanf3704762011-08-29 21:15:46 +00007644 // Zero out the upper parts of the register.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00007645 Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget->hasXMMInt(),
7646 DAG);
Eli Friedmanf3704762011-08-29 21:15:46 +00007647
Owen Anderson825b72b2009-08-11 20:47:22 +00007648 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007649 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007650 DAG.getIntPtrConstant(0));
7651
7652 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007653 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007654 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007655 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007656 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007657 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007658 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007659 MVT::v2f64, Bias)));
7660 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007661 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007662 DAG.getIntPtrConstant(0));
7663
7664 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007665 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007666
7667 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00007668 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00007669
Owen Anderson825b72b2009-08-11 20:47:22 +00007670 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007671 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00007672 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007673 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007674 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00007675 }
7676
7677 // Handle final rounding.
7678 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00007679}
7680
Dan Gohmand858e902010-04-17 15:26:15 +00007681SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
7682 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00007683 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007684 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007685
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007686 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00007687 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
7688 // the optimization here.
7689 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00007690 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00007691
Owen Andersone50ed302009-08-10 22:56:29 +00007692 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007693 EVT DstVT = Op.getValueType();
7694 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007695 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007696 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007697 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00007698
7699 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00007700 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007701 if (SrcVT == MVT::i32) {
7702 SDValue WordOff = DAG.getConstant(4, getPointerTy());
7703 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
7704 getPointerTy(), StackSlot, WordOff);
7705 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007706 StackSlot, MachinePointerInfo(),
7707 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007708 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007709 OffsetSlot, MachinePointerInfo(),
7710 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007711 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
7712 return Fild;
7713 }
7714
7715 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
7716 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007717 StackSlot, MachinePointerInfo(),
7718 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007719 // For i64 source, we need to add the appropriate power of 2 if the input
7720 // was negative. This is the same as the optimization in
7721 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
7722 // we must be careful to do the computation in x87 extended precision, not
7723 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00007724 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
7725 MachineMemOperand *MMO =
7726 DAG.getMachineFunction()
7727 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7728 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007729
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007730 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
7731 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007732 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
7733 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007734
7735 APInt FF(32, 0x5F800000ULL);
7736
7737 // Check whether the sign bit is set.
7738 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
7739 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
7740 ISD::SETLT);
7741
7742 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
7743 SDValue FudgePtr = DAG.getConstantPool(
7744 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
7745 getPointerTy());
7746
7747 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
7748 SDValue Zero = DAG.getIntPtrConstant(0);
7749 SDValue Four = DAG.getIntPtrConstant(4);
7750 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
7751 Zero, Four);
7752 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
7753
7754 // Load the value out, extending it from f32 to f80.
7755 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00007756 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00007757 FudgePtr, MachinePointerInfo::getConstantPool(),
7758 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007759 // Extend everything to 80 bits to force it to be done on x87.
7760 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
7761 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007762}
7763
Dan Gohman475871a2008-07-27 21:46:04 +00007764std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00007765FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00007766 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00007767
Owen Andersone50ed302009-08-10 22:56:29 +00007768 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00007769
7770 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007771 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
7772 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00007773 }
7774
Owen Anderson825b72b2009-08-11 20:47:22 +00007775 assert(DstTy.getSimpleVT() <= MVT::i64 &&
7776 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00007777 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00007778
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007779 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007780 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00007781 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007782 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00007783 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00007784 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00007785 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007786 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007787
Evan Cheng87c89352007-10-15 20:11:21 +00007788 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
7789 // stack slot.
7790 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00007791 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00007792 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007793 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00007794
Michael J. Spencerec38de22010-10-10 22:04:20 +00007795
7796
Evan Cheng0db9fe62006-04-25 20:13:52 +00007797 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00007798 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007799 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007800 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
7801 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
7802 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007803 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007804
Dan Gohman475871a2008-07-27 21:46:04 +00007805 SDValue Chain = DAG.getEntryNode();
7806 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00007807 EVT TheVT = Op.getOperand(0).getValueType();
7808 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007809 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00007810 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007811 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007812 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007813 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00007814 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00007815 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00007816 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00007817
Chris Lattner492a43e2010-09-22 01:28:21 +00007818 MachineMemOperand *MMO =
7819 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7820 MachineMemOperand::MOLoad, MemSize, MemSize);
7821 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
7822 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007823 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00007824 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007825 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
7826 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007827
Chris Lattner07290932010-09-22 01:05:16 +00007828 MachineMemOperand *MMO =
7829 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7830 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007831
Evan Cheng0db9fe62006-04-25 20:13:52 +00007832 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00007833 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00007834 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
7835 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00007836
Chris Lattner27a6c732007-11-24 07:07:01 +00007837 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007838}
7839
Dan Gohmand858e902010-04-17 15:26:15 +00007840SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
7841 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00007842 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007843 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007844
Eli Friedman948e95a2009-05-23 09:59:16 +00007845 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00007846 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00007847 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
7848 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00007849
Chris Lattner27a6c732007-11-24 07:07:01 +00007850 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007851 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007852 FIST, StackSlot, MachinePointerInfo(),
7853 false, false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00007854}
7855
Dan Gohmand858e902010-04-17 15:26:15 +00007856SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
7857 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00007858 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
7859 SDValue FIST = Vals.first, StackSlot = Vals.second;
7860 assert(FIST.getNode() && "Unexpected failure");
7861
7862 // Load the result.
7863 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007864 FIST, StackSlot, MachinePointerInfo(),
7865 false, false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007866}
7867
Dan Gohmand858e902010-04-17 15:26:15 +00007868SDValue X86TargetLowering::LowerFABS(SDValue Op,
7869 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007870 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007871 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007872 EVT VT = Op.getValueType();
7873 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007874 if (VT.isVector())
7875 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007876 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007877 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007878 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00007879 CV.push_back(C);
7880 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007881 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007882 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00007883 CV.push_back(C);
7884 CV.push_back(C);
7885 CV.push_back(C);
7886 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007887 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007888 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007889 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007890 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007891 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007892 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007893 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007894}
7895
Dan Gohmand858e902010-04-17 15:26:15 +00007896SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007897 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007898 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007899 EVT VT = Op.getValueType();
7900 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00007901 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00007902 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007903 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007904 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007905 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00007906 CV.push_back(C);
7907 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007908 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007909 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00007910 CV.push_back(C);
7911 CV.push_back(C);
7912 CV.push_back(C);
7913 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007914 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007915 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007916 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007917 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007918 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007919 false, false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007920 if (VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007921 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007922 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007923 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007924 Op.getOperand(0)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007925 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00007926 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007927 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00007928 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00007929}
7930
Dan Gohmand858e902010-04-17 15:26:15 +00007931SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007932 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00007933 SDValue Op0 = Op.getOperand(0);
7934 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007935 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007936 EVT VT = Op.getValueType();
7937 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00007938
7939 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007940 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007941 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00007942 SrcVT = VT;
7943 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007944 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007945 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007946 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007947 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007948 }
7949
7950 // At this point the operands and the result should have the same
7951 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00007952
Evan Cheng68c47cb2007-01-05 07:55:56 +00007953 // First get the sign bit of second operand.
7954 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007955 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007956 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
7957 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007958 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007959 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
7960 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7961 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7962 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007963 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007964 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007965 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007966 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007967 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007968 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007969 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007970
7971 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007972 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007973 // Op0 is MVT::f32, Op1 is MVT::f64.
7974 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
7975 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
7976 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007977 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00007978 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00007979 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007980 }
7981
Evan Cheng73d6cf12007-01-05 21:37:56 +00007982 // Clear first operand sign bit.
7983 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00007984 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007985 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
7986 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007987 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007988 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
7989 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7990 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7991 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007992 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007993 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007994 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007995 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007996 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007997 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007998 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00007999
8000 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00008001 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008002}
8003
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008004SDValue X86TargetLowering::LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) const {
8005 SDValue N0 = Op.getOperand(0);
8006 DebugLoc dl = Op.getDebugLoc();
8007 EVT VT = Op.getValueType();
8008
8009 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
8010 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
8011 DAG.getConstant(1, VT));
8012 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
8013}
8014
Dan Gohman076aee32009-03-04 19:44:21 +00008015/// Emit nodes that will be selected as "test Op0,Op0", or something
8016/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008017SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008018 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008019 DebugLoc dl = Op.getDebugLoc();
8020
Dan Gohman31125812009-03-07 01:58:32 +00008021 // CF and OF aren't always set the way we want. Determine which
8022 // of these we need.
8023 bool NeedCF = false;
8024 bool NeedOF = false;
8025 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008026 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00008027 case X86::COND_A: case X86::COND_AE:
8028 case X86::COND_B: case X86::COND_BE:
8029 NeedCF = true;
8030 break;
8031 case X86::COND_G: case X86::COND_GE:
8032 case X86::COND_L: case X86::COND_LE:
8033 case X86::COND_O: case X86::COND_NO:
8034 NeedOF = true;
8035 break;
Dan Gohman31125812009-03-07 01:58:32 +00008036 }
8037
Dan Gohman076aee32009-03-04 19:44:21 +00008038 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00008039 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
8040 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008041 if (Op.getResNo() != 0 || NeedOF || NeedCF)
8042 // Emit a CMP with 0, which is the TEST pattern.
8043 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8044 DAG.getConstant(0, Op.getValueType()));
8045
8046 unsigned Opcode = 0;
8047 unsigned NumOperands = 0;
8048 switch (Op.getNode()->getOpcode()) {
8049 case ISD::ADD:
8050 // Due to an isel shortcoming, be conservative if this add is likely to be
8051 // selected as part of a load-modify-store instruction. When the root node
8052 // in a match is a store, isel doesn't know how to remap non-chain non-flag
8053 // uses of other nodes in the match, such as the ADD in this case. This
8054 // leads to the ADD being left around and reselected, with the result being
8055 // two adds in the output. Alas, even if none our users are stores, that
8056 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
8057 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
8058 // climbing the DAG back to the root, and it doesn't seem to be worth the
8059 // effort.
8060 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Pete Cooper2d496892011-11-15 21:57:53 +00008061 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8062 if (UI->getOpcode() != ISD::CopyToReg &&
8063 UI->getOpcode() != ISD::SETCC &&
8064 UI->getOpcode() != ISD::STORE)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008065 goto default_case;
8066
8067 if (ConstantSDNode *C =
8068 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
8069 // An add of one will be selected as an INC.
8070 if (C->getAPIntValue() == 1) {
8071 Opcode = X86ISD::INC;
8072 NumOperands = 1;
8073 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00008074 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008075
8076 // An add of negative one (subtract of one) will be selected as a DEC.
8077 if (C->getAPIntValue().isAllOnesValue()) {
8078 Opcode = X86ISD::DEC;
8079 NumOperands = 1;
8080 break;
8081 }
Dan Gohman076aee32009-03-04 19:44:21 +00008082 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008083
8084 // Otherwise use a regular EFLAGS-setting add.
8085 Opcode = X86ISD::ADD;
8086 NumOperands = 2;
8087 break;
8088 case ISD::AND: {
8089 // If the primary and result isn't used, don't bother using X86ISD::AND,
8090 // because a TEST instruction will be better.
8091 bool NonFlagUse = false;
8092 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8093 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
8094 SDNode *User = *UI;
8095 unsigned UOpNo = UI.getOperandNo();
8096 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
8097 // Look pass truncate.
8098 UOpNo = User->use_begin().getOperandNo();
8099 User = *User->use_begin();
8100 }
8101
8102 if (User->getOpcode() != ISD::BRCOND &&
8103 User->getOpcode() != ISD::SETCC &&
8104 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
8105 NonFlagUse = true;
8106 break;
8107 }
Dan Gohman076aee32009-03-04 19:44:21 +00008108 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008109
8110 if (!NonFlagUse)
8111 break;
8112 }
8113 // FALL THROUGH
8114 case ISD::SUB:
8115 case ISD::OR:
8116 case ISD::XOR:
8117 // Due to the ISEL shortcoming noted above, be conservative if this op is
8118 // likely to be selected as part of a load-modify-store instruction.
8119 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8120 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8121 if (UI->getOpcode() == ISD::STORE)
8122 goto default_case;
8123
8124 // Otherwise use a regular EFLAGS-setting instruction.
8125 switch (Op.getNode()->getOpcode()) {
8126 default: llvm_unreachable("unexpected operator!");
8127 case ISD::SUB: Opcode = X86ISD::SUB; break;
8128 case ISD::OR: Opcode = X86ISD::OR; break;
8129 case ISD::XOR: Opcode = X86ISD::XOR; break;
8130 case ISD::AND: Opcode = X86ISD::AND; break;
8131 }
8132
8133 NumOperands = 2;
8134 break;
8135 case X86ISD::ADD:
8136 case X86ISD::SUB:
8137 case X86ISD::INC:
8138 case X86ISD::DEC:
8139 case X86ISD::OR:
8140 case X86ISD::XOR:
8141 case X86ISD::AND:
8142 return SDValue(Op.getNode(), 1);
8143 default:
8144 default_case:
8145 break;
Dan Gohman076aee32009-03-04 19:44:21 +00008146 }
8147
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008148 if (Opcode == 0)
8149 // Emit a CMP with 0, which is the TEST pattern.
8150 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8151 DAG.getConstant(0, Op.getValueType()));
8152
8153 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
8154 SmallVector<SDValue, 4> Ops;
8155 for (unsigned i = 0; i != NumOperands; ++i)
8156 Ops.push_back(Op.getOperand(i));
8157
8158 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
8159 DAG.ReplaceAllUsesWith(Op, New);
8160 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00008161}
8162
8163/// Emit nodes that will be selected as "cmp Op0,Op1", or something
8164/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008165SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008166 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008167 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
8168 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00008169 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00008170
8171 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00008172 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00008173}
8174
Evan Chengd40d03e2010-01-06 19:38:29 +00008175/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
8176/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00008177SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
8178 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008179 SDValue Op0 = And.getOperand(0);
8180 SDValue Op1 = And.getOperand(1);
8181 if (Op0.getOpcode() == ISD::TRUNCATE)
8182 Op0 = Op0.getOperand(0);
8183 if (Op1.getOpcode() == ISD::TRUNCATE)
8184 Op1 = Op1.getOperand(0);
8185
Evan Chengd40d03e2010-01-06 19:38:29 +00008186 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008187 if (Op1.getOpcode() == ISD::SHL)
8188 std::swap(Op0, Op1);
8189 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008190 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
8191 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008192 // If we looked past a truncate, check that it's only truncating away
8193 // known zeros.
8194 unsigned BitWidth = Op0.getValueSizeInBits();
8195 unsigned AndBitWidth = And.getValueSizeInBits();
8196 if (BitWidth > AndBitWidth) {
8197 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
8198 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
8199 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
8200 return SDValue();
8201 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008202 LHS = Op1;
8203 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00008204 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008205 } else if (Op1.getOpcode() == ISD::Constant) {
8206 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
Benjamin Kramerf238f502011-11-23 13:54:17 +00008207 uint64_t AndRHSVal = AndRHS->getZExtValue();
Evan Cheng2c755ba2010-02-27 07:36:59 +00008208 SDValue AndLHS = Op0;
Benjamin Kramerf238f502011-11-23 13:54:17 +00008209
8210 if (AndRHSVal == 1 && AndLHS.getOpcode() == ISD::SRL) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008211 LHS = AndLHS.getOperand(0);
8212 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008213 }
Benjamin Kramerf238f502011-11-23 13:54:17 +00008214
8215 // Use BT if the immediate can't be encoded in a TEST instruction.
8216 if (!isUInt<32>(AndRHSVal) && isPowerOf2_64(AndRHSVal)) {
8217 LHS = AndLHS;
8218 RHS = DAG.getConstant(Log2_64_Ceil(AndRHSVal), LHS.getValueType());
8219 }
Evan Chengd40d03e2010-01-06 19:38:29 +00008220 }
Evan Cheng0488db92007-09-25 01:57:46 +00008221
Evan Chengd40d03e2010-01-06 19:38:29 +00008222 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00008223 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00008224 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00008225 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00008226 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00008227 // Also promote i16 to i32 for performance / code size reason.
8228 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00008229 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00008230 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00008231
Evan Chengd40d03e2010-01-06 19:38:29 +00008232 // If the operand types disagree, extend the shift amount to match. Since
8233 // BT ignores high bits (like shifts) we can use anyextend.
8234 if (LHS.getValueType() != RHS.getValueType())
8235 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008236
Evan Chengd40d03e2010-01-06 19:38:29 +00008237 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
8238 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
8239 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8240 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00008241 }
8242
Evan Cheng54de3ea2010-01-05 06:52:31 +00008243 return SDValue();
8244}
8245
Dan Gohmand858e902010-04-17 15:26:15 +00008246SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Duncan Sands28b77e92011-09-06 19:07:46 +00008247
8248 if (Op.getValueType().isVector()) return LowerVSETCC(Op, DAG);
8249
Evan Cheng54de3ea2010-01-05 06:52:31 +00008250 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
8251 SDValue Op0 = Op.getOperand(0);
8252 SDValue Op1 = Op.getOperand(1);
8253 DebugLoc dl = Op.getDebugLoc();
8254 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
8255
8256 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00008257 // Lower (X & (1 << N)) == 0 to BT(X, N).
8258 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
8259 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00008260 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008261 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00008262 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008263 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
8264 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
8265 if (NewSetCC.getNode())
8266 return NewSetCC;
8267 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00008268
Chris Lattner481eebc2010-12-19 21:23:48 +00008269 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
8270 // these.
8271 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00008272 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00008273 cast<ConstantSDNode>(Op1)->isNullValue()) &&
8274 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008275
Chris Lattner481eebc2010-12-19 21:23:48 +00008276 // If the input is a setcc, then reuse the input setcc or use a new one with
8277 // the inverted condition.
8278 if (Op0.getOpcode() == X86ISD::SETCC) {
8279 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
8280 bool Invert = (CC == ISD::SETNE) ^
8281 cast<ConstantSDNode>(Op1)->isNullValue();
8282 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008283
Evan Cheng2c755ba2010-02-27 07:36:59 +00008284 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00008285 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8286 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
8287 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008288 }
8289
Evan Chenge5b51ac2010-04-17 06:13:15 +00008290 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00008291 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008292 if (X86CC == X86::COND_INVALID)
8293 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008294
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008295 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00008296 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008297 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00008298}
8299
Craig Topper89af15e2011-09-18 08:03:58 +00008300// Lower256IntVSETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008301// ones, and then concatenate the result back.
Craig Topper89af15e2011-09-18 08:03:58 +00008302static SDValue Lower256IntVSETCC(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008303 EVT VT = Op.getValueType();
8304
Duncan Sands28b77e92011-09-06 19:07:46 +00008305 assert(VT.getSizeInBits() == 256 && Op.getOpcode() == ISD::SETCC &&
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008306 "Unsupported value type for operation");
8307
8308 int NumElems = VT.getVectorNumElements();
8309 DebugLoc dl = Op.getDebugLoc();
8310 SDValue CC = Op.getOperand(2);
8311 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
8312 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
8313
8314 // Extract the LHS vectors
8315 SDValue LHS = Op.getOperand(0);
8316 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
8317 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
8318
8319 // Extract the RHS vectors
8320 SDValue RHS = Op.getOperand(1);
8321 SDValue RHS1 = Extract128BitVector(RHS, Idx0, DAG, dl);
8322 SDValue RHS2 = Extract128BitVector(RHS, Idx1, DAG, dl);
8323
8324 // Issue the operation on the smaller types and concatenate the result back
8325 MVT EltVT = VT.getVectorElementType().getSimpleVT();
8326 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
8327 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
8328 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
8329 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
8330}
8331
8332
Dan Gohmand858e902010-04-17 15:26:15 +00008333SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008334 SDValue Cond;
8335 SDValue Op0 = Op.getOperand(0);
8336 SDValue Op1 = Op.getOperand(1);
8337 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00008338 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00008339 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
8340 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008341 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00008342
8343 if (isFP) {
8344 unsigned SSECC = 8;
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008345 EVT EltVT = Op0.getValueType().getVectorElementType();
8346 assert(EltVT == MVT::f32 || EltVT == MVT::f64);
8347
8348 unsigned Opc = EltVT == MVT::f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00008349 bool Swap = false;
8350
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008351 // SSE Condition code mapping:
8352 // 0 - EQ
8353 // 1 - LT
8354 // 2 - LE
8355 // 3 - UNORD
8356 // 4 - NEQ
8357 // 5 - NLT
8358 // 6 - NLE
8359 // 7 - ORD
Nate Begeman30a0de92008-07-17 16:51:19 +00008360 switch (SetCCOpcode) {
8361 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008362 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00008363 case ISD::SETEQ: SSECC = 0; break;
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008364 case ISD::SETOGT:
8365 case ISD::SETGT: Swap = true; // Fallthrough
Bruno Cardoso Lopes457d53d2011-09-12 21:24:07 +00008366 case ISD::SETLT:
8367 case ISD::SETOLT: SSECC = 1; break;
8368 case ISD::SETOGE:
8369 case ISD::SETGE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008370 case ISD::SETLE:
8371 case ISD::SETOLE: SSECC = 2; break;
8372 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008373 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00008374 case ISD::SETNE: SSECC = 4; break;
8375 case ISD::SETULE: Swap = true;
8376 case ISD::SETUGE: SSECC = 5; break;
8377 case ISD::SETULT: Swap = true;
8378 case ISD::SETUGT: SSECC = 6; break;
8379 case ISD::SETO: SSECC = 7; break;
8380 }
8381 if (Swap)
8382 std::swap(Op0, Op1);
8383
Nate Begemanfb8ead02008-07-25 19:05:58 +00008384 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00008385 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00008386 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00008387 SDValue UNORD, EQ;
Bruno Cardoso Lopes809f17f2011-09-13 19:33:00 +00008388 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
8389 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00008390 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Craig Topper0a150352011-11-09 08:06:13 +00008391 } else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00008392 SDValue ORD, NEQ;
Bruno Cardoso Lopes809f17f2011-09-13 19:33:00 +00008393 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
8394 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00008395 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00008396 }
Torok Edwinc23197a2009-07-14 16:55:14 +00008397 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00008398 }
8399 // Handle all other FP comparisons here.
Bruno Cardoso Lopes809f17f2011-09-13 19:33:00 +00008400 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00008401 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008402
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008403 // Break 256-bit integer vector compare into smaller ones.
Craig Topper0a150352011-11-09 08:06:13 +00008404 if (VT.getSizeInBits() == 256 && !Subtarget->hasAVX2())
Craig Topper89af15e2011-09-18 08:03:58 +00008405 return Lower256IntVSETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008406
Nate Begeman30a0de92008-07-17 16:51:19 +00008407 // We are handling one of the integer comparisons here. Since SSE only has
8408 // GT and EQ comparisons for integer, swapping operands and multiple
8409 // operations may be required for some comparisons.
8410 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
8411 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00008412
Craig Topper0a150352011-11-09 08:06:13 +00008413 switch (VT.getVectorElementType().getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00008414 default: break;
Craig Topper0a150352011-11-09 08:06:13 +00008415 case MVT::i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
8416 case MVT::i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
8417 case MVT::i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
8418 case MVT::i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008419 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008420
Nate Begeman30a0de92008-07-17 16:51:19 +00008421 switch (SetCCOpcode) {
8422 default: break;
8423 case ISD::SETNE: Invert = true;
8424 case ISD::SETEQ: Opc = EQOpc; break;
8425 case ISD::SETLT: Swap = true;
8426 case ISD::SETGT: Opc = GTOpc; break;
8427 case ISD::SETGE: Swap = true;
8428 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
8429 case ISD::SETULT: Swap = true;
8430 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
8431 case ISD::SETUGE: Swap = true;
8432 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
8433 }
8434 if (Swap)
8435 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008436
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008437 // Check that the operation in question is available (most are plain SSE2,
8438 // but PCMPGTQ and PCMPEQQ have different requirements).
Craig Topperc0d82852011-11-22 00:44:41 +00008439 if (Opc == X86ISD::PCMPGTQ && !Subtarget->hasSSE42orAVX())
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008440 return SDValue();
Craig Topperc0d82852011-11-22 00:44:41 +00008441 if (Opc == X86ISD::PCMPEQQ && !Subtarget->hasSSE41orAVX())
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008442 return SDValue();
8443
Nate Begeman30a0de92008-07-17 16:51:19 +00008444 // Since SSE has no unsigned integer comparisons, we need to flip the sign
8445 // bits of the inputs before performing those operations.
8446 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00008447 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00008448 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
8449 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00008450 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00008451 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
8452 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00008453 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
8454 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00008455 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008456
Dale Johannesenace16102009-02-03 19:33:06 +00008457 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00008458
8459 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00008460 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00008461 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00008462
Nate Begeman30a0de92008-07-17 16:51:19 +00008463 return Result;
8464}
Evan Cheng0488db92007-09-25 01:57:46 +00008465
Evan Cheng370e5342008-12-03 08:38:43 +00008466// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00008467static bool isX86LogicalCmp(SDValue Op) {
8468 unsigned Opc = Op.getNode()->getOpcode();
8469 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
8470 return true;
8471 if (Op.getResNo() == 1 &&
8472 (Opc == X86ISD::ADD ||
8473 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00008474 Opc == X86ISD::ADC ||
8475 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00008476 Opc == X86ISD::SMUL ||
8477 Opc == X86ISD::UMUL ||
8478 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00008479 Opc == X86ISD::DEC ||
8480 Opc == X86ISD::OR ||
8481 Opc == X86ISD::XOR ||
8482 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00008483 return true;
8484
Chris Lattner9637d5b2010-12-05 07:49:54 +00008485 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
8486 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008487
Dan Gohman076aee32009-03-04 19:44:21 +00008488 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00008489}
8490
Chris Lattnera2b56002010-12-05 01:23:24 +00008491static bool isZero(SDValue V) {
8492 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8493 return C && C->isNullValue();
8494}
8495
Chris Lattner96908b12010-12-05 02:00:51 +00008496static bool isAllOnes(SDValue V) {
8497 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8498 return C && C->isAllOnesValue();
8499}
8500
Dan Gohmand858e902010-04-17 15:26:15 +00008501SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008502 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008503 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00008504 SDValue Op1 = Op.getOperand(1);
8505 SDValue Op2 = Op.getOperand(2);
8506 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008507 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00008508
Dan Gohman1a492952009-10-20 16:22:37 +00008509 if (Cond.getOpcode() == ISD::SETCC) {
8510 SDValue NewCond = LowerSETCC(Cond, DAG);
8511 if (NewCond.getNode())
8512 Cond = NewCond;
8513 }
Evan Cheng734503b2006-09-11 02:19:56 +00008514
Chris Lattnera2b56002010-12-05 01:23:24 +00008515 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008516 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00008517 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008518 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008519 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00008520 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
8521 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008522 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008523
Chris Lattnera2b56002010-12-05 01:23:24 +00008524 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008525
8526 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00008527 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
8528 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00008529
8530 SDValue CmpOp0 = Cmp.getOperand(0);
8531 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
8532 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008533
Chris Lattner96908b12010-12-05 02:00:51 +00008534 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00008535 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8536 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008537
Chris Lattner96908b12010-12-05 02:00:51 +00008538 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
8539 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008540
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008541 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00008542 if (N2C == 0 || !N2C->isNullValue())
8543 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
8544 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008545 }
8546 }
8547
Chris Lattnera2b56002010-12-05 01:23:24 +00008548 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00008549 if (Cond.getOpcode() == ISD::AND &&
8550 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8551 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008552 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008553 Cond = Cond.getOperand(0);
8554 }
8555
Evan Cheng3f41d662007-10-08 22:16:29 +00008556 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8557 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00008558 unsigned CondOpcode = Cond.getOpcode();
8559 if (CondOpcode == X86ISD::SETCC ||
8560 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008561 CC = Cond.getOperand(0);
8562
Dan Gohman475871a2008-07-27 21:46:04 +00008563 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008564 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00008565 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00008566
Evan Cheng3f41d662007-10-08 22:16:29 +00008567 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008568 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00008569 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00008570 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00008571
Chris Lattnerd1980a52009-03-12 06:52:53 +00008572 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
8573 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00008574 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008575 addTest = false;
8576 }
Dan Gohman65fd6562011-11-03 21:49:52 +00008577 } else if (CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
8578 CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
8579 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
8580 Cond.getOperand(0).getValueType() != MVT::i8)) {
8581 SDValue LHS = Cond.getOperand(0);
8582 SDValue RHS = Cond.getOperand(1);
8583 unsigned X86Opcode;
8584 unsigned X86Cond;
8585 SDVTList VTs;
8586 switch (CondOpcode) {
8587 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
8588 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
8589 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
8590 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
8591 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
8592 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
8593 default: llvm_unreachable("unexpected overflowing operator");
8594 }
8595 if (CondOpcode == ISD::UMULO)
8596 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
8597 MVT::i32);
8598 else
8599 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
8600
8601 SDValue X86Op = DAG.getNode(X86Opcode, DL, VTs, LHS, RHS);
8602
8603 if (CondOpcode == ISD::UMULO)
8604 Cond = X86Op.getValue(2);
8605 else
8606 Cond = X86Op.getValue(1);
8607
8608 CC = DAG.getConstant(X86Cond, MVT::i8);
8609 addTest = false;
Evan Cheng0488db92007-09-25 01:57:46 +00008610 }
8611
8612 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008613 // Look pass the truncate.
8614 if (Cond.getOpcode() == ISD::TRUNCATE)
8615 Cond = Cond.getOperand(0);
8616
8617 // We know the result of AND is compared against zero. Try to match
8618 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00008619 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00008620 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00008621 if (NewSetCC.getNode()) {
8622 CC = NewSetCC.getOperand(0);
8623 Cond = NewSetCC.getOperand(1);
8624 addTest = false;
8625 }
8626 }
8627 }
8628
8629 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008630 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00008631 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00008632 }
8633
Benjamin Kramere915ff32010-12-22 23:09:28 +00008634 // a < b ? -1 : 0 -> RES = ~setcc_carry
8635 // a < b ? 0 : -1 -> RES = setcc_carry
8636 // a >= b ? -1 : 0 -> RES = setcc_carry
8637 // a >= b ? 0 : -1 -> RES = ~setcc_carry
8638 if (Cond.getOpcode() == X86ISD::CMP) {
8639 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
8640
8641 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
8642 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
8643 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8644 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
8645 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
8646 return DAG.getNOT(DL, Res, Res.getValueType());
8647 return Res;
8648 }
8649 }
8650
Evan Cheng0488db92007-09-25 01:57:46 +00008651 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
8652 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008653 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008654 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00008655 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00008656}
8657
Evan Cheng370e5342008-12-03 08:38:43 +00008658// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
8659// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
8660// from the AND / OR.
8661static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
8662 Opc = Op.getOpcode();
8663 if (Opc != ISD::OR && Opc != ISD::AND)
8664 return false;
8665 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8666 Op.getOperand(0).hasOneUse() &&
8667 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
8668 Op.getOperand(1).hasOneUse());
8669}
8670
Evan Cheng961d6d42009-02-02 08:19:07 +00008671// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
8672// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00008673static bool isXor1OfSetCC(SDValue Op) {
8674 if (Op.getOpcode() != ISD::XOR)
8675 return false;
8676 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
8677 if (N1C && N1C->getAPIntValue() == 1) {
8678 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8679 Op.getOperand(0).hasOneUse();
8680 }
8681 return false;
8682}
8683
Dan Gohmand858e902010-04-17 15:26:15 +00008684SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008685 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008686 SDValue Chain = Op.getOperand(0);
8687 SDValue Cond = Op.getOperand(1);
8688 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008689 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008690 SDValue CC;
Dan Gohman65fd6562011-11-03 21:49:52 +00008691 bool Inverted = false;
Evan Cheng734503b2006-09-11 02:19:56 +00008692
Dan Gohman1a492952009-10-20 16:22:37 +00008693 if (Cond.getOpcode() == ISD::SETCC) {
Dan Gohman65fd6562011-11-03 21:49:52 +00008694 // Check for setcc([su]{add,sub,mul}o == 0).
8695 if (cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETEQ &&
8696 isa<ConstantSDNode>(Cond.getOperand(1)) &&
8697 cast<ConstantSDNode>(Cond.getOperand(1))->isNullValue() &&
8698 Cond.getOperand(0).getResNo() == 1 &&
8699 (Cond.getOperand(0).getOpcode() == ISD::SADDO ||
8700 Cond.getOperand(0).getOpcode() == ISD::UADDO ||
8701 Cond.getOperand(0).getOpcode() == ISD::SSUBO ||
8702 Cond.getOperand(0).getOpcode() == ISD::USUBO ||
8703 Cond.getOperand(0).getOpcode() == ISD::SMULO ||
8704 Cond.getOperand(0).getOpcode() == ISD::UMULO)) {
8705 Inverted = true;
8706 Cond = Cond.getOperand(0);
8707 } else {
8708 SDValue NewCond = LowerSETCC(Cond, DAG);
8709 if (NewCond.getNode())
8710 Cond = NewCond;
8711 }
Dan Gohman1a492952009-10-20 16:22:37 +00008712 }
Chris Lattnere55484e2008-12-25 05:34:37 +00008713#if 0
8714 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00008715 else if (Cond.getOpcode() == X86ISD::ADD ||
8716 Cond.getOpcode() == X86ISD::SUB ||
8717 Cond.getOpcode() == X86ISD::SMUL ||
8718 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00008719 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00008720#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00008721
Evan Chengad9c0a32009-12-15 00:53:42 +00008722 // Look pass (and (setcc_carry (cmp ...)), 1).
8723 if (Cond.getOpcode() == ISD::AND &&
8724 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8725 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008726 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008727 Cond = Cond.getOperand(0);
8728 }
8729
Evan Cheng3f41d662007-10-08 22:16:29 +00008730 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8731 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00008732 unsigned CondOpcode = Cond.getOpcode();
8733 if (CondOpcode == X86ISD::SETCC ||
8734 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008735 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008736
Dan Gohman475871a2008-07-27 21:46:04 +00008737 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008738 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00008739 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00008740 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00008741 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008742 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00008743 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00008744 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00008745 default: break;
8746 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00008747 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00008748 // These can only come from an arithmetic instruction with overflow,
8749 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00008750 Cond = Cond.getNode()->getOperand(1);
8751 addTest = false;
8752 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00008753 }
Evan Cheng0488db92007-09-25 01:57:46 +00008754 }
Dan Gohman65fd6562011-11-03 21:49:52 +00008755 }
8756 CondOpcode = Cond.getOpcode();
8757 if (CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
8758 CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
8759 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
8760 Cond.getOperand(0).getValueType() != MVT::i8)) {
8761 SDValue LHS = Cond.getOperand(0);
8762 SDValue RHS = Cond.getOperand(1);
8763 unsigned X86Opcode;
8764 unsigned X86Cond;
8765 SDVTList VTs;
8766 switch (CondOpcode) {
8767 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
8768 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
8769 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
8770 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
8771 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
8772 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
8773 default: llvm_unreachable("unexpected overflowing operator");
8774 }
8775 if (Inverted)
8776 X86Cond = X86::GetOppositeBranchCondition((X86::CondCode)X86Cond);
8777 if (CondOpcode == ISD::UMULO)
8778 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
8779 MVT::i32);
8780 else
8781 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
8782
8783 SDValue X86Op = DAG.getNode(X86Opcode, dl, VTs, LHS, RHS);
8784
8785 if (CondOpcode == ISD::UMULO)
8786 Cond = X86Op.getValue(2);
8787 else
8788 Cond = X86Op.getValue(1);
8789
8790 CC = DAG.getConstant(X86Cond, MVT::i8);
8791 addTest = false;
Evan Cheng370e5342008-12-03 08:38:43 +00008792 } else {
8793 unsigned CondOpc;
8794 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
8795 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00008796 if (CondOpc == ISD::OR) {
8797 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
8798 // two branches instead of an explicit OR instruction with a
8799 // separate test.
8800 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00008801 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00008802 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008803 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00008804 Chain, Dest, CC, Cmp);
8805 CC = Cond.getOperand(1).getOperand(0);
8806 Cond = Cmp;
8807 addTest = false;
8808 }
8809 } else { // ISD::AND
8810 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
8811 // two branches instead of an explicit AND instruction with a
8812 // separate test. However, we only do this if this block doesn't
8813 // have a fall-through edge, because this requires an explicit
8814 // jmp when the condition is false.
8815 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00008816 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00008817 Op.getNode()->hasOneUse()) {
8818 X86::CondCode CCode =
8819 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
8820 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008821 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00008822 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00008823 // Look for an unconditional branch following this conditional branch.
8824 // We need this because we need to reverse the successors in order
8825 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00008826 if (User->getOpcode() == ISD::BR) {
8827 SDValue FalseBB = User->getOperand(1);
8828 SDNode *NewBR =
8829 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00008830 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00008831 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00008832 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00008833
Dale Johannesene4d209d2009-02-03 20:21:25 +00008834 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00008835 Chain, Dest, CC, Cmp);
8836 X86::CondCode CCode =
8837 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
8838 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008839 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00008840 Cond = Cmp;
8841 addTest = false;
8842 }
8843 }
Dan Gohman279c22e2008-10-21 03:29:32 +00008844 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00008845 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
8846 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
8847 // It should be transformed during dag combiner except when the condition
8848 // is set by a arithmetics with overflow node.
8849 X86::CondCode CCode =
8850 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
8851 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008852 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00008853 Cond = Cond.getOperand(0).getOperand(1);
8854 addTest = false;
Dan Gohman65fd6562011-11-03 21:49:52 +00008855 } else if (Cond.getOpcode() == ISD::SETCC &&
8856 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETOEQ) {
8857 // For FCMP_OEQ, we can emit
8858 // two branches instead of an explicit AND instruction with a
8859 // separate test. However, we only do this if this block doesn't
8860 // have a fall-through edge, because this requires an explicit
8861 // jmp when the condition is false.
8862 if (Op.getNode()->hasOneUse()) {
8863 SDNode *User = *Op.getNode()->use_begin();
8864 // Look for an unconditional branch following this conditional branch.
8865 // We need this because we need to reverse the successors in order
8866 // to implement FCMP_OEQ.
8867 if (User->getOpcode() == ISD::BR) {
8868 SDValue FalseBB = User->getOperand(1);
8869 SDNode *NewBR =
8870 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
8871 assert(NewBR == User);
8872 (void)NewBR;
8873 Dest = FalseBB;
8874
8875 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
8876 Cond.getOperand(0), Cond.getOperand(1));
8877 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
8878 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
8879 Chain, Dest, CC, Cmp);
8880 CC = DAG.getConstant(X86::COND_P, MVT::i8);
8881 Cond = Cmp;
8882 addTest = false;
8883 }
8884 }
8885 } else if (Cond.getOpcode() == ISD::SETCC &&
8886 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETUNE) {
8887 // For FCMP_UNE, we can emit
8888 // two branches instead of an explicit AND instruction with a
8889 // separate test. However, we only do this if this block doesn't
8890 // have a fall-through edge, because this requires an explicit
8891 // jmp when the condition is false.
8892 if (Op.getNode()->hasOneUse()) {
8893 SDNode *User = *Op.getNode()->use_begin();
8894 // Look for an unconditional branch following this conditional branch.
8895 // We need this because we need to reverse the successors in order
8896 // to implement FCMP_UNE.
8897 if (User->getOpcode() == ISD::BR) {
8898 SDValue FalseBB = User->getOperand(1);
8899 SDNode *NewBR =
8900 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
8901 assert(NewBR == User);
8902 (void)NewBR;
8903
8904 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
8905 Cond.getOperand(0), Cond.getOperand(1));
8906 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
8907 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
8908 Chain, Dest, CC, Cmp);
8909 CC = DAG.getConstant(X86::COND_NP, MVT::i8);
8910 Cond = Cmp;
8911 addTest = false;
8912 Dest = FalseBB;
8913 }
8914 }
Dan Gohman279c22e2008-10-21 03:29:32 +00008915 }
Evan Cheng0488db92007-09-25 01:57:46 +00008916 }
8917
8918 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008919 // Look pass the truncate.
8920 if (Cond.getOpcode() == ISD::TRUNCATE)
8921 Cond = Cond.getOperand(0);
8922
8923 // We know the result of AND is compared against zero. Try to match
8924 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00008925 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008926 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
8927 if (NewSetCC.getNode()) {
8928 CC = NewSetCC.getOperand(0);
8929 Cond = NewSetCC.getOperand(1);
8930 addTest = false;
8931 }
8932 }
8933 }
8934
8935 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008936 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00008937 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00008938 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00008939 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00008940 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00008941}
8942
Anton Korobeynikove060b532007-04-17 19:34:00 +00008943
8944// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
8945// Calls to _alloca is needed to probe the stack when allocating more than 4k
8946// bytes in one go. Touching the stack at 4K increments is necessary to ensure
8947// that the guard pages used by the OS virtual memory manager are allocated in
8948// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00008949SDValue
8950X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008951 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008952 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
Nick Lewycky8a8d4792011-12-02 22:16:29 +00008953 getTargetMachine().Options.EnableSegmentedStacks) &&
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008954 "This should be used only on Windows targets or when segmented stacks "
Rafael Espindola96428ce2011-09-06 18:43:08 +00008955 "are being used");
8956 assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008957 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008958
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008959 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00008960 SDValue Chain = Op.getOperand(0);
8961 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008962 // FIXME: Ensure alignment here
8963
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008964 bool Is64Bit = Subtarget->is64Bit();
8965 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008966
Nick Lewycky8a8d4792011-12-02 22:16:29 +00008967 if (getTargetMachine().Options.EnableSegmentedStacks) {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008968 MachineFunction &MF = DAG.getMachineFunction();
8969 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008970
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008971 if (Is64Bit) {
8972 // The 64 bit implementation of segmented stacks needs to clobber both r10
Rafael Espindola96428ce2011-09-06 18:43:08 +00008973 // r11. This makes it impossible to use it along with nested parameters.
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008974 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00008975
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008976 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
8977 I != E; I++)
8978 if (I->hasNestAttr())
8979 report_fatal_error("Cannot use segmented stacks with functions that "
8980 "have nested arguments.");
8981 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00008982
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008983 const TargetRegisterClass *AddrRegClass =
8984 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
8985 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
8986 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
8987 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
8988 DAG.getRegister(Vreg, SPTy));
8989 SDValue Ops1[2] = { Value, Chain };
8990 return DAG.getMergeValues(Ops1, 2, dl);
8991 } else {
8992 SDValue Flag;
8993 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008994
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008995 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
8996 Flag = Chain.getValue(1);
8997 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008998
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008999 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
9000 Flag = Chain.getValue(1);
9001
9002 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
9003
9004 SDValue Ops1[2] = { Chain.getValue(0), Chain };
9005 return DAG.getMergeValues(Ops1, 2, dl);
9006 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009007}
9008
Dan Gohmand858e902010-04-17 15:26:15 +00009009SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00009010 MachineFunction &MF = DAG.getMachineFunction();
9011 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
9012
Dan Gohman69de1932008-02-06 22:27:42 +00009013 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00009014 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00009015
Anton Korobeynikove7beda12010-10-03 22:52:07 +00009016 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00009017 // vastart just stores the address of the VarArgsFrameIndex slot into the
9018 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00009019 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9020 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009021 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
9022 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009023 }
9024
9025 // __va_list_tag:
9026 // gp_offset (0 - 6 * 8)
9027 // fp_offset (48 - 48 + 8 * 16)
9028 // overflow_arg_area (point to parameters coming in memory).
9029 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00009030 SmallVector<SDValue, 8> MemOps;
9031 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00009032 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009033 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009034 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
9035 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009036 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009037 MemOps.push_back(Store);
9038
9039 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009040 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009041 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009042 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009043 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
9044 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009045 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009046 MemOps.push_back(Store);
9047
9048 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00009049 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009050 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00009051 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9052 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009053 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
9054 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00009055 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009056 MemOps.push_back(Store);
9057
9058 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00009059 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009060 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00009061 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
9062 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009063 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
9064 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009065 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009066 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00009067 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00009068}
9069
Dan Gohmand858e902010-04-17 15:26:15 +00009070SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00009071 assert(Subtarget->is64Bit() &&
9072 "LowerVAARG only handles 64-bit va_arg!");
9073 assert((Subtarget->isTargetLinux() ||
9074 Subtarget->isTargetDarwin()) &&
9075 "Unhandled target in LowerVAARG");
9076 assert(Op.getNode()->getNumOperands() == 4);
9077 SDValue Chain = Op.getOperand(0);
9078 SDValue SrcPtr = Op.getOperand(1);
9079 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
9080 unsigned Align = Op.getConstantOperandVal(3);
9081 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00009082
Dan Gohman320afb82010-10-12 18:00:49 +00009083 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009084 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Dan Gohman320afb82010-10-12 18:00:49 +00009085 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
9086 uint8_t ArgMode;
9087
9088 // Decide which area this value should be read from.
9089 // TODO: Implement the AMD64 ABI in its entirety. This simple
9090 // selection mechanism works only for the basic types.
9091 if (ArgVT == MVT::f80) {
9092 llvm_unreachable("va_arg for f80 not yet implemented");
9093 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
9094 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
9095 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
9096 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
9097 } else {
9098 llvm_unreachable("Unhandled argument type in LowerVAARG");
9099 }
9100
9101 if (ArgMode == 2) {
9102 // Sanity Check: Make sure using fp_offset makes sense.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009103 assert(!getTargetMachine().Options.UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00009104 !(DAG.getMachineFunction()
9105 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00009106 Subtarget->hasXMM());
Dan Gohman320afb82010-10-12 18:00:49 +00009107 }
9108
9109 // Insert VAARG_64 node into the DAG
9110 // VAARG_64 returns two values: Variable Argument Address, Chain
9111 SmallVector<SDValue, 11> InstOps;
9112 InstOps.push_back(Chain);
9113 InstOps.push_back(SrcPtr);
9114 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
9115 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
9116 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
9117 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
9118 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
9119 VTs, &InstOps[0], InstOps.size(),
9120 MVT::i64,
9121 MachinePointerInfo(SV),
9122 /*Align=*/0,
9123 /*Volatile=*/false,
9124 /*ReadMem=*/true,
9125 /*WriteMem=*/true);
9126 Chain = VAARG.getValue(1);
9127
9128 // Load the next argument and return it
9129 return DAG.getLoad(ArgVT, dl,
9130 Chain,
9131 VAARG,
9132 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009133 false, false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00009134}
9135
Dan Gohmand858e902010-04-17 15:26:15 +00009136SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00009137 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00009138 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00009139 SDValue Chain = Op.getOperand(0);
9140 SDValue DstPtr = Op.getOperand(1);
9141 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00009142 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
9143 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00009144 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00009145
Chris Lattnere72f2022010-09-21 05:40:29 +00009146 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00009147 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00009148 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00009149 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00009150}
9151
Dan Gohman475871a2008-07-27 21:46:04 +00009152SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00009153X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009154 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009155 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00009156 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00009157 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00009158 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00009159 case Intrinsic::x86_sse_comieq_ss:
9160 case Intrinsic::x86_sse_comilt_ss:
9161 case Intrinsic::x86_sse_comile_ss:
9162 case Intrinsic::x86_sse_comigt_ss:
9163 case Intrinsic::x86_sse_comige_ss:
9164 case Intrinsic::x86_sse_comineq_ss:
9165 case Intrinsic::x86_sse_ucomieq_ss:
9166 case Intrinsic::x86_sse_ucomilt_ss:
9167 case Intrinsic::x86_sse_ucomile_ss:
9168 case Intrinsic::x86_sse_ucomigt_ss:
9169 case Intrinsic::x86_sse_ucomige_ss:
9170 case Intrinsic::x86_sse_ucomineq_ss:
9171 case Intrinsic::x86_sse2_comieq_sd:
9172 case Intrinsic::x86_sse2_comilt_sd:
9173 case Intrinsic::x86_sse2_comile_sd:
9174 case Intrinsic::x86_sse2_comigt_sd:
9175 case Intrinsic::x86_sse2_comige_sd:
9176 case Intrinsic::x86_sse2_comineq_sd:
9177 case Intrinsic::x86_sse2_ucomieq_sd:
9178 case Intrinsic::x86_sse2_ucomilt_sd:
9179 case Intrinsic::x86_sse2_ucomile_sd:
9180 case Intrinsic::x86_sse2_ucomigt_sd:
9181 case Intrinsic::x86_sse2_ucomige_sd:
9182 case Intrinsic::x86_sse2_ucomineq_sd: {
9183 unsigned Opc = 0;
9184 ISD::CondCode CC = ISD::SETCC_INVALID;
9185 switch (IntNo) {
9186 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009187 case Intrinsic::x86_sse_comieq_ss:
9188 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009189 Opc = X86ISD::COMI;
9190 CC = ISD::SETEQ;
9191 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009192 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009193 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009194 Opc = X86ISD::COMI;
9195 CC = ISD::SETLT;
9196 break;
9197 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009198 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009199 Opc = X86ISD::COMI;
9200 CC = ISD::SETLE;
9201 break;
9202 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009203 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009204 Opc = X86ISD::COMI;
9205 CC = ISD::SETGT;
9206 break;
9207 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009208 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009209 Opc = X86ISD::COMI;
9210 CC = ISD::SETGE;
9211 break;
9212 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009213 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009214 Opc = X86ISD::COMI;
9215 CC = ISD::SETNE;
9216 break;
9217 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009218 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009219 Opc = X86ISD::UCOMI;
9220 CC = ISD::SETEQ;
9221 break;
9222 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009223 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009224 Opc = X86ISD::UCOMI;
9225 CC = ISD::SETLT;
9226 break;
9227 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009228 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009229 Opc = X86ISD::UCOMI;
9230 CC = ISD::SETLE;
9231 break;
9232 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009233 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009234 Opc = X86ISD::UCOMI;
9235 CC = ISD::SETGT;
9236 break;
9237 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009238 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009239 Opc = X86ISD::UCOMI;
9240 CC = ISD::SETGE;
9241 break;
9242 case Intrinsic::x86_sse_ucomineq_ss:
9243 case Intrinsic::x86_sse2_ucomineq_sd:
9244 Opc = X86ISD::UCOMI;
9245 CC = ISD::SETNE;
9246 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009247 }
Evan Cheng734503b2006-09-11 02:19:56 +00009248
Dan Gohman475871a2008-07-27 21:46:04 +00009249 SDValue LHS = Op.getOperand(1);
9250 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00009251 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00009252 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00009253 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
9254 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9255 DAG.getConstant(X86CC, MVT::i8), Cond);
9256 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00009257 }
Duncan Sands04aa4ae2011-09-23 16:10:22 +00009258 // Arithmetic intrinsics.
9259 case Intrinsic::x86_sse3_hadd_ps:
9260 case Intrinsic::x86_sse3_hadd_pd:
9261 case Intrinsic::x86_avx_hadd_ps_256:
9262 case Intrinsic::x86_avx_hadd_pd_256:
9263 return DAG.getNode(X86ISD::FHADD, dl, Op.getValueType(),
9264 Op.getOperand(1), Op.getOperand(2));
9265 case Intrinsic::x86_sse3_hsub_ps:
9266 case Intrinsic::x86_sse3_hsub_pd:
9267 case Intrinsic::x86_avx_hsub_ps_256:
9268 case Intrinsic::x86_avx_hsub_pd_256:
9269 return DAG.getNode(X86ISD::FHSUB, dl, Op.getValueType(),
9270 Op.getOperand(1), Op.getOperand(2));
Craig Topper98fc7292011-11-19 17:46:46 +00009271 case Intrinsic::x86_avx2_psllv_d:
9272 case Intrinsic::x86_avx2_psllv_q:
9273 case Intrinsic::x86_avx2_psllv_d_256:
9274 case Intrinsic::x86_avx2_psllv_q_256:
9275 return DAG.getNode(ISD::SHL, dl, Op.getValueType(),
9276 Op.getOperand(1), Op.getOperand(2));
9277 case Intrinsic::x86_avx2_psrlv_d:
9278 case Intrinsic::x86_avx2_psrlv_q:
9279 case Intrinsic::x86_avx2_psrlv_d_256:
9280 case Intrinsic::x86_avx2_psrlv_q_256:
9281 return DAG.getNode(ISD::SRL, dl, Op.getValueType(),
9282 Op.getOperand(1), Op.getOperand(2));
9283 case Intrinsic::x86_avx2_psrav_d:
9284 case Intrinsic::x86_avx2_psrav_d_256:
9285 return DAG.getNode(ISD::SRA, dl, Op.getValueType(),
9286 Op.getOperand(1), Op.getOperand(2));
9287
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009288 // ptest and testp intrinsics. The intrinsic these come from are designed to
9289 // return an integer value, not just an instruction so lower it to the ptest
9290 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00009291 case Intrinsic::x86_sse41_ptestz:
9292 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009293 case Intrinsic::x86_sse41_ptestnzc:
9294 case Intrinsic::x86_avx_ptestz_256:
9295 case Intrinsic::x86_avx_ptestc_256:
9296 case Intrinsic::x86_avx_ptestnzc_256:
9297 case Intrinsic::x86_avx_vtestz_ps:
9298 case Intrinsic::x86_avx_vtestc_ps:
9299 case Intrinsic::x86_avx_vtestnzc_ps:
9300 case Intrinsic::x86_avx_vtestz_pd:
9301 case Intrinsic::x86_avx_vtestc_pd:
9302 case Intrinsic::x86_avx_vtestnzc_pd:
9303 case Intrinsic::x86_avx_vtestz_ps_256:
9304 case Intrinsic::x86_avx_vtestc_ps_256:
9305 case Intrinsic::x86_avx_vtestnzc_ps_256:
9306 case Intrinsic::x86_avx_vtestz_pd_256:
9307 case Intrinsic::x86_avx_vtestc_pd_256:
9308 case Intrinsic::x86_avx_vtestnzc_pd_256: {
9309 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00009310 unsigned X86CC = 0;
9311 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00009312 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009313 case Intrinsic::x86_avx_vtestz_ps:
9314 case Intrinsic::x86_avx_vtestz_pd:
9315 case Intrinsic::x86_avx_vtestz_ps_256:
9316 case Intrinsic::x86_avx_vtestz_pd_256:
9317 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009318 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009319 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009320 // ZF = 1
9321 X86CC = X86::COND_E;
9322 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009323 case Intrinsic::x86_avx_vtestc_ps:
9324 case Intrinsic::x86_avx_vtestc_pd:
9325 case Intrinsic::x86_avx_vtestc_ps_256:
9326 case Intrinsic::x86_avx_vtestc_pd_256:
9327 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009328 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009329 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009330 // CF = 1
9331 X86CC = X86::COND_B;
9332 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009333 case Intrinsic::x86_avx_vtestnzc_ps:
9334 case Intrinsic::x86_avx_vtestnzc_pd:
9335 case Intrinsic::x86_avx_vtestnzc_ps_256:
9336 case Intrinsic::x86_avx_vtestnzc_pd_256:
9337 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00009338 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009339 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009340 // ZF and CF = 0
9341 X86CC = X86::COND_A;
9342 break;
9343 }
Eric Christopherfd179292009-08-27 18:07:15 +00009344
Eric Christopher71c67532009-07-29 00:28:05 +00009345 SDValue LHS = Op.getOperand(1);
9346 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009347 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
9348 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00009349 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
9350 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
9351 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00009352 }
Evan Cheng5759f972008-05-04 09:15:50 +00009353
9354 // Fix vector shift instructions where the last operand is a non-immediate
9355 // i32 value.
Craig Topper7be5dfd2011-11-12 09:58:49 +00009356 case Intrinsic::x86_avx2_pslli_w:
9357 case Intrinsic::x86_avx2_pslli_d:
9358 case Intrinsic::x86_avx2_pslli_q:
9359 case Intrinsic::x86_avx2_psrli_w:
9360 case Intrinsic::x86_avx2_psrli_d:
9361 case Intrinsic::x86_avx2_psrli_q:
9362 case Intrinsic::x86_avx2_psrai_w:
9363 case Intrinsic::x86_avx2_psrai_d:
Evan Cheng5759f972008-05-04 09:15:50 +00009364 case Intrinsic::x86_sse2_pslli_w:
9365 case Intrinsic::x86_sse2_pslli_d:
9366 case Intrinsic::x86_sse2_pslli_q:
9367 case Intrinsic::x86_sse2_psrli_w:
9368 case Intrinsic::x86_sse2_psrli_d:
9369 case Intrinsic::x86_sse2_psrli_q:
9370 case Intrinsic::x86_sse2_psrai_w:
9371 case Intrinsic::x86_sse2_psrai_d:
9372 case Intrinsic::x86_mmx_pslli_w:
9373 case Intrinsic::x86_mmx_pslli_d:
9374 case Intrinsic::x86_mmx_pslli_q:
9375 case Intrinsic::x86_mmx_psrli_w:
9376 case Intrinsic::x86_mmx_psrli_d:
9377 case Intrinsic::x86_mmx_psrli_q:
9378 case Intrinsic::x86_mmx_psrai_w:
9379 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00009380 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00009381 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00009382 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00009383
9384 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00009385 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00009386 switch (IntNo) {
9387 case Intrinsic::x86_sse2_pslli_w:
9388 NewIntNo = Intrinsic::x86_sse2_psll_w;
9389 break;
9390 case Intrinsic::x86_sse2_pslli_d:
9391 NewIntNo = Intrinsic::x86_sse2_psll_d;
9392 break;
9393 case Intrinsic::x86_sse2_pslli_q:
9394 NewIntNo = Intrinsic::x86_sse2_psll_q;
9395 break;
9396 case Intrinsic::x86_sse2_psrli_w:
9397 NewIntNo = Intrinsic::x86_sse2_psrl_w;
9398 break;
9399 case Intrinsic::x86_sse2_psrli_d:
9400 NewIntNo = Intrinsic::x86_sse2_psrl_d;
9401 break;
9402 case Intrinsic::x86_sse2_psrli_q:
9403 NewIntNo = Intrinsic::x86_sse2_psrl_q;
9404 break;
9405 case Intrinsic::x86_sse2_psrai_w:
9406 NewIntNo = Intrinsic::x86_sse2_psra_w;
9407 break;
9408 case Intrinsic::x86_sse2_psrai_d:
9409 NewIntNo = Intrinsic::x86_sse2_psra_d;
9410 break;
Craig Topper7be5dfd2011-11-12 09:58:49 +00009411 case Intrinsic::x86_avx2_pslli_w:
9412 NewIntNo = Intrinsic::x86_avx2_psll_w;
9413 break;
9414 case Intrinsic::x86_avx2_pslli_d:
9415 NewIntNo = Intrinsic::x86_avx2_psll_d;
9416 break;
9417 case Intrinsic::x86_avx2_pslli_q:
9418 NewIntNo = Intrinsic::x86_avx2_psll_q;
9419 break;
9420 case Intrinsic::x86_avx2_psrli_w:
9421 NewIntNo = Intrinsic::x86_avx2_psrl_w;
9422 break;
9423 case Intrinsic::x86_avx2_psrli_d:
9424 NewIntNo = Intrinsic::x86_avx2_psrl_d;
9425 break;
9426 case Intrinsic::x86_avx2_psrli_q:
9427 NewIntNo = Intrinsic::x86_avx2_psrl_q;
9428 break;
9429 case Intrinsic::x86_avx2_psrai_w:
9430 NewIntNo = Intrinsic::x86_avx2_psra_w;
9431 break;
9432 case Intrinsic::x86_avx2_psrai_d:
9433 NewIntNo = Intrinsic::x86_avx2_psra_d;
9434 break;
Evan Cheng5759f972008-05-04 09:15:50 +00009435 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00009436 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00009437 switch (IntNo) {
9438 case Intrinsic::x86_mmx_pslli_w:
9439 NewIntNo = Intrinsic::x86_mmx_psll_w;
9440 break;
9441 case Intrinsic::x86_mmx_pslli_d:
9442 NewIntNo = Intrinsic::x86_mmx_psll_d;
9443 break;
9444 case Intrinsic::x86_mmx_pslli_q:
9445 NewIntNo = Intrinsic::x86_mmx_psll_q;
9446 break;
9447 case Intrinsic::x86_mmx_psrli_w:
9448 NewIntNo = Intrinsic::x86_mmx_psrl_w;
9449 break;
9450 case Intrinsic::x86_mmx_psrli_d:
9451 NewIntNo = Intrinsic::x86_mmx_psrl_d;
9452 break;
9453 case Intrinsic::x86_mmx_psrli_q:
9454 NewIntNo = Intrinsic::x86_mmx_psrl_q;
9455 break;
9456 case Intrinsic::x86_mmx_psrai_w:
9457 NewIntNo = Intrinsic::x86_mmx_psra_w;
9458 break;
9459 case Intrinsic::x86_mmx_psrai_d:
9460 NewIntNo = Intrinsic::x86_mmx_psra_d;
9461 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00009462 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00009463 }
9464 break;
9465 }
9466 }
Mon P Wangefa42202009-09-03 19:56:25 +00009467
9468 // The vector shift intrinsics with scalars uses 32b shift amounts but
9469 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
9470 // to be zero.
9471 SDValue ShOps[4];
9472 ShOps[0] = ShAmt;
9473 ShOps[1] = DAG.getConstant(0, MVT::i32);
9474 if (ShAmtVT == MVT::v4i32) {
9475 ShOps[2] = DAG.getUNDEF(MVT::i32);
9476 ShOps[3] = DAG.getUNDEF(MVT::i32);
9477 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
9478 } else {
9479 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00009480// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00009481 }
9482
Owen Andersone50ed302009-08-10 22:56:29 +00009483 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009484 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009485 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009486 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00009487 Op.getOperand(1), ShAmt);
9488 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00009489 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00009490}
Evan Cheng72261582005-12-20 06:22:03 +00009491
Dan Gohmand858e902010-04-17 15:26:15 +00009492SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
9493 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00009494 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9495 MFI->setReturnAddressIsTaken(true);
9496
Bill Wendling64e87322009-01-16 19:25:27 +00009497 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009498 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00009499
9500 if (Depth > 0) {
9501 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
9502 SDValue Offset =
9503 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00009504 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009505 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00009506 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009507 FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009508 MachinePointerInfo(), false, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00009509 }
9510
9511 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00009512 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00009513 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009514 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00009515}
9516
Dan Gohmand858e902010-04-17 15:26:15 +00009517SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00009518 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9519 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00009520
Owen Andersone50ed302009-08-10 22:56:29 +00009521 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009522 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00009523 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
9524 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00009525 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00009526 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00009527 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
9528 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009529 false, false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00009530 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00009531}
9532
Dan Gohman475871a2008-07-27 21:46:04 +00009533SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009534 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00009535 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009536}
9537
Dan Gohmand858e902010-04-17 15:26:15 +00009538SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009539 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00009540 SDValue Chain = Op.getOperand(0);
9541 SDValue Offset = Op.getOperand(1);
9542 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009543 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009544
Dan Gohmand8816272010-08-11 18:14:00 +00009545 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
9546 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
9547 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009548 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009549
Dan Gohmand8816272010-08-11 18:14:00 +00009550 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
9551 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009552 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009553 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
9554 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00009555 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009556 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009557
Dale Johannesene4d209d2009-02-03 20:21:25 +00009558 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00009559 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009560 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009561}
9562
Duncan Sands4a544a72011-09-06 13:37:06 +00009563SDValue X86TargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op,
9564 SelectionDAG &DAG) const {
9565 return Op.getOperand(0);
9566}
9567
9568SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
9569 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00009570 SDValue Root = Op.getOperand(0);
9571 SDValue Trmp = Op.getOperand(1); // trampoline
9572 SDValue FPtr = Op.getOperand(2); // nested function
9573 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009574 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009575
Dan Gohman69de1932008-02-06 22:27:42 +00009576 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009577
9578 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00009579 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00009580
9581 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00009582 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
9583 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00009584
Evan Cheng0e6a0522011-07-18 20:57:22 +00009585 const unsigned char N86R10 = X86_MC::getX86RegNum(X86::R10);
9586 const unsigned char N86R11 = X86_MC::getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00009587
9588 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
9589
9590 // Load the pointer to the nested function into R11.
9591 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00009592 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00009593 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009594 Addr, MachinePointerInfo(TrmpAddr),
9595 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009596
Owen Anderson825b72b2009-08-11 20:47:22 +00009597 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9598 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009599 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
9600 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00009601 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00009602
9603 // Load the 'nest' parameter value into R10.
9604 // R10 is specified in X86CallingConv.td
9605 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00009606 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9607 DAG.getConstant(10, MVT::i64));
9608 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009609 Addr, MachinePointerInfo(TrmpAddr, 10),
9610 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009611
Owen Anderson825b72b2009-08-11 20:47:22 +00009612 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9613 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009614 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
9615 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00009616 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00009617
9618 // Jump to the nested function.
9619 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00009620 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9621 DAG.getConstant(20, MVT::i64));
9622 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009623 Addr, MachinePointerInfo(TrmpAddr, 20),
9624 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009625
9626 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00009627 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9628 DAG.getConstant(22, MVT::i64));
9629 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00009630 MachinePointerInfo(TrmpAddr, 22),
9631 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009632
Duncan Sands4a544a72011-09-06 13:37:06 +00009633 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009634 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00009635 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00009636 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00009637 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00009638 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009639
9640 switch (CC) {
9641 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00009642 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00009643 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00009644 case CallingConv::X86_StdCall: {
9645 // Pass 'nest' parameter in ECX.
9646 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00009647 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009648
9649 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009650 FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00009651 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009652
Chris Lattner58d74912008-03-12 17:45:29 +00009653 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00009654 unsigned InRegCount = 0;
9655 unsigned Idx = 1;
9656
9657 for (FunctionType::param_iterator I = FTy->param_begin(),
9658 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00009659 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00009660 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00009661 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009662
9663 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00009664 report_fatal_error("Nest register in use - reduce number of inreg"
9665 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00009666 }
9667 }
9668 break;
9669 }
9670 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00009671 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00009672 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00009673 // Pass 'nest' parameter in EAX.
9674 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00009675 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009676 break;
9677 }
9678
Dan Gohman475871a2008-07-27 21:46:04 +00009679 SDValue OutChains[4];
9680 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009681
Owen Anderson825b72b2009-08-11 20:47:22 +00009682 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9683 DAG.getConstant(10, MVT::i32));
9684 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009685
Chris Lattnera62fe662010-02-05 19:20:30 +00009686 // This is storing the opcode for MOV32ri.
9687 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Evan Cheng0e6a0522011-07-18 20:57:22 +00009688 const unsigned char N86Reg = X86_MC::getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009689 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00009690 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009691 Trmp, MachinePointerInfo(TrmpAddr),
9692 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009693
Owen Anderson825b72b2009-08-11 20:47:22 +00009694 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9695 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009696 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
9697 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00009698 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009699
Chris Lattnera62fe662010-02-05 19:20:30 +00009700 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00009701 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9702 DAG.getConstant(5, MVT::i32));
9703 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00009704 MachinePointerInfo(TrmpAddr, 5),
9705 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009706
Owen Anderson825b72b2009-08-11 20:47:22 +00009707 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9708 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009709 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
9710 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00009711 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009712
Duncan Sands4a544a72011-09-06 13:37:06 +00009713 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009714 }
9715}
9716
Dan Gohmand858e902010-04-17 15:26:15 +00009717SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
9718 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009719 /*
9720 The rounding mode is in bits 11:10 of FPSR, and has the following
9721 settings:
9722 00 Round to nearest
9723 01 Round to -inf
9724 10 Round to +inf
9725 11 Round to 0
9726
9727 FLT_ROUNDS, on the other hand, expects the following:
9728 -1 Undefined
9729 0 Round to 0
9730 1 Round to nearest
9731 2 Round to +inf
9732 3 Round to -inf
9733
9734 To perform the conversion, we do:
9735 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
9736 */
9737
9738 MachineFunction &MF = DAG.getMachineFunction();
9739 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00009740 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009741 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00009742 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00009743 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009744
9745 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00009746 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00009747 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009748
Michael J. Spencerec38de22010-10-10 22:04:20 +00009749
Chris Lattner2156b792010-09-22 01:11:26 +00009750 MachineMemOperand *MMO =
9751 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
9752 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009753
Chris Lattner2156b792010-09-22 01:11:26 +00009754 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
9755 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
9756 DAG.getVTList(MVT::Other),
9757 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009758
9759 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00009760 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +00009761 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009762
9763 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00009764 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00009765 DAG.getNode(ISD::SRL, DL, MVT::i16,
9766 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00009767 CWD, DAG.getConstant(0x800, MVT::i16)),
9768 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00009769 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00009770 DAG.getNode(ISD::SRL, DL, MVT::i16,
9771 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00009772 CWD, DAG.getConstant(0x400, MVT::i16)),
9773 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009774
Dan Gohman475871a2008-07-27 21:46:04 +00009775 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00009776 DAG.getNode(ISD::AND, DL, MVT::i16,
9777 DAG.getNode(ISD::ADD, DL, MVT::i16,
9778 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00009779 DAG.getConstant(1, MVT::i16)),
9780 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009781
9782
Duncan Sands83ec4b62008-06-06 12:08:01 +00009783 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00009784 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009785}
9786
Dan Gohmand858e902010-04-17 15:26:15 +00009787SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009788 EVT VT = Op.getValueType();
9789 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009790 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009791 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00009792
9793 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009794 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00009795 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00009796 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00009797 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009798 }
Evan Cheng18efe262007-12-14 02:13:44 +00009799
Evan Cheng152804e2007-12-14 08:30:15 +00009800 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00009801 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009802 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00009803
9804 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00009805 SDValue Ops[] = {
9806 Op,
9807 DAG.getConstant(NumBits+NumBits-1, OpVT),
9808 DAG.getConstant(X86::COND_E, MVT::i8),
9809 Op.getValue(1)
9810 };
9811 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00009812
9813 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00009814 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00009815
Owen Anderson825b72b2009-08-11 20:47:22 +00009816 if (VT == MVT::i8)
9817 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009818 return Op;
9819}
9820
Dan Gohmand858e902010-04-17 15:26:15 +00009821SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009822 EVT VT = Op.getValueType();
9823 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009824 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009825 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00009826
9827 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009828 if (VT == MVT::i8) {
9829 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00009830 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009831 }
Evan Cheng152804e2007-12-14 08:30:15 +00009832
9833 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00009834 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009835 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00009836
9837 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00009838 SDValue Ops[] = {
9839 Op,
9840 DAG.getConstant(NumBits, OpVT),
9841 DAG.getConstant(X86::COND_E, MVT::i8),
9842 Op.getValue(1)
9843 };
9844 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00009845
Owen Anderson825b72b2009-08-11 20:47:22 +00009846 if (VT == MVT::i8)
9847 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009848 return Op;
9849}
9850
Craig Topper13894fa2011-08-24 06:14:18 +00009851// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
9852// ones, and then concatenate the result back.
9853static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00009854 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +00009855
9856 assert(VT.getSizeInBits() == 256 && VT.isInteger() &&
9857 "Unsupported value type for operation");
9858
9859 int NumElems = VT.getVectorNumElements();
9860 DebugLoc dl = Op.getDebugLoc();
9861 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
9862 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
9863
9864 // Extract the LHS vectors
9865 SDValue LHS = Op.getOperand(0);
9866 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
9867 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
9868
9869 // Extract the RHS vectors
9870 SDValue RHS = Op.getOperand(1);
9871 SDValue RHS1 = Extract128BitVector(RHS, Idx0, DAG, dl);
9872 SDValue RHS2 = Extract128BitVector(RHS, Idx1, DAG, dl);
9873
9874 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9875 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
9876
9877 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
9878 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
9879 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
9880}
9881
9882SDValue X86TargetLowering::LowerADD(SDValue Op, SelectionDAG &DAG) const {
9883 assert(Op.getValueType().getSizeInBits() == 256 &&
9884 Op.getValueType().isInteger() &&
9885 "Only handle AVX 256-bit vector integer operation");
9886 return Lower256IntArith(Op, DAG);
9887}
9888
9889SDValue X86TargetLowering::LowerSUB(SDValue Op, SelectionDAG &DAG) const {
9890 assert(Op.getValueType().getSizeInBits() == 256 &&
9891 Op.getValueType().isInteger() &&
9892 "Only handle AVX 256-bit vector integer operation");
9893 return Lower256IntArith(Op, DAG);
9894}
9895
9896SDValue X86TargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
9897 EVT VT = Op.getValueType();
9898
9899 // Decompose 256-bit ops into smaller 128-bit ops.
Craig Topperaaa643c2011-11-09 07:28:55 +00009900 if (VT.getSizeInBits() == 256 && !Subtarget->hasAVX2())
Craig Topper13894fa2011-08-24 06:14:18 +00009901 return Lower256IntArith(Op, DAG);
9902
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009903 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00009904
Craig Topperaaa643c2011-11-09 07:28:55 +00009905 SDValue A = Op.getOperand(0);
9906 SDValue B = Op.getOperand(1);
9907
9908 if (VT == MVT::v4i64) {
9909 assert(Subtarget->hasAVX2() && "Lowering v4i64 multiply requires AVX2");
9910
9911 // ulong2 Ahi = __builtin_ia32_psrlqi256( a, 32);
9912 // ulong2 Bhi = __builtin_ia32_psrlqi256( b, 32);
9913 // ulong2 AloBlo = __builtin_ia32_pmuludq256( a, b );
9914 // ulong2 AloBhi = __builtin_ia32_pmuludq256( a, Bhi );
9915 // ulong2 AhiBlo = __builtin_ia32_pmuludq256( Ahi, b );
9916 //
9917 // AloBhi = __builtin_ia32_psllqi256( AloBhi, 32 );
9918 // AhiBlo = __builtin_ia32_psllqi256( AhiBlo, 32 );
9919 // return AloBlo + AloBhi + AhiBlo;
9920
9921 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9922 DAG.getConstant(Intrinsic::x86_avx2_psrli_q, MVT::i32),
9923 A, DAG.getConstant(32, MVT::i32));
9924 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9925 DAG.getConstant(Intrinsic::x86_avx2_psrli_q, MVT::i32),
9926 B, DAG.getConstant(32, MVT::i32));
9927 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9928 DAG.getConstant(Intrinsic::x86_avx2_pmulu_dq, MVT::i32),
9929 A, B);
9930 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9931 DAG.getConstant(Intrinsic::x86_avx2_pmulu_dq, MVT::i32),
9932 A, Bhi);
9933 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9934 DAG.getConstant(Intrinsic::x86_avx2_pmulu_dq, MVT::i32),
9935 Ahi, B);
9936 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9937 DAG.getConstant(Intrinsic::x86_avx2_pslli_q, MVT::i32),
9938 AloBhi, DAG.getConstant(32, MVT::i32));
9939 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9940 DAG.getConstant(Intrinsic::x86_avx2_pslli_q, MVT::i32),
9941 AhiBlo, DAG.getConstant(32, MVT::i32));
9942 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
9943 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
9944 return Res;
9945 }
9946
9947 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
9948
Mon P Wangaf9b9522008-12-18 21:42:19 +00009949 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
9950 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
9951 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
9952 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
9953 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
9954 //
9955 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
9956 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
9957 // return AloBlo + AloBhi + AhiBlo;
9958
Dale Johannesene4d209d2009-02-03 20:21:25 +00009959 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009960 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
9961 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009962 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009963 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
9964 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009965 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009966 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00009967 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009968 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009969 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00009970 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009971 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009972 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00009973 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009974 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009975 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
9976 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009977 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009978 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
9979 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009980 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
9981 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00009982 return Res;
9983}
9984
Nadav Rotem43012222011-05-11 08:12:09 +00009985SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
9986
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009987 EVT VT = Op.getValueType();
9988 DebugLoc dl = Op.getDebugLoc();
9989 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +00009990 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009991 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009992
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00009993 if (!Subtarget->hasXMMInt())
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00009994 return SDValue();
9995
Nadav Rotem43012222011-05-11 08:12:09 +00009996 // Optimize shl/srl/sra with constant shift amount.
9997 if (isSplatVector(Amt.getNode())) {
9998 SDValue SclrAmt = Amt->getOperand(0);
9999 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
10000 uint64_t ShiftAmt = C->getZExtValue();
10001
Benjamin Kramerdade3c12011-10-30 17:31:21 +000010002 if (VT == MVT::v16i8 && Op.getOpcode() == ISD::SHL) {
10003 // Make a large shift.
10004 SDValue SHL =
10005 DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10006 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
10007 R, DAG.getConstant(ShiftAmt, MVT::i32));
10008 // Zero out the rightmost bits.
10009 SmallVector<SDValue, 16> V(16, DAG.getConstant(uint8_t(-1U << ShiftAmt),
10010 MVT::i8));
10011 return DAG.getNode(ISD::AND, dl, VT, SHL,
10012 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
10013 }
10014
Nadav Rotem43012222011-05-11 08:12:09 +000010015 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SHL)
10016 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10017 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
10018 R, DAG.getConstant(ShiftAmt, MVT::i32));
10019
10020 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SHL)
10021 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10022 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
10023 R, DAG.getConstant(ShiftAmt, MVT::i32));
10024
10025 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SHL)
10026 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10027 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
10028 R, DAG.getConstant(ShiftAmt, MVT::i32));
10029
Benjamin Kramerdade3c12011-10-30 17:31:21 +000010030 if (VT == MVT::v16i8 && Op.getOpcode() == ISD::SRL) {
10031 // Make a large shift.
10032 SDValue SRL =
10033 DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10034 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
10035 R, DAG.getConstant(ShiftAmt, MVT::i32));
10036 // Zero out the leftmost bits.
10037 SmallVector<SDValue, 16> V(16, DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
10038 MVT::i8));
10039 return DAG.getNode(ISD::AND, dl, VT, SRL,
10040 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
10041 }
10042
Nadav Rotem43012222011-05-11 08:12:09 +000010043 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SRL)
10044 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10045 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
10046 R, DAG.getConstant(ShiftAmt, MVT::i32));
10047
10048 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRL)
10049 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10050 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
10051 R, DAG.getConstant(ShiftAmt, MVT::i32));
10052
10053 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRL)
10054 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10055 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
10056 R, DAG.getConstant(ShiftAmt, MVT::i32));
10057
10058 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRA)
10059 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10060 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
10061 R, DAG.getConstant(ShiftAmt, MVT::i32));
10062
10063 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRA)
10064 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10065 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
10066 R, DAG.getConstant(ShiftAmt, MVT::i32));
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010067
10068 if (VT == MVT::v16i8 && Op.getOpcode() == ISD::SRA) {
10069 if (ShiftAmt == 7) {
10070 // R s>> 7 === R s< 0
10071 SDValue Zeros = getZeroVector(VT, true /* HasXMMInt */, DAG, dl);
10072 return DAG.getNode(X86ISD::PCMPGTB, dl, VT, Zeros, R);
10073 }
10074
10075 // R s>> a === ((R u>> a) ^ m) - m
10076 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
10077 SmallVector<SDValue, 16> V(16, DAG.getConstant(128 >> ShiftAmt,
10078 MVT::i8));
10079 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16);
10080 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
10081 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
10082 return Res;
10083 }
Craig Topper46154eb2011-11-11 07:39:23 +000010084
Craig Topper0d86d462011-11-20 00:12:05 +000010085 if (Subtarget->hasAVX2() && VT == MVT::v32i8) {
10086 if (Op.getOpcode() == ISD::SHL) {
10087 // Make a large shift.
10088 SDValue SHL =
10089 DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10090 DAG.getConstant(Intrinsic::x86_avx2_pslli_w, MVT::i32),
10091 R, DAG.getConstant(ShiftAmt, MVT::i32));
10092 // Zero out the rightmost bits.
10093 SmallVector<SDValue, 32> V(32, DAG.getConstant(uint8_t(-1U << ShiftAmt),
10094 MVT::i8));
10095 return DAG.getNode(ISD::AND, dl, VT, SHL,
10096 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
Craig Topper46154eb2011-11-11 07:39:23 +000010097 }
Craig Topper0d86d462011-11-20 00:12:05 +000010098 if (Op.getOpcode() == ISD::SRL) {
10099 // Make a large shift.
10100 SDValue SRL =
10101 DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10102 DAG.getConstant(Intrinsic::x86_avx2_psrli_w, MVT::i32),
10103 R, DAG.getConstant(ShiftAmt, MVT::i32));
10104 // Zero out the leftmost bits.
10105 SmallVector<SDValue, 32> V(32, DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
10106 MVT::i8));
10107 return DAG.getNode(ISD::AND, dl, VT, SRL,
10108 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
10109 }
10110 if (Op.getOpcode() == ISD::SRA) {
10111 if (ShiftAmt == 7) {
10112 // R s>> 7 === R s< 0
10113 SDValue Zeros = getZeroVector(VT, true /* HasXMMInt */, DAG, dl);
10114 return DAG.getNode(X86ISD::PCMPGTB, dl, VT, Zeros, R);
10115 }
10116
10117 // R s>> a === ((R u>> a) ^ m) - m
10118 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
10119 SmallVector<SDValue, 32> V(32, DAG.getConstant(128 >> ShiftAmt,
10120 MVT::i8));
10121 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32);
10122 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
10123 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
10124 return Res;
10125 }
10126 }
Nadav Rotem43012222011-05-11 08:12:09 +000010127 }
10128 }
10129
10130 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +000010131 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +000010132 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10133 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
10134 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
10135
10136 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010137
Nate Begeman51409212010-07-28 00:21:48 +000010138 std::vector<Constant*> CV(4, CI);
10139 Constant *C = ConstantVector::get(CV);
10140 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10141 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010142 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010143 false, false, false, 16);
Nate Begeman51409212010-07-28 00:21:48 +000010144
10145 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010146 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +000010147 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
10148 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
10149 }
Nadav Rotem43012222011-05-11 08:12:09 +000010150 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +000010151 // a = a << 5;
10152 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10153 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
10154 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
10155
10156 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
10157 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
10158
10159 std::vector<Constant*> CVM1(16, CM1);
10160 std::vector<Constant*> CVM2(16, CM2);
10161 Constant *C = ConstantVector::get(CVM1);
10162 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10163 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010164 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010165 false, false, false, 16);
Nate Begeman51409212010-07-28 00:21:48 +000010166
10167 // r = pblendv(r, psllw(r & (char16)15, 4), a);
10168 M = DAG.getNode(ISD::AND, dl, VT, R, M);
10169 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10170 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
10171 DAG.getConstant(4, MVT::i32));
Nadav Rotemfbad25e2011-09-11 15:02:23 +000010172 R = DAG.getNode(ISD::VSELECT, dl, VT, Op, R, M);
Nate Begeman51409212010-07-28 00:21:48 +000010173 // a += a
10174 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010175
Nate Begeman51409212010-07-28 00:21:48 +000010176 C = ConstantVector::get(CVM2);
10177 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10178 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010179 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010180 false, false, false, 16);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010181
Nate Begeman51409212010-07-28 00:21:48 +000010182 // r = pblendv(r, psllw(r & (char16)63, 2), a);
10183 M = DAG.getNode(ISD::AND, dl, VT, R, M);
10184 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10185 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
10186 DAG.getConstant(2, MVT::i32));
Nadav Rotemfbad25e2011-09-11 15:02:23 +000010187 R = DAG.getNode(ISD::VSELECT, dl, VT, Op, R, M);
Nate Begeman51409212010-07-28 00:21:48 +000010188 // a += a
10189 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010190
Nate Begeman51409212010-07-28 00:21:48 +000010191 // return pblendv(r, r+r, a);
Nadav Rotemfbad25e2011-09-11 15:02:23 +000010192 R = DAG.getNode(ISD::VSELECT, dl, VT, Op,
10193 R, DAG.getNode(ISD::ADD, dl, VT, R, R));
Nate Begeman51409212010-07-28 00:21:48 +000010194 return R;
10195 }
Craig Topper46154eb2011-11-11 07:39:23 +000010196
10197 // Decompose 256-bit shifts into smaller 128-bit shifts.
10198 if (VT.getSizeInBits() == 256) {
10199 int NumElems = VT.getVectorNumElements();
10200 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10201 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10202
10203 // Extract the two vectors
10204 SDValue V1 = Extract128BitVector(R, DAG.getConstant(0, MVT::i32), DAG, dl);
10205 SDValue V2 = Extract128BitVector(R, DAG.getConstant(NumElems/2, MVT::i32),
10206 DAG, dl);
10207
10208 // Recreate the shift amount vectors
10209 SDValue Amt1, Amt2;
10210 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
10211 // Constant shift amount
10212 SmallVector<SDValue, 4> Amt1Csts;
10213 SmallVector<SDValue, 4> Amt2Csts;
10214 for (int i = 0; i < NumElems/2; ++i)
10215 Amt1Csts.push_back(Amt->getOperand(i));
10216 for (int i = NumElems/2; i < NumElems; ++i)
10217 Amt2Csts.push_back(Amt->getOperand(i));
10218
10219 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
10220 &Amt1Csts[0], NumElems/2);
10221 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
10222 &Amt2Csts[0], NumElems/2);
10223 } else {
10224 // Variable shift amount
10225 Amt1 = Extract128BitVector(Amt, DAG.getConstant(0, MVT::i32), DAG, dl);
10226 Amt2 = Extract128BitVector(Amt, DAG.getConstant(NumElems/2, MVT::i32),
10227 DAG, dl);
10228 }
10229
10230 // Issue new vector shifts for the smaller types
10231 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
10232 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
10233
10234 // Concatenate the result back
10235 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
10236 }
10237
Nate Begeman51409212010-07-28 00:21:48 +000010238 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010239}
Mon P Wangaf9b9522008-12-18 21:42:19 +000010240
Dan Gohmand858e902010-04-17 15:26:15 +000010241SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +000010242 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
10243 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +000010244 // looks for this combo and may remove the "setcc" instruction if the "setcc"
10245 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +000010246 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +000010247 SDValue LHS = N->getOperand(0);
10248 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +000010249 unsigned BaseOp = 0;
10250 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010251 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +000010252 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010253 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +000010254 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +000010255 // A subtract of one will be selected as a INC. Note that INC doesn't
10256 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010257 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10258 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010259 BaseOp = X86ISD::INC;
10260 Cond = X86::COND_O;
10261 break;
10262 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010263 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +000010264 Cond = X86::COND_O;
10265 break;
10266 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010267 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +000010268 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010269 break;
10270 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +000010271 // A subtract of one will be selected as a DEC. Note that DEC doesn't
10272 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010273 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10274 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010275 BaseOp = X86ISD::DEC;
10276 Cond = X86::COND_O;
10277 break;
10278 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010279 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +000010280 Cond = X86::COND_O;
10281 break;
10282 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010283 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +000010284 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010285 break;
10286 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000010287 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000010288 Cond = X86::COND_O;
10289 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010290 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
10291 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
10292 MVT::i32);
10293 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010294
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010295 SDValue SetCC =
10296 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10297 DAG.getConstant(X86::COND_O, MVT::i32),
10298 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010299
Dan Gohman6e5fda22011-07-22 18:45:15 +000010300 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010301 }
Bill Wendling74c37652008-12-09 22:08:41 +000010302 }
Bill Wendling3fafd932008-11-26 22:37:40 +000010303
Bill Wendling61edeb52008-12-02 01:06:39 +000010304 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010305 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010306 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000010307
Bill Wendling61edeb52008-12-02 01:06:39 +000010308 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010309 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
10310 DAG.getConstant(Cond, MVT::i32),
10311 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000010312
Dan Gohman6e5fda22011-07-22 18:45:15 +000010313 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000010314}
10315
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010316SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op, SelectionDAG &DAG) const{
10317 DebugLoc dl = Op.getDebugLoc();
Craig Toppera124f942011-11-21 01:12:36 +000010318 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
10319 EVT VT = Op.getValueType();
10320
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010321 if (Subtarget->hasXMMInt() && VT.isVector()) {
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010322 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
10323 ExtraVT.getScalarType().getSizeInBits();
10324 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
10325
10326 unsigned SHLIntrinsicsID = 0;
10327 unsigned SRAIntrinsicsID = 0;
10328 switch (VT.getSimpleVT().SimpleTy) {
10329 default:
10330 return SDValue();
Craig Toppera124f942011-11-21 01:12:36 +000010331 case MVT::v4i32:
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010332 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_d;
10333 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_d;
10334 break;
Craig Toppera124f942011-11-21 01:12:36 +000010335 case MVT::v8i16:
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010336 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_w;
10337 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_w;
10338 break;
Craig Toppera124f942011-11-21 01:12:36 +000010339 case MVT::v8i32:
10340 case MVT::v16i16:
10341 if (!Subtarget->hasAVX())
10342 return SDValue();
10343 if (!Subtarget->hasAVX2()) {
10344 // needs to be split
10345 int NumElems = VT.getVectorNumElements();
10346 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
10347 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
10348
10349 // Extract the LHS vectors
10350 SDValue LHS = Op.getOperand(0);
10351 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
10352 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
10353
10354 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10355 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10356
10357 EVT ExtraEltVT = ExtraVT.getVectorElementType();
10358 int ExtraNumElems = ExtraVT.getVectorNumElements();
10359 ExtraVT = EVT::getVectorVT(*DAG.getContext(), ExtraEltVT,
10360 ExtraNumElems/2);
10361 SDValue Extra = DAG.getValueType(ExtraVT);
10362
10363 LHS1 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, Extra);
10364 LHS2 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, Extra);
10365
10366 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, LHS1, LHS2);;
10367 }
10368 if (VT == MVT::v8i32) {
10369 SHLIntrinsicsID = Intrinsic::x86_avx2_pslli_d;
10370 SRAIntrinsicsID = Intrinsic::x86_avx2_psrai_d;
10371 } else {
10372 SHLIntrinsicsID = Intrinsic::x86_avx2_pslli_w;
10373 SRAIntrinsicsID = Intrinsic::x86_avx2_psrai_w;
10374 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010375 }
10376
10377 SDValue Tmp1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10378 DAG.getConstant(SHLIntrinsicsID, MVT::i32),
Craig Toppera124f942011-11-21 01:12:36 +000010379 Op.getOperand(0), ShAmt);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010380
Nadav Rotema7934dd2011-10-10 19:31:45 +000010381 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10382 DAG.getConstant(SRAIntrinsicsID, MVT::i32),
10383 Tmp1, ShAmt);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010384 }
10385
10386 return SDValue();
10387}
10388
10389
Eric Christopher9a9d2752010-07-22 02:48:34 +000010390SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
10391 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010392
Eric Christopher77ed1352011-07-08 00:04:56 +000010393 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
10394 // There isn't any reason to disable it if the target processor supports it.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010395 if (!Subtarget->hasXMMInt() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +000010396 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +000010397 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +000010398 SDValue Ops[] = {
10399 DAG.getRegister(X86::ESP, MVT::i32), // Base
10400 DAG.getTargetConstant(1, MVT::i8), // Scale
10401 DAG.getRegister(0, MVT::i32), // Index
10402 DAG.getTargetConstant(0, MVT::i32), // Disp
10403 DAG.getRegister(0, MVT::i32), // Segment.
10404 Zero,
10405 Chain
10406 };
Michael J. Spencerec38de22010-10-10 22:04:20 +000010407 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +000010408 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
10409 array_lengthof(Ops));
10410 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +000010411 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010412
Eric Christopher9a9d2752010-07-22 02:48:34 +000010413 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +000010414 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +000010415 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010416
Chris Lattner132929a2010-08-14 17:26:09 +000010417 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10418 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
10419 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
10420 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010421
Chris Lattner132929a2010-08-14 17:26:09 +000010422 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
10423 if (!Op1 && !Op2 && !Op3 && Op4)
10424 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010425
Chris Lattner132929a2010-08-14 17:26:09 +000010426 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
10427 if (Op1 && !Op2 && !Op3 && !Op4)
10428 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010429
10430 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +000010431 // (MFENCE)>;
10432 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +000010433}
10434
Eli Friedman14648462011-07-27 22:21:52 +000010435SDValue X86TargetLowering::LowerATOMIC_FENCE(SDValue Op,
10436 SelectionDAG &DAG) const {
10437 DebugLoc dl = Op.getDebugLoc();
10438 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
10439 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
10440 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
10441 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
10442
10443 // The only fence that needs an instruction is a sequentially-consistent
10444 // cross-thread fence.
10445 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
10446 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
10447 // no-sse2). There isn't any reason to disable it if the target processor
10448 // supports it.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010449 if (Subtarget->hasXMMInt() || Subtarget->is64Bit())
Eli Friedman14648462011-07-27 22:21:52 +000010450 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
10451
10452 SDValue Chain = Op.getOperand(0);
10453 SDValue Zero = DAG.getConstant(0, MVT::i32);
10454 SDValue Ops[] = {
10455 DAG.getRegister(X86::ESP, MVT::i32), // Base
10456 DAG.getTargetConstant(1, MVT::i8), // Scale
10457 DAG.getRegister(0, MVT::i32), // Index
10458 DAG.getTargetConstant(0, MVT::i32), // Disp
10459 DAG.getRegister(0, MVT::i32), // Segment.
10460 Zero,
10461 Chain
10462 };
10463 SDNode *Res =
10464 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
10465 array_lengthof(Ops));
10466 return SDValue(Res, 0);
10467 }
10468
10469 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
10470 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
10471}
10472
10473
Dan Gohmand858e902010-04-17 15:26:15 +000010474SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010475 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010476 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000010477 unsigned Reg = 0;
10478 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000010479 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000010480 default:
10481 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000010482 case MVT::i8: Reg = X86::AL; size = 1; break;
10483 case MVT::i16: Reg = X86::AX; size = 2; break;
10484 case MVT::i32: Reg = X86::EAX; size = 4; break;
10485 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000010486 assert(Subtarget->is64Bit() && "Node not type legal!");
10487 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000010488 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000010489 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010490 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000010491 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000010492 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000010493 Op.getOperand(1),
10494 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000010495 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000010496 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010497 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010498 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
10499 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
10500 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000010501 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010502 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000010503 return cpOut;
10504}
10505
Duncan Sands1607f052008-12-01 11:39:25 +000010506SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010507 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +000010508 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010509 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000010510 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010511 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +000010512 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010513 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
10514 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000010515 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000010516 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
10517 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000010518 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000010519 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000010520 rdx.getValue(1)
10521 };
Dale Johannesene4d209d2009-02-03 20:21:25 +000010522 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010523}
10524
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010525SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +000010526 SelectionDAG &DAG) const {
10527 EVT SrcVT = Op.getOperand(0).getValueType();
10528 EVT DstVT = Op.getValueType();
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010529 assert(Subtarget->is64Bit() && !Subtarget->hasXMMInt() &&
Chris Lattner2a786eb2010-12-19 20:19:20 +000010530 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000010531 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000010532 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010533 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000010534 // i64 <=> MMX conversions are Legal.
10535 if (SrcVT==MVT::i64 && DstVT.isVector())
10536 return Op;
10537 if (DstVT==MVT::i64 && SrcVT.isVector())
10538 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000010539 // MMX <=> MMX conversions are Legal.
10540 if (SrcVT.isVector() && DstVT.isVector())
10541 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000010542 // All other conversions need to be expanded.
10543 return SDValue();
10544}
Chris Lattner5b856542010-12-20 00:59:46 +000010545
Dan Gohmand858e902010-04-17 15:26:15 +000010546SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000010547 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +000010548 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000010549 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010550 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000010551 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010552 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010553 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000010554 Node->getOperand(0),
10555 Node->getOperand(1), negOp,
10556 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000010557 cast<AtomicSDNode>(Node)->getAlignment(),
10558 cast<AtomicSDNode>(Node)->getOrdering(),
10559 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000010560}
10561
Eli Friedman327236c2011-08-24 20:50:09 +000010562static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
10563 SDNode *Node = Op.getNode();
10564 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010565 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000010566
10567 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010568 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
10569 // FIXME: On 32-bit, store -> fist or movq would be more efficient
10570 // (The only way to get a 16-byte store is cmpxchg16b)
10571 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
10572 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
10573 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000010574 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
10575 cast<AtomicSDNode>(Node)->getMemoryVT(),
10576 Node->getOperand(0),
10577 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010578 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000010579 cast<AtomicSDNode>(Node)->getOrdering(),
10580 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000010581 return Swap.getValue(1);
10582 }
10583 // Other atomic stores have a simple pattern.
10584 return Op;
10585}
10586
Chris Lattner5b856542010-12-20 00:59:46 +000010587static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
10588 EVT VT = Op.getNode()->getValueType(0);
10589
10590 // Let legalize expand this if it isn't a legal type yet.
10591 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
10592 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010593
Chris Lattner5b856542010-12-20 00:59:46 +000010594 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010595
Chris Lattner5b856542010-12-20 00:59:46 +000010596 unsigned Opc;
10597 bool ExtraOp = false;
10598 switch (Op.getOpcode()) {
10599 default: assert(0 && "Invalid code");
10600 case ISD::ADDC: Opc = X86ISD::ADD; break;
10601 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
10602 case ISD::SUBC: Opc = X86ISD::SUB; break;
10603 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
10604 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010605
Chris Lattner5b856542010-12-20 00:59:46 +000010606 if (!ExtraOp)
10607 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
10608 Op.getOperand(1));
10609 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
10610 Op.getOperand(1), Op.getOperand(2));
10611}
10612
Evan Cheng0db9fe62006-04-25 20:13:52 +000010613/// LowerOperation - Provide custom lowering hooks for some operations.
10614///
Dan Gohmand858e902010-04-17 15:26:15 +000010615SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000010616 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010617 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010618 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Eric Christopher9a9d2752010-07-22 02:48:34 +000010619 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Eli Friedman14648462011-07-27 22:21:52 +000010620 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010621 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
10622 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000010623 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010624 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000010625 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010626 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
10627 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
10628 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +000010629 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +000010630 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010631 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
10632 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
10633 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000010634 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000010635 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000010636 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010637 case ISD::SHL_PARTS:
10638 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000010639 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010640 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000010641 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010642 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000010643 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010644 case ISD::FABS: return LowerFABS(Op, DAG);
10645 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000010646 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000010647 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000010648 case ISD::SETCC: return LowerSETCC(Op, DAG);
10649 case ISD::SELECT: return LowerSELECT(Op, DAG);
10650 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010651 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010652 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000010653 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +000010654 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010655 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000010656 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
10657 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010658 case ISD::FRAME_TO_ARGS_OFFSET:
10659 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010660 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010661 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +000010662 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
10663 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000010664 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000010665 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
10666 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000010667 case ISD::MUL: return LowerMUL(Op, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000010668 case ISD::SRA:
10669 case ISD::SRL:
10670 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000010671 case ISD::SADDO:
10672 case ISD::UADDO:
10673 case ISD::SSUBO:
10674 case ISD::USUBO:
10675 case ISD::SMULO:
10676 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +000010677 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010678 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000010679 case ISD::ADDC:
10680 case ISD::ADDE:
10681 case ISD::SUBC:
10682 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000010683 case ISD::ADD: return LowerADD(Op, DAG);
10684 case ISD::SUB: return LowerSUB(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010685 }
Chris Lattner27a6c732007-11-24 07:07:01 +000010686}
10687
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010688static void ReplaceATOMIC_LOAD(SDNode *Node,
10689 SmallVectorImpl<SDValue> &Results,
10690 SelectionDAG &DAG) {
10691 DebugLoc dl = Node->getDebugLoc();
10692 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
10693
10694 // Convert wide load -> cmpxchg8b/cmpxchg16b
10695 // FIXME: On 32-bit, load -> fild or movq would be more efficient
10696 // (The only way to get a 16-byte load is cmpxchg16b)
10697 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000010698 SDValue Zero = DAG.getConstant(0, VT);
10699 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010700 Node->getOperand(0),
10701 Node->getOperand(1), Zero, Zero,
10702 cast<AtomicSDNode>(Node)->getMemOperand(),
10703 cast<AtomicSDNode>(Node)->getOrdering(),
10704 cast<AtomicSDNode>(Node)->getSynchScope());
10705 Results.push_back(Swap.getValue(0));
10706 Results.push_back(Swap.getValue(1));
10707}
10708
Duncan Sands1607f052008-12-01 11:39:25 +000010709void X86TargetLowering::
10710ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000010711 SelectionDAG &DAG, unsigned NewOp) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010712 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +000010713 assert (Node->getValueType(0) == MVT::i64 &&
10714 "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000010715
10716 SDValue Chain = Node->getOperand(0);
10717 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010718 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010719 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000010720 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010721 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000010722 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000010723 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000010724 SDValue Result =
10725 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
10726 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000010727 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000010728 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010729 Results.push_back(Result.getValue(2));
10730}
10731
Duncan Sands126d9072008-07-04 11:47:58 +000010732/// ReplaceNodeResults - Replace a node with an illegal result type
10733/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000010734void X86TargetLowering::ReplaceNodeResults(SDNode *N,
10735 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000010736 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010737 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +000010738 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000010739 default:
Duncan Sands1607f052008-12-01 11:39:25 +000010740 assert(false && "Do not know how to custom type legalize this operation!");
10741 return;
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010742 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000010743 case ISD::ADDC:
10744 case ISD::ADDE:
10745 case ISD::SUBC:
10746 case ISD::SUBE:
10747 // We don't want to expand or promote these.
10748 return;
Duncan Sands1607f052008-12-01 11:39:25 +000010749 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +000010750 std::pair<SDValue,SDValue> Vals =
10751 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +000010752 SDValue FIST = Vals.first, StackSlot = Vals.second;
10753 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000010754 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000010755 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +000010756 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +000010757 MachinePointerInfo(),
10758 false, false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +000010759 }
10760 return;
10761 }
10762 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010763 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000010764 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010765 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010766 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000010767 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000010768 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010769 eax.getValue(2));
10770 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
10771 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +000010772 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010773 Results.push_back(edx.getValue(1));
10774 return;
10775 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010776 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000010777 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000010778 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000010779 bool Regs64bit = T == MVT::i128;
10780 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000010781 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000010782 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
10783 DAG.getConstant(0, HalfT));
10784 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
10785 DAG.getConstant(1, HalfT));
10786 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
10787 Regs64bit ? X86::RAX : X86::EAX,
10788 cpInL, SDValue());
10789 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
10790 Regs64bit ? X86::RDX : X86::EDX,
10791 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000010792 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000010793 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
10794 DAG.getConstant(0, HalfT));
10795 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
10796 DAG.getConstant(1, HalfT));
10797 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
10798 Regs64bit ? X86::RBX : X86::EBX,
10799 swapInL, cpInH.getValue(1));
10800 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
10801 Regs64bit ? X86::RCX : X86::ECX,
10802 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000010803 SDValue Ops[] = { swapInH.getValue(0),
10804 N->getOperand(1),
10805 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010806 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000010807 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000010808 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
10809 X86ISD::LCMPXCHG8_DAG;
10810 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000010811 Ops, 3, T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000010812 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
10813 Regs64bit ? X86::RAX : X86::EAX,
10814 HalfT, Result.getValue(1));
10815 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
10816 Regs64bit ? X86::RDX : X86::EDX,
10817 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000010818 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000010819 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010820 Results.push_back(cpOutH.getValue(1));
10821 return;
10822 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010823 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +000010824 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
10825 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010826 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +000010827 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
10828 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010829 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +000010830 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
10831 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010832 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +000010833 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
10834 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010835 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +000010836 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
10837 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010838 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +000010839 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
10840 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010841 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +000010842 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
10843 return;
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010844 case ISD::ATOMIC_LOAD:
10845 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000010846 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000010847}
10848
Evan Cheng72261582005-12-20 06:22:03 +000010849const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
10850 switch (Opcode) {
10851 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000010852 case X86ISD::BSF: return "X86ISD::BSF";
10853 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000010854 case X86ISD::SHLD: return "X86ISD::SHLD";
10855 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000010856 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000010857 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000010858 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000010859 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000010860 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000010861 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000010862 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
10863 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
10864 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000010865 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000010866 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000010867 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000010868 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000010869 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000010870 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000010871 case X86ISD::COMI: return "X86ISD::COMI";
10872 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000010873 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000010874 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000010875 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
10876 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000010877 case X86ISD::CMOV: return "X86ISD::CMOV";
10878 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000010879 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000010880 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
10881 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000010882 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000010883 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000010884 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000010885 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000010886 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000010887 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
10888 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000010889 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000010890 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000010891 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Craig Topper31133842011-11-19 07:33:10 +000010892 case X86ISD::PSIGN: return "X86ISD::PSIGN";
Craig Toppere6a62772011-11-13 17:31:07 +000010893 case X86ISD::BLENDV: return "X86ISD::BLENDV";
10894 case X86ISD::FHADD: return "X86ISD::FHADD";
10895 case X86ISD::FHSUB: return "X86ISD::FHSUB";
Evan Cheng8ca29322006-11-10 21:43:37 +000010896 case X86ISD::FMAX: return "X86ISD::FMAX";
10897 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +000010898 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
10899 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000010900 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000010901 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010902 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000010903 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010904 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +000010905 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
10906 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010907 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
10908 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
10909 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
10910 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
10911 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
10912 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000010913 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
10914 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +000010915 case X86ISD::VSHL: return "X86ISD::VSHL";
10916 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +000010917 case X86ISD::CMPPD: return "X86ISD::CMPPD";
10918 case X86ISD::CMPPS: return "X86ISD::CMPPS";
10919 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
10920 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
10921 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
10922 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
10923 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
10924 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
10925 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
10926 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010927 case X86ISD::ADD: return "X86ISD::ADD";
10928 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000010929 case X86ISD::ADC: return "X86ISD::ADC";
10930 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000010931 case X86ISD::SMUL: return "X86ISD::SMUL";
10932 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000010933 case X86ISD::INC: return "X86ISD::INC";
10934 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000010935 case X86ISD::OR: return "X86ISD::OR";
10936 case X86ISD::XOR: return "X86ISD::XOR";
10937 case X86ISD::AND: return "X86ISD::AND";
Craig Topper54a11172011-10-14 07:06:56 +000010938 case X86ISD::ANDN: return "X86ISD::ANDN";
Craig Toppere6a62772011-11-13 17:31:07 +000010939 case X86ISD::BLSI: return "X86ISD::BLSI";
10940 case X86ISD::BLSMSK: return "X86ISD::BLSMSK";
10941 case X86ISD::BLSR: return "X86ISD::BLSR";
Evan Cheng73f24c92009-03-30 21:36:47 +000010942 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000010943 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010944 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010945 case X86ISD::PALIGN: return "X86ISD::PALIGN";
10946 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
10947 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
10948 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
10949 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
10950 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
10951 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
10952 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
10953 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010954 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000010955 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010956 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000010957 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
10958 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010959 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
10960 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
10961 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
10962 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
10963 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
10964 case X86ISD::MOVSD: return "X86ISD::MOVSD";
10965 case X86ISD::MOVSS: return "X86ISD::MOVSS";
Craig Topper34671b82011-12-06 08:21:25 +000010966 case X86ISD::UNPCKL: return "X86ISD::UNPCKL";
10967 case X86ISD::UNPCKH: return "X86ISD::UNPCKH";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000010968 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Craig Topper316cd2a2011-11-30 06:25:25 +000010969 case X86ISD::VPERMILP: return "X86ISD::VPERMILP";
Craig Topperec24e612011-11-30 07:47:51 +000010970 case X86ISD::VPERM2X128: return "X86ISD::VPERM2X128";
Dan Gohmand6708ea2009-08-15 01:38:56 +000010971 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000010972 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010973 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000010974 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000010975 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +000010976 }
10977}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010978
Chris Lattnerc9addb72007-03-30 23:15:24 +000010979// isLegalAddressingMode - Return true if the addressing mode represented
10980// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000010981bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010982 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000010983 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010984 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000010985 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000010986
Chris Lattnerc9addb72007-03-30 23:15:24 +000010987 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010988 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000010989 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000010990
Chris Lattnerc9addb72007-03-30 23:15:24 +000010991 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000010992 unsigned GVFlags =
10993 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010994
Chris Lattnerdfed4132009-07-10 07:38:24 +000010995 // If a reference to this global requires an extra load, we can't fold it.
10996 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000010997 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010998
Chris Lattnerdfed4132009-07-10 07:38:24 +000010999 // If BaseGV requires a register for the PIC base, we cannot also have a
11000 // BaseReg specified.
11001 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000011002 return false;
Evan Cheng52787842007-08-01 23:46:47 +000011003
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011004 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000011005 if ((M != CodeModel::Small || R != Reloc::Static) &&
11006 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011007 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000011008 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011009
Chris Lattnerc9addb72007-03-30 23:15:24 +000011010 switch (AM.Scale) {
11011 case 0:
11012 case 1:
11013 case 2:
11014 case 4:
11015 case 8:
11016 // These scales always work.
11017 break;
11018 case 3:
11019 case 5:
11020 case 9:
11021 // These scales are formed with basereg+scalereg. Only accept if there is
11022 // no basereg yet.
11023 if (AM.HasBaseReg)
11024 return false;
11025 break;
11026 default: // Other stuff never works.
11027 return false;
11028 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011029
Chris Lattnerc9addb72007-03-30 23:15:24 +000011030 return true;
11031}
11032
11033
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011034bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011035 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000011036 return false;
Evan Chenge127a732007-10-29 07:57:50 +000011037 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
11038 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000011039 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +000011040 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000011041 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +000011042}
11043
Owen Andersone50ed302009-08-10 22:56:29 +000011044bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000011045 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011046 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000011047 unsigned NumBits1 = VT1.getSizeInBits();
11048 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000011049 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011050 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000011051 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011052}
Evan Cheng2bd122c2007-10-26 01:56:11 +000011053
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011054bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000011055 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011056 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000011057}
11058
Owen Andersone50ed302009-08-10 22:56:29 +000011059bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000011060 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000011061 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000011062}
11063
Owen Andersone50ed302009-08-10 22:56:29 +000011064bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000011065 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000011066 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000011067}
11068
Evan Cheng60c07e12006-07-05 22:17:51 +000011069/// isShuffleMaskLegal - Targets can use this to indicate that they only
11070/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
11071/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
11072/// are assumed to be legal.
11073bool
Eric Christopherfd179292009-08-27 18:07:15 +000011074X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000011075 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000011076 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000011077 if (VT.getSizeInBits() == 64)
Craig Topper1dc0fbc2011-12-05 07:27:14 +000011078 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +000011079
Nate Begemana09008b2009-10-19 02:17:23 +000011080 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000011081 return (VT.getVectorNumElements() == 2 ||
11082 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
11083 isMOVLMask(M, VT) ||
11084 isSHUFPMask(M, VT) ||
11085 isPSHUFDMask(M, VT) ||
11086 isPSHUFHWMask(M, VT) ||
11087 isPSHUFLWMask(M, VT) ||
Craig Topperc0d82852011-11-22 00:44:41 +000011088 isPALIGNRMask(M, VT, Subtarget->hasSSSE3orAVX()) ||
Craig Topper6347e862011-11-21 06:57:39 +000011089 isUNPCKLMask(M, VT, Subtarget->hasAVX2()) ||
11090 isUNPCKHMask(M, VT, Subtarget->hasAVX2()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000011091 isUNPCKL_v_undef_Mask(M, VT) ||
11092 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +000011093}
11094
Dan Gohman7d8143f2008-04-09 20:09:42 +000011095bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000011096X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000011097 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000011098 unsigned NumElts = VT.getVectorNumElements();
11099 // FIXME: This collection of masks seems suspect.
11100 if (NumElts == 2)
11101 return true;
11102 if (NumElts == 4 && VT.getSizeInBits() == 128) {
11103 return (isMOVLMask(Mask, VT) ||
11104 isCommutedMOVLMask(Mask, VT, true) ||
11105 isSHUFPMask(Mask, VT) ||
Craig Topper1ff73d72011-12-06 04:59:07 +000011106 isSHUFPMask(Mask, VT, /* Commuted */ true));
Evan Cheng60c07e12006-07-05 22:17:51 +000011107 }
11108 return false;
11109}
11110
11111//===----------------------------------------------------------------------===//
11112// X86 Scheduler Hooks
11113//===----------------------------------------------------------------------===//
11114
Mon P Wang63307c32008-05-05 19:05:59 +000011115// private utility function
11116MachineBasicBlock *
11117X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
11118 MachineBasicBlock *MBB,
11119 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011120 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011121 unsigned LoadOpc,
11122 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011123 unsigned notOpc,
11124 unsigned EAXreg,
11125 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011126 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000011127 // For the atomic bitwise operator, we generate
11128 // thisMBB:
11129 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000011130 // ld t1 = [bitinstr.addr]
11131 // op t2 = t1, [bitinstr.val]
11132 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000011133 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
11134 // bz newMBB
11135 // fallthrough -->nextMBB
11136 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11137 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011138 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000011139 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011140
Mon P Wang63307c32008-05-05 19:05:59 +000011141 /// First build the CFG
11142 MachineFunction *F = MBB->getParent();
11143 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011144 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11145 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11146 F->insert(MBBIter, newMBB);
11147 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011148
Dan Gohman14152b42010-07-06 20:24:04 +000011149 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11150 nextMBB->splice(nextMBB->begin(), thisMBB,
11151 llvm::next(MachineBasicBlock::iterator(bInstr)),
11152 thisMBB->end());
11153 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011154
Mon P Wang63307c32008-05-05 19:05:59 +000011155 // Update thisMBB to fall through to newMBB
11156 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011157
Mon P Wang63307c32008-05-05 19:05:59 +000011158 // newMBB jumps to itself and fall through to nextMBB
11159 newMBB->addSuccessor(nextMBB);
11160 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011161
Mon P Wang63307c32008-05-05 19:05:59 +000011162 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011163 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011164 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +000011165 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000011166 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011167 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000011168 int numArgs = bInstr->getNumOperands() - 1;
11169 for (int i=0; i < numArgs; ++i)
11170 argOpers[i] = &bInstr->getOperand(i+1);
11171
11172 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011173 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011174 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000011175
Dale Johannesen140be2d2008-08-19 18:47:28 +000011176 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011177 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011178 for (int i=0; i <= lastAddrIndx; ++i)
11179 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011180
Dale Johannesen140be2d2008-08-19 18:47:28 +000011181 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011182 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011183 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011184 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011185 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011186 tt = t1;
11187
Dale Johannesen140be2d2008-08-19 18:47:28 +000011188 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +000011189 assert((argOpers[valArgIndx]->isReg() ||
11190 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000011191 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +000011192 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011193 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000011194 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011195 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011196 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +000011197 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011198
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011199 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +000011200 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +000011201
Dale Johannesene4d209d2009-02-03 20:21:25 +000011202 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +000011203 for (int i=0; i <= lastAddrIndx; ++i)
11204 (*MIB).addOperand(*argOpers[i]);
11205 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +000011206 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011207 (*MIB).setMemRefs(bInstr->memoperands_begin(),
11208 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +000011209
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011210 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +000011211 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +000011212
Mon P Wang63307c32008-05-05 19:05:59 +000011213 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011214 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000011215
Dan Gohman14152b42010-07-06 20:24:04 +000011216 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000011217 return nextMBB;
11218}
11219
Dale Johannesen1b54c7f2008-10-03 19:41:08 +000011220// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +000011221MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011222X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
11223 MachineBasicBlock *MBB,
11224 unsigned regOpcL,
11225 unsigned regOpcH,
11226 unsigned immOpcL,
11227 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011228 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011229 // For the atomic bitwise operator, we generate
11230 // thisMBB (instructions are in pairs, except cmpxchg8b)
11231 // ld t1,t2 = [bitinstr.addr]
11232 // newMBB:
11233 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
11234 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +000011235 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011236 // mov ECX, EBX <- t5, t6
11237 // mov EAX, EDX <- t1, t2
11238 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
11239 // mov t3, t4 <- EAX, EDX
11240 // bz newMBB
11241 // result in out1, out2
11242 // fallthrough -->nextMBB
11243
11244 const TargetRegisterClass *RC = X86::GR32RegisterClass;
11245 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011246 const unsigned NotOpc = X86::NOT32r;
11247 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11248 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11249 MachineFunction::iterator MBBIter = MBB;
11250 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011251
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011252 /// First build the CFG
11253 MachineFunction *F = MBB->getParent();
11254 MachineBasicBlock *thisMBB = MBB;
11255 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11256 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11257 F->insert(MBBIter, newMBB);
11258 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011259
Dan Gohman14152b42010-07-06 20:24:04 +000011260 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11261 nextMBB->splice(nextMBB->begin(), thisMBB,
11262 llvm::next(MachineBasicBlock::iterator(bInstr)),
11263 thisMBB->end());
11264 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011265
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011266 // Update thisMBB to fall through to newMBB
11267 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011268
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011269 // newMBB jumps to itself and fall through to nextMBB
11270 newMBB->addSuccessor(nextMBB);
11271 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011272
Dale Johannesene4d209d2009-02-03 20:21:25 +000011273 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011274 // Insert instructions into newMBB based on incoming instruction
11275 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011276 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011277 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011278 MachineOperand& dest1Oper = bInstr->getOperand(0);
11279 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011280 MachineOperand* argOpers[2 + X86::AddrNumOperands];
11281 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011282 argOpers[i] = &bInstr->getOperand(i+2);
11283
Dan Gohman71ea4e52010-05-14 21:01:44 +000011284 // We use some of the operands multiple times, so conservatively just
11285 // clear any kill flags that might be present.
11286 if (argOpers[i]->isReg() && argOpers[i]->isUse())
11287 argOpers[i]->setIsKill(false);
11288 }
11289
Evan Chengad5b52f2010-01-08 19:14:57 +000011290 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011291 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +000011292
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011293 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011294 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011295 for (int i=0; i <= lastAddrIndx; ++i)
11296 (*MIB).addOperand(*argOpers[i]);
11297 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011298 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +000011299 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +000011300 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011301 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +000011302 MachineOperand newOp3 = *(argOpers[3]);
11303 if (newOp3.isImm())
11304 newOp3.setImm(newOp3.getImm()+4);
11305 else
11306 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011307 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +000011308 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011309
11310 // t3/4 are defined later, at the bottom of the loop
11311 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
11312 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011313 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011314 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011315 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011316 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
11317
Evan Cheng306b4ca2010-01-08 23:41:50 +000011318 // The subsequent operations should be using the destination registers of
11319 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +000011320 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +000011321 t1 = F->getRegInfo().createVirtualRegister(RC);
11322 t2 = F->getRegInfo().createVirtualRegister(RC);
11323 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
11324 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011325 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +000011326 t1 = dest1Oper.getReg();
11327 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011328 }
11329
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011330 int valArgIndx = lastAddrIndx + 1;
11331 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +000011332 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011333 "invalid operand");
11334 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
11335 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011336 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011337 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011338 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011339 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +000011340 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011341 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011342 (*MIB).addOperand(*argOpers[valArgIndx]);
11343 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011344 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011345 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011346 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011347 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011348 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011349 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011350 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +000011351 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011352 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011353 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011354
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011355 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011356 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011357 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011358 MIB.addReg(t2);
11359
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011360 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011361 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011362 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011363 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +000011364
Dale Johannesene4d209d2009-02-03 20:21:25 +000011365 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011366 for (int i=0; i <= lastAddrIndx; ++i)
11367 (*MIB).addOperand(*argOpers[i]);
11368
11369 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011370 (*MIB).setMemRefs(bInstr->memoperands_begin(),
11371 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011372
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011373 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011374 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011375 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011376 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000011377
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011378 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011379 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011380
Dan Gohman14152b42010-07-06 20:24:04 +000011381 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011382 return nextMBB;
11383}
11384
11385// private utility function
11386MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +000011387X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
11388 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011389 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000011390 // For the atomic min/max operator, we generate
11391 // thisMBB:
11392 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000011393 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +000011394 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +000011395 // cmp t1, t2
11396 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +000011397 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000011398 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
11399 // bz newMBB
11400 // fallthrough -->nextMBB
11401 //
11402 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11403 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011404 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000011405 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011406
Mon P Wang63307c32008-05-05 19:05:59 +000011407 /// First build the CFG
11408 MachineFunction *F = MBB->getParent();
11409 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011410 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11411 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11412 F->insert(MBBIter, newMBB);
11413 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011414
Dan Gohman14152b42010-07-06 20:24:04 +000011415 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11416 nextMBB->splice(nextMBB->begin(), thisMBB,
11417 llvm::next(MachineBasicBlock::iterator(mInstr)),
11418 thisMBB->end());
11419 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011420
Mon P Wang63307c32008-05-05 19:05:59 +000011421 // Update thisMBB to fall through to newMBB
11422 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011423
Mon P Wang63307c32008-05-05 19:05:59 +000011424 // newMBB jumps to newMBB and fall through to nextMBB
11425 newMBB->addSuccessor(nextMBB);
11426 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011427
Dale Johannesene4d209d2009-02-03 20:21:25 +000011428 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000011429 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011430 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011431 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +000011432 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011433 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000011434 int numArgs = mInstr->getNumOperands() - 1;
11435 for (int i=0; i < numArgs; ++i)
11436 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +000011437
Mon P Wang63307c32008-05-05 19:05:59 +000011438 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011439 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011440 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000011441
Mon P Wangab3e7472008-05-05 22:56:23 +000011442 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011443 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011444 for (int i=0; i <= lastAddrIndx; ++i)
11445 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +000011446
Mon P Wang63307c32008-05-05 19:05:59 +000011447 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +000011448 assert((argOpers[valArgIndx]->isReg() ||
11449 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000011450 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +000011451
11452 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +000011453 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011454 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +000011455 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011456 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000011457 (*MIB).addOperand(*argOpers[valArgIndx]);
11458
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011459 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +000011460 MIB.addReg(t1);
11461
Dale Johannesene4d209d2009-02-03 20:21:25 +000011462 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +000011463 MIB.addReg(t1);
11464 MIB.addReg(t2);
11465
11466 // Generate movc
11467 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011468 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +000011469 MIB.addReg(t2);
11470 MIB.addReg(t1);
11471
11472 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +000011473 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +000011474 for (int i=0; i <= lastAddrIndx; ++i)
11475 (*MIB).addOperand(*argOpers[i]);
11476 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +000011477 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011478 (*MIB).setMemRefs(mInstr->memoperands_begin(),
11479 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +000011480
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011481 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +000011482 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +000011483
Mon P Wang63307c32008-05-05 19:05:59 +000011484 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011485 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000011486
Dan Gohman14152b42010-07-06 20:24:04 +000011487 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000011488 return nextMBB;
11489}
11490
Eric Christopherf83a5de2009-08-27 18:08:16 +000011491// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011492// or XMM0_V32I8 in AVX all of this code can be replaced with that
11493// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +000011494MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +000011495X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +000011496 unsigned numArgs, bool memArg) const {
Craig Topperc0d82852011-11-22 00:44:41 +000011497 assert(Subtarget->hasSSE42orAVX() &&
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011498 "Target must have SSE4.2 or AVX features enabled");
11499
Eric Christopherb120ab42009-08-18 22:50:32 +000011500 DebugLoc dl = MI->getDebugLoc();
11501 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +000011502 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011503 if (!Subtarget->hasAVX()) {
11504 if (memArg)
11505 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
11506 else
11507 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
11508 } else {
11509 if (memArg)
11510 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
11511 else
11512 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
11513 }
Eric Christopherb120ab42009-08-18 22:50:32 +000011514
Eric Christopher41c902f2010-11-30 08:20:21 +000011515 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +000011516 for (unsigned i = 0; i < numArgs; ++i) {
11517 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +000011518 if (!(Op.isReg() && Op.isImplicit()))
11519 MIB.addOperand(Op);
11520 }
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011521 BuildMI(*BB, MI, dl,
11522 TII->get(Subtarget->hasAVX() ? X86::VMOVAPSrr : X86::MOVAPSrr),
11523 MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000011524 .addReg(X86::XMM0);
11525
Dan Gohman14152b42010-07-06 20:24:04 +000011526 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000011527 return BB;
11528}
11529
11530MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +000011531X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000011532 DebugLoc dl = MI->getDebugLoc();
11533 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011534
Eric Christopher228232b2010-11-30 07:20:12 +000011535 // Address into RAX/EAX, other two args into ECX, EDX.
11536 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
11537 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
11538 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
11539 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000011540 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011541
Eric Christopher228232b2010-11-30 07:20:12 +000011542 unsigned ValOps = X86::AddrNumOperands;
11543 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
11544 .addReg(MI->getOperand(ValOps).getReg());
11545 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
11546 .addReg(MI->getOperand(ValOps+1).getReg());
11547
11548 // The instruction doesn't actually take any operands though.
11549 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011550
Eric Christopher228232b2010-11-30 07:20:12 +000011551 MI->eraseFromParent(); // The pseudo is gone now.
11552 return BB;
11553}
11554
11555MachineBasicBlock *
11556X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000011557 DebugLoc dl = MI->getDebugLoc();
11558 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011559
Eric Christopher228232b2010-11-30 07:20:12 +000011560 // First arg in ECX, the second in EAX.
11561 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
11562 .addReg(MI->getOperand(0).getReg());
11563 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
11564 .addReg(MI->getOperand(1).getReg());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011565
Eric Christopher228232b2010-11-30 07:20:12 +000011566 // The instruction doesn't actually take any operands though.
11567 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011568
Eric Christopher228232b2010-11-30 07:20:12 +000011569 MI->eraseFromParent(); // The pseudo is gone now.
11570 return BB;
11571}
11572
11573MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000011574X86TargetLowering::EmitVAARG64WithCustomInserter(
11575 MachineInstr *MI,
11576 MachineBasicBlock *MBB) const {
11577 // Emit va_arg instruction on X86-64.
11578
11579 // Operands to this pseudo-instruction:
11580 // 0 ) Output : destination address (reg)
11581 // 1-5) Input : va_list address (addr, i64mem)
11582 // 6 ) ArgSize : Size (in bytes) of vararg type
11583 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
11584 // 8 ) Align : Alignment of type
11585 // 9 ) EFLAGS (implicit-def)
11586
11587 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
11588 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
11589
11590 unsigned DestReg = MI->getOperand(0).getReg();
11591 MachineOperand &Base = MI->getOperand(1);
11592 MachineOperand &Scale = MI->getOperand(2);
11593 MachineOperand &Index = MI->getOperand(3);
11594 MachineOperand &Disp = MI->getOperand(4);
11595 MachineOperand &Segment = MI->getOperand(5);
11596 unsigned ArgSize = MI->getOperand(6).getImm();
11597 unsigned ArgMode = MI->getOperand(7).getImm();
11598 unsigned Align = MI->getOperand(8).getImm();
11599
11600 // Memory Reference
11601 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
11602 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
11603 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
11604
11605 // Machine Information
11606 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11607 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
11608 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
11609 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
11610 DebugLoc DL = MI->getDebugLoc();
11611
11612 // struct va_list {
11613 // i32 gp_offset
11614 // i32 fp_offset
11615 // i64 overflow_area (address)
11616 // i64 reg_save_area (address)
11617 // }
11618 // sizeof(va_list) = 24
11619 // alignment(va_list) = 8
11620
11621 unsigned TotalNumIntRegs = 6;
11622 unsigned TotalNumXMMRegs = 8;
11623 bool UseGPOffset = (ArgMode == 1);
11624 bool UseFPOffset = (ArgMode == 2);
11625 unsigned MaxOffset = TotalNumIntRegs * 8 +
11626 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
11627
11628 /* Align ArgSize to a multiple of 8 */
11629 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
11630 bool NeedsAlign = (Align > 8);
11631
11632 MachineBasicBlock *thisMBB = MBB;
11633 MachineBasicBlock *overflowMBB;
11634 MachineBasicBlock *offsetMBB;
11635 MachineBasicBlock *endMBB;
11636
11637 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
11638 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
11639 unsigned OffsetReg = 0;
11640
11641 if (!UseGPOffset && !UseFPOffset) {
11642 // If we only pull from the overflow region, we don't create a branch.
11643 // We don't need to alter control flow.
11644 OffsetDestReg = 0; // unused
11645 OverflowDestReg = DestReg;
11646
11647 offsetMBB = NULL;
11648 overflowMBB = thisMBB;
11649 endMBB = thisMBB;
11650 } else {
11651 // First emit code to check if gp_offset (or fp_offset) is below the bound.
11652 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
11653 // If not, pull from overflow_area. (branch to overflowMBB)
11654 //
11655 // thisMBB
11656 // | .
11657 // | .
11658 // offsetMBB overflowMBB
11659 // | .
11660 // | .
11661 // endMBB
11662
11663 // Registers for the PHI in endMBB
11664 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
11665 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
11666
11667 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11668 MachineFunction *MF = MBB->getParent();
11669 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11670 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11671 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11672
11673 MachineFunction::iterator MBBIter = MBB;
11674 ++MBBIter;
11675
11676 // Insert the new basic blocks
11677 MF->insert(MBBIter, offsetMBB);
11678 MF->insert(MBBIter, overflowMBB);
11679 MF->insert(MBBIter, endMBB);
11680
11681 // Transfer the remainder of MBB and its successor edges to endMBB.
11682 endMBB->splice(endMBB->begin(), thisMBB,
11683 llvm::next(MachineBasicBlock::iterator(MI)),
11684 thisMBB->end());
11685 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
11686
11687 // Make offsetMBB and overflowMBB successors of thisMBB
11688 thisMBB->addSuccessor(offsetMBB);
11689 thisMBB->addSuccessor(overflowMBB);
11690
11691 // endMBB is a successor of both offsetMBB and overflowMBB
11692 offsetMBB->addSuccessor(endMBB);
11693 overflowMBB->addSuccessor(endMBB);
11694
11695 // Load the offset value into a register
11696 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
11697 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
11698 .addOperand(Base)
11699 .addOperand(Scale)
11700 .addOperand(Index)
11701 .addDisp(Disp, UseFPOffset ? 4 : 0)
11702 .addOperand(Segment)
11703 .setMemRefs(MMOBegin, MMOEnd);
11704
11705 // Check if there is enough room left to pull this argument.
11706 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
11707 .addReg(OffsetReg)
11708 .addImm(MaxOffset + 8 - ArgSizeA8);
11709
11710 // Branch to "overflowMBB" if offset >= max
11711 // Fall through to "offsetMBB" otherwise
11712 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
11713 .addMBB(overflowMBB);
11714 }
11715
11716 // In offsetMBB, emit code to use the reg_save_area.
11717 if (offsetMBB) {
11718 assert(OffsetReg != 0);
11719
11720 // Read the reg_save_area address.
11721 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
11722 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
11723 .addOperand(Base)
11724 .addOperand(Scale)
11725 .addOperand(Index)
11726 .addDisp(Disp, 16)
11727 .addOperand(Segment)
11728 .setMemRefs(MMOBegin, MMOEnd);
11729
11730 // Zero-extend the offset
11731 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
11732 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
11733 .addImm(0)
11734 .addReg(OffsetReg)
11735 .addImm(X86::sub_32bit);
11736
11737 // Add the offset to the reg_save_area to get the final address.
11738 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
11739 .addReg(OffsetReg64)
11740 .addReg(RegSaveReg);
11741
11742 // Compute the offset for the next argument
11743 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
11744 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
11745 .addReg(OffsetReg)
11746 .addImm(UseFPOffset ? 16 : 8);
11747
11748 // Store it back into the va_list.
11749 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
11750 .addOperand(Base)
11751 .addOperand(Scale)
11752 .addOperand(Index)
11753 .addDisp(Disp, UseFPOffset ? 4 : 0)
11754 .addOperand(Segment)
11755 .addReg(NextOffsetReg)
11756 .setMemRefs(MMOBegin, MMOEnd);
11757
11758 // Jump to endMBB
11759 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
11760 .addMBB(endMBB);
11761 }
11762
11763 //
11764 // Emit code to use overflow area
11765 //
11766
11767 // Load the overflow_area address into a register.
11768 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
11769 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
11770 .addOperand(Base)
11771 .addOperand(Scale)
11772 .addOperand(Index)
11773 .addDisp(Disp, 8)
11774 .addOperand(Segment)
11775 .setMemRefs(MMOBegin, MMOEnd);
11776
11777 // If we need to align it, do so. Otherwise, just copy the address
11778 // to OverflowDestReg.
11779 if (NeedsAlign) {
11780 // Align the overflow address
11781 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
11782 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
11783
11784 // aligned_addr = (addr + (align-1)) & ~(align-1)
11785 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
11786 .addReg(OverflowAddrReg)
11787 .addImm(Align-1);
11788
11789 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
11790 .addReg(TmpReg)
11791 .addImm(~(uint64_t)(Align-1));
11792 } else {
11793 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
11794 .addReg(OverflowAddrReg);
11795 }
11796
11797 // Compute the next overflow address after this argument.
11798 // (the overflow address should be kept 8-byte aligned)
11799 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
11800 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
11801 .addReg(OverflowDestReg)
11802 .addImm(ArgSizeA8);
11803
11804 // Store the new overflow address.
11805 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
11806 .addOperand(Base)
11807 .addOperand(Scale)
11808 .addOperand(Index)
11809 .addDisp(Disp, 8)
11810 .addOperand(Segment)
11811 .addReg(NextAddrReg)
11812 .setMemRefs(MMOBegin, MMOEnd);
11813
11814 // If we branched, emit the PHI to the front of endMBB.
11815 if (offsetMBB) {
11816 BuildMI(*endMBB, endMBB->begin(), DL,
11817 TII->get(X86::PHI), DestReg)
11818 .addReg(OffsetDestReg).addMBB(offsetMBB)
11819 .addReg(OverflowDestReg).addMBB(overflowMBB);
11820 }
11821
11822 // Erase the pseudo instruction
11823 MI->eraseFromParent();
11824
11825 return endMBB;
11826}
11827
11828MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000011829X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
11830 MachineInstr *MI,
11831 MachineBasicBlock *MBB) const {
11832 // Emit code to save XMM registers to the stack. The ABI says that the
11833 // number of registers to save is given in %al, so it's theoretically
11834 // possible to do an indirect jump trick to avoid saving all of them,
11835 // however this code takes a simpler approach and just executes all
11836 // of the stores if %al is non-zero. It's less code, and it's probably
11837 // easier on the hardware branch predictor, and stores aren't all that
11838 // expensive anyway.
11839
11840 // Create the new basic blocks. One block contains all the XMM stores,
11841 // and one block is the final destination regardless of whether any
11842 // stores were performed.
11843 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11844 MachineFunction *F = MBB->getParent();
11845 MachineFunction::iterator MBBIter = MBB;
11846 ++MBBIter;
11847 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
11848 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
11849 F->insert(MBBIter, XMMSaveMBB);
11850 F->insert(MBBIter, EndMBB);
11851
Dan Gohman14152b42010-07-06 20:24:04 +000011852 // Transfer the remainder of MBB and its successor edges to EndMBB.
11853 EndMBB->splice(EndMBB->begin(), MBB,
11854 llvm::next(MachineBasicBlock::iterator(MI)),
11855 MBB->end());
11856 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
11857
Dan Gohmand6708ea2009-08-15 01:38:56 +000011858 // The original block will now fall through to the XMM save block.
11859 MBB->addSuccessor(XMMSaveMBB);
11860 // The XMMSaveMBB will fall through to the end block.
11861 XMMSaveMBB->addSuccessor(EndMBB);
11862
11863 // Now add the instructions.
11864 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11865 DebugLoc DL = MI->getDebugLoc();
11866
11867 unsigned CountReg = MI->getOperand(0).getReg();
11868 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
11869 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
11870
11871 if (!Subtarget->isTargetWin64()) {
11872 // If %al is 0, branch around the XMM save block.
11873 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011874 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000011875 MBB->addSuccessor(EndMBB);
11876 }
11877
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011878 unsigned MOVOpc = Subtarget->hasAVX() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000011879 // In the XMM save block, save all the XMM argument registers.
11880 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
11881 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000011882 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000011883 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000011884 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000011885 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000011886 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011887 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000011888 .addFrameIndex(RegSaveFrameIndex)
11889 .addImm(/*Scale=*/1)
11890 .addReg(/*IndexReg=*/0)
11891 .addImm(/*Disp=*/Offset)
11892 .addReg(/*Segment=*/0)
11893 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000011894 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000011895 }
11896
Dan Gohman14152b42010-07-06 20:24:04 +000011897 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000011898
11899 return EndMBB;
11900}
Mon P Wang63307c32008-05-05 19:05:59 +000011901
Evan Cheng60c07e12006-07-05 22:17:51 +000011902MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000011903X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000011904 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000011905 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11906 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000011907
Chris Lattner52600972009-09-02 05:57:00 +000011908 // To "insert" a SELECT_CC instruction, we actually have to insert the
11909 // diamond control-flow pattern. The incoming instruction knows the
11910 // destination vreg to set, the condition code register to branch on, the
11911 // true/false values to select between, and a branch opcode to use.
11912 const BasicBlock *LLVM_BB = BB->getBasicBlock();
11913 MachineFunction::iterator It = BB;
11914 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000011915
Chris Lattner52600972009-09-02 05:57:00 +000011916 // thisMBB:
11917 // ...
11918 // TrueVal = ...
11919 // cmpTY ccX, r1, r2
11920 // bCC copy1MBB
11921 // fallthrough --> copy0MBB
11922 MachineBasicBlock *thisMBB = BB;
11923 MachineFunction *F = BB->getParent();
11924 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
11925 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000011926 F->insert(It, copy0MBB);
11927 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000011928
Bill Wendling730c07e2010-06-25 20:48:10 +000011929 // If the EFLAGS register isn't dead in the terminator, then claim that it's
11930 // live into the sink and copy blocks.
Jakob Stoklund Olesen4a1b9d82011-09-02 23:52:49 +000011931 if (!MI->killsRegister(X86::EFLAGS)) {
11932 copy0MBB->addLiveIn(X86::EFLAGS);
11933 sinkMBB->addLiveIn(X86::EFLAGS);
Bill Wendling730c07e2010-06-25 20:48:10 +000011934 }
11935
Dan Gohman14152b42010-07-06 20:24:04 +000011936 // Transfer the remainder of BB and its successor edges to sinkMBB.
11937 sinkMBB->splice(sinkMBB->begin(), BB,
11938 llvm::next(MachineBasicBlock::iterator(MI)),
11939 BB->end());
11940 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
11941
11942 // Add the true and fallthrough blocks as its successors.
11943 BB->addSuccessor(copy0MBB);
11944 BB->addSuccessor(sinkMBB);
11945
11946 // Create the conditional branch instruction.
11947 unsigned Opc =
11948 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
11949 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
11950
Chris Lattner52600972009-09-02 05:57:00 +000011951 // copy0MBB:
11952 // %FalseValue = ...
11953 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000011954 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000011955
Chris Lattner52600972009-09-02 05:57:00 +000011956 // sinkMBB:
11957 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
11958 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000011959 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
11960 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000011961 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
11962 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
11963
Dan Gohman14152b42010-07-06 20:24:04 +000011964 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000011965 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000011966}
11967
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011968MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000011969X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
11970 bool Is64Bit) const {
11971 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11972 DebugLoc DL = MI->getDebugLoc();
11973 MachineFunction *MF = BB->getParent();
11974 const BasicBlock *LLVM_BB = BB->getBasicBlock();
11975
Nick Lewycky8a8d4792011-12-02 22:16:29 +000011976 assert(getTargetMachine().Options.EnableSegmentedStacks);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000011977
11978 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
11979 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
11980
11981 // BB:
11982 // ... [Till the alloca]
11983 // If stacklet is not large enough, jump to mallocMBB
11984 //
11985 // bumpMBB:
11986 // Allocate by subtracting from RSP
11987 // Jump to continueMBB
11988 //
11989 // mallocMBB:
11990 // Allocate by call to runtime
11991 //
11992 // continueMBB:
11993 // ...
11994 // [rest of original BB]
11995 //
11996
11997 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11998 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11999 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12000
12001 MachineRegisterInfo &MRI = MF->getRegInfo();
12002 const TargetRegisterClass *AddrRegClass =
12003 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
12004
12005 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
12006 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
12007 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola66bf7432011-10-26 21:16:41 +000012008 SPLimitVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012009 sizeVReg = MI->getOperand(1).getReg(),
12010 physSPReg = Is64Bit ? X86::RSP : X86::ESP;
12011
12012 MachineFunction::iterator MBBIter = BB;
12013 ++MBBIter;
12014
12015 MF->insert(MBBIter, bumpMBB);
12016 MF->insert(MBBIter, mallocMBB);
12017 MF->insert(MBBIter, continueMBB);
12018
12019 continueMBB->splice(continueMBB->begin(), BB, llvm::next
12020 (MachineBasicBlock::iterator(MI)), BB->end());
12021 continueMBB->transferSuccessorsAndUpdatePHIs(BB);
12022
12023 // Add code to the main basic block to check if the stack limit has been hit,
12024 // and if so, jump to mallocMBB otherwise to bumpMBB.
12025 BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
Rafael Espindola66bf7432011-10-26 21:16:41 +000012026 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), SPLimitVReg)
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012027 .addReg(tmpSPVReg).addReg(sizeVReg);
12028 BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
12029 .addReg(0).addImm(0).addReg(0).addImm(TlsOffset).addReg(TlsReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012030 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012031 BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
12032
12033 // bumpMBB simply decreases the stack pointer, since we know the current
12034 // stacklet has enough space.
12035 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012036 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012037 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012038 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012039 BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
12040
12041 // Calls into a routine in libgcc to allocate more space from the heap.
12042 if (Is64Bit) {
12043 BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
12044 .addReg(sizeVReg);
12045 BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
12046 .addExternalSymbol("__morestack_allocate_stack_space").addReg(X86::RDI);
12047 } else {
12048 BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
12049 .addImm(12);
12050 BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
12051 BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
12052 .addExternalSymbol("__morestack_allocate_stack_space");
12053 }
12054
12055 if (!Is64Bit)
12056 BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
12057 .addImm(16);
12058
12059 BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
12060 .addReg(Is64Bit ? X86::RAX : X86::EAX);
12061 BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
12062
12063 // Set up the CFG correctly.
12064 BB->addSuccessor(bumpMBB);
12065 BB->addSuccessor(mallocMBB);
12066 mallocMBB->addSuccessor(continueMBB);
12067 bumpMBB->addSuccessor(continueMBB);
12068
12069 // Take care of the PHI nodes.
12070 BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
12071 MI->getOperand(0).getReg())
12072 .addReg(mallocPtrVReg).addMBB(mallocMBB)
12073 .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
12074
12075 // Delete the original pseudo instruction.
12076 MI->eraseFromParent();
12077
12078 // And we're done.
12079 return continueMBB;
12080}
12081
12082MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012083X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012084 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012085 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12086 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012087
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012088 assert(!Subtarget->isTargetEnvMacho());
12089
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012090 // The lowering is pretty easy: we're just emitting the call to _alloca. The
12091 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012092
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012093 if (Subtarget->isTargetWin64()) {
12094 if (Subtarget->isTargetCygMing()) {
12095 // ___chkstk(Mingw64):
12096 // Clobbers R10, R11, RAX and EFLAGS.
12097 // Updates RSP.
12098 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
12099 .addExternalSymbol("___chkstk")
12100 .addReg(X86::RAX, RegState::Implicit)
12101 .addReg(X86::RSP, RegState::Implicit)
12102 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
12103 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
12104 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12105 } else {
12106 // __chkstk(MSVCRT): does not update stack pointer.
12107 // Clobbers R10, R11 and EFLAGS.
12108 // FIXME: RAX(allocated size) might be reused and not killed.
12109 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
12110 .addExternalSymbol("__chkstk")
12111 .addReg(X86::RAX, RegState::Implicit)
12112 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12113 // RAX has the offset to subtracted from RSP.
12114 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
12115 .addReg(X86::RSP)
12116 .addReg(X86::RAX);
12117 }
12118 } else {
12119 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012120 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
12121
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012122 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
12123 .addExternalSymbol(StackProbeSymbol)
12124 .addReg(X86::EAX, RegState::Implicit)
12125 .addReg(X86::ESP, RegState::Implicit)
12126 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
12127 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
12128 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12129 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012130
Dan Gohman14152b42010-07-06 20:24:04 +000012131 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012132 return BB;
12133}
Chris Lattner52600972009-09-02 05:57:00 +000012134
12135MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000012136X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
12137 MachineBasicBlock *BB) const {
12138 // This is pretty easy. We're taking the value that we received from
12139 // our load from the relocation, sticking it in either RDI (x86-64)
12140 // or EAX and doing an indirect call. The return value will then
12141 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000012142 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000012143 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000012144 DebugLoc DL = MI->getDebugLoc();
12145 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000012146
12147 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000012148 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000012149
Eric Christopher30ef0e52010-06-03 04:07:48 +000012150 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000012151 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12152 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000012153 .addReg(X86::RIP)
12154 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012155 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000012156 MI->getOperand(3).getTargetFlags())
12157 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000012158 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000012159 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +000012160 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000012161 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12162 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000012163 .addReg(0)
12164 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012165 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000012166 MI->getOperand(3).getTargetFlags())
12167 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000012168 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000012169 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012170 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000012171 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12172 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000012173 .addReg(TII->getGlobalBaseReg(F))
12174 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012175 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000012176 MI->getOperand(3).getTargetFlags())
12177 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000012178 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000012179 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012180 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000012181
Dan Gohman14152b42010-07-06 20:24:04 +000012182 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000012183 return BB;
12184}
12185
12186MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000012187X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012188 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000012189 switch (MI->getOpcode()) {
Richard Trieu23946fc2011-09-21 03:09:09 +000012190 default: assert(0 && "Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000012191 case X86::TAILJMPd64:
12192 case X86::TAILJMPr64:
12193 case X86::TAILJMPm64:
Richard Trieu23946fc2011-09-21 03:09:09 +000012194 assert(0 && "TAILJMP64 would not be touched here.");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000012195 case X86::TCRETURNdi64:
12196 case X86::TCRETURNri64:
12197 case X86::TCRETURNmi64:
12198 // Defs of TCRETURNxx64 has Win64's callee-saved registers, as subset.
12199 // On AMD64, additional defs should be added before register allocation.
12200 if (!Subtarget->isTargetWin64()) {
12201 MI->addRegisterDefined(X86::RSI);
12202 MI->addRegisterDefined(X86::RDI);
12203 MI->addRegisterDefined(X86::XMM6);
12204 MI->addRegisterDefined(X86::XMM7);
12205 MI->addRegisterDefined(X86::XMM8);
12206 MI->addRegisterDefined(X86::XMM9);
12207 MI->addRegisterDefined(X86::XMM10);
12208 MI->addRegisterDefined(X86::XMM11);
12209 MI->addRegisterDefined(X86::XMM12);
12210 MI->addRegisterDefined(X86::XMM13);
12211 MI->addRegisterDefined(X86::XMM14);
12212 MI->addRegisterDefined(X86::XMM15);
12213 }
12214 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012215 case X86::WIN_ALLOCA:
12216 return EmitLoweredWinAlloca(MI, BB);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012217 case X86::SEG_ALLOCA_32:
12218 return EmitLoweredSegAlloca(MI, BB, false);
12219 case X86::SEG_ALLOCA_64:
12220 return EmitLoweredSegAlloca(MI, BB, true);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012221 case X86::TLSCall_32:
12222 case X86::TLSCall_64:
12223 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000012224 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000012225 case X86::CMOV_FR32:
12226 case X86::CMOV_FR64:
12227 case X86::CMOV_V4F32:
12228 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000012229 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000012230 case X86::CMOV_V8F32:
12231 case X86::CMOV_V4F64:
12232 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000012233 case X86::CMOV_GR16:
12234 case X86::CMOV_GR32:
12235 case X86::CMOV_RFP32:
12236 case X86::CMOV_RFP64:
12237 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012238 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000012239
Dale Johannesen849f2142007-07-03 00:53:03 +000012240 case X86::FP32_TO_INT16_IN_MEM:
12241 case X86::FP32_TO_INT32_IN_MEM:
12242 case X86::FP32_TO_INT64_IN_MEM:
12243 case X86::FP64_TO_INT16_IN_MEM:
12244 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000012245 case X86::FP64_TO_INT64_IN_MEM:
12246 case X86::FP80_TO_INT16_IN_MEM:
12247 case X86::FP80_TO_INT32_IN_MEM:
12248 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000012249 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12250 DebugLoc DL = MI->getDebugLoc();
12251
Evan Cheng60c07e12006-07-05 22:17:51 +000012252 // Change the floating point control register to use "round towards zero"
12253 // mode when truncating to an integer value.
12254 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000012255 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000012256 addFrameReference(BuildMI(*BB, MI, DL,
12257 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012258
12259 // Load the old value of the high byte of the control word...
12260 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000012261 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000012262 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000012263 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012264
12265 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000012266 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012267 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000012268
12269 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000012270 addFrameReference(BuildMI(*BB, MI, DL,
12271 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012272
12273 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000012274 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012275 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000012276
12277 // Get the X86 opcode to use.
12278 unsigned Opc;
12279 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000012280 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000012281 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
12282 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
12283 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
12284 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
12285 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
12286 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000012287 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
12288 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
12289 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000012290 }
12291
12292 X86AddressMode AM;
12293 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000012294 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012295 AM.BaseType = X86AddressMode::RegBase;
12296 AM.Base.Reg = Op.getReg();
12297 } else {
12298 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000012299 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000012300 }
12301 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000012302 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012303 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012304 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000012305 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012306 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012307 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000012308 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012309 AM.GV = Op.getGlobal();
12310 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000012311 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012312 }
Dan Gohman14152b42010-07-06 20:24:04 +000012313 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000012314 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000012315
12316 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000012317 addFrameReference(BuildMI(*BB, MI, DL,
12318 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012319
Dan Gohman14152b42010-07-06 20:24:04 +000012320 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000012321 return BB;
12322 }
Eric Christopherb120ab42009-08-18 22:50:32 +000012323 // String/text processing lowering.
12324 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012325 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000012326 return EmitPCMP(MI, BB, 3, false /* in-mem */);
12327 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012328 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000012329 return EmitPCMP(MI, BB, 3, true /* in-mem */);
12330 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012331 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000012332 return EmitPCMP(MI, BB, 5, false /* in mem */);
12333 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012334 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000012335 return EmitPCMP(MI, BB, 5, true /* in mem */);
12336
Eric Christopher228232b2010-11-30 07:20:12 +000012337 // Thread synchronization.
12338 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012339 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000012340 case X86::MWAIT:
12341 return EmitMwait(MI, BB);
12342
Eric Christopherb120ab42009-08-18 22:50:32 +000012343 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000012344 case X86::ATOMAND32:
12345 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012346 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012347 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012348 X86::NOT32r, X86::EAX,
12349 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000012350 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000012351 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
12352 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012353 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012354 X86::NOT32r, X86::EAX,
12355 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000012356 case X86::ATOMXOR32:
12357 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012358 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012359 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012360 X86::NOT32r, X86::EAX,
12361 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000012362 case X86::ATOMNAND32:
12363 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012364 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012365 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012366 X86::NOT32r, X86::EAX,
12367 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000012368 case X86::ATOMMIN32:
12369 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
12370 case X86::ATOMMAX32:
12371 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
12372 case X86::ATOMUMIN32:
12373 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
12374 case X86::ATOMUMAX32:
12375 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000012376
12377 case X86::ATOMAND16:
12378 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
12379 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012380 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012381 X86::NOT16r, X86::AX,
12382 X86::GR16RegisterClass);
12383 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000012384 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012385 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012386 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012387 X86::NOT16r, X86::AX,
12388 X86::GR16RegisterClass);
12389 case X86::ATOMXOR16:
12390 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
12391 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012392 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012393 X86::NOT16r, X86::AX,
12394 X86::GR16RegisterClass);
12395 case X86::ATOMNAND16:
12396 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
12397 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012398 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012399 X86::NOT16r, X86::AX,
12400 X86::GR16RegisterClass, true);
12401 case X86::ATOMMIN16:
12402 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
12403 case X86::ATOMMAX16:
12404 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
12405 case X86::ATOMUMIN16:
12406 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
12407 case X86::ATOMUMAX16:
12408 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
12409
12410 case X86::ATOMAND8:
12411 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
12412 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012413 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012414 X86::NOT8r, X86::AL,
12415 X86::GR8RegisterClass);
12416 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000012417 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012418 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012419 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012420 X86::NOT8r, X86::AL,
12421 X86::GR8RegisterClass);
12422 case X86::ATOMXOR8:
12423 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
12424 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012425 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012426 X86::NOT8r, X86::AL,
12427 X86::GR8RegisterClass);
12428 case X86::ATOMNAND8:
12429 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
12430 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012431 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012432 X86::NOT8r, X86::AL,
12433 X86::GR8RegisterClass, true);
12434 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012435 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000012436 case X86::ATOMAND64:
12437 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012438 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012439 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012440 X86::NOT64r, X86::RAX,
12441 X86::GR64RegisterClass);
12442 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000012443 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
12444 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012445 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012446 X86::NOT64r, X86::RAX,
12447 X86::GR64RegisterClass);
12448 case X86::ATOMXOR64:
12449 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012450 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012451 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012452 X86::NOT64r, X86::RAX,
12453 X86::GR64RegisterClass);
12454 case X86::ATOMNAND64:
12455 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
12456 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012457 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012458 X86::NOT64r, X86::RAX,
12459 X86::GR64RegisterClass, true);
12460 case X86::ATOMMIN64:
12461 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
12462 case X86::ATOMMAX64:
12463 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
12464 case X86::ATOMUMIN64:
12465 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
12466 case X86::ATOMUMAX64:
12467 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012468
12469 // This group does 64-bit operations on a 32-bit host.
12470 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012471 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012472 X86::AND32rr, X86::AND32rr,
12473 X86::AND32ri, X86::AND32ri,
12474 false);
12475 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012476 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012477 X86::OR32rr, X86::OR32rr,
12478 X86::OR32ri, X86::OR32ri,
12479 false);
12480 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012481 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012482 X86::XOR32rr, X86::XOR32rr,
12483 X86::XOR32ri, X86::XOR32ri,
12484 false);
12485 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012486 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012487 X86::AND32rr, X86::AND32rr,
12488 X86::AND32ri, X86::AND32ri,
12489 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012490 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012491 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012492 X86::ADD32rr, X86::ADC32rr,
12493 X86::ADD32ri, X86::ADC32ri,
12494 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012495 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012496 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012497 X86::SUB32rr, X86::SBB32rr,
12498 X86::SUB32ri, X86::SBB32ri,
12499 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000012500 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012501 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000012502 X86::MOV32rr, X86::MOV32rr,
12503 X86::MOV32ri, X86::MOV32ri,
12504 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012505 case X86::VASTART_SAVE_XMM_REGS:
12506 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000012507
12508 case X86::VAARG_64:
12509 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000012510 }
12511}
12512
12513//===----------------------------------------------------------------------===//
12514// X86 Optimization Hooks
12515//===----------------------------------------------------------------------===//
12516
Dan Gohman475871a2008-07-27 21:46:04 +000012517void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000012518 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000012519 APInt &KnownZero,
12520 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000012521 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000012522 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012523 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000012524 assert((Opc >= ISD::BUILTIN_OP_END ||
12525 Opc == ISD::INTRINSIC_WO_CHAIN ||
12526 Opc == ISD::INTRINSIC_W_CHAIN ||
12527 Opc == ISD::INTRINSIC_VOID) &&
12528 "Should use MaskedValueIsZero if you don't know whether Op"
12529 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012530
Dan Gohmanf4f92f52008-02-13 23:07:24 +000012531 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012532 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000012533 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012534 case X86ISD::ADD:
12535 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000012536 case X86ISD::ADC:
12537 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012538 case X86ISD::SMUL:
12539 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000012540 case X86ISD::INC:
12541 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000012542 case X86ISD::OR:
12543 case X86ISD::XOR:
12544 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012545 // These nodes' second result is a boolean.
12546 if (Op.getResNo() == 0)
12547 break;
12548 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012549 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000012550 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
12551 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000012552 break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000012553 case ISD::INTRINSIC_WO_CHAIN: {
12554 unsigned IntId = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
12555 unsigned NumLoBits = 0;
12556 switch (IntId) {
12557 default: break;
12558 case Intrinsic::x86_sse_movmsk_ps:
12559 case Intrinsic::x86_avx_movmsk_ps_256:
12560 case Intrinsic::x86_sse2_movmsk_pd:
12561 case Intrinsic::x86_avx_movmsk_pd_256:
12562 case Intrinsic::x86_mmx_pmovmskb:
12563 case Intrinsic::x86_sse2_pmovmskb_128: {
12564 // High bits of movmskp{s|d}, pmovmskb are known zero.
12565 switch (IntId) {
12566 case Intrinsic::x86_sse_movmsk_ps: NumLoBits = 4; break;
12567 case Intrinsic::x86_avx_movmsk_ps_256: NumLoBits = 8; break;
12568 case Intrinsic::x86_sse2_movmsk_pd: NumLoBits = 2; break;
12569 case Intrinsic::x86_avx_movmsk_pd_256: NumLoBits = 4; break;
12570 case Intrinsic::x86_mmx_pmovmskb: NumLoBits = 8; break;
12571 case Intrinsic::x86_sse2_pmovmskb_128: NumLoBits = 16; break;
12572 }
12573 KnownZero = APInt::getHighBitsSet(Mask.getBitWidth(),
12574 Mask.getBitWidth() - NumLoBits);
12575 break;
12576 }
12577 }
12578 break;
12579 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012580 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012581}
Chris Lattner259e97c2006-01-31 19:43:35 +000012582
Owen Andersonbc146b02010-09-21 20:42:50 +000012583unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
12584 unsigned Depth) const {
12585 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
12586 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
12587 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000012588
Owen Andersonbc146b02010-09-21 20:42:50 +000012589 // Fallback case.
12590 return 1;
12591}
12592
Evan Cheng206ee9d2006-07-07 08:33:52 +000012593/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000012594/// node is a GlobalAddress + offset.
12595bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000012596 const GlobalValue* &GA,
12597 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000012598 if (N->getOpcode() == X86ISD::Wrapper) {
12599 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000012600 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000012601 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000012602 return true;
12603 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000012604 }
Evan Chengad4196b2008-05-12 19:56:52 +000012605 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000012606}
12607
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012608/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
12609/// same as extracting the high 128-bit part of 256-bit vector and then
12610/// inserting the result into the low part of a new 256-bit vector
12611static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
12612 EVT VT = SVOp->getValueType(0);
12613 int NumElems = VT.getVectorNumElements();
12614
12615 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
12616 for (int i = 0, j = NumElems/2; i < NumElems/2; ++i, ++j)
12617 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
12618 SVOp->getMaskElt(j) >= 0)
12619 return false;
12620
12621 return true;
12622}
12623
12624/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
12625/// same as extracting the low 128-bit part of 256-bit vector and then
12626/// inserting the result into the high part of a new 256-bit vector
12627static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
12628 EVT VT = SVOp->getValueType(0);
12629 int NumElems = VT.getVectorNumElements();
12630
12631 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
12632 for (int i = NumElems/2, j = 0; i < NumElems; ++i, ++j)
12633 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
12634 SVOp->getMaskElt(j) >= 0)
12635 return false;
12636
12637 return true;
12638}
12639
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012640/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
12641static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
12642 TargetLowering::DAGCombinerInfo &DCI) {
12643 DebugLoc dl = N->getDebugLoc();
12644 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
12645 SDValue V1 = SVOp->getOperand(0);
12646 SDValue V2 = SVOp->getOperand(1);
12647 EVT VT = SVOp->getValueType(0);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012648 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012649
12650 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
12651 V2.getOpcode() == ISD::CONCAT_VECTORS) {
12652 //
12653 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000012654 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012655 // V UNDEF BUILD_VECTOR UNDEF
12656 // \ / \ /
12657 // CONCAT_VECTOR CONCAT_VECTOR
12658 // \ /
12659 // \ /
12660 // RESULT: V + zero extended
12661 //
12662 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
12663 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
12664 V1.getOperand(1).getOpcode() != ISD::UNDEF)
12665 return SDValue();
12666
12667 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
12668 return SDValue();
12669
12670 // To match the shuffle mask, the first half of the mask should
12671 // be exactly the first vector, and all the rest a splat with the
12672 // first element of the second one.
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012673 for (int i = 0; i < NumElems/2; ++i)
12674 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
12675 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
12676 return SDValue();
12677
12678 // Emit a zeroed vector and insert the desired subvector on its
12679 // first half.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000012680 SDValue Zeros = getZeroVector(VT, true /* HasXMMInt */, DAG, dl);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012681 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0),
12682 DAG.getConstant(0, MVT::i32), DAG, dl);
12683 return DCI.CombineTo(N, InsV);
12684 }
12685
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012686 //===--------------------------------------------------------------------===//
12687 // Combine some shuffles into subvector extracts and inserts:
12688 //
12689
12690 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
12691 if (isShuffleHigh128VectorInsertLow(SVOp)) {
12692 SDValue V = Extract128BitVector(V1, DAG.getConstant(NumElems/2, MVT::i32),
12693 DAG, dl);
12694 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT),
12695 V, DAG.getConstant(0, MVT::i32), DAG, dl);
12696 return DCI.CombineTo(N, InsV);
12697 }
12698
12699 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
12700 if (isShuffleLow128VectorInsertHigh(SVOp)) {
12701 SDValue V = Extract128BitVector(V1, DAG.getConstant(0, MVT::i32), DAG, dl);
12702 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT),
12703 V, DAG.getConstant(NumElems/2, MVT::i32), DAG, dl);
12704 return DCI.CombineTo(N, InsV);
12705 }
12706
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012707 return SDValue();
12708}
12709
12710/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000012711static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000012712 TargetLowering::DAGCombinerInfo &DCI,
12713 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000012714 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000012715 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000012716
Mon P Wanga0fd0d52010-12-19 23:55:53 +000012717 // Don't create instructions with illegal types after legalize types has run.
12718 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12719 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
12720 return SDValue();
12721
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000012722 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
12723 if (Subtarget->hasAVX() && VT.getSizeInBits() == 256 &&
12724 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012725 return PerformShuffleCombine256(N, DAG, DCI);
12726
12727 // Only handle 128 wide vector from here on.
12728 if (VT.getSizeInBits() != 128)
12729 return SDValue();
12730
12731 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
12732 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
12733 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000012734 SmallVector<SDValue, 16> Elts;
12735 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012736 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000012737
Nate Begemanfdea31a2010-03-24 20:49:50 +000012738 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000012739}
Evan Chengd880b972008-05-09 21:53:03 +000012740
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000012741/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
12742/// generation and convert it from being a bunch of shuffles and extracts
12743/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012744static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
12745 const TargetLowering &TLI) {
12746 SDValue InputVector = N->getOperand(0);
12747
12748 // Only operate on vectors of 4 elements, where the alternative shuffling
12749 // gets to be more expensive.
12750 if (InputVector.getValueType() != MVT::v4i32)
12751 return SDValue();
12752
12753 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
12754 // single use which is a sign-extend or zero-extend, and all elements are
12755 // used.
12756 SmallVector<SDNode *, 4> Uses;
12757 unsigned ExtractedElements = 0;
12758 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
12759 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
12760 if (UI.getUse().getResNo() != InputVector.getResNo())
12761 return SDValue();
12762
12763 SDNode *Extract = *UI;
12764 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
12765 return SDValue();
12766
12767 if (Extract->getValueType(0) != MVT::i32)
12768 return SDValue();
12769 if (!Extract->hasOneUse())
12770 return SDValue();
12771 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
12772 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
12773 return SDValue();
12774 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
12775 return SDValue();
12776
12777 // Record which element was extracted.
12778 ExtractedElements |=
12779 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
12780
12781 Uses.push_back(Extract);
12782 }
12783
12784 // If not all the elements were used, this may not be worthwhile.
12785 if (ExtractedElements != 15)
12786 return SDValue();
12787
12788 // Ok, we've now decided to do the transformation.
12789 DebugLoc dl = InputVector.getDebugLoc();
12790
12791 // Store the value to a temporary stack slot.
12792 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000012793 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
12794 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012795
12796 // Replace each use (extract) with a load of the appropriate element.
12797 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
12798 UE = Uses.end(); UI != UE; ++UI) {
12799 SDNode *Extract = *UI;
12800
Nadav Rotem86694292011-05-17 08:31:57 +000012801 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012802 SDValue Idx = Extract->getOperand(1);
12803 unsigned EltSize =
12804 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
12805 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
12806 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
12807
Nadav Rotem86694292011-05-17 08:31:57 +000012808 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000012809 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012810
12811 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000012812 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000012813 ScalarAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000012814 false, false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012815
12816 // Replace the exact with the load.
12817 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
12818 }
12819
12820 // The replacement was made in place; don't return anything.
12821 return SDValue();
12822}
12823
Duncan Sands6bcd2192011-09-17 16:49:39 +000012824/// PerformSELECTCombine - Do target-specific dag combines on SELECT and VSELECT
12825/// nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000012826static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000012827 const X86Subtarget *Subtarget) {
12828 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000012829 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000012830 // Get the LHS/RHS of the select.
12831 SDValue LHS = N->getOperand(1);
12832 SDValue RHS = N->getOperand(2);
Bruno Cardoso Lopes149f29f2011-09-20 22:34:45 +000012833 EVT VT = LHS.getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +000012834
Dan Gohman670e5392009-09-21 18:03:22 +000012835 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000012836 // instructions match the semantics of the common C idiom x<y?x:y but not
12837 // x<=y?x:y, because of how they handle negative zero (which can be
12838 // ignored in unsafe-math mode).
Benjamin Kramer2c2ccbf2011-09-22 03:27:22 +000012839 if (Cond.getOpcode() == ISD::SETCC && VT.isFloatingPoint() &&
12840 VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT) &&
12841 (Subtarget->hasXMMInt() ||
12842 (Subtarget->hasSSE1() && VT.getScalarType() == MVT::f32))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000012843 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012844
Chris Lattner47b4ce82009-03-11 05:48:52 +000012845 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000012846 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000012847 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
12848 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000012849 switch (CC) {
12850 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000012851 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000012852 // Converting this to a min would handle NaNs incorrectly, and swapping
12853 // the operands would cause it to handle comparisons between positive
12854 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000012855 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000012856 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000012857 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
12858 break;
12859 std::swap(LHS, RHS);
12860 }
Dan Gohman670e5392009-09-21 18:03:22 +000012861 Opcode = X86ISD::FMIN;
12862 break;
12863 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000012864 // Converting this to a min would handle comparisons between positive
12865 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000012866 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000012867 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
12868 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012869 Opcode = X86ISD::FMIN;
12870 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000012871 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000012872 // Converting this to a min would handle both negative zeros and NaNs
12873 // incorrectly, but we can swap the operands to fix both.
12874 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012875 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012876 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000012877 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012878 Opcode = X86ISD::FMIN;
12879 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012880
Dan Gohman670e5392009-09-21 18:03:22 +000012881 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012882 // Converting this to a max would handle comparisons between positive
12883 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000012884 if (!DAG.getTarget().Options.UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000012885 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012886 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012887 Opcode = X86ISD::FMAX;
12888 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000012889 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000012890 // Converting this to a max would handle NaNs incorrectly, and swapping
12891 // the operands would cause it to handle comparisons between positive
12892 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000012893 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000012894 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000012895 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
12896 break;
12897 std::swap(LHS, RHS);
12898 }
Dan Gohman670e5392009-09-21 18:03:22 +000012899 Opcode = X86ISD::FMAX;
12900 break;
12901 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012902 // Converting this to a max would handle both negative zeros and NaNs
12903 // incorrectly, but we can swap the operands to fix both.
12904 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012905 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012906 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012907 case ISD::SETGE:
12908 Opcode = X86ISD::FMAX;
12909 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000012910 }
Dan Gohman670e5392009-09-21 18:03:22 +000012911 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000012912 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
12913 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000012914 switch (CC) {
12915 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000012916 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012917 // Converting this to a min would handle comparisons between positive
12918 // and negative zero incorrectly, and swapping the operands would
12919 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000012920 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000012921 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000012922 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012923 break;
12924 std::swap(LHS, RHS);
12925 }
Dan Gohman670e5392009-09-21 18:03:22 +000012926 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000012927 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012928 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000012929 // Converting this to a min would handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000012930 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000012931 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
12932 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012933 Opcode = X86ISD::FMIN;
12934 break;
12935 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012936 // Converting this to a min would handle both negative zeros and NaNs
12937 // incorrectly, but we can swap the operands to fix both.
12938 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012939 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012940 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012941 case ISD::SETGE:
12942 Opcode = X86ISD::FMIN;
12943 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012944
Dan Gohman670e5392009-09-21 18:03:22 +000012945 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000012946 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000012947 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012948 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012949 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000012950 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012951 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000012952 // Converting this to a max would handle comparisons between positive
12953 // and negative zero incorrectly, and swapping the operands would
12954 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000012955 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000012956 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000012957 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012958 break;
12959 std::swap(LHS, RHS);
12960 }
Dan Gohman670e5392009-09-21 18:03:22 +000012961 Opcode = X86ISD::FMAX;
12962 break;
12963 case ISD::SETULE:
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::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012968 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000012969 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012970 Opcode = X86ISD::FMAX;
12971 break;
12972 }
Chris Lattner83e6c992006-10-04 06:57:07 +000012973 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012974
Chris Lattner47b4ce82009-03-11 05:48:52 +000012975 if (Opcode)
12976 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000012977 }
Eric Christopherfd179292009-08-27 18:07:15 +000012978
Chris Lattnerd1980a52009-03-12 06:52:53 +000012979 // If this is a select between two integer constants, try to do some
12980 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000012981 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
12982 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000012983 // Don't do this for crazy integer types.
12984 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
12985 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000012986 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000012987 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000012988
Chris Lattnercee56e72009-03-13 05:53:31 +000012989 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000012990 // Efficiently invertible.
12991 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
12992 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
12993 isa<ConstantSDNode>(Cond.getOperand(1))))) {
12994 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000012995 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000012996 }
Eric Christopherfd179292009-08-27 18:07:15 +000012997
Chris Lattnerd1980a52009-03-12 06:52:53 +000012998 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000012999 if (FalseC->getAPIntValue() == 0 &&
13000 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000013001 if (NeedsCondInvert) // Invert the condition if needed.
13002 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13003 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013004
Chris Lattnerd1980a52009-03-12 06:52:53 +000013005 // Zero extend the condition if needed.
13006 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013007
Chris Lattnercee56e72009-03-13 05:53:31 +000013008 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000013009 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000013010 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000013011 }
Eric Christopherfd179292009-08-27 18:07:15 +000013012
Chris Lattner97a29a52009-03-13 05:22:11 +000013013 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000013014 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000013015 if (NeedsCondInvert) // Invert the condition if needed.
13016 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13017 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013018
Chris Lattner97a29a52009-03-13 05:22:11 +000013019 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000013020 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
13021 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000013022 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000013023 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000013024 }
Eric Christopherfd179292009-08-27 18:07:15 +000013025
Chris Lattnercee56e72009-03-13 05:53:31 +000013026 // Optimize cases that will turn into an LEA instruction. This requires
13027 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000013028 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000013029 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000013030 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000013031
Chris Lattnercee56e72009-03-13 05:53:31 +000013032 bool isFastMultiplier = false;
13033 if (Diff < 10) {
13034 switch ((unsigned char)Diff) {
13035 default: break;
13036 case 1: // result = add base, cond
13037 case 2: // result = lea base( , cond*2)
13038 case 3: // result = lea base(cond, cond*2)
13039 case 4: // result = lea base( , cond*4)
13040 case 5: // result = lea base(cond, cond*4)
13041 case 8: // result = lea base( , cond*8)
13042 case 9: // result = lea base(cond, cond*8)
13043 isFastMultiplier = true;
13044 break;
13045 }
13046 }
Eric Christopherfd179292009-08-27 18:07:15 +000013047
Chris Lattnercee56e72009-03-13 05:53:31 +000013048 if (isFastMultiplier) {
13049 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
13050 if (NeedsCondInvert) // Invert the condition if needed.
13051 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13052 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013053
Chris Lattnercee56e72009-03-13 05:53:31 +000013054 // Zero extend the condition if needed.
13055 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
13056 Cond);
13057 // Scale the condition by the difference.
13058 if (Diff != 1)
13059 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
13060 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013061
Chris Lattnercee56e72009-03-13 05:53:31 +000013062 // Add the base if non-zero.
13063 if (FalseC->getAPIntValue() != 0)
13064 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
13065 SDValue(FalseC, 0));
13066 return Cond;
13067 }
Eric Christopherfd179292009-08-27 18:07:15 +000013068 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000013069 }
13070 }
Eric Christopherfd179292009-08-27 18:07:15 +000013071
Dan Gohman475871a2008-07-27 21:46:04 +000013072 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000013073}
13074
Chris Lattnerd1980a52009-03-12 06:52:53 +000013075/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
13076static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
13077 TargetLowering::DAGCombinerInfo &DCI) {
13078 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000013079
Chris Lattnerd1980a52009-03-12 06:52:53 +000013080 // If the flag operand isn't dead, don't touch this CMOV.
13081 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
13082 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000013083
Evan Chengb5a55d92011-05-24 01:48:22 +000013084 SDValue FalseOp = N->getOperand(0);
13085 SDValue TrueOp = N->getOperand(1);
13086 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
13087 SDValue Cond = N->getOperand(3);
13088 if (CC == X86::COND_E || CC == X86::COND_NE) {
13089 switch (Cond.getOpcode()) {
13090 default: break;
13091 case X86ISD::BSR:
13092 case X86ISD::BSF:
13093 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
13094 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
13095 return (CC == X86::COND_E) ? FalseOp : TrueOp;
13096 }
13097 }
13098
Chris Lattnerd1980a52009-03-12 06:52:53 +000013099 // If this is a select between two integer constants, try to do some
13100 // optimizations. Note that the operands are ordered the opposite of SELECT
13101 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000013102 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
13103 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000013104 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
13105 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000013106 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
13107 CC = X86::GetOppositeBranchCondition(CC);
13108 std::swap(TrueC, FalseC);
13109 }
Eric Christopherfd179292009-08-27 18:07:15 +000013110
Chris Lattnerd1980a52009-03-12 06:52:53 +000013111 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000013112 // This is efficient for any integer data type (including i8/i16) and
13113 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000013114 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000013115 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
13116 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013117
Chris Lattnerd1980a52009-03-12 06:52:53 +000013118 // Zero extend the condition if needed.
13119 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013120
Chris Lattnerd1980a52009-03-12 06:52:53 +000013121 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
13122 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000013123 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000013124 if (N->getNumValues() == 2) // Dead flag value?
13125 return DCI.CombineTo(N, Cond, SDValue());
13126 return Cond;
13127 }
Eric Christopherfd179292009-08-27 18:07:15 +000013128
Chris Lattnercee56e72009-03-13 05:53:31 +000013129 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
13130 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000013131 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000013132 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
13133 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013134
Chris Lattner97a29a52009-03-13 05:22:11 +000013135 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000013136 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
13137 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000013138 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
13139 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000013140
Chris Lattner97a29a52009-03-13 05:22:11 +000013141 if (N->getNumValues() == 2) // Dead flag value?
13142 return DCI.CombineTo(N, Cond, SDValue());
13143 return Cond;
13144 }
Eric Christopherfd179292009-08-27 18:07:15 +000013145
Chris Lattnercee56e72009-03-13 05:53:31 +000013146 // Optimize cases that will turn into an LEA instruction. This requires
13147 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000013148 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000013149 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000013150 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000013151
Chris Lattnercee56e72009-03-13 05:53:31 +000013152 bool isFastMultiplier = false;
13153 if (Diff < 10) {
13154 switch ((unsigned char)Diff) {
13155 default: break;
13156 case 1: // result = add base, cond
13157 case 2: // result = lea base( , cond*2)
13158 case 3: // result = lea base(cond, cond*2)
13159 case 4: // result = lea base( , cond*4)
13160 case 5: // result = lea base(cond, cond*4)
13161 case 8: // result = lea base( , cond*8)
13162 case 9: // result = lea base(cond, cond*8)
13163 isFastMultiplier = true;
13164 break;
13165 }
13166 }
Eric Christopherfd179292009-08-27 18:07:15 +000013167
Chris Lattnercee56e72009-03-13 05:53:31 +000013168 if (isFastMultiplier) {
13169 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000013170 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
13171 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000013172 // Zero extend the condition if needed.
13173 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
13174 Cond);
13175 // Scale the condition by the difference.
13176 if (Diff != 1)
13177 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
13178 DAG.getConstant(Diff, Cond.getValueType()));
13179
13180 // Add the base if non-zero.
13181 if (FalseC->getAPIntValue() != 0)
13182 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
13183 SDValue(FalseC, 0));
13184 if (N->getNumValues() == 2) // Dead flag value?
13185 return DCI.CombineTo(N, Cond, SDValue());
13186 return Cond;
13187 }
Eric Christopherfd179292009-08-27 18:07:15 +000013188 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000013189 }
13190 }
13191 return SDValue();
13192}
13193
13194
Evan Cheng0b0cd912009-03-28 05:57:29 +000013195/// PerformMulCombine - Optimize a single multiply with constant into two
13196/// in order to implement it with two cheaper instructions, e.g.
13197/// LEA + SHL, LEA + LEA.
13198static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
13199 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000013200 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
13201 return SDValue();
13202
Owen Andersone50ed302009-08-10 22:56:29 +000013203 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000013204 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000013205 return SDValue();
13206
13207 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
13208 if (!C)
13209 return SDValue();
13210 uint64_t MulAmt = C->getZExtValue();
13211 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
13212 return SDValue();
13213
13214 uint64_t MulAmt1 = 0;
13215 uint64_t MulAmt2 = 0;
13216 if ((MulAmt % 9) == 0) {
13217 MulAmt1 = 9;
13218 MulAmt2 = MulAmt / 9;
13219 } else if ((MulAmt % 5) == 0) {
13220 MulAmt1 = 5;
13221 MulAmt2 = MulAmt / 5;
13222 } else if ((MulAmt % 3) == 0) {
13223 MulAmt1 = 3;
13224 MulAmt2 = MulAmt / 3;
13225 }
13226 if (MulAmt2 &&
13227 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
13228 DebugLoc DL = N->getDebugLoc();
13229
13230 if (isPowerOf2_64(MulAmt2) &&
13231 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
13232 // If second multiplifer is pow2, issue it first. We want the multiply by
13233 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
13234 // is an add.
13235 std::swap(MulAmt1, MulAmt2);
13236
13237 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000013238 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000013239 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000013240 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000013241 else
Evan Cheng73f24c92009-03-30 21:36:47 +000013242 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000013243 DAG.getConstant(MulAmt1, VT));
13244
Eric Christopherfd179292009-08-27 18:07:15 +000013245 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000013246 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000013247 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000013248 else
Evan Cheng73f24c92009-03-30 21:36:47 +000013249 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000013250 DAG.getConstant(MulAmt2, VT));
13251
13252 // Do not add new nodes to DAG combiner worklist.
13253 DCI.CombineTo(N, NewMul, false);
13254 }
13255 return SDValue();
13256}
13257
Evan Chengad9c0a32009-12-15 00:53:42 +000013258static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
13259 SDValue N0 = N->getOperand(0);
13260 SDValue N1 = N->getOperand(1);
13261 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
13262 EVT VT = N0.getValueType();
13263
13264 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
13265 // since the result of setcc_c is all zero's or all ones.
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000013266 if (VT.isInteger() && !VT.isVector() &&
13267 N1C && N0.getOpcode() == ISD::AND &&
Evan Chengad9c0a32009-12-15 00:53:42 +000013268 N0.getOperand(1).getOpcode() == ISD::Constant) {
13269 SDValue N00 = N0.getOperand(0);
13270 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
13271 ((N00.getOpcode() == ISD::ANY_EXTEND ||
13272 N00.getOpcode() == ISD::ZERO_EXTEND) &&
13273 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
13274 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
13275 APInt ShAmt = N1C->getAPIntValue();
13276 Mask = Mask.shl(ShAmt);
13277 if (Mask != 0)
13278 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
13279 N00, DAG.getConstant(Mask, VT));
13280 }
13281 }
13282
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000013283
13284 // Hardware support for vector shifts is sparse which makes us scalarize the
13285 // vector operations in many cases. Also, on sandybridge ADD is faster than
13286 // shl.
13287 // (shl V, 1) -> add V,V
13288 if (isSplatVector(N1.getNode())) {
13289 assert(N0.getValueType().isVector() && "Invalid vector shift type");
13290 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1->getOperand(0));
13291 // We shift all of the values by one. In many cases we do not have
13292 // hardware support for this operation. This is better expressed as an ADD
13293 // of two values.
13294 if (N1C && (1 == N1C->getZExtValue())) {
13295 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, N0);
13296 }
13297 }
13298
Evan Chengad9c0a32009-12-15 00:53:42 +000013299 return SDValue();
13300}
Evan Cheng0b0cd912009-03-28 05:57:29 +000013301
Nate Begeman740ab032009-01-26 00:52:55 +000013302/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
13303/// when possible.
13304static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
13305 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000013306 EVT VT = N->getValueType(0);
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000013307 if (N->getOpcode() == ISD::SHL) {
13308 SDValue V = PerformSHLCombine(N, DAG);
13309 if (V.getNode()) return V;
13310 }
Evan Chengad9c0a32009-12-15 00:53:42 +000013311
Nate Begeman740ab032009-01-26 00:52:55 +000013312 // On X86 with SSE2 support, we can transform this to a vector shift if
13313 // all elements are shifted by the same amount. We can't do this in legalize
13314 // because the a constant vector is typically transformed to a constant pool
13315 // so we have no knowledge of the shift amount.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013316 if (!Subtarget->hasXMMInt())
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013317 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000013318
Craig Topper7be5dfd2011-11-12 09:58:49 +000013319 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16 &&
13320 (!Subtarget->hasAVX2() ||
13321 (VT != MVT::v4i64 && VT != MVT::v8i32 && VT != MVT::v16i16)))
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013322 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000013323
Mon P Wang3becd092009-01-28 08:12:05 +000013324 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000013325 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000013326 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000013327 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000013328 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
13329 unsigned NumElts = VT.getVectorNumElements();
13330 unsigned i = 0;
13331 for (; i != NumElts; ++i) {
13332 SDValue Arg = ShAmtOp.getOperand(i);
13333 if (Arg.getOpcode() == ISD::UNDEF) continue;
13334 BaseShAmt = Arg;
13335 break;
13336 }
13337 for (; i != NumElts; ++i) {
13338 SDValue Arg = ShAmtOp.getOperand(i);
13339 if (Arg.getOpcode() == ISD::UNDEF) continue;
13340 if (Arg != BaseShAmt) {
13341 return SDValue();
13342 }
13343 }
13344 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000013345 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000013346 SDValue InVec = ShAmtOp.getOperand(0);
13347 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
13348 unsigned NumElts = InVec.getValueType().getVectorNumElements();
13349 unsigned i = 0;
13350 for (; i != NumElts; ++i) {
13351 SDValue Arg = InVec.getOperand(i);
13352 if (Arg.getOpcode() == ISD::UNDEF) continue;
13353 BaseShAmt = Arg;
13354 break;
13355 }
13356 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
13357 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000013358 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000013359 if (C->getZExtValue() == SplatIdx)
13360 BaseShAmt = InVec.getOperand(1);
13361 }
13362 }
13363 if (BaseShAmt.getNode() == 0)
13364 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
13365 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000013366 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013367 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000013368
Mon P Wangefa42202009-09-03 19:56:25 +000013369 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000013370 if (EltVT.bitsGT(MVT::i32))
13371 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
13372 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000013373 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000013374
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013375 // The shift amount is identical so we can do a vector shift.
13376 SDValue ValOp = N->getOperand(0);
13377 switch (N->getOpcode()) {
13378 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000013379 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013380 break;
13381 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000013382 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013383 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013384 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013385 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013386 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013387 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013388 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013389 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013390 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013391 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013392 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013393 ValOp, BaseShAmt);
Craig Topper7be5dfd2011-11-12 09:58:49 +000013394 if (VT == MVT::v4i64)
13395 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13396 DAG.getConstant(Intrinsic::x86_avx2_pslli_q, MVT::i32),
13397 ValOp, BaseShAmt);
13398 if (VT == MVT::v8i32)
13399 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13400 DAG.getConstant(Intrinsic::x86_avx2_pslli_d, MVT::i32),
13401 ValOp, BaseShAmt);
13402 if (VT == MVT::v16i16)
13403 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13404 DAG.getConstant(Intrinsic::x86_avx2_pslli_w, MVT::i32),
13405 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013406 break;
13407 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000013408 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013409 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013410 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013411 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013412 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013413 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013414 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013415 ValOp, BaseShAmt);
Craig Topper7be5dfd2011-11-12 09:58:49 +000013416 if (VT == MVT::v8i32)
13417 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13418 DAG.getConstant(Intrinsic::x86_avx2_psrai_d, MVT::i32),
13419 ValOp, BaseShAmt);
13420 if (VT == MVT::v16i16)
13421 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13422 DAG.getConstant(Intrinsic::x86_avx2_psrai_w, MVT::i32),
13423 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013424 break;
13425 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000013426 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013427 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013428 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013429 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013430 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013431 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013432 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013433 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013434 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013435 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013436 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013437 ValOp, BaseShAmt);
Craig Topper7be5dfd2011-11-12 09:58:49 +000013438 if (VT == MVT::v4i64)
13439 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13440 DAG.getConstant(Intrinsic::x86_avx2_psrli_q, MVT::i32),
13441 ValOp, BaseShAmt);
13442 if (VT == MVT::v8i32)
13443 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13444 DAG.getConstant(Intrinsic::x86_avx2_psrli_d, MVT::i32),
13445 ValOp, BaseShAmt);
13446 if (VT == MVT::v16i16)
13447 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13448 DAG.getConstant(Intrinsic::x86_avx2_psrli_w, MVT::i32),
13449 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013450 break;
Nate Begeman740ab032009-01-26 00:52:55 +000013451 }
13452 return SDValue();
13453}
13454
Nate Begemanb65c1752010-12-17 22:55:37 +000013455
Stuart Hastings865f0932011-06-03 23:53:54 +000013456// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
13457// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
13458// and friends. Likewise for OR -> CMPNEQSS.
13459static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
13460 TargetLowering::DAGCombinerInfo &DCI,
13461 const X86Subtarget *Subtarget) {
13462 unsigned opcode;
13463
13464 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
13465 // we're requiring SSE2 for both.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013466 if (Subtarget->hasXMMInt() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
Stuart Hastings865f0932011-06-03 23:53:54 +000013467 SDValue N0 = N->getOperand(0);
13468 SDValue N1 = N->getOperand(1);
13469 SDValue CMP0 = N0->getOperand(1);
13470 SDValue CMP1 = N1->getOperand(1);
13471 DebugLoc DL = N->getDebugLoc();
13472
13473 // The SETCCs should both refer to the same CMP.
13474 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
13475 return SDValue();
13476
13477 SDValue CMP00 = CMP0->getOperand(0);
13478 SDValue CMP01 = CMP0->getOperand(1);
13479 EVT VT = CMP00.getValueType();
13480
13481 if (VT == MVT::f32 || VT == MVT::f64) {
13482 bool ExpectingFlags = false;
13483 // Check for any users that want flags:
13484 for (SDNode::use_iterator UI = N->use_begin(),
13485 UE = N->use_end();
13486 !ExpectingFlags && UI != UE; ++UI)
13487 switch (UI->getOpcode()) {
13488 default:
13489 case ISD::BR_CC:
13490 case ISD::BRCOND:
13491 case ISD::SELECT:
13492 ExpectingFlags = true;
13493 break;
13494 case ISD::CopyToReg:
13495 case ISD::SIGN_EXTEND:
13496 case ISD::ZERO_EXTEND:
13497 case ISD::ANY_EXTEND:
13498 break;
13499 }
13500
13501 if (!ExpectingFlags) {
13502 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
13503 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
13504
13505 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
13506 X86::CondCode tmp = cc0;
13507 cc0 = cc1;
13508 cc1 = tmp;
13509 }
13510
13511 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
13512 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
13513 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
13514 X86ISD::NodeType NTOperator = is64BitFP ?
13515 X86ISD::FSETCCsd : X86ISD::FSETCCss;
13516 // FIXME: need symbolic constants for these magic numbers.
13517 // See X86ATTInstPrinter.cpp:printSSECC().
13518 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
13519 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
13520 DAG.getConstant(x86cc, MVT::i8));
13521 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
13522 OnesOrZeroesF);
13523 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
13524 DAG.getConstant(1, MVT::i32));
13525 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
13526 return OneBitOfTruth;
13527 }
13528 }
13529 }
13530 }
13531 return SDValue();
13532}
13533
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013534/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
13535/// so it can be folded inside ANDNP.
13536static bool CanFoldXORWithAllOnes(const SDNode *N) {
13537 EVT VT = N->getValueType(0);
13538
13539 // Match direct AllOnes for 128 and 256-bit vectors
13540 if (ISD::isBuildVectorAllOnes(N))
13541 return true;
13542
13543 // Look through a bit convert.
13544 if (N->getOpcode() == ISD::BITCAST)
13545 N = N->getOperand(0).getNode();
13546
13547 // Sometimes the operand may come from a insert_subvector building a 256-bit
13548 // allones vector
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013549 if (VT.getSizeInBits() == 256 &&
Bill Wendling456a9252011-08-04 00:32:58 +000013550 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
13551 SDValue V1 = N->getOperand(0);
13552 SDValue V2 = N->getOperand(1);
13553
13554 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
13555 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
13556 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
13557 ISD::isBuildVectorAllOnes(V2.getNode()))
13558 return true;
13559 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013560
13561 return false;
13562}
13563
Nate Begemanb65c1752010-12-17 22:55:37 +000013564static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
13565 TargetLowering::DAGCombinerInfo &DCI,
13566 const X86Subtarget *Subtarget) {
13567 if (DCI.isBeforeLegalizeOps())
13568 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013569
Stuart Hastings865f0932011-06-03 23:53:54 +000013570 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
13571 if (R.getNode())
13572 return R;
13573
Craig Topper54a11172011-10-14 07:06:56 +000013574 EVT VT = N->getValueType(0);
13575
Craig Topperb4c94572011-10-21 06:55:01 +000013576 // Create ANDN, BLSI, and BLSR instructions
13577 // BLSI is X & (-X)
13578 // BLSR is X & (X-1)
Craig Topper54a11172011-10-14 07:06:56 +000013579 if (Subtarget->hasBMI() && (VT == MVT::i32 || VT == MVT::i64)) {
13580 SDValue N0 = N->getOperand(0);
13581 SDValue N1 = N->getOperand(1);
13582 DebugLoc DL = N->getDebugLoc();
13583
13584 // Check LHS for not
13585 if (N0.getOpcode() == ISD::XOR && isAllOnes(N0.getOperand(1)))
13586 return DAG.getNode(X86ISD::ANDN, DL, VT, N0.getOperand(0), N1);
13587 // Check RHS for not
13588 if (N1.getOpcode() == ISD::XOR && isAllOnes(N1.getOperand(1)))
13589 return DAG.getNode(X86ISD::ANDN, DL, VT, N1.getOperand(0), N0);
13590
Craig Topperb4c94572011-10-21 06:55:01 +000013591 // Check LHS for neg
13592 if (N0.getOpcode() == ISD::SUB && N0.getOperand(1) == N1 &&
13593 isZero(N0.getOperand(0)))
13594 return DAG.getNode(X86ISD::BLSI, DL, VT, N1);
13595
13596 // Check RHS for neg
13597 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1) == N0 &&
13598 isZero(N1.getOperand(0)))
13599 return DAG.getNode(X86ISD::BLSI, DL, VT, N0);
13600
13601 // Check LHS for X-1
13602 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
13603 isAllOnes(N0.getOperand(1)))
13604 return DAG.getNode(X86ISD::BLSR, DL, VT, N1);
13605
13606 // Check RHS for X-1
13607 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
13608 isAllOnes(N1.getOperand(1)))
13609 return DAG.getNode(X86ISD::BLSR, DL, VT, N0);
13610
Craig Topper54a11172011-10-14 07:06:56 +000013611 return SDValue();
13612 }
13613
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000013614 // Want to form ANDNP nodes:
13615 // 1) In the hopes of then easily combining them with OR and AND nodes
13616 // to form PBLEND/PSIGN.
13617 // 2) To match ANDN packed intrinsics
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000013618 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000013619 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013620
Nate Begemanb65c1752010-12-17 22:55:37 +000013621 SDValue N0 = N->getOperand(0);
13622 SDValue N1 = N->getOperand(1);
13623 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013624
Nate Begemanb65c1752010-12-17 22:55:37 +000013625 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013626 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013627 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
13628 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013629 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000013630
13631 // Check RHS for vnot
13632 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013633 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
13634 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013635 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013636
Nate Begemanb65c1752010-12-17 22:55:37 +000013637 return SDValue();
13638}
13639
Evan Cheng760d1942010-01-04 21:22:48 +000013640static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000013641 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000013642 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000013643 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000013644 return SDValue();
13645
Stuart Hastings865f0932011-06-03 23:53:54 +000013646 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
13647 if (R.getNode())
13648 return R;
13649
Evan Cheng760d1942010-01-04 21:22:48 +000013650 EVT VT = N->getValueType(0);
Evan Cheng760d1942010-01-04 21:22:48 +000013651
Evan Cheng760d1942010-01-04 21:22:48 +000013652 SDValue N0 = N->getOperand(0);
13653 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013654
Nate Begemanb65c1752010-12-17 22:55:37 +000013655 // look for psign/blend
Craig Topper1666cb62011-11-19 07:07:26 +000013656 if (VT == MVT::v2i64 || VT == MVT::v4i64) {
Craig Topperc0d82852011-11-22 00:44:41 +000013657 if (!Subtarget->hasSSSE3orAVX() ||
Craig Topper1666cb62011-11-19 07:07:26 +000013658 (VT == MVT::v4i64 && !Subtarget->hasAVX2()))
13659 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013660
Craig Topper1666cb62011-11-19 07:07:26 +000013661 // Canonicalize pandn to RHS
13662 if (N0.getOpcode() == X86ISD::ANDNP)
13663 std::swap(N0, N1);
13664 // or (and (m, x), (pandn m, y))
13665 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
13666 SDValue Mask = N1.getOperand(0);
13667 SDValue X = N1.getOperand(1);
13668 SDValue Y;
13669 if (N0.getOperand(0) == Mask)
13670 Y = N0.getOperand(1);
13671 if (N0.getOperand(1) == Mask)
13672 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013673
Craig Topper1666cb62011-11-19 07:07:26 +000013674 // Check to see if the mask appeared in both the AND and ANDNP and
13675 if (!Y.getNode())
13676 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013677
Craig Topper1666cb62011-11-19 07:07:26 +000013678 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
13679 if (Mask.getOpcode() != ISD::BITCAST ||
13680 X.getOpcode() != ISD::BITCAST ||
13681 Y.getOpcode() != ISD::BITCAST)
13682 return SDValue();
Nate Begemanb65c1752010-12-17 22:55:37 +000013683
Craig Topper1666cb62011-11-19 07:07:26 +000013684 // Look through mask bitcast.
13685 Mask = Mask.getOperand(0);
13686 EVT MaskVT = Mask.getValueType();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013687
Craig Topper1666cb62011-11-19 07:07:26 +000013688 // Validate that the Mask operand is a vector sra node. The sra node
13689 // will be an intrinsic.
13690 if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
13691 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013692
Craig Topper1666cb62011-11-19 07:07:26 +000013693 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
13694 // there is no psrai.b
13695 switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
13696 case Intrinsic::x86_sse2_psrai_w:
13697 case Intrinsic::x86_sse2_psrai_d:
13698 case Intrinsic::x86_avx2_psrai_w:
13699 case Intrinsic::x86_avx2_psrai_d:
13700 break;
13701 default: return SDValue();
Nate Begemanb65c1752010-12-17 22:55:37 +000013702 }
Craig Topper1666cb62011-11-19 07:07:26 +000013703
13704 // Check that the SRA is all signbits.
13705 SDValue SraC = Mask.getOperand(2);
13706 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
13707 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
13708 if ((SraAmt + 1) != EltBits)
13709 return SDValue();
13710
13711 DebugLoc DL = N->getDebugLoc();
13712
13713 // Now we know we at least have a plendvb with the mask val. See if
13714 // we can form a psignb/w/d.
13715 // psign = x.type == y.type == mask.type && y = sub(0, x);
13716 X = X.getOperand(0);
13717 Y = Y.getOperand(0);
13718 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
13719 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
Craig Topper31133842011-11-19 07:33:10 +000013720 X.getValueType() == MaskVT && X.getValueType() == Y.getValueType() &&
13721 (EltBits == 8 || EltBits == 16 || EltBits == 32)) {
13722 SDValue Sign = DAG.getNode(X86ISD::PSIGN, DL, MaskVT, X,
13723 Mask.getOperand(1));
13724 return DAG.getNode(ISD::BITCAST, DL, VT, Sign);
Craig Topper1666cb62011-11-19 07:07:26 +000013725 }
13726 // PBLENDVB only available on SSE 4.1
Craig Topperc0d82852011-11-22 00:44:41 +000013727 if (!Subtarget->hasSSE41orAVX())
Craig Topper1666cb62011-11-19 07:07:26 +000013728 return SDValue();
13729
13730 EVT BlendVT = (VT == MVT::v4i64) ? MVT::v32i8 : MVT::v16i8;
13731
13732 X = DAG.getNode(ISD::BITCAST, DL, BlendVT, X);
13733 Y = DAG.getNode(ISD::BITCAST, DL, BlendVT, Y);
13734 Mask = DAG.getNode(ISD::BITCAST, DL, BlendVT, Mask);
Nadav Rotem18197d72011-11-30 10:13:37 +000013735 Mask = DAG.getNode(ISD::VSELECT, DL, BlendVT, Mask, Y, X);
Craig Topper1666cb62011-11-19 07:07:26 +000013736 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000013737 }
13738 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013739
Craig Topper1666cb62011-11-19 07:07:26 +000013740 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
13741 return SDValue();
13742
Nate Begemanb65c1752010-12-17 22:55:37 +000013743 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000013744 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
13745 std::swap(N0, N1);
13746 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
13747 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000013748 if (!N0.hasOneUse() || !N1.hasOneUse())
13749 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000013750
13751 SDValue ShAmt0 = N0.getOperand(1);
13752 if (ShAmt0.getValueType() != MVT::i8)
13753 return SDValue();
13754 SDValue ShAmt1 = N1.getOperand(1);
13755 if (ShAmt1.getValueType() != MVT::i8)
13756 return SDValue();
13757 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
13758 ShAmt0 = ShAmt0.getOperand(0);
13759 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
13760 ShAmt1 = ShAmt1.getOperand(0);
13761
13762 DebugLoc DL = N->getDebugLoc();
13763 unsigned Opc = X86ISD::SHLD;
13764 SDValue Op0 = N0.getOperand(0);
13765 SDValue Op1 = N1.getOperand(0);
13766 if (ShAmt0.getOpcode() == ISD::SUB) {
13767 Opc = X86ISD::SHRD;
13768 std::swap(Op0, Op1);
13769 std::swap(ShAmt0, ShAmt1);
13770 }
13771
Evan Cheng8b1190a2010-04-28 01:18:01 +000013772 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000013773 if (ShAmt1.getOpcode() == ISD::SUB) {
13774 SDValue Sum = ShAmt1.getOperand(0);
13775 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000013776 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
13777 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
13778 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
13779 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000013780 return DAG.getNode(Opc, DL, VT,
13781 Op0, Op1,
13782 DAG.getNode(ISD::TRUNCATE, DL,
13783 MVT::i8, ShAmt0));
13784 }
13785 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
13786 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
13787 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000013788 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000013789 return DAG.getNode(Opc, DL, VT,
13790 N0.getOperand(0), N1.getOperand(0),
13791 DAG.getNode(ISD::TRUNCATE, DL,
13792 MVT::i8, ShAmt0));
13793 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013794
Evan Cheng760d1942010-01-04 21:22:48 +000013795 return SDValue();
13796}
13797
Craig Topperb4c94572011-10-21 06:55:01 +000013798static SDValue PerformXorCombine(SDNode *N, SelectionDAG &DAG,
13799 TargetLowering::DAGCombinerInfo &DCI,
13800 const X86Subtarget *Subtarget) {
13801 if (DCI.isBeforeLegalizeOps())
13802 return SDValue();
13803
13804 EVT VT = N->getValueType(0);
13805
13806 if (VT != MVT::i32 && VT != MVT::i64)
13807 return SDValue();
13808
13809 // Create BLSMSK instructions by finding X ^ (X-1)
13810 SDValue N0 = N->getOperand(0);
13811 SDValue N1 = N->getOperand(1);
13812 DebugLoc DL = N->getDebugLoc();
13813
13814 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
13815 isAllOnes(N0.getOperand(1)))
13816 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N1);
13817
13818 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
13819 isAllOnes(N1.getOperand(1)))
13820 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N0);
13821
13822 return SDValue();
13823}
13824
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013825/// PerformLOADCombine - Do target-specific dag combines on LOAD nodes.
13826static SDValue PerformLOADCombine(SDNode *N, SelectionDAG &DAG,
13827 const X86Subtarget *Subtarget) {
13828 LoadSDNode *Ld = cast<LoadSDNode>(N);
13829 EVT RegVT = Ld->getValueType(0);
13830 EVT MemVT = Ld->getMemoryVT();
13831 DebugLoc dl = Ld->getDebugLoc();
13832 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13833
13834 ISD::LoadExtType Ext = Ld->getExtensionType();
13835
Nadav Rotemca6f2962011-09-18 19:00:23 +000013836 // If this is a vector EXT Load then attempt to optimize it using a
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013837 // shuffle. We need SSE4 for the shuffles.
13838 // TODO: It is possible to support ZExt by zeroing the undef values
13839 // during the shuffle phase or after the shuffle.
13840 if (RegVT.isVector() && Ext == ISD::EXTLOAD && Subtarget->hasSSE41()) {
13841 assert(MemVT != RegVT && "Cannot extend to the same type");
13842 assert(MemVT.isVector() && "Must load a vector from memory");
13843
13844 unsigned NumElems = RegVT.getVectorNumElements();
13845 unsigned RegSz = RegVT.getSizeInBits();
13846 unsigned MemSz = MemVT.getSizeInBits();
13847 assert(RegSz > MemSz && "Register size must be greater than the mem size");
Nadav Rotemca6f2962011-09-18 19:00:23 +000013848 // All sizes must be a power of two
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013849 if (!isPowerOf2_32(RegSz * MemSz * NumElems)) return SDValue();
13850
13851 // Attempt to load the original value using a single load op.
13852 // Find a scalar type which is equal to the loaded word size.
13853 MVT SclrLoadTy = MVT::i8;
13854 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
13855 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
13856 MVT Tp = (MVT::SimpleValueType)tp;
13857 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() == MemSz) {
13858 SclrLoadTy = Tp;
13859 break;
13860 }
13861 }
13862
13863 // Proceed if a load word is found.
13864 if (SclrLoadTy.getSizeInBits() != MemSz) return SDValue();
13865
13866 EVT LoadUnitVecVT = EVT::getVectorVT(*DAG.getContext(), SclrLoadTy,
13867 RegSz/SclrLoadTy.getSizeInBits());
13868
13869 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
13870 RegSz/MemVT.getScalarType().getSizeInBits());
13871 // Can't shuffle using an illegal type.
13872 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
13873
13874 // Perform a single load.
13875 SDValue ScalarLoad = DAG.getLoad(SclrLoadTy, dl, Ld->getChain(),
13876 Ld->getBasePtr(),
13877 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000013878 Ld->isNonTemporal(), Ld->isInvariant(),
13879 Ld->getAlignment());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013880
13881 // Insert the word loaded into a vector.
13882 SDValue ScalarInVector = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
13883 LoadUnitVecVT, ScalarLoad);
13884
13885 // Bitcast the loaded value to a vector of the original element type, in
13886 // the size of the target vector type.
13887 SDValue SlicedVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, ScalarInVector);
13888 unsigned SizeRatio = RegSz/MemSz;
13889
13890 // Redistribute the loaded elements into the different locations.
13891 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
13892 for (unsigned i = 0; i < NumElems; i++) ShuffleVec[i*SizeRatio] = i;
13893
13894 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
13895 DAG.getUNDEF(SlicedVec.getValueType()),
13896 ShuffleVec.data());
13897
13898 // Bitcast to the requested type.
13899 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
13900 // Replace the original load with the new sequence
13901 // and return the new chain.
13902 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Shuff);
13903 return SDValue(ScalarLoad.getNode(), 1);
13904 }
13905
13906 return SDValue();
13907}
13908
Chris Lattner149a4e52008-02-22 02:09:43 +000013909/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000013910static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000013911 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000013912 StoreSDNode *St = cast<StoreSDNode>(N);
13913 EVT VT = St->getValue().getValueType();
13914 EVT StVT = St->getMemoryVT();
13915 DebugLoc dl = St->getDebugLoc();
Nadav Rotem5e742a32011-08-11 16:41:21 +000013916 SDValue StoredVal = St->getOperand(1);
13917 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13918
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013919 // If we are saving a concatenation of two XMM registers, perform two stores.
Nadav Rotem6236f7f2011-08-11 17:05:47 +000013920 // This is better in Sandy Bridge cause one 256-bit mem op is done via two
13921 // 128-bit ones. If in the future the cost becomes only one memory access the
13922 // first version would be better.
Nadav Rotem5e742a32011-08-11 16:41:21 +000013923 if (VT.getSizeInBits() == 256 &&
13924 StoredVal.getNode()->getOpcode() == ISD::CONCAT_VECTORS &&
13925 StoredVal.getNumOperands() == 2) {
13926
13927 SDValue Value0 = StoredVal.getOperand(0);
13928 SDValue Value1 = StoredVal.getOperand(1);
13929
13930 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
13931 SDValue Ptr0 = St->getBasePtr();
13932 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
13933
13934 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
13935 St->getPointerInfo(), St->isVolatile(),
13936 St->isNonTemporal(), St->getAlignment());
13937 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
13938 St->getPointerInfo(), St->isVolatile(),
13939 St->isNonTemporal(), St->getAlignment());
13940 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
13941 }
Nadav Rotem614061b2011-08-10 19:30:14 +000013942
13943 // Optimize trunc store (of multiple scalars) to shuffle and store.
13944 // First, pack all of the elements in one place. Next, store to memory
13945 // in fewer chunks.
13946 if (St->isTruncatingStore() && VT.isVector()) {
13947 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13948 unsigned NumElems = VT.getVectorNumElements();
13949 assert(StVT != VT && "Cannot truncate to the same type");
13950 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
13951 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
13952
13953 // From, To sizes and ElemCount must be pow of two
13954 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000013955 // We are going to use the original vector elt for storing.
Nadav Rotem64ac73b2011-09-21 17:14:40 +000013956 // Accumulated smaller vector elements must be a multiple of the store size.
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000013957 if (0 != (NumElems * FromSz) % ToSz) return SDValue();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013958
Nadav Rotem614061b2011-08-10 19:30:14 +000013959 unsigned SizeRatio = FromSz / ToSz;
13960
13961 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
13962
13963 // Create a type on which we perform the shuffle
13964 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
13965 StVT.getScalarType(), NumElems*SizeRatio);
13966
13967 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
13968
13969 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
13970 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
13971 for (unsigned i = 0; i < NumElems; i++ ) ShuffleVec[i] = i * SizeRatio;
13972
13973 // Can't shuffle using an illegal type
13974 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
13975
13976 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
13977 DAG.getUNDEF(WideVec.getValueType()),
13978 ShuffleVec.data());
13979 // At this point all of the data is stored at the bottom of the
13980 // register. We now need to save it to mem.
13981
13982 // Find the largest store unit
13983 MVT StoreType = MVT::i8;
13984 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
13985 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
13986 MVT Tp = (MVT::SimpleValueType)tp;
13987 if (TLI.isTypeLegal(Tp) && StoreType.getSizeInBits() < NumElems * ToSz)
13988 StoreType = Tp;
13989 }
13990
13991 // Bitcast the original vector into a vector of store-size units
13992 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
13993 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
13994 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
13995 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
13996 SmallVector<SDValue, 8> Chains;
13997 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
13998 TLI.getPointerTy());
13999 SDValue Ptr = St->getBasePtr();
14000
14001 // Perform one or more big stores into memory.
14002 for (unsigned i = 0; i < (ToSz*NumElems)/StoreType.getSizeInBits() ; i++) {
14003 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
14004 StoreType, ShuffWide,
14005 DAG.getIntPtrConstant(i));
14006 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
14007 St->getPointerInfo(), St->isVolatile(),
14008 St->isNonTemporal(), St->getAlignment());
14009 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
14010 Chains.push_back(Ch);
14011 }
14012
14013 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
14014 Chains.size());
14015 }
14016
14017
Chris Lattner149a4e52008-02-22 02:09:43 +000014018 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
14019 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000014020 // A preferable solution to the general problem is to figure out the right
14021 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000014022
14023 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000014024 if (VT.getSizeInBits() != 64)
14025 return SDValue();
14026
Devang Patel578efa92009-06-05 21:57:13 +000014027 const Function *F = DAG.getMachineFunction().getFunction();
14028 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014029 bool F64IsLegal = !DAG.getTarget().Options.UseSoftFloat && !NoImplicitFloatOps
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000014030 && Subtarget->hasXMMInt();
Evan Cheng536e6672009-03-12 05:59:15 +000014031 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000014032 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000014033 isa<LoadSDNode>(St->getValue()) &&
14034 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
14035 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000014036 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014037 LoadSDNode *Ld = 0;
14038 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000014039 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000014040 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014041 // Must be a store of a load. We currently handle two cases: the load
14042 // is a direct child, and it's under an intervening TokenFactor. It is
14043 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000014044 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000014045 Ld = cast<LoadSDNode>(St->getChain());
14046 else if (St->getValue().hasOneUse() &&
14047 ChainVal->getOpcode() == ISD::TokenFactor) {
14048 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000014049 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000014050 TokenFactorIndex = i;
14051 Ld = cast<LoadSDNode>(St->getValue());
14052 } else
14053 Ops.push_back(ChainVal->getOperand(i));
14054 }
14055 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000014056
Evan Cheng536e6672009-03-12 05:59:15 +000014057 if (!Ld || !ISD::isNormalLoad(Ld))
14058 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014059
Evan Cheng536e6672009-03-12 05:59:15 +000014060 // If this is not the MMX case, i.e. we are just turning i64 load/store
14061 // into f64 load/store, avoid the transformation if there are multiple
14062 // uses of the loaded value.
14063 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
14064 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014065
Evan Cheng536e6672009-03-12 05:59:15 +000014066 DebugLoc LdDL = Ld->getDebugLoc();
14067 DebugLoc StDL = N->getDebugLoc();
14068 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
14069 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
14070 // pair instead.
14071 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000014072 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000014073 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
14074 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014075 Ld->isNonTemporal(), Ld->isInvariant(),
14076 Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000014077 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000014078 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000014079 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000014080 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000014081 Ops.size());
14082 }
Evan Cheng536e6672009-03-12 05:59:15 +000014083 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000014084 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000014085 St->isVolatile(), St->isNonTemporal(),
14086 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000014087 }
Evan Cheng536e6672009-03-12 05:59:15 +000014088
14089 // Otherwise, lower to two pairs of 32-bit loads / stores.
14090 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000014091 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
14092 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000014093
Owen Anderson825b72b2009-08-11 20:47:22 +000014094 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000014095 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000014096 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014097 Ld->isInvariant(), Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000014098 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000014099 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000014100 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014101 Ld->isInvariant(),
Evan Cheng536e6672009-03-12 05:59:15 +000014102 MinAlign(Ld->getAlignment(), 4));
14103
14104 SDValue NewChain = LoLd.getValue(1);
14105 if (TokenFactorIndex != -1) {
14106 Ops.push_back(LoLd);
14107 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000014108 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000014109 Ops.size());
14110 }
14111
14112 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000014113 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
14114 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000014115
14116 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000014117 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000014118 St->isVolatile(), St->isNonTemporal(),
14119 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000014120 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000014121 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000014122 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000014123 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000014124 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000014125 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000014126 }
Dan Gohman475871a2008-07-27 21:46:04 +000014127 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000014128}
14129
Duncan Sands17470be2011-09-22 20:15:48 +000014130/// isHorizontalBinOp - Return 'true' if this vector operation is "horizontal"
14131/// and return the operands for the horizontal operation in LHS and RHS. A
14132/// horizontal operation performs the binary operation on successive elements
14133/// of its first operand, then on successive elements of its second operand,
14134/// returning the resulting values in a vector. For example, if
14135/// A = < float a0, float a1, float a2, float a3 >
14136/// and
14137/// B = < float b0, float b1, float b2, float b3 >
14138/// then the result of doing a horizontal operation on A and B is
14139/// A horizontal-op B = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >.
14140/// In short, LHS and RHS are inspected to see if LHS op RHS is of the form
14141/// A horizontal-op B, for some already available A and B, and if so then LHS is
14142/// set to A, RHS to B, and the routine returns 'true'.
14143/// Note that the binary operation should have the property that if one of the
14144/// operands is UNDEF then the result is UNDEF.
Craig Topperbeabc6c2011-12-05 06:56:46 +000014145static bool isHorizontalBinOp(SDValue &LHS, SDValue &RHS, bool IsCommutative) {
Duncan Sands17470be2011-09-22 20:15:48 +000014146 // Look for the following pattern: if
14147 // A = < float a0, float a1, float a2, float a3 >
14148 // B = < float b0, float b1, float b2, float b3 >
14149 // and
14150 // LHS = VECTOR_SHUFFLE A, B, <0, 2, 4, 6>
14151 // RHS = VECTOR_SHUFFLE A, B, <1, 3, 5, 7>
14152 // then LHS op RHS = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >
14153 // which is A horizontal-op B.
14154
14155 // At least one of the operands should be a vector shuffle.
14156 if (LHS.getOpcode() != ISD::VECTOR_SHUFFLE &&
14157 RHS.getOpcode() != ISD::VECTOR_SHUFFLE)
14158 return false;
14159
14160 EVT VT = LHS.getValueType();
Craig Topperf8363302011-12-02 08:18:41 +000014161
14162 assert((VT.is128BitVector() || VT.is256BitVector()) &&
14163 "Unsupported vector type for horizontal add/sub");
14164
14165 // Handle 128 and 256-bit vector lengths. AVX defines horizontal add/sub to
14166 // operate independently on 128-bit lanes.
Craig Topperb72039c2011-11-30 09:10:50 +000014167 unsigned NumElts = VT.getVectorNumElements();
14168 unsigned NumLanes = VT.getSizeInBits()/128;
14169 unsigned NumLaneElts = NumElts / NumLanes;
Craig Topperf8363302011-12-02 08:18:41 +000014170 assert((NumLaneElts % 2 == 0) &&
14171 "Vector type should have an even number of elements in each lane");
14172 unsigned HalfLaneElts = NumLaneElts/2;
Duncan Sands17470be2011-09-22 20:15:48 +000014173
14174 // View LHS in the form
14175 // LHS = VECTOR_SHUFFLE A, B, LMask
14176 // If LHS is not a shuffle then pretend it is the shuffle
14177 // LHS = VECTOR_SHUFFLE LHS, undef, <0, 1, ..., N-1>
14178 // NOTE: in what follows a default initialized SDValue represents an UNDEF of
14179 // type VT.
14180 SDValue A, B;
Craig Topperb72039c2011-11-30 09:10:50 +000014181 SmallVector<int, 16> LMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000014182 if (LHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
14183 if (LHS.getOperand(0).getOpcode() != ISD::UNDEF)
14184 A = LHS.getOperand(0);
14185 if (LHS.getOperand(1).getOpcode() != ISD::UNDEF)
14186 B = LHS.getOperand(1);
14187 cast<ShuffleVectorSDNode>(LHS.getNode())->getMask(LMask);
14188 } else {
14189 if (LHS.getOpcode() != ISD::UNDEF)
14190 A = LHS;
Craig Topperb72039c2011-11-30 09:10:50 +000014191 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000014192 LMask[i] = i;
14193 }
14194
14195 // Likewise, view RHS in the form
14196 // RHS = VECTOR_SHUFFLE C, D, RMask
14197 SDValue C, D;
Craig Topperb72039c2011-11-30 09:10:50 +000014198 SmallVector<int, 16> RMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000014199 if (RHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
14200 if (RHS.getOperand(0).getOpcode() != ISD::UNDEF)
14201 C = RHS.getOperand(0);
14202 if (RHS.getOperand(1).getOpcode() != ISD::UNDEF)
14203 D = RHS.getOperand(1);
14204 cast<ShuffleVectorSDNode>(RHS.getNode())->getMask(RMask);
14205 } else {
14206 if (RHS.getOpcode() != ISD::UNDEF)
14207 C = RHS;
Craig Topperb72039c2011-11-30 09:10:50 +000014208 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000014209 RMask[i] = i;
14210 }
14211
14212 // Check that the shuffles are both shuffling the same vectors.
14213 if (!(A == C && B == D) && !(A == D && B == C))
14214 return false;
14215
14216 // If everything is UNDEF then bail out: it would be better to fold to UNDEF.
14217 if (!A.getNode() && !B.getNode())
14218 return false;
14219
14220 // If A and B occur in reverse order in RHS, then "swap" them (which means
14221 // rewriting the mask).
14222 if (A != C)
Craig Topperbeabc6c2011-12-05 06:56:46 +000014223 CommuteVectorShuffleMask(RMask, NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000014224
14225 // At this point LHS and RHS are equivalent to
14226 // LHS = VECTOR_SHUFFLE A, B, LMask
14227 // RHS = VECTOR_SHUFFLE A, B, RMask
14228 // Check that the masks correspond to performing a horizontal operation.
Craig Topperf8363302011-12-02 08:18:41 +000014229 for (unsigned i = 0; i != NumElts; ++i) {
Craig Topperbeabc6c2011-12-05 06:56:46 +000014230 int LIdx = LMask[i], RIdx = RMask[i];
Duncan Sands17470be2011-09-22 20:15:48 +000014231
Craig Topperf8363302011-12-02 08:18:41 +000014232 // Ignore any UNDEF components.
Craig Topperbeabc6c2011-12-05 06:56:46 +000014233 if (LIdx < 0 || RIdx < 0 ||
14234 (!A.getNode() && (LIdx < (int)NumElts || RIdx < (int)NumElts)) ||
14235 (!B.getNode() && (LIdx >= (int)NumElts || RIdx >= (int)NumElts)))
Craig Topperf8363302011-12-02 08:18:41 +000014236 continue;
Duncan Sands17470be2011-09-22 20:15:48 +000014237
Craig Topperf8363302011-12-02 08:18:41 +000014238 // Check that successive elements are being operated on. If not, this is
14239 // not a horizontal operation.
14240 unsigned Src = (i/HalfLaneElts) % 2; // each lane is split between srcs
14241 unsigned LaneStart = (i/NumLaneElts) * NumLaneElts;
Craig Topperbeabc6c2011-12-05 06:56:46 +000014242 int Index = 2*(i%HalfLaneElts) + NumElts*Src + LaneStart;
Craig Topperf8363302011-12-02 08:18:41 +000014243 if (!(LIdx == Index && RIdx == Index + 1) &&
Craig Topperbeabc6c2011-12-05 06:56:46 +000014244 !(IsCommutative && LIdx == Index + 1 && RIdx == Index))
Craig Topperf8363302011-12-02 08:18:41 +000014245 return false;
Duncan Sands17470be2011-09-22 20:15:48 +000014246 }
14247
14248 LHS = A.getNode() ? A : B; // If A is 'UNDEF', use B for it.
14249 RHS = B.getNode() ? B : A; // If B is 'UNDEF', use A for it.
14250 return true;
14251}
14252
14253/// PerformFADDCombine - Do target-specific dag combines on floating point adds.
14254static SDValue PerformFADDCombine(SDNode *N, SelectionDAG &DAG,
14255 const X86Subtarget *Subtarget) {
14256 EVT VT = N->getValueType(0);
14257 SDValue LHS = N->getOperand(0);
14258 SDValue RHS = N->getOperand(1);
14259
14260 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topper138a5c62011-12-02 07:16:01 +000014261 if (((Subtarget->hasSSE3orAVX() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
14262 (Subtarget->hasAVX() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000014263 isHorizontalBinOp(LHS, RHS, true))
14264 return DAG.getNode(X86ISD::FHADD, N->getDebugLoc(), VT, LHS, RHS);
14265 return SDValue();
14266}
14267
14268/// PerformFSUBCombine - Do target-specific dag combines on floating point subs.
14269static SDValue PerformFSUBCombine(SDNode *N, SelectionDAG &DAG,
14270 const X86Subtarget *Subtarget) {
14271 EVT VT = N->getValueType(0);
14272 SDValue LHS = N->getOperand(0);
14273 SDValue RHS = N->getOperand(1);
14274
14275 // Try to synthesize horizontal subs from subs of shuffles.
Craig Topper138a5c62011-12-02 07:16:01 +000014276 if (((Subtarget->hasSSE3orAVX() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
14277 (Subtarget->hasAVX() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000014278 isHorizontalBinOp(LHS, RHS, false))
14279 return DAG.getNode(X86ISD::FHSUB, N->getDebugLoc(), VT, LHS, RHS);
14280 return SDValue();
14281}
14282
Chris Lattner6cf73262008-01-25 06:14:17 +000014283/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
14284/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000014285static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000014286 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
14287 // F[X]OR(0.0, x) -> x
14288 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000014289 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
14290 if (C->getValueAPF().isPosZero())
14291 return N->getOperand(1);
14292 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
14293 if (C->getValueAPF().isPosZero())
14294 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000014295 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000014296}
14297
14298/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000014299static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000014300 // FAND(0.0, x) -> 0.0
14301 // FAND(x, 0.0) -> 0.0
14302 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
14303 if (C->getValueAPF().isPosZero())
14304 return N->getOperand(0);
14305 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
14306 if (C->getValueAPF().isPosZero())
14307 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000014308 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000014309}
14310
Dan Gohmane5af2d32009-01-29 01:59:02 +000014311static SDValue PerformBTCombine(SDNode *N,
14312 SelectionDAG &DAG,
14313 TargetLowering::DAGCombinerInfo &DCI) {
14314 // BT ignores high bits in the bit index operand.
14315 SDValue Op1 = N->getOperand(1);
14316 if (Op1.hasOneUse()) {
14317 unsigned BitWidth = Op1.getValueSizeInBits();
14318 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
14319 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014320 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
14321 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000014322 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000014323 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
14324 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
14325 DCI.CommitTargetLoweringOpt(TLO);
14326 }
14327 return SDValue();
14328}
Chris Lattner83e6c992006-10-04 06:57:07 +000014329
Eli Friedman7a5e5552009-06-07 06:52:44 +000014330static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
14331 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000014332 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000014333 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000014334 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000014335 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000014336 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000014337 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000014338 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000014339 }
14340 return SDValue();
14341}
14342
Evan Cheng2e489c42009-12-16 00:53:11 +000014343static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
14344 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
14345 // (and (i32 x86isd::setcc_carry), 1)
14346 // This eliminates the zext. This transformation is necessary because
14347 // ISD::SETCC is always legalized to i8.
14348 DebugLoc dl = N->getDebugLoc();
14349 SDValue N0 = N->getOperand(0);
14350 EVT VT = N->getValueType(0);
14351 if (N0.getOpcode() == ISD::AND &&
14352 N0.hasOneUse() &&
14353 N0.getOperand(0).hasOneUse()) {
14354 SDValue N00 = N0.getOperand(0);
14355 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
14356 return SDValue();
14357 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
14358 if (!C || C->getZExtValue() != 1)
14359 return SDValue();
14360 return DAG.getNode(ISD::AND, dl, VT,
14361 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
14362 N00.getOperand(0), N00.getOperand(1)),
14363 DAG.getConstant(1, VT));
14364 }
14365
14366 return SDValue();
14367}
14368
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014369// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
14370static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
14371 unsigned X86CC = N->getConstantOperandVal(0);
14372 SDValue EFLAG = N->getOperand(1);
14373 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014374
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014375 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
14376 // a zext and produces an all-ones bit which is more useful than 0/1 in some
14377 // cases.
14378 if (X86CC == X86::COND_B)
14379 return DAG.getNode(ISD::AND, DL, MVT::i8,
14380 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
14381 DAG.getConstant(X86CC, MVT::i8), EFLAG),
14382 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014383
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014384 return SDValue();
14385}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014386
Benjamin Kramer1396c402011-06-18 11:09:41 +000014387static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
14388 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000014389 SDValue Op0 = N->getOperand(0);
14390 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
14391 // a 32-bit target where SSE doesn't support i64->FP operations.
14392 if (Op0.getOpcode() == ISD::LOAD) {
14393 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
14394 EVT VT = Ld->getValueType(0);
14395 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
14396 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
14397 !XTLI->getSubtarget()->is64Bit() &&
14398 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000014399 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
14400 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000014401 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
14402 return FILDChain;
14403 }
14404 }
14405 return SDValue();
14406}
14407
Chris Lattner23a01992010-12-20 01:37:09 +000014408// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
14409static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
14410 X86TargetLowering::DAGCombinerInfo &DCI) {
14411 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
14412 // the result is either zero or one (depending on the input carry bit).
14413 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
14414 if (X86::isZeroNode(N->getOperand(0)) &&
14415 X86::isZeroNode(N->getOperand(1)) &&
14416 // We don't have a good way to replace an EFLAGS use, so only do this when
14417 // dead right now.
14418 SDValue(N, 1).use_empty()) {
14419 DebugLoc DL = N->getDebugLoc();
14420 EVT VT = N->getValueType(0);
14421 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
14422 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
14423 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
14424 DAG.getConstant(X86::COND_B,MVT::i8),
14425 N->getOperand(2)),
14426 DAG.getConstant(1, VT));
14427 return DCI.CombineTo(N, Res1, CarryOut);
14428 }
14429
14430 return SDValue();
14431}
14432
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014433// fold (add Y, (sete X, 0)) -> adc 0, Y
14434// (add Y, (setne X, 0)) -> sbb -1, Y
14435// (sub (sete X, 0), Y) -> sbb 0, Y
14436// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014437static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014438 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014439
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014440 // Look through ZExts.
14441 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
14442 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
14443 return SDValue();
14444
14445 SDValue SetCC = Ext.getOperand(0);
14446 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
14447 return SDValue();
14448
14449 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
14450 if (CC != X86::COND_E && CC != X86::COND_NE)
14451 return SDValue();
14452
14453 SDValue Cmp = SetCC.getOperand(1);
14454 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000014455 !X86::isZeroNode(Cmp.getOperand(1)) ||
14456 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014457 return SDValue();
14458
14459 SDValue CmpOp0 = Cmp.getOperand(0);
14460 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
14461 DAG.getConstant(1, CmpOp0.getValueType()));
14462
14463 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
14464 if (CC == X86::COND_NE)
14465 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
14466 DL, OtherVal.getValueType(), OtherVal,
14467 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
14468 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
14469 DL, OtherVal.getValueType(), OtherVal,
14470 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
14471}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014472
Craig Topper54f952a2011-11-19 09:02:40 +000014473/// PerformADDCombine - Do target-specific dag combines on integer adds.
14474static SDValue PerformAddCombine(SDNode *N, SelectionDAG &DAG,
14475 const X86Subtarget *Subtarget) {
14476 EVT VT = N->getValueType(0);
14477 SDValue Op0 = N->getOperand(0);
14478 SDValue Op1 = N->getOperand(1);
14479
14480 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperb72039c2011-11-30 09:10:50 +000014481 if (((Subtarget->hasSSSE3orAVX() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
14482 (Subtarget->hasAVX2() && (VT == MVT::v16i16 || MVT::v8i32))) &&
Craig Topper54f952a2011-11-19 09:02:40 +000014483 isHorizontalBinOp(Op0, Op1, true))
14484 return DAG.getNode(X86ISD::HADD, N->getDebugLoc(), VT, Op0, Op1);
14485
14486 return OptimizeConditionalInDecrement(N, DAG);
14487}
14488
14489static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG,
14490 const X86Subtarget *Subtarget) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014491 SDValue Op0 = N->getOperand(0);
14492 SDValue Op1 = N->getOperand(1);
14493
14494 // X86 can't encode an immediate LHS of a sub. See if we can push the
14495 // negation into a preceding instruction.
14496 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014497 // If the RHS of the sub is a XOR with one use and a constant, invert the
14498 // immediate. Then add one to the LHS of the sub so we can turn
14499 // X-Y -> X+~Y+1, saving one register.
14500 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
14501 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000014502 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014503 EVT VT = Op0.getValueType();
14504 SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
14505 Op1.getOperand(0),
14506 DAG.getConstant(~XorC, VT));
14507 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000014508 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014509 }
14510 }
14511
Craig Topper54f952a2011-11-19 09:02:40 +000014512 // Try to synthesize horizontal adds from adds of shuffles.
14513 EVT VT = N->getValueType(0);
Craig Topperb72039c2011-11-30 09:10:50 +000014514 if (((Subtarget->hasSSSE3orAVX() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
14515 (Subtarget->hasAVX2() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
14516 isHorizontalBinOp(Op0, Op1, true))
Craig Topper54f952a2011-11-19 09:02:40 +000014517 return DAG.getNode(X86ISD::HSUB, N->getDebugLoc(), VT, Op0, Op1);
14518
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014519 return OptimizeConditionalInDecrement(N, DAG);
14520}
14521
Dan Gohman475871a2008-07-27 21:46:04 +000014522SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000014523 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000014524 SelectionDAG &DAG = DCI.DAG;
14525 switch (N->getOpcode()) {
14526 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014527 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014528 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Duncan Sands6bcd2192011-09-17 16:49:39 +000014529 case ISD::VSELECT:
Chris Lattneraf723b92008-01-25 05:46:26 +000014530 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000014531 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Craig Topper54f952a2011-11-19 09:02:40 +000014532 case ISD::ADD: return PerformAddCombine(N, DAG, Subtarget);
14533 case ISD::SUB: return PerformSubCombine(N, DAG, Subtarget);
Chris Lattner23a01992010-12-20 01:37:09 +000014534 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000014535 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000014536 case ISD::SHL:
14537 case ISD::SRA:
14538 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000014539 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000014540 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Craig Topperb4c94572011-10-21 06:55:01 +000014541 case ISD::XOR: return PerformXorCombine(N, DAG, DCI, Subtarget);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014542 case ISD::LOAD: return PerformLOADCombine(N, DAG, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000014543 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000014544 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Duncan Sands17470be2011-09-22 20:15:48 +000014545 case ISD::FADD: return PerformFADDCombine(N, DAG, Subtarget);
14546 case ISD::FSUB: return PerformFSUBCombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000014547 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000014548 case X86ISD::FOR: return PerformFORCombine(N, DAG);
14549 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000014550 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000014551 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000014552 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014553 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014554 case X86ISD::SHUFPS: // Handle all target specific shuffles
14555 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000014556 case X86ISD::PALIGN:
Craig Topper34671b82011-12-06 08:21:25 +000014557 case X86ISD::UNPCKH:
14558 case X86ISD::UNPCKL:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014559 case X86ISD::MOVHLPS:
14560 case X86ISD::MOVLHPS:
14561 case X86ISD::PSHUFD:
14562 case X86ISD::PSHUFHW:
14563 case X86ISD::PSHUFLW:
14564 case X86ISD::MOVSS:
14565 case X86ISD::MOVSD:
Craig Topper316cd2a2011-11-30 06:25:25 +000014566 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +000014567 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014568 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000014569 }
14570
Dan Gohman475871a2008-07-27 21:46:04 +000014571 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000014572}
14573
Evan Chenge5b51ac2010-04-17 06:13:15 +000014574/// isTypeDesirableForOp - Return true if the target has native support for
14575/// the specified value type and it is 'desirable' to use the type for the
14576/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
14577/// instruction encodings are longer and some i16 instructions are slow.
14578bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
14579 if (!isTypeLegal(VT))
14580 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014581 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000014582 return true;
14583
14584 switch (Opc) {
14585 default:
14586 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000014587 case ISD::LOAD:
14588 case ISD::SIGN_EXTEND:
14589 case ISD::ZERO_EXTEND:
14590 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000014591 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000014592 case ISD::SRL:
14593 case ISD::SUB:
14594 case ISD::ADD:
14595 case ISD::MUL:
14596 case ISD::AND:
14597 case ISD::OR:
14598 case ISD::XOR:
14599 return false;
14600 }
14601}
14602
14603/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000014604/// beneficial for dag combiner to promote the specified node. If true, it
14605/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000014606bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000014607 EVT VT = Op.getValueType();
14608 if (VT != MVT::i16)
14609 return false;
14610
Evan Cheng4c26e932010-04-19 19:29:22 +000014611 bool Promote = false;
14612 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014613 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000014614 default: break;
14615 case ISD::LOAD: {
14616 LoadSDNode *LD = cast<LoadSDNode>(Op);
14617 // If the non-extending load has a single use and it's not live out, then it
14618 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014619 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
14620 Op.hasOneUse()*/) {
14621 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
14622 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
14623 // The only case where we'd want to promote LOAD (rather then it being
14624 // promoted as an operand is when it's only use is liveout.
14625 if (UI->getOpcode() != ISD::CopyToReg)
14626 return false;
14627 }
14628 }
Evan Cheng4c26e932010-04-19 19:29:22 +000014629 Promote = true;
14630 break;
14631 }
14632 case ISD::SIGN_EXTEND:
14633 case ISD::ZERO_EXTEND:
14634 case ISD::ANY_EXTEND:
14635 Promote = true;
14636 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014637 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014638 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000014639 SDValue N0 = Op.getOperand(0);
14640 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000014641 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000014642 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000014643 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014644 break;
14645 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000014646 case ISD::ADD:
14647 case ISD::MUL:
14648 case ISD::AND:
14649 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000014650 case ISD::XOR:
14651 Commute = true;
14652 // fallthrough
14653 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000014654 SDValue N0 = Op.getOperand(0);
14655 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000014656 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014657 return false;
14658 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000014659 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014660 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000014661 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014662 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000014663 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014664 }
14665 }
14666
14667 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000014668 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014669}
14670
Evan Cheng60c07e12006-07-05 22:17:51 +000014671//===----------------------------------------------------------------------===//
14672// X86 Inline Assembly Support
14673//===----------------------------------------------------------------------===//
14674
Chris Lattnerb8105652009-07-20 17:51:36 +000014675bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
14676 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000014677
14678 std::string AsmStr = IA->getAsmString();
14679
14680 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000014681 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000014682 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000014683
14684 switch (AsmPieces.size()) {
14685 default: return false;
14686 case 1:
14687 AsmStr = AsmPieces[0];
14688 AsmPieces.clear();
14689 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
14690
Chris Lattner7a2bdde2011-04-15 05:18:47 +000014691 // FIXME: this should verify that we are targeting a 486 or better. If not,
Evan Cheng55d42002011-01-08 01:24:27 +000014692 // we will turn this bswap into something that will be lowered to logical ops
14693 // instead of emitting the bswap asm. For now, we don't support 486 or lower
14694 // so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000014695 // bswap $0
14696 if (AsmPieces.size() == 2 &&
14697 (AsmPieces[0] == "bswap" ||
14698 AsmPieces[0] == "bswapq" ||
14699 AsmPieces[0] == "bswapl") &&
14700 (AsmPieces[1] == "$0" ||
14701 AsmPieces[1] == "${0:q}")) {
14702 // No need to check constraints, nothing other than the equivalent of
14703 // "=r,0" would be valid here.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014704 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014705 if (!Ty || Ty->getBitWidth() % 16 != 0)
14706 return false;
14707 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000014708 }
14709 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000014710 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000014711 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000014712 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000014713 AsmPieces[1] == "$$8," &&
14714 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000014715 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
14716 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014717 const std::string &ConstraintsStr = IA->getConstraintString();
14718 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000014719 std::sort(AsmPieces.begin(), AsmPieces.end());
14720 if (AsmPieces.size() == 4 &&
14721 AsmPieces[0] == "~{cc}" &&
14722 AsmPieces[1] == "~{dirflag}" &&
14723 AsmPieces[2] == "~{flags}" &&
14724 AsmPieces[3] == "~{fpsr}") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014725 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014726 if (!Ty || Ty->getBitWidth() % 16 != 0)
14727 return false;
14728 return IntrinsicLowering::LowerToByteSwap(CI);
Dan Gohman0ef701e2010-03-04 19:58:08 +000014729 }
Chris Lattnerb8105652009-07-20 17:51:36 +000014730 }
14731 break;
14732 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000014733 if (CI->getType()->isIntegerTy(32) &&
14734 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
14735 SmallVector<StringRef, 4> Words;
14736 SplitString(AsmPieces[0], Words, " \t,");
14737 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
14738 Words[2] == "${0:w}") {
14739 Words.clear();
14740 SplitString(AsmPieces[1], Words, " \t,");
14741 if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
14742 Words[2] == "$0") {
14743 Words.clear();
14744 SplitString(AsmPieces[2], Words, " \t,");
14745 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
14746 Words[2] == "${0:w}") {
14747 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014748 const std::string &ConstraintsStr = IA->getConstraintString();
14749 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Peter Collingbourne948cf022010-11-13 19:54:30 +000014750 std::sort(AsmPieces.begin(), AsmPieces.end());
14751 if (AsmPieces.size() == 4 &&
14752 AsmPieces[0] == "~{cc}" &&
14753 AsmPieces[1] == "~{dirflag}" &&
14754 AsmPieces[2] == "~{flags}" &&
14755 AsmPieces[3] == "~{fpsr}") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014756 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014757 if (!Ty || Ty->getBitWidth() % 16 != 0)
14758 return false;
14759 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000014760 }
14761 }
14762 }
14763 }
14764 }
Evan Cheng55d42002011-01-08 01:24:27 +000014765
14766 if (CI->getType()->isIntegerTy(64)) {
14767 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
14768 if (Constraints.size() >= 2 &&
14769 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
14770 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
14771 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
14772 SmallVector<StringRef, 4> Words;
14773 SplitString(AsmPieces[0], Words, " \t");
14774 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
Chris Lattnerb8105652009-07-20 17:51:36 +000014775 Words.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014776 SplitString(AsmPieces[1], Words, " \t");
14777 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
14778 Words.clear();
14779 SplitString(AsmPieces[2], Words, " \t,");
14780 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
14781 Words[2] == "%edx") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014782 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014783 if (!Ty || Ty->getBitWidth() % 16 != 0)
14784 return false;
14785 return IntrinsicLowering::LowerToByteSwap(CI);
14786 }
Chris Lattnerb8105652009-07-20 17:51:36 +000014787 }
14788 }
14789 }
14790 }
14791 break;
14792 }
14793 return false;
14794}
14795
14796
14797
Chris Lattnerf4dff842006-07-11 02:54:03 +000014798/// getConstraintType - Given a constraint letter, return the type of
14799/// constraint it is for this target.
14800X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000014801X86TargetLowering::getConstraintType(const std::string &Constraint) const {
14802 if (Constraint.size() == 1) {
14803 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000014804 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000014805 case 'q':
14806 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000014807 case 'f':
14808 case 't':
14809 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000014810 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000014811 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000014812 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000014813 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000014814 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000014815 case 'a':
14816 case 'b':
14817 case 'c':
14818 case 'd':
14819 case 'S':
14820 case 'D':
14821 case 'A':
14822 return C_Register;
14823 case 'I':
14824 case 'J':
14825 case 'K':
14826 case 'L':
14827 case 'M':
14828 case 'N':
14829 case 'G':
14830 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000014831 case 'e':
14832 case 'Z':
14833 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000014834 default:
14835 break;
14836 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000014837 }
Chris Lattner4234f572007-03-25 02:14:49 +000014838 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000014839}
14840
John Thompson44ab89e2010-10-29 17:29:13 +000014841/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000014842/// This object must already have been set up with the operand type
14843/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000014844TargetLowering::ConstraintWeight
14845 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000014846 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000014847 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014848 Value *CallOperandVal = info.CallOperandVal;
14849 // If we don't have a value, we can't do a match,
14850 // but allow it at the lowest weight.
14851 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000014852 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014853 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000014854 // Look at the constraint type.
14855 switch (*constraint) {
14856 default:
John Thompson44ab89e2010-10-29 17:29:13 +000014857 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
14858 case 'R':
14859 case 'q':
14860 case 'Q':
14861 case 'a':
14862 case 'b':
14863 case 'c':
14864 case 'd':
14865 case 'S':
14866 case 'D':
14867 case 'A':
14868 if (CallOperandVal->getType()->isIntegerTy())
14869 weight = CW_SpecificReg;
14870 break;
14871 case 'f':
14872 case 't':
14873 case 'u':
14874 if (type->isFloatingPointTy())
14875 weight = CW_SpecificReg;
14876 break;
14877 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000014878 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000014879 weight = CW_SpecificReg;
14880 break;
14881 case 'x':
14882 case 'Y':
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014883 if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
John Thompson44ab89e2010-10-29 17:29:13 +000014884 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014885 break;
14886 case 'I':
14887 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
14888 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000014889 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014890 }
14891 break;
John Thompson44ab89e2010-10-29 17:29:13 +000014892 case 'J':
14893 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14894 if (C->getZExtValue() <= 63)
14895 weight = CW_Constant;
14896 }
14897 break;
14898 case 'K':
14899 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14900 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
14901 weight = CW_Constant;
14902 }
14903 break;
14904 case 'L':
14905 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14906 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
14907 weight = CW_Constant;
14908 }
14909 break;
14910 case 'M':
14911 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14912 if (C->getZExtValue() <= 3)
14913 weight = CW_Constant;
14914 }
14915 break;
14916 case 'N':
14917 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14918 if (C->getZExtValue() <= 0xff)
14919 weight = CW_Constant;
14920 }
14921 break;
14922 case 'G':
14923 case 'C':
14924 if (dyn_cast<ConstantFP>(CallOperandVal)) {
14925 weight = CW_Constant;
14926 }
14927 break;
14928 case 'e':
14929 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14930 if ((C->getSExtValue() >= -0x80000000LL) &&
14931 (C->getSExtValue() <= 0x7fffffffLL))
14932 weight = CW_Constant;
14933 }
14934 break;
14935 case 'Z':
14936 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14937 if (C->getZExtValue() <= 0xffffffff)
14938 weight = CW_Constant;
14939 }
14940 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014941 }
14942 return weight;
14943}
14944
Dale Johannesenba2a0b92008-01-29 02:21:21 +000014945/// LowerXConstraint - try to replace an X constraint, which matches anything,
14946/// with another that has more specific requirements based on the type of the
14947/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000014948const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000014949LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000014950 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
14951 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000014952 if (ConstraintVT.isFloatingPoint()) {
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014953 if (Subtarget->hasXMMInt())
Chris Lattner5e764232008-04-26 23:02:14 +000014954 return "Y";
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014955 if (Subtarget->hasXMM())
Chris Lattner5e764232008-04-26 23:02:14 +000014956 return "x";
14957 }
Scott Michelfdc40a02009-02-17 22:15:04 +000014958
Chris Lattner5e764232008-04-26 23:02:14 +000014959 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000014960}
14961
Chris Lattner48884cd2007-08-25 00:47:38 +000014962/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
14963/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000014964void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000014965 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000014966 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000014967 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000014968 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000014969
Eric Christopher100c8332011-06-02 23:16:42 +000014970 // Only support length 1 constraints for now.
14971 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000014972
Eric Christopher100c8332011-06-02 23:16:42 +000014973 char ConstraintLetter = Constraint[0];
14974 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000014975 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000014976 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000014977 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000014978 if (C->getZExtValue() <= 31) {
14979 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000014980 break;
14981 }
Devang Patel84f7fd22007-03-17 00:13:28 +000014982 }
Chris Lattner48884cd2007-08-25 00:47:38 +000014983 return;
Evan Cheng364091e2008-09-22 23:57:37 +000014984 case 'J':
14985 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000014986 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000014987 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
14988 break;
14989 }
14990 }
14991 return;
14992 case 'K':
14993 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000014994 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000014995 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
14996 break;
14997 }
14998 }
14999 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000015000 case 'N':
15001 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000015002 if (C->getZExtValue() <= 255) {
15003 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000015004 break;
15005 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000015006 }
Chris Lattner48884cd2007-08-25 00:47:38 +000015007 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000015008 case 'e': {
15009 // 32-bit signed value
15010 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000015011 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
15012 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000015013 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000015014 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000015015 break;
15016 }
15017 // FIXME gcc accepts some relocatable values here too, but only in certain
15018 // memory models; it's complicated.
15019 }
15020 return;
15021 }
15022 case 'Z': {
15023 // 32-bit unsigned value
15024 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000015025 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
15026 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000015027 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
15028 break;
15029 }
15030 }
15031 // FIXME gcc accepts some relocatable values here too, but only in certain
15032 // memory models; it's complicated.
15033 return;
15034 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000015035 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000015036 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000015037 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000015038 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000015039 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000015040 break;
15041 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015042
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000015043 // In any sort of PIC mode addresses need to be computed at runtime by
15044 // adding in a register or some sort of table lookup. These can't
15045 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000015046 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000015047 return;
15048
Chris Lattnerdc43a882007-05-03 16:52:29 +000015049 // If we are in non-pic codegen mode, we allow the address of a global (with
15050 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000015051 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000015052 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000015053
Chris Lattner49921962009-05-08 18:23:14 +000015054 // Match either (GA), (GA+C), (GA+C1+C2), etc.
15055 while (1) {
15056 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
15057 Offset += GA->getOffset();
15058 break;
15059 } else if (Op.getOpcode() == ISD::ADD) {
15060 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
15061 Offset += C->getZExtValue();
15062 Op = Op.getOperand(0);
15063 continue;
15064 }
15065 } else if (Op.getOpcode() == ISD::SUB) {
15066 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
15067 Offset += -C->getZExtValue();
15068 Op = Op.getOperand(0);
15069 continue;
15070 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000015071 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000015072
Chris Lattner49921962009-05-08 18:23:14 +000015073 // Otherwise, this isn't something we can handle, reject it.
15074 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000015075 }
Eric Christopherfd179292009-08-27 18:07:15 +000015076
Dan Gohman46510a72010-04-15 01:51:59 +000015077 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000015078 // If we require an extra load to get this address, as in PIC mode, we
15079 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000015080 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
15081 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000015082 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000015083
Devang Patel0d881da2010-07-06 22:08:15 +000015084 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
15085 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000015086 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000015087 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000015088 }
Scott Michelfdc40a02009-02-17 22:15:04 +000015089
Gabor Greifba36cb52008-08-28 21:40:38 +000015090 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000015091 Ops.push_back(Result);
15092 return;
15093 }
Dale Johannesen1784d162010-06-25 21:55:36 +000015094 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000015095}
15096
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015097std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000015098X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000015099 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000015100 // First, see if this is a constraint that directly corresponds to an LLVM
15101 // register class.
15102 if (Constraint.size() == 1) {
15103 // GCC Constraint Letters
15104 switch (Constraint[0]) {
15105 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000015106 // TODO: Slight differences here in allocation order and leaving
15107 // RIP in the class. Do they matter any more here than they do
15108 // in the normal allocation?
15109 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
15110 if (Subtarget->is64Bit()) {
Nick Lewycky9bf45d02011-07-08 00:19:27 +000015111 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000015112 return std::make_pair(0U, X86::GR32RegisterClass);
15113 else if (VT == MVT::i16)
15114 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000015115 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000015116 return std::make_pair(0U, X86::GR8RegisterClass);
Nick Lewycky9bf45d02011-07-08 00:19:27 +000015117 else if (VT == MVT::i64 || VT == MVT::f64)
Eric Christopherd176af82011-06-29 17:23:50 +000015118 return std::make_pair(0U, X86::GR64RegisterClass);
15119 break;
15120 }
15121 // 32-bit fallthrough
15122 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000015123 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000015124 return std::make_pair(0U, X86::GR32_ABCDRegisterClass);
15125 else if (VT == MVT::i16)
15126 return std::make_pair(0U, X86::GR16_ABCDRegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000015127 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000015128 return std::make_pair(0U, X86::GR8_ABCD_LRegisterClass);
15129 else if (VT == MVT::i64)
15130 return std::make_pair(0U, X86::GR64_ABCDRegisterClass);
15131 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000015132 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000015133 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000015134 if (VT == MVT::i8 || VT == MVT::i1)
Chris Lattner0f65cad2007-04-09 05:49:22 +000015135 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000015136 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000015137 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000015138 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000015139 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000015140 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000015141 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000015142 if (VT == MVT::i8 || VT == MVT::i1)
Dale Johannesen5f3663e2009-10-07 22:47:20 +000015143 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
15144 if (VT == MVT::i16)
15145 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
15146 if (VT == MVT::i32 || !Subtarget->is64Bit())
15147 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
15148 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000015149 case 'f': // FP Stack registers.
15150 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
15151 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000015152 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000015153 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000015154 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000015155 return std::make_pair(0U, X86::RFP64RegisterClass);
15156 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000015157 case 'y': // MMX_REGS if MMX allowed.
15158 if (!Subtarget->hasMMX()) break;
15159 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000015160 case 'Y': // SSE_REGS if SSE2 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000015161 if (!Subtarget->hasXMMInt()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000015162 // FALL THROUGH.
15163 case 'x': // SSE_REGS if SSE1 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000015164 if (!Subtarget->hasXMM()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000015165
Owen Anderson825b72b2009-08-11 20:47:22 +000015166 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000015167 default: break;
15168 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000015169 case MVT::f32:
15170 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000015171 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000015172 case MVT::f64:
15173 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000015174 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000015175 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000015176 case MVT::v16i8:
15177 case MVT::v8i16:
15178 case MVT::v4i32:
15179 case MVT::v2i64:
15180 case MVT::v4f32:
15181 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000015182 return std::make_pair(0U, X86::VR128RegisterClass);
15183 }
Chris Lattnerad043e82007-04-09 05:11:28 +000015184 break;
15185 }
15186 }
Scott Michelfdc40a02009-02-17 22:15:04 +000015187
Chris Lattnerf76d1802006-07-31 23:26:50 +000015188 // Use the default implementation in TargetLowering to convert the register
15189 // constraint into a member of a register class.
15190 std::pair<unsigned, const TargetRegisterClass*> Res;
15191 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000015192
15193 // Not found as a standard register?
15194 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000015195 // Map st(0) -> st(7) -> ST0
15196 if (Constraint.size() == 7 && Constraint[0] == '{' &&
15197 tolower(Constraint[1]) == 's' &&
15198 tolower(Constraint[2]) == 't' &&
15199 Constraint[3] == '(' &&
15200 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
15201 Constraint[5] == ')' &&
15202 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000015203
Chris Lattner56d77c72009-09-13 22:41:48 +000015204 Res.first = X86::ST0+Constraint[4]-'0';
15205 Res.second = X86::RFP80RegisterClass;
15206 return Res;
15207 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000015208
Chris Lattner56d77c72009-09-13 22:41:48 +000015209 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000015210 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000015211 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000015212 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000015213 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000015214 }
Chris Lattner56d77c72009-09-13 22:41:48 +000015215
15216 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000015217 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000015218 Res.first = X86::EFLAGS;
15219 Res.second = X86::CCRRegisterClass;
15220 return Res;
15221 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000015222
Dale Johannesen330169f2008-11-13 21:52:36 +000015223 // 'A' means EAX + EDX.
15224 if (Constraint == "A") {
15225 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000015226 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000015227 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000015228 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000015229 return Res;
15230 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015231
Chris Lattnerf76d1802006-07-31 23:26:50 +000015232 // Otherwise, check to see if this is a register class of the wrong value
15233 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
15234 // turn into {ax},{dx}.
15235 if (Res.second->hasType(VT))
15236 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015237
Chris Lattnerf76d1802006-07-31 23:26:50 +000015238 // All of the single-register GCC register classes map their values onto
15239 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
15240 // really want an 8-bit or 32-bit register, map to the appropriate register
15241 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000015242 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015243 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000015244 unsigned DestReg = 0;
15245 switch (Res.first) {
15246 default: break;
15247 case X86::AX: DestReg = X86::AL; break;
15248 case X86::DX: DestReg = X86::DL; break;
15249 case X86::CX: DestReg = X86::CL; break;
15250 case X86::BX: DestReg = X86::BL; break;
15251 }
15252 if (DestReg) {
15253 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000015254 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000015255 }
Owen Anderson825b72b2009-08-11 20:47:22 +000015256 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000015257 unsigned DestReg = 0;
15258 switch (Res.first) {
15259 default: break;
15260 case X86::AX: DestReg = X86::EAX; break;
15261 case X86::DX: DestReg = X86::EDX; break;
15262 case X86::CX: DestReg = X86::ECX; break;
15263 case X86::BX: DestReg = X86::EBX; break;
15264 case X86::SI: DestReg = X86::ESI; break;
15265 case X86::DI: DestReg = X86::EDI; break;
15266 case X86::BP: DestReg = X86::EBP; break;
15267 case X86::SP: DestReg = X86::ESP; break;
15268 }
15269 if (DestReg) {
15270 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000015271 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000015272 }
Owen Anderson825b72b2009-08-11 20:47:22 +000015273 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000015274 unsigned DestReg = 0;
15275 switch (Res.first) {
15276 default: break;
15277 case X86::AX: DestReg = X86::RAX; break;
15278 case X86::DX: DestReg = X86::RDX; break;
15279 case X86::CX: DestReg = X86::RCX; break;
15280 case X86::BX: DestReg = X86::RBX; break;
15281 case X86::SI: DestReg = X86::RSI; break;
15282 case X86::DI: DestReg = X86::RDI; break;
15283 case X86::BP: DestReg = X86::RBP; break;
15284 case X86::SP: DestReg = X86::RSP; break;
15285 }
15286 if (DestReg) {
15287 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000015288 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000015289 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000015290 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000015291 } else if (Res.second == X86::FR32RegisterClass ||
15292 Res.second == X86::FR64RegisterClass ||
15293 Res.second == X86::VR128RegisterClass) {
15294 // Handle references to XMM physical registers that got mapped into the
15295 // wrong class. This can happen with constraints like {xmm0} where the
15296 // target independent register mapper will just pick the first match it can
15297 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000015298 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000015299 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000015300 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000015301 Res.second = X86::FR64RegisterClass;
15302 else if (X86::VR128RegisterClass->hasType(VT))
15303 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000015304 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015305
Chris Lattnerf76d1802006-07-31 23:26:50 +000015306 return Res;
15307}