blob: 175d23750e2836e512f95703f1b9f539a3b2c647 [file] [log] [blame]
Arnold Schwaighofer92226dd2007-10-12 21:53:12 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
Evan Chengb1712452010-01-27 06:25:16 +000015#define DEBUG_TYPE "x86-isel"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000016#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000017#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000018#include "X86ISelLowering.h"
19#include "X86TargetMachine.h"
Chris Lattner8c6ed052009-09-16 01:46:41 +000020#include "X86TargetObjectFile.h"
David Greene583b68f2011-02-17 19:18:59 +000021#include "Utils/X86ShuffleDecode.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000022#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000023#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000024#include "llvm/DerivedTypes.h"
Chris Lattnerb903bed2009-06-26 21:20:29 +000025#include "llvm/GlobalAlias.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000026#include "llvm/GlobalVariable.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000027#include "llvm/Function.h"
Chris Lattnerb8105652009-07-20 17:51:36 +000028#include "llvm/Instructions.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000029#include "llvm/Intrinsics.h"
Owen Andersona90b3dc2009-07-15 21:51:10 +000030#include "llvm/LLVMContext.h"
Evan Cheng55d42002011-01-08 01:24:27 +000031#include "llvm/CodeGen/IntrinsicLowering.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000032#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000033#include "llvm/CodeGen/MachineFunction.h"
34#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner5e1df8d2010-01-25 23:38:14 +000035#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000036#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000037#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000038#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattner589c6f62010-01-26 06:28:43 +000039#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000040#include "llvm/MC/MCContext.h"
Daniel Dunbar4e815f82010-03-15 23:51:06 +000041#include "llvm/MC/MCExpr.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000042#include "llvm/MC/MCSymbol.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000043#include "llvm/ADT/BitVector.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000044#include "llvm/ADT/SmallSet.h"
Evan Chengb1712452010-01-27 06:25:16 +000045#include "llvm/ADT/Statistic.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000046#include "llvm/ADT/StringExtras.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000047#include "llvm/ADT/VectorExtras.h"
Evan Cheng485fafc2011-03-21 01:19:09 +000048#include "llvm/Support/CallSite.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000049#include "llvm/Support/Debug.h"
Bill Wendlingec041eb2010-03-12 19:20:40 +000050#include "llvm/Support/Dwarf.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000051#include "llvm/Support/ErrorHandling.h"
52#include "llvm/Support/MathExtras.h"
Torok Edwindac237e2009-07-08 20:53:28 +000053#include "llvm/Support/raw_ostream.h"
Rafael Espindola151ab3e2011-08-30 19:47:04 +000054#include "llvm/Target/TargetOptions.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000055using namespace llvm;
Bill Wendlingec041eb2010-03-12 19:20:40 +000056using namespace dwarf;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000057
Evan Chengb1712452010-01-27 06:25:16 +000058STATISTIC(NumTailCalls, "Number of tail calls");
59
Evan Cheng10e86422008-04-25 19:11:04 +000060// Forward declarations.
Owen Andersone50ed302009-08-10 22:56:29 +000061static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +000062 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000063
David Greenea5f26012011-02-07 19:36:54 +000064static SDValue Insert128BitVector(SDValue Result,
65 SDValue Vec,
66 SDValue Idx,
67 SelectionDAG &DAG,
68 DebugLoc dl);
David Greenef125a292011-02-08 19:04:41 +000069
David Greenea5f26012011-02-07 19:36:54 +000070static SDValue Extract128BitVector(SDValue Vec,
71 SDValue Idx,
72 SelectionDAG &DAG,
73 DebugLoc dl);
74
75/// Generate a DAG to grab 128-bits from a vector > 128 bits. This
76/// sets things up to match to an AVX VEXTRACTF128 instruction or a
David Greene74a579d2011-02-10 16:57:36 +000077/// simple subregister reference. Idx is an index in the 128 bits we
78/// want. It need not be aligned to a 128-bit bounday. That makes
79/// lowering EXTRACT_VECTOR_ELT operations easier.
David Greenea5f26012011-02-07 19:36:54 +000080static SDValue Extract128BitVector(SDValue Vec,
81 SDValue Idx,
82 SelectionDAG &DAG,
83 DebugLoc dl) {
84 EVT VT = Vec.getValueType();
85 assert(VT.getSizeInBits() == 256 && "Unexpected vector size!");
David Greenea5f26012011-02-07 19:36:54 +000086 EVT ElVT = VT.getVectorElementType();
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +000087 int Factor = VT.getSizeInBits()/128;
88 EVT ResultVT = EVT::getVectorVT(*DAG.getContext(), ElVT,
89 VT.getVectorNumElements()/Factor);
David Greenea5f26012011-02-07 19:36:54 +000090
91 // Extract from UNDEF is UNDEF.
92 if (Vec.getOpcode() == ISD::UNDEF)
93 return DAG.getNode(ISD::UNDEF, dl, ResultVT);
94
95 if (isa<ConstantSDNode>(Idx)) {
96 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
97
98 // Extract the relevant 128 bits. Generate an EXTRACT_SUBVECTOR
99 // we can match to VEXTRACTF128.
100 unsigned ElemsPerChunk = 128 / ElVT.getSizeInBits();
101
102 // This is the index of the first element of the 128-bit chunk
103 // we want.
104 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits()) / 128)
105 * ElemsPerChunk);
106
107 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
David Greenea5f26012011-02-07 19:36:54 +0000108 SDValue Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, ResultVT, Vec,
109 VecIdx);
110
111 return Result;
112 }
113
114 return SDValue();
115}
116
117/// Generate a DAG to put 128-bits into a vector > 128 bits. This
118/// sets things up to match to an AVX VINSERTF128 instruction or a
David Greene6b381262011-02-09 15:32:06 +0000119/// simple superregister reference. Idx is an index in the 128 bits
120/// we want. It need not be aligned to a 128-bit bounday. That makes
121/// lowering INSERT_VECTOR_ELT operations easier.
David Greenea5f26012011-02-07 19:36:54 +0000122static SDValue Insert128BitVector(SDValue Result,
123 SDValue Vec,
124 SDValue Idx,
125 SelectionDAG &DAG,
126 DebugLoc dl) {
127 if (isa<ConstantSDNode>(Idx)) {
128 EVT VT = Vec.getValueType();
129 assert(VT.getSizeInBits() == 128 && "Unexpected vector size!");
130
131 EVT ElVT = VT.getVectorElementType();
David Greenea5f26012011-02-07 19:36:54 +0000132 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
David Greenea5f26012011-02-07 19:36:54 +0000133 EVT ResultVT = Result.getValueType();
134
135 // Insert the relevant 128 bits.
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +0000136 unsigned ElemsPerChunk = 128/ElVT.getSizeInBits();
David Greenea5f26012011-02-07 19:36:54 +0000137
138 // This is the index of the first element of the 128-bit chunk
139 // we want.
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +0000140 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits())/128)
David Greenea5f26012011-02-07 19:36:54 +0000141 * ElemsPerChunk);
142
143 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
David Greenea5f26012011-02-07 19:36:54 +0000144 Result = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResultVT, Result, Vec,
145 VecIdx);
146 return Result;
147 }
148
149 return SDValue();
150}
151
Chris Lattnerf0144122009-07-28 03:13:23 +0000152static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
Evan Cheng2bffee22011-02-01 01:14:13 +0000153 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
154 bool is64Bit = Subtarget->is64Bit();
NAKAMURA Takumi27635382011-02-05 15:10:54 +0000155
Evan Cheng2bffee22011-02-01 01:14:13 +0000156 if (Subtarget->isTargetEnvMacho()) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000157 if (is64Bit)
158 return new X8664_MachoTargetObjectFile();
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000159 return new TargetLoweringObjectFileMachO();
Michael J. Spencerec38de22010-10-10 22:04:20 +0000160 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000161
Evan Cheng203576a2011-07-20 19:50:42 +0000162 if (Subtarget->isTargetELF())
163 return new TargetLoweringObjectFileELF();
Evan Cheng2bffee22011-02-01 01:14:13 +0000164 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
Chris Lattnere019ec12010-12-19 20:07:10 +0000165 return new TargetLoweringObjectFileCOFF();
Eric Christopher62f35a22010-07-05 19:26:33 +0000166 llvm_unreachable("unknown subtarget type");
Chris Lattnerf0144122009-07-28 03:13:23 +0000167}
168
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +0000169X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +0000170 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +0000171 Subtarget = &TM.getSubtarget<X86Subtarget>();
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +0000172 X86ScalarSSEf64 = Subtarget->hasXMMInt();
173 X86ScalarSSEf32 = Subtarget->hasXMM();
Evan Cheng25ab6902006-09-08 06:48:29 +0000174 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000175
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000176 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000177 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000178
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000179 // Set up the TargetLowering object.
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000180 static MVT IntVTs[] = { MVT::i8, MVT::i16, MVT::i32, MVT::i64 };
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000181
182 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Duncan Sands03228082008-11-23 15:47:28 +0000183 setBooleanContents(ZeroOrOneBooleanContent);
Duncan Sands28b77e92011-09-06 19:07:46 +0000184 // X86-SSE is even stranger. It uses -1 or 0 for vector masks.
185 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Eric Christopher471e4222011-06-08 23:55:35 +0000186
Eric Christopherde5e1012011-03-11 01:05:58 +0000187 // For 64-bit since we have so many registers use the ILP scheduler, for
188 // 32-bit code use the register pressure specific scheduling.
189 if (Subtarget->is64Bit())
190 setSchedulingPreference(Sched::ILP);
191 else
192 setSchedulingPreference(Sched::RegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +0000193 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +0000194
Michael J. Spencer92bf38c2010-10-10 23:11:06 +0000195 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing()) {
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000196 // Setup Windows compiler runtime calls.
197 setLibcallName(RTLIB::SDIV_I64, "_alldiv");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000198 setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
Julien Lerougef2960822011-07-08 21:40:25 +0000199 setLibcallName(RTLIB::SREM_I64, "_allrem");
200 setLibcallName(RTLIB::UREM_I64, "_aullrem");
201 setLibcallName(RTLIB::MUL_I64, "_allmul");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000202 setLibcallName(RTLIB::FPTOUINT_F64_I64, "_ftol2");
Michael J. Spencer94f7eeb2010-10-19 07:32:52 +0000203 setLibcallName(RTLIB::FPTOUINT_F32_I64, "_ftol2");
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000204 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer335b8062010-10-11 05:29:15 +0000205 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
Julien Lerougef2960822011-07-08 21:40:25 +0000206 setLibcallCallingConv(RTLIB::SREM_I64, CallingConv::X86_StdCall);
207 setLibcallCallingConv(RTLIB::UREM_I64, CallingConv::X86_StdCall);
208 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::X86_StdCall);
Michael J. Spencer6dad10e2010-10-27 18:52:38 +0000209 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::C);
210 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::C);
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000211 }
212
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000213 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000214 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000215 setUseUnderscoreSetJmp(false);
216 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000217 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000218 // MS runtime is weird: it exports _setjmp, but longjmp!
219 setUseUnderscoreSetJmp(true);
220 setUseUnderscoreLongJmp(false);
221 } else {
222 setUseUnderscoreSetJmp(true);
223 setUseUnderscoreLongJmp(true);
224 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000225
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000226 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000227 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
Dan Gohman71edb242010-04-30 18:30:26 +0000228 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000229 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000230 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000231 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000232
Owen Anderson825b72b2009-08-11 20:47:22 +0000233 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000234
Scott Michelfdc40a02009-02-17 22:15:04 +0000235 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000236 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000237 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000238 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000239 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000240 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
241 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000242
243 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000244 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
245 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
246 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
247 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
248 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
249 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000250
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000251 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
252 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000253 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
254 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
255 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000256
Evan Cheng25ab6902006-09-08 06:48:29 +0000257 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000258 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
259 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Eli Friedman948e95a2009-05-23 09:59:16 +0000260 } else if (!UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000261 // We have an algorithm for SSE2->double, and we turn this into a
262 // 64-bit FILD followed by conditional FADD for other targets.
263 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000264 // We have an algorithm for SSE2, and we turn this into a 64-bit
265 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000266 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000267 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000268
269 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
270 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000271 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
272 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000273
Devang Patel6a784892009-06-05 18:48:29 +0000274 if (!UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000275 // SSE has no i16 to fp conversion, only i32
276 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000277 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000278 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000279 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000280 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000281 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
282 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000283 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000284 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000285 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
286 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000287 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000288
Dale Johannesen73328d12007-09-19 23:55:34 +0000289 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
290 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000291 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
292 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000293
Evan Cheng02568ff2006-01-30 22:13:22 +0000294 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
295 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000296 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
297 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000298
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000299 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000300 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000301 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000302 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000303 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000304 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
305 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000306 }
307
308 // Handle FP_TO_UINT by promoting the destination to a larger signed
309 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000310 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
311 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
312 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000313
Evan Cheng25ab6902006-09-08 06:48:29 +0000314 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000315 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
316 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman948e95a2009-05-23 09:59:16 +0000317 } else if (!UseSoftFloat) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +0000318 // Since AVX is a superset of SSE3, only check for SSE here.
319 if (Subtarget->hasSSE1() && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000320 // Expand FP_TO_UINT into a select.
321 // FIXME: We would like to use a Custom expander here eventually to do
322 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000323 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000324 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000325 // With SSE3 we can use fisttpll to convert to a signed i64; without
326 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000327 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000328 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000329
Chris Lattner399610a2006-12-05 18:22:22 +0000330 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000331 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000332 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
333 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000334 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000335 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000336 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000337 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000338 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000339 }
Chris Lattner21f66852005-12-23 05:15:23 +0000340
Dan Gohmanb00ee212008-02-18 19:34:53 +0000341 // Scalar integer divide and remainder are lowered to use operations that
342 // produce two results, to match the available instructions. This exposes
343 // the two-result form to trivial CSE, which is able to combine x/y and x%y
344 // into a single instruction.
345 //
346 // Scalar integer multiply-high is also lowered to use two-result
347 // operations, to match the available instructions. However, plain multiply
348 // (low) operations are left as Legal, as there are single-result
349 // instructions for this in x86. Using the two-result multiply instructions
350 // when both high and low results are needed must be arranged by dagcombine.
Chris Lattnere019ec12010-12-19 20:07:10 +0000351 for (unsigned i = 0, e = 4; i != e; ++i) {
352 MVT VT = IntVTs[i];
353 setOperationAction(ISD::MULHS, VT, Expand);
354 setOperationAction(ISD::MULHU, VT, Expand);
355 setOperationAction(ISD::SDIV, VT, Expand);
356 setOperationAction(ISD::UDIV, VT, Expand);
357 setOperationAction(ISD::SREM, VT, Expand);
358 setOperationAction(ISD::UREM, VT, Expand);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000359
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000360 // Add/Sub overflow ops with MVT::Glues are lowered to EFLAGS dependences.
Chris Lattnerd8ff7ec2010-12-20 01:03:27 +0000361 setOperationAction(ISD::ADDC, VT, Custom);
362 setOperationAction(ISD::ADDE, VT, Custom);
363 setOperationAction(ISD::SUBC, VT, Custom);
364 setOperationAction(ISD::SUBE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000365 }
Dan Gohmana37c9f72007-09-25 18:23:27 +0000366
Owen Anderson825b72b2009-08-11 20:47:22 +0000367 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
368 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
369 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
370 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000371 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000372 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
373 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
374 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
375 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
376 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
377 setOperationAction(ISD::FREM , MVT::f32 , Expand);
378 setOperationAction(ISD::FREM , MVT::f64 , Expand);
379 setOperationAction(ISD::FREM , MVT::f80 , Expand);
380 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000381
Owen Anderson825b72b2009-08-11 20:47:22 +0000382 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
383 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000384 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
385 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000386 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
387 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000388 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000389 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
390 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000391 }
392
Benjamin Kramer1292c222010-12-04 20:32:23 +0000393 if (Subtarget->hasPOPCNT()) {
394 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
395 } else {
396 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
397 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
398 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
399 if (Subtarget->is64Bit())
400 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
401 }
402
Owen Anderson825b72b2009-08-11 20:47:22 +0000403 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
404 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000405
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000406 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000407 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000408 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000409 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000410 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000411 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
412 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
413 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
414 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
415 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000416 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000417 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
418 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
419 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
420 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000421 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000422 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
Andrew Trickf6c39412011-03-23 23:11:02 +0000423 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000424 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000425 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000426
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000427 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000428 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
429 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
430 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
431 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000432 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000433 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
434 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000435 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000436 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000437 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
438 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
439 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
440 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000441 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000442 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000443 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000444 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
445 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
446 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000447 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000448 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
449 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
450 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000451 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000452
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000453 if (Subtarget->hasXMM())
Owen Anderson825b72b2009-08-11 20:47:22 +0000454 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000455
Eric Christopher9a9d2752010-07-22 02:48:34 +0000456 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000457 setOperationAction(ISD::ATOMIC_FENCE , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000458
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000459 // On X86 and X86-64, atomic operations are lowered to locked instructions.
460 // Locked instructions, in turn, have implicit fence semantics (all memory
461 // operations are flushed before issuing the locked instruction, and they
462 // are not buffered), so we can fold away the common pattern of
463 // fence-atomic-fence.
464 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000465
Mon P Wang63307c32008-05-05 19:05:59 +0000466 // Expand certain atomics
Chris Lattnere019ec12010-12-19 20:07:10 +0000467 for (unsigned i = 0, e = 4; i != e; ++i) {
468 MVT VT = IntVTs[i];
469 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
470 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
Eli Friedman327236c2011-08-24 20:50:09 +0000471 setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000472 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000473
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000474 if (!Subtarget->is64Bit()) {
Eli Friedmanf8f90f02011-08-24 22:33:28 +0000475 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000476 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
477 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
478 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
479 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
480 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
481 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
482 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000483 }
484
Eli Friedman43f51ae2011-08-26 21:21:21 +0000485 if (Subtarget->hasCmpxchg16b()) {
486 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i128, Custom);
487 }
488
Evan Cheng3c992d22006-03-07 02:02:57 +0000489 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000490 if (!Subtarget->isTargetDarwin() &&
491 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000492 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000493 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000494 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000495
Owen Anderson825b72b2009-08-11 20:47:22 +0000496 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
497 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
498 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
499 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000500 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000501 setExceptionPointerRegister(X86::RAX);
502 setExceptionSelectorRegister(X86::RDX);
503 } else {
504 setExceptionPointerRegister(X86::EAX);
505 setExceptionSelectorRegister(X86::EDX);
506 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000507 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
508 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000509
Duncan Sands4a544a72011-09-06 13:37:06 +0000510 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
511 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000512
Owen Anderson825b72b2009-08-11 20:47:22 +0000513 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000514
Nate Begemanacc398c2006-01-25 18:21:52 +0000515 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000516 setOperationAction(ISD::VASTART , MVT::Other, Custom);
517 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000518 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000519 setOperationAction(ISD::VAARG , MVT::Other, Custom);
520 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000521 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000522 setOperationAction(ISD::VAARG , MVT::Other, Expand);
523 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000524 }
Evan Chengae642192007-03-02 23:16:35 +0000525
Owen Anderson825b72b2009-08-11 20:47:22 +0000526 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
527 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eric Christopherc967ad82011-08-31 04:17:21 +0000528
529 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
530 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
531 MVT::i64 : MVT::i32, Custom);
532 else if (EnableSegmentedStacks)
533 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
534 MVT::i64 : MVT::i32, Custom);
535 else
536 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
537 MVT::i64 : MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000538
Evan Chengc7ce29b2009-02-13 22:36:38 +0000539 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000540 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000541 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000542 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
543 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000544
Evan Cheng223547a2006-01-31 22:28:30 +0000545 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000546 setOperationAction(ISD::FABS , MVT::f64, Custom);
547 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000548
549 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000550 setOperationAction(ISD::FNEG , MVT::f64, Custom);
551 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000552
Evan Cheng68c47cb2007-01-05 07:55:56 +0000553 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000554 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
555 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000556
Stuart Hastings4fd0dee2011-06-01 04:39:42 +0000557 // Lower this to FGETSIGNx86 plus an AND.
558 setOperationAction(ISD::FGETSIGN, MVT::i64, Custom);
559 setOperationAction(ISD::FGETSIGN, MVT::i32, Custom);
560
Evan Chengd25e9e82006-02-02 00:28:23 +0000561 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000562 setOperationAction(ISD::FSIN , MVT::f64, Expand);
563 setOperationAction(ISD::FCOS , MVT::f64, Expand);
564 setOperationAction(ISD::FSIN , MVT::f32, Expand);
565 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000566
Chris Lattnera54aa942006-01-29 06:26:08 +0000567 // Expand FP immediates into loads from the stack, except for the special
568 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000569 addLegalFPImmediate(APFloat(+0.0)); // xorpd
570 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000571 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000572 // Use SSE for f32, x87 for f64.
573 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000574 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
575 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000576
577 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000578 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000579
580 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000581 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000582
Owen Anderson825b72b2009-08-11 20:47:22 +0000583 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000584
585 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000586 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
587 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000588
589 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000590 setOperationAction(ISD::FSIN , MVT::f32, Expand);
591 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000592
Nate Begemane1795842008-02-14 08:57:00 +0000593 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000594 addLegalFPImmediate(APFloat(+0.0f)); // xorps
595 addLegalFPImmediate(APFloat(+0.0)); // FLD0
596 addLegalFPImmediate(APFloat(+1.0)); // FLD1
597 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
598 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
599
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000600 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000601 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
602 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000603 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000604 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000605 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000606 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000607 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
608 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000609
Owen Anderson825b72b2009-08-11 20:47:22 +0000610 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
611 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
612 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
613 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000614
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000615 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000616 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
617 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000618 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000619 addLegalFPImmediate(APFloat(+0.0)); // FLD0
620 addLegalFPImmediate(APFloat(+1.0)); // FLD1
621 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
622 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000623 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
624 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
625 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
626 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000627 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000628
Cameron Zwarich33390842011-07-08 21:39:21 +0000629 // We don't support FMA.
630 setOperationAction(ISD::FMA, MVT::f64, Expand);
631 setOperationAction(ISD::FMA, MVT::f32, Expand);
632
Dale Johannesen59a58732007-08-05 18:49:15 +0000633 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000634 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000635 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
636 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
637 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000638 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000639 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000640 addLegalFPImmediate(TmpFlt); // FLD0
641 TmpFlt.changeSign();
642 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000643
644 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000645 APFloat TmpFlt2(+1.0);
646 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
647 &ignored);
648 addLegalFPImmediate(TmpFlt2); // FLD1
649 TmpFlt2.changeSign();
650 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
651 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000652
Evan Chengc7ce29b2009-02-13 22:36:38 +0000653 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000654 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
655 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000656 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000657
658 setOperationAction(ISD::FMA, MVT::f80, Expand);
Dale Johannesen2f429012007-09-26 21:10:55 +0000659 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000660
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000661 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000662 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
663 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
664 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000665
Owen Anderson825b72b2009-08-11 20:47:22 +0000666 setOperationAction(ISD::FLOG, MVT::f80, Expand);
667 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
668 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
669 setOperationAction(ISD::FEXP, MVT::f80, Expand);
670 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000671
Mon P Wangf007a8b2008-11-06 05:31:54 +0000672 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000673 // (for widening) or expand (for scalarization). Then we will selectively
674 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000675 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
676 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
677 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
678 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
679 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
680 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
681 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
682 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
683 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
684 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
685 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
686 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
687 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
688 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
689 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
690 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
691 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000692 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
David Greenecfe33c42011-01-26 19:13:22 +0000693 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
694 setOperationAction(ISD::INSERT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000695 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
696 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
697 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
698 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
699 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
700 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
701 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
702 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
703 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
704 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
705 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
706 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
707 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
708 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
709 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
710 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
711 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
712 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
713 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
714 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
715 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
Duncan Sands28b77e92011-09-06 19:07:46 +0000716 setOperationAction(ISD::SETCC, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000717 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
718 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
719 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
720 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
721 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
722 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
723 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
724 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
725 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000726 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000727 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
728 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
729 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
730 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
Nadav Rotemaec58612011-09-13 19:17:42 +0000731 setOperationAction(ISD::VSELECT, (MVT::SimpleValueType)VT, Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000732 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
733 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
734 setTruncStoreAction((MVT::SimpleValueType)VT,
735 (MVT::SimpleValueType)InnerVT, Expand);
736 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
737 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
738 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000739 }
740
Evan Chengc7ce29b2009-02-13 22:36:38 +0000741 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
742 // with -msoft-float, disable use of MMX as well.
Chris Lattner2a786eb2010-12-19 20:19:20 +0000743 if (!UseSoftFloat && Subtarget->hasMMX()) {
Dale Johannesene93d99c2010-10-20 21:32:10 +0000744 addRegisterClass(MVT::x86mmx, X86::VR64RegisterClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000745 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000746 }
747
Dale Johannesen0488fb62010-09-30 23:57:10 +0000748 // MMX-sized vectors (other than x86mmx) are expected to be expanded
749 // into smaller operations.
750 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
751 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
752 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
753 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
754 setOperationAction(ISD::AND, MVT::v8i8, Expand);
755 setOperationAction(ISD::AND, MVT::v4i16, Expand);
756 setOperationAction(ISD::AND, MVT::v2i32, Expand);
757 setOperationAction(ISD::AND, MVT::v1i64, Expand);
758 setOperationAction(ISD::OR, MVT::v8i8, Expand);
759 setOperationAction(ISD::OR, MVT::v4i16, Expand);
760 setOperationAction(ISD::OR, MVT::v2i32, Expand);
761 setOperationAction(ISD::OR, MVT::v1i64, Expand);
762 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
763 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
764 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
765 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
766 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
767 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
768 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
769 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
770 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
771 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
772 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
773 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
774 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000775 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
776 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
777 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
778 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000779
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000780 if (!UseSoftFloat && Subtarget->hasXMM()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000781 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000782
Owen Anderson825b72b2009-08-11 20:47:22 +0000783 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
784 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
785 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
786 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
787 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
788 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
789 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
790 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
791 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
792 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
793 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000794 setOperationAction(ISD::SETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000795 }
796
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000797 if (!UseSoftFloat && Subtarget->hasXMMInt()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000798 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000799
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000800 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
801 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000802 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
803 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
804 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
805 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000806
Owen Anderson825b72b2009-08-11 20:47:22 +0000807 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
808 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
809 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
810 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
811 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
812 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
813 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
814 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
815 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
816 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
817 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
818 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
819 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
820 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
821 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
822 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000823
Nadav Rotem354efd82011-09-18 14:57:03 +0000824 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000825 setOperationAction(ISD::SETCC, MVT::v16i8, Custom);
826 setOperationAction(ISD::SETCC, MVT::v8i16, Custom);
827 setOperationAction(ISD::SETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000828
Owen Anderson825b72b2009-08-11 20:47:22 +0000829 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
830 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
831 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
832 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
833 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000834
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000835 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
836 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
837 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
838 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
839 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
840
Evan Cheng2c3ae372006-04-12 21:21:57 +0000841 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000842 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
843 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000844 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000845 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000846 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000847 // Do not attempt to custom lower non-128-bit vectors
848 if (!VT.is128BitVector())
849 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000850 setOperationAction(ISD::BUILD_VECTOR,
851 VT.getSimpleVT().SimpleTy, Custom);
852 setOperationAction(ISD::VECTOR_SHUFFLE,
853 VT.getSimpleVT().SimpleTy, Custom);
854 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
855 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000856 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000857
Owen Anderson825b72b2009-08-11 20:47:22 +0000858 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
859 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
860 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
861 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
862 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
863 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000864
Nate Begemancdd1eec2008-02-12 22:51:28 +0000865 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000866 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
867 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000868 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000869
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000870 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000871 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
872 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000873 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000874
875 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000876 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000877 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000878
Owen Andersond6662ad2009-08-10 20:46:15 +0000879 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000880 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000881 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000882 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000883 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000884 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000885 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000886 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000887 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000888 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000889 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000890
Owen Anderson825b72b2009-08-11 20:47:22 +0000891 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000892
Evan Cheng2c3ae372006-04-12 21:21:57 +0000893 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000894 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
895 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
896 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
897 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000898
Owen Anderson825b72b2009-08-11 20:47:22 +0000899 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
900 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000901 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000902
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000903 if (Subtarget->hasSSE41() || Subtarget->hasAVX()) {
Dale Johannesen54feef22010-05-27 20:12:41 +0000904 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
905 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
906 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
907 setOperationAction(ISD::FRINT, MVT::f32, Legal);
908 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
909 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
910 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
911 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
912 setOperationAction(ISD::FRINT, MVT::f64, Legal);
913 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
914
Nate Begeman14d12ca2008-02-11 04:19:36 +0000915 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000916 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000917
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000918 // Can turn SHL into an integer multiply.
919 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
Nate Begeman51409212010-07-28 00:21:48 +0000920 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000921
Nadav Rotemfbad25e2011-09-11 15:02:23 +0000922 setOperationAction(ISD::VSELECT, MVT::v2f64, Legal);
923 setOperationAction(ISD::VSELECT, MVT::v2i64, Legal);
924 setOperationAction(ISD::VSELECT, MVT::v16i8, Legal);
925 setOperationAction(ISD::VSELECT, MVT::v4i32, Legal);
926 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
Nadav Rotemffe3e7d2011-09-08 08:11:19 +0000927
Nate Begeman14d12ca2008-02-11 04:19:36 +0000928 // i8 and i16 vectors are custom , because the source register and source
929 // source memory operand types are not the same width. f32 vectors are
930 // custom since the immediate controlling the insert encodes additional
931 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000932 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
933 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
934 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
935 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000936
Owen Anderson825b72b2009-08-11 20:47:22 +0000937 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
938 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
939 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
940 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000941
942 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000943 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
944 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000945 }
946 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000947
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +0000948 if (Subtarget->hasXMMInt()) {
Nadav Rotem43012222011-05-11 08:12:09 +0000949 setOperationAction(ISD::SRL, MVT::v2i64, Custom);
950 setOperationAction(ISD::SRL, MVT::v4i32, Custom);
951 setOperationAction(ISD::SRL, MVT::v16i8, Custom);
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000952 setOperationAction(ISD::SRL, MVT::v8i16, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +0000953
954 setOperationAction(ISD::SHL, MVT::v2i64, Custom);
955 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
956 setOperationAction(ISD::SHL, MVT::v8i16, Custom);
957
958 setOperationAction(ISD::SRA, MVT::v4i32, Custom);
959 setOperationAction(ISD::SRA, MVT::v8i16, Custom);
960 }
961
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000962 if (Subtarget->hasSSE42() || Subtarget->hasAVX())
Duncan Sands28b77e92011-09-06 19:07:46 +0000963 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000964
David Greene9b9838d2009-06-29 16:47:10 +0000965 if (!UseSoftFloat && Subtarget->hasAVX()) {
Bruno Cardoso Lopesdbd4fe22011-07-21 02:24:08 +0000966 addRegisterClass(MVT::v32i8, X86::VR256RegisterClass);
967 addRegisterClass(MVT::v16i16, X86::VR256RegisterClass);
968 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
969 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
970 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
971 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000972
Owen Anderson825b72b2009-08-11 20:47:22 +0000973 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +0000974 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
975 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +0000976
Owen Anderson825b72b2009-08-11 20:47:22 +0000977 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
978 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
979 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
980 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
981 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
982 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000983
Owen Anderson825b72b2009-08-11 20:47:22 +0000984 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
985 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
986 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
987 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
988 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
989 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000990
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +0000991 setOperationAction(ISD::FP_TO_SINT, MVT::v8i32, Legal);
992 setOperationAction(ISD::SINT_TO_FP, MVT::v8i32, Legal);
Bruno Cardoso Lopes55244ce2011-08-01 21:54:09 +0000993 setOperationAction(ISD::FP_ROUND, MVT::v4f32, Legal);
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +0000994
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +0000995 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4f64, Custom);
996 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i64, Custom);
997 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8f32, Custom);
998 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i32, Custom);
999 setOperationAction(ISD::CONCAT_VECTORS, MVT::v32i8, Custom);
1000 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i16, Custom);
1001
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001002 setOperationAction(ISD::SRL, MVT::v4i64, Custom);
1003 setOperationAction(ISD::SRL, MVT::v8i32, Custom);
1004 setOperationAction(ISD::SRL, MVT::v16i16, Custom);
1005 setOperationAction(ISD::SRL, MVT::v32i8, Custom);
1006
1007 setOperationAction(ISD::SHL, MVT::v4i64, Custom);
1008 setOperationAction(ISD::SHL, MVT::v8i32, Custom);
1009 setOperationAction(ISD::SHL, MVT::v16i16, Custom);
1010 setOperationAction(ISD::SHL, MVT::v32i8, Custom);
1011
1012 setOperationAction(ISD::SRA, MVT::v8i32, Custom);
1013 setOperationAction(ISD::SRA, MVT::v16i16, Custom);
1014
Duncan Sands28b77e92011-09-06 19:07:46 +00001015 setOperationAction(ISD::SETCC, MVT::v32i8, Custom);
1016 setOperationAction(ISD::SETCC, MVT::v16i16, Custom);
1017 setOperationAction(ISD::SETCC, MVT::v8i32, Custom);
1018 setOperationAction(ISD::SETCC, MVT::v4i64, Custom);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00001019
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +00001020 setOperationAction(ISD::SELECT, MVT::v4f64, Custom);
1021 setOperationAction(ISD::SELECT, MVT::v4i64, Custom);
1022 setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
1023
Nadav Rotemfbad25e2011-09-11 15:02:23 +00001024 setOperationAction(ISD::VSELECT, MVT::v4f64, Legal);
1025 setOperationAction(ISD::VSELECT, MVT::v4i64, Legal);
1026 setOperationAction(ISD::VSELECT, MVT::v8i32, Legal);
1027 setOperationAction(ISD::VSELECT, MVT::v8f32, Legal);
Nadav Rotem8ffad562011-09-09 20:29:17 +00001028
Craig Topper13894fa2011-08-24 06:14:18 +00001029 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
1030 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
1031 setOperationAction(ISD::ADD, MVT::v16i16, Custom);
1032 setOperationAction(ISD::ADD, MVT::v32i8, Custom);
1033
1034 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
1035 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
1036 setOperationAction(ISD::SUB, MVT::v16i16, Custom);
1037 setOperationAction(ISD::SUB, MVT::v32i8, Custom);
1038
1039 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1040 setOperationAction(ISD::MUL, MVT::v8i32, Custom);
1041 setOperationAction(ISD::MUL, MVT::v16i16, Custom);
1042 // Don't lower v32i8 because there is no 128-bit byte mul
1043
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001044 // Custom lower several nodes for 256-bit types.
David Greene54d8eba2011-01-27 22:38:56 +00001045 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001046 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
1047 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
1048 EVT VT = SVT;
1049
1050 // Extract subvector is special because the value type
1051 // (result) is 128-bit but the source is 256-bit wide.
1052 if (VT.is128BitVector())
1053 setOperationAction(ISD::EXTRACT_SUBVECTOR, SVT, Custom);
1054
1055 // Do not attempt to custom lower other non-256-bit vectors
1056 if (!VT.is256BitVector())
David Greene9b9838d2009-06-29 16:47:10 +00001057 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001058
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001059 setOperationAction(ISD::BUILD_VECTOR, SVT, Custom);
1060 setOperationAction(ISD::VECTOR_SHUFFLE, SVT, Custom);
1061 setOperationAction(ISD::INSERT_VECTOR_ELT, SVT, Custom);
1062 setOperationAction(ISD::EXTRACT_VECTOR_ELT, SVT, Custom);
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00001063 setOperationAction(ISD::SCALAR_TO_VECTOR, SVT, Custom);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001064 setOperationAction(ISD::INSERT_SUBVECTOR, SVT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001065 }
1066
David Greene54d8eba2011-01-27 22:38:56 +00001067 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001068 for (unsigned i = (unsigned)MVT::v32i8; i != (unsigned)MVT::v4i64; ++i) {
1069 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
1070 EVT VT = SVT;
David Greene54d8eba2011-01-27 22:38:56 +00001071
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001072 // Do not attempt to promote non-256-bit vectors
1073 if (!VT.is256BitVector())
David Greene54d8eba2011-01-27 22:38:56 +00001074 continue;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001075
1076 setOperationAction(ISD::AND, SVT, Promote);
1077 AddPromotedToType (ISD::AND, SVT, MVT::v4i64);
1078 setOperationAction(ISD::OR, SVT, Promote);
1079 AddPromotedToType (ISD::OR, SVT, MVT::v4i64);
1080 setOperationAction(ISD::XOR, SVT, Promote);
1081 AddPromotedToType (ISD::XOR, SVT, MVT::v4i64);
1082 setOperationAction(ISD::LOAD, SVT, Promote);
1083 AddPromotedToType (ISD::LOAD, SVT, MVT::v4i64);
1084 setOperationAction(ISD::SELECT, SVT, Promote);
1085 AddPromotedToType (ISD::SELECT, SVT, MVT::v4i64);
David Greene54d8eba2011-01-27 22:38:56 +00001086 }
David Greene9b9838d2009-06-29 16:47:10 +00001087 }
1088
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001089 // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
1090 // of this type with custom code.
1091 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1092 VT != (unsigned)MVT::LAST_VECTOR_VALUETYPE; VT++) {
1093 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT, Custom);
1094 }
1095
Evan Cheng6be2c582006-04-05 23:38:46 +00001096 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001097 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001098
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001099
Eli Friedman962f5492010-06-02 19:35:46 +00001100 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1101 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001102 //
Eli Friedman962f5492010-06-02 19:35:46 +00001103 // FIXME: We really should do custom legalization for addition and
1104 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1105 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001106 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1107 // Add/Sub/Mul with overflow operations are custom lowered.
1108 MVT VT = IntVTs[i];
1109 setOperationAction(ISD::SADDO, VT, Custom);
1110 setOperationAction(ISD::UADDO, VT, Custom);
1111 setOperationAction(ISD::SSUBO, VT, Custom);
1112 setOperationAction(ISD::USUBO, VT, Custom);
1113 setOperationAction(ISD::SMULO, VT, Custom);
1114 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001115 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001116
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001117 // There are no 8-bit 3-address imul/mul instructions
1118 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1119 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001120
Evan Chengd54f2d52009-03-31 19:38:51 +00001121 if (!Subtarget->is64Bit()) {
1122 // These libcalls are not available in 32-bit.
1123 setLibcallName(RTLIB::SHL_I128, 0);
1124 setLibcallName(RTLIB::SRL_I128, 0);
1125 setLibcallName(RTLIB::SRA_I128, 0);
1126 }
1127
Evan Cheng206ee9d2006-07-07 08:33:52 +00001128 // We have target-specific dag combine patterns for the following nodes:
1129 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001130 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +00001131 setTargetDAGCombine(ISD::BUILD_VECTOR);
Duncan Sands6bcd2192011-09-17 16:49:39 +00001132 setTargetDAGCombine(ISD::VSELECT);
Chris Lattner83e6c992006-10-04 06:57:07 +00001133 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001134 setTargetDAGCombine(ISD::SHL);
1135 setTargetDAGCombine(ISD::SRA);
1136 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001137 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001138 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001139 setTargetDAGCombine(ISD::ADD);
1140 setTargetDAGCombine(ISD::SUB);
Nadav Rotem91e43fd2011-09-18 10:39:32 +00001141 setTargetDAGCombine(ISD::LOAD);
Chris Lattner149a4e52008-02-22 02:09:43 +00001142 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001143 setTargetDAGCombine(ISD::ZERO_EXTEND);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001144 setTargetDAGCombine(ISD::SINT_TO_FP);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001145 if (Subtarget->is64Bit())
1146 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001147
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001148 computeRegisterProperties();
1149
Evan Cheng05219282011-01-06 06:52:41 +00001150 // On Darwin, -Os means optimize for size without hurting performance,
1151 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001152 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001153 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001154 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001155 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1156 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1157 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengfb8075d2008-02-28 00:43:03 +00001158 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001159 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001160
1161 setPrefFunctionAlignment(4);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001162}
1163
Scott Michel5b8f82e2008-03-10 15:42:14 +00001164
Duncan Sands28b77e92011-09-06 19:07:46 +00001165EVT X86TargetLowering::getSetCCResultType(EVT VT) const {
1166 if (!VT.isVector()) return MVT::i8;
1167 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +00001168}
1169
1170
Evan Cheng29286502008-01-23 23:17:41 +00001171/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1172/// the desired ByVal argument alignment.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001173static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
Evan Cheng29286502008-01-23 23:17:41 +00001174 if (MaxAlign == 16)
1175 return;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001176 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001177 if (VTy->getBitWidth() == 128)
1178 MaxAlign = 16;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001179 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001180 unsigned EltAlign = 0;
1181 getMaxByValAlign(ATy->getElementType(), EltAlign);
1182 if (EltAlign > MaxAlign)
1183 MaxAlign = EltAlign;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001184 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001185 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1186 unsigned EltAlign = 0;
1187 getMaxByValAlign(STy->getElementType(i), EltAlign);
1188 if (EltAlign > MaxAlign)
1189 MaxAlign = EltAlign;
1190 if (MaxAlign == 16)
1191 break;
1192 }
1193 }
1194 return;
1195}
1196
1197/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1198/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001199/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1200/// are at 4-byte boundaries.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001201unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001202 if (Subtarget->is64Bit()) {
1203 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001204 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001205 if (TyAlign > 8)
1206 return TyAlign;
1207 return 8;
1208 }
1209
Evan Cheng29286502008-01-23 23:17:41 +00001210 unsigned Align = 4;
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001211 if (Subtarget->hasXMM())
Dale Johannesen0c191872008-02-08 19:48:20 +00001212 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001213 return Align;
1214}
Chris Lattner2b02a442007-02-25 08:29:00 +00001215
Evan Chengf0df0312008-05-15 08:39:06 +00001216/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001217/// and store operations as a result of memset, memcpy, and memmove
1218/// lowering. If DstAlign is zero that means it's safe to destination
1219/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1220/// means there isn't a need to check it against alignment requirement,
1221/// probably because the source does not need to be loaded. If
1222/// 'NonScalarIntSafe' is true, that means it's safe to return a
1223/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1224/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1225/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001226/// It returns EVT::Other if the type should be determined using generic
1227/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001228EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001229X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1230 unsigned DstAlign, unsigned SrcAlign,
Evan Chengf28f8bc2010-04-02 19:36:14 +00001231 bool NonScalarIntSafe,
Evan Chengc3b0c342010-04-08 07:37:57 +00001232 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001233 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001234 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1235 // linux. This is because the stack realignment code can't handle certain
1236 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001237 const Function *F = MF.getFunction();
Evan Chenga5e13622011-01-07 19:35:30 +00001238 if (NonScalarIntSafe &&
1239 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001240 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001241 (Subtarget->isUnalignedMemAccessFast() ||
1242 ((DstAlign == 0 || DstAlign >= 16) &&
1243 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001244 Subtarget->getStackAlignment() >= 16) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00001245 if (Subtarget->hasAVX() &&
1246 Subtarget->getStackAlignment() >= 32)
1247 return MVT::v8f32;
1248 if (Subtarget->hasXMMInt())
Evan Cheng255f20f2010-04-01 06:04:33 +00001249 return MVT::v4i32;
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00001250 if (Subtarget->hasXMM())
Evan Cheng255f20f2010-04-01 06:04:33 +00001251 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001252 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001253 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001254 Subtarget->getStackAlignment() >= 8 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001255 Subtarget->hasXMMInt()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001256 // Do not use f64 to lower memcpy if source is string constant. It's
1257 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001258 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001259 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001260 }
Evan Chengf0df0312008-05-15 08:39:06 +00001261 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001262 return MVT::i64;
1263 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001264}
1265
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001266/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1267/// current function. The returned value is a member of the
1268/// MachineJumpTableInfo::JTEntryKind enum.
1269unsigned X86TargetLowering::getJumpTableEncoding() const {
1270 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1271 // symbol.
1272 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1273 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001274 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001275
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001276 // Otherwise, use the normal jump table encoding heuristics.
1277 return TargetLowering::getJumpTableEncoding();
1278}
1279
Chris Lattnerc64daab2010-01-26 05:02:42 +00001280const MCExpr *
1281X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1282 const MachineBasicBlock *MBB,
1283 unsigned uid,MCContext &Ctx) const{
1284 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1285 Subtarget->isPICStyleGOT());
1286 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1287 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001288 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1289 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001290}
1291
Evan Chengcc415862007-11-09 01:32:10 +00001292/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1293/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001294SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001295 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001296 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001297 // This doesn't have DebugLoc associated with it, but is not really the
1298 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001299 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001300 return Table;
1301}
1302
Chris Lattner589c6f62010-01-26 06:28:43 +00001303/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1304/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1305/// MCExpr.
1306const MCExpr *X86TargetLowering::
1307getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1308 MCContext &Ctx) const {
1309 // X86-64 uses RIP relative addressing based on the jump table label.
1310 if (Subtarget->isPICStyleRIPRel())
1311 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1312
1313 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001314 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001315}
1316
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001317// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001318std::pair<const TargetRegisterClass*, uint8_t>
1319X86TargetLowering::findRepresentativeClass(EVT VT) const{
1320 const TargetRegisterClass *RRC = 0;
1321 uint8_t Cost = 1;
1322 switch (VT.getSimpleVT().SimpleTy) {
1323 default:
1324 return TargetLowering::findRepresentativeClass(VT);
1325 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
1326 RRC = (Subtarget->is64Bit()
1327 ? X86::GR64RegisterClass : X86::GR32RegisterClass);
1328 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001329 case MVT::x86mmx:
Evan Chengdee81012010-07-26 21:50:05 +00001330 RRC = X86::VR64RegisterClass;
1331 break;
1332 case MVT::f32: case MVT::f64:
1333 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1334 case MVT::v4f32: case MVT::v2f64:
1335 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1336 case MVT::v4f64:
1337 RRC = X86::VR128RegisterClass;
1338 break;
1339 }
1340 return std::make_pair(RRC, Cost);
1341}
1342
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001343bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1344 unsigned &Offset) const {
1345 if (!Subtarget->isTargetLinux())
1346 return false;
1347
1348 if (Subtarget->is64Bit()) {
1349 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1350 Offset = 0x28;
1351 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1352 AddressSpace = 256;
1353 else
1354 AddressSpace = 257;
1355 } else {
1356 // %gs:0x14 on i386
1357 Offset = 0x14;
1358 AddressSpace = 256;
1359 }
1360 return true;
1361}
1362
1363
Chris Lattner2b02a442007-02-25 08:29:00 +00001364//===----------------------------------------------------------------------===//
1365// Return Value Calling Convention Implementation
1366//===----------------------------------------------------------------------===//
1367
Chris Lattner59ed56b2007-02-28 04:55:35 +00001368#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001369
Michael J. Spencerec38de22010-10-10 22:04:20 +00001370bool
Eric Christopher471e4222011-06-08 23:55:35 +00001371X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
1372 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001373 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001374 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001375 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001376 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001377 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001378 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001379}
1380
Dan Gohman98ca4f22009-08-05 01:29:28 +00001381SDValue
1382X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001383 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001384 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001385 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001386 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001387 MachineFunction &MF = DAG.getMachineFunction();
1388 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001389
Chris Lattner9774c912007-02-27 05:28:59 +00001390 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001391 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001392 RVLocs, *DAG.getContext());
1393 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001394
Evan Chengdcea1632010-02-04 02:40:39 +00001395 // Add the regs to the liveout set for the function.
1396 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1397 for (unsigned i = 0; i != RVLocs.size(); ++i)
1398 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1399 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001400
Dan Gohman475871a2008-07-27 21:46:04 +00001401 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001402
Dan Gohman475871a2008-07-27 21:46:04 +00001403 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001404 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1405 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001406 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1407 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001408
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001409 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001410 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1411 CCValAssign &VA = RVLocs[i];
1412 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001413 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001414 EVT ValVT = ValToCopy.getValueType();
1415
Dale Johannesenc4510512010-09-24 19:05:48 +00001416 // If this is x86-64, and we disabled SSE, we can't return FP values,
1417 // or SSE or MMX vectors.
1418 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1419 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001420 (Subtarget->is64Bit() && !Subtarget->hasXMM())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001421 report_fatal_error("SSE register return with SSE disabled");
1422 }
1423 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1424 // llvm-gcc has never done it right and no one has noticed, so this
1425 // should be OK for now.
1426 if (ValVT == MVT::f64 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001427 (Subtarget->is64Bit() && !Subtarget->hasXMMInt()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001428 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001429
Chris Lattner447ff682008-03-11 03:23:40 +00001430 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1431 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001432 if (VA.getLocReg() == X86::ST0 ||
1433 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001434 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1435 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001436 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001437 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001438 RetOps.push_back(ValToCopy);
1439 // Don't emit a copytoreg.
1440 continue;
1441 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001442
Evan Cheng242b38b2009-02-23 09:03:22 +00001443 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1444 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001445 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001446 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001447 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001448 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001449 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1450 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001451 // If we don't have SSE2 available, convert to v4f32 so the generated
1452 // register is legal.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00001453 if (!Subtarget->hasXMMInt())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001454 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001455 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001456 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001457 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001458
Dale Johannesendd64c412009-02-04 00:33:20 +00001459 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001460 Flag = Chain.getValue(1);
1461 }
Dan Gohman61a92132008-04-21 23:59:07 +00001462
1463 // The x86-64 ABI for returning structs by value requires that we copy
1464 // the sret argument into %rax for the return. We saved the argument into
1465 // a virtual register in the entry block, so now we copy the value out
1466 // and into %rax.
1467 if (Subtarget->is64Bit() &&
1468 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1469 MachineFunction &MF = DAG.getMachineFunction();
1470 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1471 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001472 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001473 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001474 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001475
Dale Johannesendd64c412009-02-04 00:33:20 +00001476 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001477 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001478
1479 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001480 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001481 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001482
Chris Lattner447ff682008-03-11 03:23:40 +00001483 RetOps[0] = Chain; // Update chain.
1484
1485 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001486 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001487 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001488
1489 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001490 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001491}
1492
Evan Cheng3d2125c2010-11-30 23:55:39 +00001493bool X86TargetLowering::isUsedByReturnOnly(SDNode *N) const {
1494 if (N->getNumValues() != 1)
1495 return false;
1496 if (!N->hasNUsesOfValue(1, 0))
1497 return false;
1498
1499 SDNode *Copy = *N->use_begin();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001500 if (Copy->getOpcode() != ISD::CopyToReg &&
1501 Copy->getOpcode() != ISD::FP_EXTEND)
Evan Cheng3d2125c2010-11-30 23:55:39 +00001502 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001503
1504 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001505 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001506 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001507 if (UI->getOpcode() != X86ISD::RET_FLAG)
1508 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001509 HasRet = true;
1510 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001511
Evan Cheng1bf891a2010-12-01 22:59:46 +00001512 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001513}
1514
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001515EVT
1516X86TargetLowering::getTypeForExtArgOrReturn(LLVMContext &Context, EVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001517 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001518 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001519 // TODO: Is this also valid on 32-bit?
1520 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001521 ReturnMVT = MVT::i8;
1522 else
1523 ReturnMVT = MVT::i32;
1524
1525 EVT MinVT = getRegisterType(Context, ReturnMVT);
1526 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001527}
1528
Dan Gohman98ca4f22009-08-05 01:29:28 +00001529/// LowerCallResult - Lower the result values of a call into the
1530/// appropriate copies out of appropriate physical registers.
1531///
1532SDValue
1533X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001534 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001535 const SmallVectorImpl<ISD::InputArg> &Ins,
1536 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001537 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001538
Chris Lattnere32bbf62007-02-28 07:09:55 +00001539 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001540 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001541 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001542 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1543 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001544 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001545
Chris Lattner3085e152007-02-25 08:59:22 +00001546 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001547 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001548 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001549 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001550
Torok Edwin3f142c32009-02-01 18:15:56 +00001551 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001552 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001553 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasXMM())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001554 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001555 }
1556
Evan Cheng79fb3b42009-02-20 20:43:02 +00001557 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001558
1559 // If this is a call to a function that returns an fp value on the floating
1560 // point stack, we must guarantee the the value is popped from the stack, so
1561 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001562 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001563 // instead.
1564 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1565 // If we prefer to use the value in xmm registers, copy it out as f80 and
1566 // use a truncate to move it from fp stack reg to xmm reg.
1567 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001568 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001569 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1570 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001571 Val = Chain.getValue(0);
1572
1573 // Round the f80 to the right size, which also moves it to the appropriate
1574 // xmm register.
1575 if (CopyVT != VA.getValVT())
1576 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1577 // This truncation won't change the value.
1578 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001579 } else {
1580 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1581 CopyVT, InFlag).getValue(1);
1582 Val = Chain.getValue(0);
1583 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001584 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001585 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001586 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001587
Dan Gohman98ca4f22009-08-05 01:29:28 +00001588 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001589}
1590
1591
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001592//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001593// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001594//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001595// StdCall calling convention seems to be standard for many Windows' API
1596// routines and around. It differs from C calling convention just a little:
1597// callee should clean up the stack, not caller. Symbols should be also
1598// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001599// For info on fast calling convention see Fast Calling Convention (tail call)
1600// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001601
Dan Gohman98ca4f22009-08-05 01:29:28 +00001602/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001603/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001604static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1605 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001606 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001607
Dan Gohman98ca4f22009-08-05 01:29:28 +00001608 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001609}
1610
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001611/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001612/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001613static bool
1614ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1615 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001616 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001617
Dan Gohman98ca4f22009-08-05 01:29:28 +00001618 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001619}
1620
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001621/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1622/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001623/// the specific parameter attribute. The copy will be passed as a byval
1624/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001625static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001626CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001627 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1628 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001629 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001630
Dale Johannesendd64c412009-02-04 00:33:20 +00001631 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001632 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001633 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001634}
1635
Chris Lattner29689432010-03-11 00:22:57 +00001636/// IsTailCallConvention - Return true if the calling convention is one that
1637/// supports tail call optimization.
1638static bool IsTailCallConvention(CallingConv::ID CC) {
1639 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1640}
1641
Evan Cheng485fafc2011-03-21 01:19:09 +00001642bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1643 if (!CI->isTailCall())
1644 return false;
1645
1646 CallSite CS(CI);
1647 CallingConv::ID CalleeCC = CS.getCallingConv();
1648 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1649 return false;
1650
1651 return true;
1652}
1653
Evan Cheng0c439eb2010-01-27 00:07:07 +00001654/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1655/// a tailcall target by changing its ABI.
1656static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001657 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001658}
1659
Dan Gohman98ca4f22009-08-05 01:29:28 +00001660SDValue
1661X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001662 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001663 const SmallVectorImpl<ISD::InputArg> &Ins,
1664 DebugLoc dl, SelectionDAG &DAG,
1665 const CCValAssign &VA,
1666 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001667 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001668 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001669 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001670 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001671 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001672 EVT ValVT;
1673
1674 // If value is passed by pointer we have address passed instead of the value
1675 // itself.
1676 if (VA.getLocInfo() == CCValAssign::Indirect)
1677 ValVT = VA.getLocVT();
1678 else
1679 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001680
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001681 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001682 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001683 // In case of tail call optimization mark all arguments mutable. Since they
1684 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001685 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001686 unsigned Bytes = Flags.getByValSize();
1687 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1688 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001689 return DAG.getFrameIndex(FI, getPointerTy());
1690 } else {
1691 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001692 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001693 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1694 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001695 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00001696 false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001697 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001698}
1699
Dan Gohman475871a2008-07-27 21:46:04 +00001700SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001701X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001702 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001703 bool isVarArg,
1704 const SmallVectorImpl<ISD::InputArg> &Ins,
1705 DebugLoc dl,
1706 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001707 SmallVectorImpl<SDValue> &InVals)
1708 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001709 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001710 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001711
Gordon Henriksen86737662008-01-05 16:56:59 +00001712 const Function* Fn = MF.getFunction();
1713 if (Fn->hasExternalLinkage() &&
1714 Subtarget->isTargetCygMing() &&
1715 Fn->getName() == "main")
1716 FuncInfo->setForceFramePointer(true);
1717
Evan Cheng1bc78042006-04-26 01:20:17 +00001718 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001719 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001720 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001721
Chris Lattner29689432010-03-11 00:22:57 +00001722 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1723 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001724
Chris Lattner638402b2007-02-28 07:00:42 +00001725 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001726 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001727 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001728 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001729
1730 // Allocate shadow area for Win64
1731 if (IsWin64) {
1732 CCInfo.AllocateStack(32, 8);
1733 }
1734
Duncan Sands45907662010-10-31 13:21:44 +00001735 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001736
Chris Lattnerf39f7712007-02-28 05:46:49 +00001737 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001738 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001739 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1740 CCValAssign &VA = ArgLocs[i];
1741 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1742 // places.
1743 assert(VA.getValNo() != LastVal &&
1744 "Don't support value assigned to multiple locs yet");
1745 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001746
Chris Lattnerf39f7712007-02-28 05:46:49 +00001747 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001748 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001749 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001750 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001751 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001752 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001753 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001754 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001755 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001756 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001757 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001758 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1759 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001760 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001761 RC = X86::VR128RegisterClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001762 else if (RegVT == MVT::x86mmx)
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001763 RC = X86::VR64RegisterClass;
1764 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001765 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001766
Devang Patel68e6bee2011-02-21 23:21:26 +00001767 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001768 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001769
Chris Lattnerf39f7712007-02-28 05:46:49 +00001770 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1771 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1772 // right size.
1773 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001774 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001775 DAG.getValueType(VA.getValVT()));
1776 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001777 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001778 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001779 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001780 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001781
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001782 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001783 // Handle MMX values passed in XMM regs.
1784 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001785 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1786 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001787 } else
1788 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001789 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001790 } else {
1791 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001792 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001793 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001794
1795 // If value is passed via pointer - do a load.
1796 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001797 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
1798 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001799
Dan Gohman98ca4f22009-08-05 01:29:28 +00001800 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001801 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001802
Dan Gohman61a92132008-04-21 23:59:07 +00001803 // The x86-64 ABI for returning structs by value requires that we copy
1804 // the sret argument into %rax for the return. Save the argument into
1805 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001806 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001807 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1808 unsigned Reg = FuncInfo->getSRetReturnReg();
1809 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001810 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001811 FuncInfo->setSRetReturnReg(Reg);
1812 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001813 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001814 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001815 }
1816
Chris Lattnerf39f7712007-02-28 05:46:49 +00001817 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001818 // Align stack specially for tail calls.
1819 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001820 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001821
Evan Cheng1bc78042006-04-26 01:20:17 +00001822 // If the function takes variable number of arguments, make a frame index for
1823 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001824 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001825 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1826 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001827 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001828 }
1829 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001830 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1831
1832 // FIXME: We should really autogenerate these arrays
1833 static const unsigned GPR64ArgRegsWin64[] = {
1834 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001835 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001836 static const unsigned GPR64ArgRegs64Bit[] = {
1837 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1838 };
1839 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001840 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1841 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1842 };
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001843 const unsigned *GPR64ArgRegs;
1844 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001845
1846 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001847 // The XMM registers which might contain var arg parameters are shadowed
1848 // in their paired GPR. So we only need to save the GPR to their home
1849 // slots.
1850 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001851 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001852 } else {
1853 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1854 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001855
1856 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit, TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001857 }
1858 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1859 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001860
Devang Patel578efa92009-06-05 21:57:13 +00001861 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001862 assert(!(NumXMMRegs && !Subtarget->hasXMM()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001863 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001864 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001865 "SSE register cannot be used when SSE is disabled!");
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001866 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasXMM())
Torok Edwin3f142c32009-02-01 18:15:56 +00001867 // Kernel mode asks for SSE to be disabled, so don't push them
1868 // on the stack.
1869 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001870
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001871 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001872 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001873 // Get to the caller-allocated home save location. Add 8 to account
1874 // for the return address.
1875 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001876 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001877 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001878 // Fixup to set vararg frame on shadow area (4 x i64).
1879 if (NumIntRegs < 4)
1880 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001881 } else {
1882 // For X86-64, if there are vararg parameters that are passed via
1883 // registers, then we must store them to their spots on the stack so they
1884 // may be loaded by deferencing the result of va_next.
1885 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1886 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1887 FuncInfo->setRegSaveFrameIndex(
1888 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00001889 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001890 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001891
Gordon Henriksen86737662008-01-05 16:56:59 +00001892 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001893 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001894 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1895 getPointerTy());
1896 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001897 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001898 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1899 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001900 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001901 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001902 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001903 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001904 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001905 MachinePointerInfo::getFixedStack(
1906 FuncInfo->getRegSaveFrameIndex(), Offset),
1907 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001908 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001909 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001910 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001911
Dan Gohmanface41a2009-08-16 21:24:25 +00001912 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1913 // Now store the XMM (fp + vector) parameter registers.
1914 SmallVector<SDValue, 11> SaveXMMOps;
1915 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001916
Devang Patel68e6bee2011-02-21 23:21:26 +00001917 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001918 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1919 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001920
Dan Gohman1e93df62010-04-17 14:41:14 +00001921 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1922 FuncInfo->getRegSaveFrameIndex()));
1923 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1924 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001925
Dan Gohmanface41a2009-08-16 21:24:25 +00001926 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001927 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001928 X86::VR128RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001929 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1930 SaveXMMOps.push_back(Val);
1931 }
1932 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1933 MVT::Other,
1934 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001935 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001936
1937 if (!MemOps.empty())
1938 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1939 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001940 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001941 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001942
Gordon Henriksen86737662008-01-05 16:56:59 +00001943 // Some CCs need callee pop.
Evan Chengef41ff62011-06-23 17:54:54 +00001944 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg, GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001945 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001946 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001947 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001948 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001949 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001950 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001951 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001952
Gordon Henriksen86737662008-01-05 16:56:59 +00001953 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001954 // RegSaveFrameIndex is X86-64 only.
1955 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001956 if (CallConv == CallingConv::X86_FastCall ||
1957 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001958 // fastcc functions can't have varargs.
1959 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001960 }
Evan Cheng25caf632006-05-23 21:06:34 +00001961
Rafael Espindola76927d752011-08-30 19:39:58 +00001962 FuncInfo->setArgumentStackSize(StackSize);
1963
Dan Gohman98ca4f22009-08-05 01:29:28 +00001964 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001965}
1966
Dan Gohman475871a2008-07-27 21:46:04 +00001967SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001968X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1969 SDValue StackPtr, SDValue Arg,
1970 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001971 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001972 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001973 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001974 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001975 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001976 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00001977 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001978
1979 return DAG.getStore(Chain, dl, Arg, PtrOff,
1980 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00001981 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001982}
1983
Bill Wendling64e87322009-01-16 19:25:27 +00001984/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001985/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001986SDValue
1987X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001988 SDValue &OutRetAddr, SDValue Chain,
1989 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00001990 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001991 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001992 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001993 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001994
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001995 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00001996 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
1997 false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001998 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001999}
2000
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002001/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002002/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00002003static SDValue
2004EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00002005 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00002006 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002007 // Store the return address to the appropriate stack slot.
2008 if (!FPDiff) return Chain;
2009 // Calculate the new stack slot for the return address.
2010 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00002011 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00002012 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00002013 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00002014 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002015 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00002016 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00002017 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002018 return Chain;
2019}
2020
Dan Gohman98ca4f22009-08-05 01:29:28 +00002021SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00002022X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002023 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00002024 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002025 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002026 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002027 const SmallVectorImpl<ISD::InputArg> &Ins,
2028 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002029 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002030 MachineFunction &MF = DAG.getMachineFunction();
2031 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002032 bool IsWin64 = Subtarget->isTargetWin64();
Dan Gohman98ca4f22009-08-05 01:29:28 +00002033 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00002034 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002035
Evan Cheng5f941932010-02-05 02:21:12 +00002036 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002037 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00002038 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
2039 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00002040 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00002041
2042 // Sibcalls are automatically detected tailcalls which do not require
2043 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00002044 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002045 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002046
2047 if (isTailCall)
2048 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002049 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002050
Chris Lattner29689432010-03-11 00:22:57 +00002051 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
2052 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002053
Chris Lattner638402b2007-02-28 07:00:42 +00002054 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002055 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002056 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002057 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002058
2059 // Allocate shadow area for Win64
2060 if (IsWin64) {
2061 CCInfo.AllocateStack(32, 8);
2062 }
2063
Duncan Sands45907662010-10-31 13:21:44 +00002064 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002065
Chris Lattner423c5f42007-02-28 05:31:48 +00002066 // Get a count of how many bytes are to be pushed on the stack.
2067 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002068 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002069 // This is a sibcall. The memory operands are available in caller's
2070 // own caller's stack.
2071 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00002072 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002073 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002074
Gordon Henriksen86737662008-01-05 16:56:59 +00002075 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002076 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002077 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00002078 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00002079 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2080 FPDiff = NumBytesCallerPushed - NumBytes;
2081
2082 // Set the delta of movement of the returnaddr stackslot.
2083 // But only set if delta is greater than previous delta.
2084 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2085 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2086 }
2087
Evan Chengf22f9b32010-02-06 03:28:46 +00002088 if (!IsSibcall)
2089 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002090
Dan Gohman475871a2008-07-27 21:46:04 +00002091 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002092 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002093 if (isTailCall && FPDiff)
2094 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2095 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002096
Dan Gohman475871a2008-07-27 21:46:04 +00002097 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2098 SmallVector<SDValue, 8> MemOpChains;
2099 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002100
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002101 // Walk the register/memloc assignments, inserting copies/loads. In the case
2102 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002103 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2104 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002105 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002106 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002107 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002108 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002109
Chris Lattner423c5f42007-02-28 05:31:48 +00002110 // Promote the value if needed.
2111 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002112 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002113 case CCValAssign::Full: break;
2114 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002115 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002116 break;
2117 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002118 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002119 break;
2120 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002121 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
2122 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002123 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002124 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2125 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002126 } else
2127 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2128 break;
2129 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002130 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002131 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002132 case CCValAssign::Indirect: {
2133 // Store the argument.
2134 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002135 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002136 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002137 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002138 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002139 Arg = SpillSlot;
2140 break;
2141 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002142 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002143
Chris Lattner423c5f42007-02-28 05:31:48 +00002144 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002145 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2146 if (isVarArg && IsWin64) {
2147 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2148 // shadow reg if callee is a varargs function.
2149 unsigned ShadowReg = 0;
2150 switch (VA.getLocReg()) {
2151 case X86::XMM0: ShadowReg = X86::RCX; break;
2152 case X86::XMM1: ShadowReg = X86::RDX; break;
2153 case X86::XMM2: ShadowReg = X86::R8; break;
2154 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002155 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002156 if (ShadowReg)
2157 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002158 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002159 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002160 assert(VA.isMemLoc());
2161 if (StackPtr.getNode() == 0)
2162 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2163 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2164 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002165 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002166 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002167
Evan Cheng32fe1032006-05-25 00:59:30 +00002168 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002169 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002170 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002171
Evan Cheng347d5f72006-04-28 21:29:37 +00002172 // Build a sequence of copy-to-reg nodes chained together with token chain
2173 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002174 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002175 // Tail call byval lowering might overwrite argument registers so in case of
2176 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002177 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002178 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002179 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002180 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002181 InFlag = Chain.getValue(1);
2182 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002183
Chris Lattner88e1fd52009-07-09 04:24:46 +00002184 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002185 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2186 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002187 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002188 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2189 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00002190 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002191 InFlag);
2192 InFlag = Chain.getValue(1);
2193 } else {
2194 // If we are tail calling and generating PIC/GOT style code load the
2195 // address of the callee into ECX. The value in ecx is used as target of
2196 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2197 // for tail calls on PIC/GOT architectures. Normally we would just put the
2198 // address of GOT into ebx and then call target@PLT. But for tail calls
2199 // ebx would be restored (since ebx is callee saved) before jumping to the
2200 // target@PLT.
2201
2202 // Note: The actual moving to ECX is done further down.
2203 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2204 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2205 !G->getGlobal()->hasProtectedVisibility())
2206 Callee = LowerGlobalAddress(Callee, DAG);
2207 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002208 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002209 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002210 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002211
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002212 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002213 // From AMD64 ABI document:
2214 // For calls that may call functions that use varargs or stdargs
2215 // (prototype-less calls or calls to functions containing ellipsis (...) in
2216 // the declaration) %al is used as hidden argument to specify the number
2217 // of SSE registers used. The contents of %al do not need to match exactly
2218 // the number of registers, but must be an ubound on the number of SSE
2219 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002220
Gordon Henriksen86737662008-01-05 16:56:59 +00002221 // Count the number of XMM registers allocated.
2222 static const unsigned XMMArgRegs[] = {
2223 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2224 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2225 };
2226 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00002227 assert((Subtarget->hasXMM() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002228 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002229
Dale Johannesendd64c412009-02-04 00:33:20 +00002230 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002231 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002232 InFlag = Chain.getValue(1);
2233 }
2234
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002235
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002236 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002237 if (isTailCall) {
2238 // Force all the incoming stack arguments to be loaded from the stack
2239 // before any new outgoing arguments are stored to the stack, because the
2240 // outgoing stack slots may alias the incoming argument stack slots, and
2241 // the alias isn't otherwise explicit. This is slightly more conservative
2242 // than necessary, because it means that each store effectively depends
2243 // on every argument instead of just those arguments it would clobber.
2244 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2245
Dan Gohman475871a2008-07-27 21:46:04 +00002246 SmallVector<SDValue, 8> MemOpChains2;
2247 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002248 int FI = 0;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002249 // Do not flag preceding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002250 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002251 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002252 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2253 CCValAssign &VA = ArgLocs[i];
2254 if (VA.isRegLoc())
2255 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002256 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002257 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002258 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002259 // Create frame index.
2260 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002261 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002262 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002263 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002264
Duncan Sands276dcbd2008-03-21 09:14:45 +00002265 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002266 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002267 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002268 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002269 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002270 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002271 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002272
Dan Gohman98ca4f22009-08-05 01:29:28 +00002273 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2274 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002275 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002276 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002277 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002278 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002279 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002280 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002281 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002282 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002283 }
2284 }
2285
2286 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002287 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002288 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002289
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002290 // Copy arguments to their registers.
2291 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002292 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002293 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002294 InFlag = Chain.getValue(1);
2295 }
Dan Gohman475871a2008-07-27 21:46:04 +00002296 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002297
Gordon Henriksen86737662008-01-05 16:56:59 +00002298 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002299 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002300 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002301 }
2302
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002303 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2304 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2305 // In the 64-bit large code model, we have to make all calls
2306 // through a register, since the call instruction's 32-bit
2307 // pc-relative offset may not be large enough to hold the whole
2308 // address.
2309 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002310 // If the callee is a GlobalAddress node (quite common, every direct call
2311 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2312 // it.
2313
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002314 // We should use extra load for direct calls to dllimported functions in
2315 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002316 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002317 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002318 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002319 bool ExtraLoad = false;
2320 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002321
Chris Lattner48a7d022009-07-09 05:02:21 +00002322 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2323 // external symbols most go through the PLT in PIC mode. If the symbol
2324 // has hidden or protected visibility, or if it is static or local, then
2325 // we don't need to use the PLT - we can directly call it.
2326 if (Subtarget->isTargetELF() &&
2327 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002328 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002329 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002330 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002331 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002332 (!Subtarget->getTargetTriple().isMacOSX() ||
2333 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002334 // PC-relative references to external symbols should go through $stub,
2335 // unless we're building with the leopard linker or later, which
2336 // automatically synthesizes these stubs.
2337 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002338 } else if (Subtarget->isPICStyleRIPRel() &&
2339 isa<Function>(GV) &&
2340 cast<Function>(GV)->hasFnAttr(Attribute::NonLazyBind)) {
2341 // If the function is marked as non-lazy, generate an indirect call
2342 // which loads from the GOT directly. This avoids runtime overhead
2343 // at the cost of eager binding (and one extra byte of encoding).
2344 OpFlags = X86II::MO_GOTPCREL;
2345 WrapperKind = X86ISD::WrapperRIP;
2346 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002347 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002348
Devang Patel0d881da2010-07-06 22:08:15 +00002349 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002350 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002351
2352 // Add a wrapper if needed.
2353 if (WrapperKind != ISD::DELETED_NODE)
2354 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2355 // Add extra indirection if needed.
2356 if (ExtraLoad)
2357 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2358 MachinePointerInfo::getGOT(),
2359 false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002360 }
Bill Wendling056292f2008-09-16 21:48:12 +00002361 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002362 unsigned char OpFlags = 0;
2363
Evan Cheng1bf891a2010-12-01 22:59:46 +00002364 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2365 // external symbols should go through the PLT.
2366 if (Subtarget->isTargetELF() &&
2367 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2368 OpFlags = X86II::MO_PLT;
2369 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002370 (!Subtarget->getTargetTriple().isMacOSX() ||
2371 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002372 // PC-relative references to external symbols should go through $stub,
2373 // unless we're building with the leopard linker or later, which
2374 // automatically synthesizes these stubs.
2375 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002376 }
Eric Christopherfd179292009-08-27 18:07:15 +00002377
Chris Lattner48a7d022009-07-09 05:02:21 +00002378 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2379 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002380 }
2381
Chris Lattnerd96d0722007-02-25 06:40:16 +00002382 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002383 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002384 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002385
Evan Chengf22f9b32010-02-06 03:28:46 +00002386 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002387 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2388 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002389 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002390 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002391
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002392 Ops.push_back(Chain);
2393 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002394
Dan Gohman98ca4f22009-08-05 01:29:28 +00002395 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002396 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002397
Gordon Henriksen86737662008-01-05 16:56:59 +00002398 // Add argument registers to the end of the list so that they are known live
2399 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002400 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2401 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2402 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002403
Evan Cheng586ccac2008-03-18 23:36:35 +00002404 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002405 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002406 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2407
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002408 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002409 if (Is64Bit && isVarArg && !IsWin64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002410 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002411
Gabor Greifba36cb52008-08-28 21:40:38 +00002412 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002413 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002414
Dan Gohman98ca4f22009-08-05 01:29:28 +00002415 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002416 // We used to do:
2417 //// If this is the first return lowered for this function, add the regs
2418 //// to the liveout set for the function.
2419 // This isn't right, although it's probably harmless on x86; liveouts
2420 // should be computed from returns not tail calls. Consider a void
2421 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002422 return DAG.getNode(X86ISD::TC_RETURN, dl,
2423 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002424 }
2425
Dale Johannesenace16102009-02-03 19:33:06 +00002426 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002427 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002428
Chris Lattner2d297092006-05-23 18:50:38 +00002429 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002430 unsigned NumBytesForCalleeToPush;
Evan Chengef41ff62011-06-23 17:54:54 +00002431 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg, GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002432 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002433 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002434 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002435 // pops the hidden struct pointer, so we have to push it back.
2436 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002437 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002438 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002439 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002440
Gordon Henriksenae636f82008-01-03 16:47:34 +00002441 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002442 if (!IsSibcall) {
2443 Chain = DAG.getCALLSEQ_END(Chain,
2444 DAG.getIntPtrConstant(NumBytes, true),
2445 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2446 true),
2447 InFlag);
2448 InFlag = Chain.getValue(1);
2449 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002450
Chris Lattner3085e152007-02-25 08:59:22 +00002451 // Handle result values, copying them out of physregs into vregs that we
2452 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002453 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2454 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002455}
2456
Evan Cheng25ab6902006-09-08 06:48:29 +00002457
2458//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002459// Fast Calling Convention (tail call) implementation
2460//===----------------------------------------------------------------------===//
2461
2462// Like std call, callee cleans arguments, convention except that ECX is
2463// reserved for storing the tail called function address. Only 2 registers are
2464// free for argument passing (inreg). Tail call optimization is performed
2465// provided:
2466// * tailcallopt is enabled
2467// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002468// On X86_64 architecture with GOT-style position independent code only local
2469// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002470// To keep the stack aligned according to platform abi the function
2471// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2472// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002473// If a tail called function callee has more arguments than the caller the
2474// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002475// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002476// original REtADDR, but before the saved framepointer or the spilled registers
2477// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2478// stack layout:
2479// arg1
2480// arg2
2481// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002482// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002483// move area ]
2484// (possible EBP)
2485// ESI
2486// EDI
2487// local1 ..
2488
2489/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2490/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002491unsigned
2492X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2493 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002494 MachineFunction &MF = DAG.getMachineFunction();
2495 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002496 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002497 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002498 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002499 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002500 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002501 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2502 // Number smaller than 12 so just add the difference.
2503 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2504 } else {
2505 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002506 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002507 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002508 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002509 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002510}
2511
Evan Cheng5f941932010-02-05 02:21:12 +00002512/// MatchingStackOffset - Return true if the given stack call argument is
2513/// already available in the same position (relatively) of the caller's
2514/// incoming argument stack.
2515static
2516bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2517 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2518 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002519 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2520 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002521 if (Arg.getOpcode() == ISD::CopyFromReg) {
2522 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002523 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002524 return false;
2525 MachineInstr *Def = MRI->getVRegDef(VR);
2526 if (!Def)
2527 return false;
2528 if (!Flags.isByVal()) {
2529 if (!TII->isLoadFromStackSlot(Def, FI))
2530 return false;
2531 } else {
2532 unsigned Opcode = Def->getOpcode();
2533 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2534 Def->getOperand(1).isFI()) {
2535 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002536 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002537 } else
2538 return false;
2539 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002540 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2541 if (Flags.isByVal())
2542 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002543 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002544 // define @foo(%struct.X* %A) {
2545 // tail call @bar(%struct.X* byval %A)
2546 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002547 return false;
2548 SDValue Ptr = Ld->getBasePtr();
2549 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2550 if (!FINode)
2551 return false;
2552 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002553 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002554 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002555 FI = FINode->getIndex();
2556 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002557 } else
2558 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002559
Evan Cheng4cae1332010-03-05 08:38:04 +00002560 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002561 if (!MFI->isFixedObjectIndex(FI))
2562 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002563 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002564}
2565
Dan Gohman98ca4f22009-08-05 01:29:28 +00002566/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2567/// for tail call optimization. Targets which want to do tail call
2568/// optimization should implement this function.
2569bool
2570X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002571 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002572 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002573 bool isCalleeStructRet,
2574 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002575 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002576 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002577 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002578 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002579 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002580 CalleeCC != CallingConv::C)
2581 return false;
2582
Evan Cheng7096ae42010-01-29 06:45:59 +00002583 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002584 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002585 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002586 CallingConv::ID CallerCC = CallerF->getCallingConv();
2587 bool CCMatch = CallerCC == CalleeCC;
2588
Dan Gohman1797ed52010-02-08 20:27:50 +00002589 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002590 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002591 return true;
2592 return false;
2593 }
2594
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002595 // Look for obvious safe cases to perform tail call optimization that do not
2596 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002597
Evan Cheng2c12cb42010-03-26 16:26:03 +00002598 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2599 // emit a special epilogue.
2600 if (RegInfo->needsStackRealignment(MF))
2601 return false;
2602
Evan Chenga375d472010-03-15 18:54:48 +00002603 // Also avoid sibcall optimization if either caller or callee uses struct
2604 // return semantics.
2605 if (isCalleeStructRet || isCallerStructRet)
2606 return false;
2607
Chad Rosier2416da32011-06-24 21:15:36 +00002608 // An stdcall caller is expected to clean up its arguments; the callee
2609 // isn't going to do that.
2610 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2611 return false;
2612
Chad Rosier871f6642011-05-18 19:59:50 +00002613 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002614 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002615 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002616
2617 // Optimizing for varargs on Win64 is unlikely to be safe without
2618 // additional testing.
2619 if (Subtarget->isTargetWin64())
2620 return false;
2621
Chad Rosier871f6642011-05-18 19:59:50 +00002622 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002623 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2624 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002625
Chad Rosier871f6642011-05-18 19:59:50 +00002626 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2627 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2628 if (!ArgLocs[i].isRegLoc())
2629 return false;
2630 }
2631
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002632 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2633 // Therefore if it's not used by the call it is not safe to optimize this into
2634 // a sibcall.
2635 bool Unused = false;
2636 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2637 if (!Ins[i].Used) {
2638 Unused = true;
2639 break;
2640 }
2641 }
2642 if (Unused) {
2643 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002644 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
2645 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002646 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002647 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002648 CCValAssign &VA = RVLocs[i];
2649 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2650 return false;
2651 }
2652 }
2653
Evan Cheng13617962010-04-30 01:12:32 +00002654 // If the calling conventions do not match, then we'd better make sure the
2655 // results are returned in the same way as what the caller expects.
2656 if (!CCMatch) {
2657 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002658 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
2659 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002660 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2661
2662 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002663 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
2664 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002665 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2666
2667 if (RVLocs1.size() != RVLocs2.size())
2668 return false;
2669 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2670 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2671 return false;
2672 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2673 return false;
2674 if (RVLocs1[i].isRegLoc()) {
2675 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2676 return false;
2677 } else {
2678 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2679 return false;
2680 }
2681 }
2682 }
2683
Evan Chenga6bff982010-01-30 01:22:00 +00002684 // If the callee takes no arguments then go on to check the results of the
2685 // call.
2686 if (!Outs.empty()) {
2687 // Check if stack adjustment is needed. For now, do not do this if any
2688 // argument is passed on the stack.
2689 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002690 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2691 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002692
2693 // Allocate shadow area for Win64
2694 if (Subtarget->isTargetWin64()) {
2695 CCInfo.AllocateStack(32, 8);
2696 }
2697
Duncan Sands45907662010-10-31 13:21:44 +00002698 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002699 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002700 MachineFunction &MF = DAG.getMachineFunction();
2701 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2702 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002703
2704 // Check if the arguments are already laid out in the right way as
2705 // the caller's fixed stack objects.
2706 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002707 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2708 const X86InstrInfo *TII =
2709 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002710 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2711 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002712 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002713 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002714 if (VA.getLocInfo() == CCValAssign::Indirect)
2715 return false;
2716 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002717 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2718 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002719 return false;
2720 }
2721 }
2722 }
Evan Cheng9c044672010-05-29 01:35:22 +00002723
2724 // If the tailcall address may be in a register, then make sure it's
2725 // possible to register allocate for it. In 32-bit, the call address can
2726 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002727 // callee-saved registers are restored. These happen to be the same
2728 // registers used to pass 'inreg' arguments so watch out for those.
2729 if (!Subtarget->is64Bit() &&
2730 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002731 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002732 unsigned NumInRegs = 0;
2733 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2734 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002735 if (!VA.isRegLoc())
2736 continue;
2737 unsigned Reg = VA.getLocReg();
2738 switch (Reg) {
2739 default: break;
2740 case X86::EAX: case X86::EDX: case X86::ECX:
2741 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002742 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002743 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002744 }
2745 }
2746 }
Evan Chenga6bff982010-01-30 01:22:00 +00002747 }
Evan Chengb1712452010-01-27 06:25:16 +00002748
Evan Cheng86809cc2010-02-03 03:28:02 +00002749 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002750}
2751
Dan Gohman3df24e62008-09-03 23:12:08 +00002752FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002753X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2754 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002755}
2756
2757
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002758//===----------------------------------------------------------------------===//
2759// Other Lowering Hooks
2760//===----------------------------------------------------------------------===//
2761
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002762static bool MayFoldLoad(SDValue Op) {
2763 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2764}
2765
2766static bool MayFoldIntoStore(SDValue Op) {
2767 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2768}
2769
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002770static bool isTargetShuffle(unsigned Opcode) {
2771 switch(Opcode) {
2772 default: return false;
2773 case X86ISD::PSHUFD:
2774 case X86ISD::PSHUFHW:
2775 case X86ISD::PSHUFLW:
2776 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002777 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002778 case X86ISD::SHUFPS:
2779 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002780 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002781 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002782 case X86ISD::MOVLPS:
2783 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002784 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002785 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002786 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002787 case X86ISD::MOVSS:
2788 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002789 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002790 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002791 case X86ISD::VUNPCKLPSY:
2792 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002793 case X86ISD::PUNPCKLWD:
2794 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002795 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002796 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002797 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002798 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00002799 case X86ISD::VUNPCKHPSY:
2800 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002801 case X86ISD::PUNPCKHWD:
2802 case X86ISD::PUNPCKHBW:
2803 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002804 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00002805 case X86ISD::VPERMILPS:
2806 case X86ISD::VPERMILPSY:
2807 case X86ISD::VPERMILPD:
2808 case X86ISD::VPERMILPDY:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00002809 case X86ISD::VPERM2F128:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002810 return true;
2811 }
2812 return false;
2813}
2814
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002815static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002816 SDValue V1, SelectionDAG &DAG) {
2817 switch(Opc) {
2818 default: llvm_unreachable("Unknown x86 shuffle node");
2819 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002820 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002821 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002822 return DAG.getNode(Opc, dl, VT, V1);
2823 }
2824
2825 return SDValue();
2826}
2827
2828static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002829 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002830 switch(Opc) {
2831 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002832 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002833 case X86ISD::PSHUFHW:
2834 case X86ISD::PSHUFLW:
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00002835 case X86ISD::VPERMILPS:
2836 case X86ISD::VPERMILPSY:
2837 case X86ISD::VPERMILPD:
2838 case X86ISD::VPERMILPDY:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002839 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2840 }
2841
2842 return SDValue();
2843}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002844
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002845static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2846 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2847 switch(Opc) {
2848 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002849 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002850 case X86ISD::SHUFPD:
2851 case X86ISD::SHUFPS:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00002852 case X86ISD::VPERM2F128:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002853 return DAG.getNode(Opc, dl, VT, V1, V2,
2854 DAG.getConstant(TargetMask, MVT::i8));
2855 }
2856 return SDValue();
2857}
2858
2859static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2860 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2861 switch(Opc) {
2862 default: llvm_unreachable("Unknown x86 shuffle node");
2863 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002864 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002865 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002866 case X86ISD::MOVLPS:
2867 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002868 case X86ISD::MOVSS:
2869 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002870 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002871 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002872 case X86ISD::VUNPCKLPSY:
2873 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002874 case X86ISD::PUNPCKLWD:
2875 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002876 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002877 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002878 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002879 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00002880 case X86ISD::VUNPCKHPSY:
2881 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002882 case X86ISD::PUNPCKHWD:
2883 case X86ISD::PUNPCKHBW:
2884 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002885 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002886 return DAG.getNode(Opc, dl, VT, V1, V2);
2887 }
2888 return SDValue();
2889}
2890
Dan Gohmand858e902010-04-17 15:26:15 +00002891SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002892 MachineFunction &MF = DAG.getMachineFunction();
2893 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2894 int ReturnAddrIndex = FuncInfo->getRAIndex();
2895
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002896 if (ReturnAddrIndex == 0) {
2897 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002898 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002899 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002900 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002901 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002902 }
2903
Evan Cheng25ab6902006-09-08 06:48:29 +00002904 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002905}
2906
2907
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002908bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2909 bool hasSymbolicDisplacement) {
2910 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002911 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002912 return false;
2913
2914 // If we don't have a symbolic displacement - we don't have any extra
2915 // restrictions.
2916 if (!hasSymbolicDisplacement)
2917 return true;
2918
2919 // FIXME: Some tweaks might be needed for medium code model.
2920 if (M != CodeModel::Small && M != CodeModel::Kernel)
2921 return false;
2922
2923 // For small code model we assume that latest object is 16MB before end of 31
2924 // bits boundary. We may also accept pretty large negative constants knowing
2925 // that all objects are in the positive half of address space.
2926 if (M == CodeModel::Small && Offset < 16*1024*1024)
2927 return true;
2928
2929 // For kernel code model we know that all object resist in the negative half
2930 // of 32bits address space. We may not accept negative offsets, since they may
2931 // be just off and we may accept pretty large positive ones.
2932 if (M == CodeModel::Kernel && Offset > 0)
2933 return true;
2934
2935 return false;
2936}
2937
Evan Chengef41ff62011-06-23 17:54:54 +00002938/// isCalleePop - Determines whether the callee is required to pop its
2939/// own arguments. Callee pop is necessary to support tail calls.
2940bool X86::isCalleePop(CallingConv::ID CallingConv,
2941 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
2942 if (IsVarArg)
2943 return false;
2944
2945 switch (CallingConv) {
2946 default:
2947 return false;
2948 case CallingConv::X86_StdCall:
2949 return !is64Bit;
2950 case CallingConv::X86_FastCall:
2951 return !is64Bit;
2952 case CallingConv::X86_ThisCall:
2953 return !is64Bit;
2954 case CallingConv::Fast:
2955 return TailCallOpt;
2956 case CallingConv::GHC:
2957 return TailCallOpt;
2958 }
2959}
2960
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002961/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2962/// specific condition code, returning the condition code and the LHS/RHS of the
2963/// comparison to make.
2964static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2965 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002966 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002967 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2968 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2969 // X > -1 -> X == 0, jump !sign.
2970 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002971 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002972 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2973 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002974 return X86::COND_S;
Andrew Trickf6c39412011-03-23 23:11:02 +00002975 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002976 // X < 1 -> X <= 0
2977 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002978 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002979 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002980 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002981
Evan Chengd9558e02006-01-06 00:43:03 +00002982 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002983 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002984 case ISD::SETEQ: return X86::COND_E;
2985 case ISD::SETGT: return X86::COND_G;
2986 case ISD::SETGE: return X86::COND_GE;
2987 case ISD::SETLT: return X86::COND_L;
2988 case ISD::SETLE: return X86::COND_LE;
2989 case ISD::SETNE: return X86::COND_NE;
2990 case ISD::SETULT: return X86::COND_B;
2991 case ISD::SETUGT: return X86::COND_A;
2992 case ISD::SETULE: return X86::COND_BE;
2993 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002994 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002995 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002996
Chris Lattner4c78e022008-12-23 23:42:27 +00002997 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002998
Chris Lattner4c78e022008-12-23 23:42:27 +00002999 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00003000 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
3001 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00003002 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
3003 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00003004 }
3005
Chris Lattner4c78e022008-12-23 23:42:27 +00003006 switch (SetCCOpcode) {
3007 default: break;
3008 case ISD::SETOLT:
3009 case ISD::SETOLE:
3010 case ISD::SETUGT:
3011 case ISD::SETUGE:
3012 std::swap(LHS, RHS);
3013 break;
3014 }
3015
3016 // On a floating point condition, the flags are set as follows:
3017 // ZF PF CF op
3018 // 0 | 0 | 0 | X > Y
3019 // 0 | 0 | 1 | X < Y
3020 // 1 | 0 | 0 | X == Y
3021 // 1 | 1 | 1 | unordered
3022 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003023 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00003024 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003025 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00003026 case ISD::SETOLT: // flipped
3027 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003028 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00003029 case ISD::SETOLE: // flipped
3030 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003031 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003032 case ISD::SETUGT: // flipped
3033 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003034 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00003035 case ISD::SETUGE: // flipped
3036 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003037 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003038 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003039 case ISD::SETNE: return X86::COND_NE;
3040 case ISD::SETUO: return X86::COND_P;
3041 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003042 case ISD::SETOEQ:
3043 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003044 }
Evan Chengd9558e02006-01-06 00:43:03 +00003045}
3046
Evan Cheng4a460802006-01-11 00:33:36 +00003047/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3048/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003049/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003050static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003051 switch (X86CC) {
3052 default:
3053 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003054 case X86::COND_B:
3055 case X86::COND_BE:
3056 case X86::COND_E:
3057 case X86::COND_P:
3058 case X86::COND_A:
3059 case X86::COND_AE:
3060 case X86::COND_NE:
3061 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003062 return true;
3063 }
3064}
3065
Evan Chengeb2f9692009-10-27 19:56:55 +00003066/// isFPImmLegal - Returns true if the target can instruction select the
3067/// specified FP immediate natively. If false, the legalizer will
3068/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003069bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003070 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3071 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3072 return true;
3073 }
3074 return false;
3075}
3076
Nate Begeman9008ca62009-04-27 18:41:29 +00003077/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3078/// the specified range (L, H].
3079static bool isUndefOrInRange(int Val, int Low, int Hi) {
3080 return (Val < 0) || (Val >= Low && Val < Hi);
3081}
3082
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00003083/// isUndefOrInRange - Return true if every element in Mask, begining
3084/// from position Pos and ending in Pos+Size, falls within the specified
3085/// range (L, L+Pos]. or is undef.
3086static bool isUndefOrInRange(const SmallVectorImpl<int> &Mask,
3087 int Pos, int Size, int Low, int Hi) {
3088 for (int i = Pos, e = Pos+Size; i != e; ++i)
3089 if (!isUndefOrInRange(Mask[i], Low, Hi))
3090 return false;
3091 return true;
3092}
3093
Nate Begeman9008ca62009-04-27 18:41:29 +00003094/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3095/// specified value.
3096static bool isUndefOrEqual(int Val, int CmpVal) {
3097 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003098 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003099 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00003100}
3101
Bruno Cardoso Lopes4002d7e2011-08-12 21:54:42 +00003102/// isSequentialOrUndefInRange - Return true if every element in Mask, begining
3103/// from position Pos and ending in Pos+Size, falls within the specified
3104/// sequential range (L, L+Pos]. or is undef.
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003105static bool isSequentialOrUndefInRange(const SmallVectorImpl<int> &Mask,
3106 int Pos, int Size, int Low) {
3107 for (int i = Pos, e = Pos+Size; i != e; ++i, ++Low)
3108 if (!isUndefOrEqual(Mask[i], Low))
3109 return false;
3110 return true;
3111}
3112
Nate Begeman9008ca62009-04-27 18:41:29 +00003113/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3114/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3115/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00003116static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003117 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003118 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003119 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003120 return (Mask[0] < 2 && Mask[1] < 2);
3121 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003122}
3123
Nate Begeman9008ca62009-04-27 18:41:29 +00003124bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003125 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003126 N->getMask(M);
3127 return ::isPSHUFDMask(M, N->getValueType(0));
3128}
Evan Cheng0188ecb2006-03-22 18:59:22 +00003129
Nate Begeman9008ca62009-04-27 18:41:29 +00003130/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3131/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00003132static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003133 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00003134 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003135
Nate Begeman9008ca62009-04-27 18:41:29 +00003136 // Lower quadword copied in order or undef.
3137 for (int i = 0; i != 4; ++i)
3138 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00003139 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003140
Evan Cheng506d3df2006-03-29 23:07:14 +00003141 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003142 for (int i = 4; i != 8; ++i)
3143 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00003144 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003145
Evan Cheng506d3df2006-03-29 23:07:14 +00003146 return true;
3147}
3148
Nate Begeman9008ca62009-04-27 18:41:29 +00003149bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003150 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003151 N->getMask(M);
3152 return ::isPSHUFHWMask(M, N->getValueType(0));
3153}
Evan Cheng506d3df2006-03-29 23:07:14 +00003154
Nate Begeman9008ca62009-04-27 18:41:29 +00003155/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3156/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00003157static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003158 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00003159 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003160
Rafael Espindola15684b22009-04-24 12:40:33 +00003161 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00003162 for (int i = 4; i != 8; ++i)
3163 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00003164 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003165
Rafael Espindola15684b22009-04-24 12:40:33 +00003166 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003167 for (int i = 0; i != 4; ++i)
3168 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00003169 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003170
Rafael Espindola15684b22009-04-24 12:40:33 +00003171 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003172}
3173
Nate Begeman9008ca62009-04-27 18:41:29 +00003174bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003175 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003176 N->getMask(M);
3177 return ::isPSHUFLWMask(M, N->getValueType(0));
3178}
3179
Nate Begemana09008b2009-10-19 02:17:23 +00003180/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3181/// is suitable for input to PALIGNR.
3182static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00003183 bool hasSSSE3OrAVX) {
Nate Begemana09008b2009-10-19 02:17:23 +00003184 int i, e = VT.getVectorNumElements();
Bruno Cardoso Lopes9065d4b2011-07-29 01:30:59 +00003185 if (VT.getSizeInBits() != 128 && VT.getSizeInBits() != 64)
3186 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003187
Nate Begemana09008b2009-10-19 02:17:23 +00003188 // Do not handle v2i64 / v2f64 shuffles with palignr.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00003189 if (e < 4 || !hasSSSE3OrAVX)
Nate Begemana09008b2009-10-19 02:17:23 +00003190 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003191
Nate Begemana09008b2009-10-19 02:17:23 +00003192 for (i = 0; i != e; ++i)
3193 if (Mask[i] >= 0)
3194 break;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003195
Nate Begemana09008b2009-10-19 02:17:23 +00003196 // All undef, not a palignr.
3197 if (i == e)
3198 return false;
3199
Eli Friedman63f8dde2011-07-25 21:36:45 +00003200 // Make sure we're shifting in the right direction.
3201 if (Mask[i] <= i)
3202 return false;
Nate Begemana09008b2009-10-19 02:17:23 +00003203
3204 int s = Mask[i] - i;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003205
Nate Begemana09008b2009-10-19 02:17:23 +00003206 // Check the rest of the elements to see if they are consecutive.
3207 for (++i; i != e; ++i) {
3208 int m = Mask[i];
Eli Friedman63f8dde2011-07-25 21:36:45 +00003209 if (m >= 0 && m != s+i)
Nate Begemana09008b2009-10-19 02:17:23 +00003210 return false;
3211 }
3212 return true;
3213}
3214
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003215/// isVSHUFPSYMask - Return true if the specified VECTOR_SHUFFLE operand
3216/// specifies a shuffle of elements that is suitable for input to 256-bit
3217/// VSHUFPSY.
3218static bool isVSHUFPSYMask(const SmallVectorImpl<int> &Mask, EVT VT,
3219 const X86Subtarget *Subtarget) {
3220 int NumElems = VT.getVectorNumElements();
3221
3222 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256)
3223 return false;
3224
3225 if (NumElems != 8)
3226 return false;
3227
3228 // VSHUFPSY divides the resulting vector into 4 chunks.
3229 // The sources are also splitted into 4 chunks, and each destination
3230 // chunk must come from a different source chunk.
3231 //
3232 // SRC1 => X7 X6 X5 X4 X3 X2 X1 X0
3233 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3234 //
3235 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3236 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3237 //
3238 int QuarterSize = NumElems/4;
3239 int HalfSize = QuarterSize*2;
3240 for (int i = 0; i < QuarterSize; ++i)
3241 if (!isUndefOrInRange(Mask[i], 0, HalfSize))
3242 return false;
3243 for (int i = QuarterSize; i < QuarterSize*2; ++i)
3244 if (!isUndefOrInRange(Mask[i], NumElems, NumElems+HalfSize))
3245 return false;
3246
3247 // The mask of the second half must be the same as the first but with
3248 // the appropriate offsets. This works in the same way as VPERMILPS
3249 // works with masks.
3250 for (int i = QuarterSize*2; i < QuarterSize*3; ++i) {
3251 if (!isUndefOrInRange(Mask[i], HalfSize, NumElems))
3252 return false;
3253 int FstHalfIdx = i-HalfSize;
3254 if (Mask[FstHalfIdx] < 0)
3255 continue;
3256 if (!isUndefOrEqual(Mask[i], Mask[FstHalfIdx]+HalfSize))
3257 return false;
3258 }
3259 for (int i = QuarterSize*3; i < NumElems; ++i) {
3260 if (!isUndefOrInRange(Mask[i], NumElems+HalfSize, NumElems*2))
3261 return false;
3262 int FstHalfIdx = i-HalfSize;
3263 if (Mask[FstHalfIdx] < 0)
3264 continue;
3265 if (!isUndefOrEqual(Mask[i], Mask[FstHalfIdx]+HalfSize))
3266 return false;
3267
3268 }
3269
3270 return true;
3271}
3272
3273/// getShuffleVSHUFPSYImmediate - Return the appropriate immediate to shuffle
3274/// the specified VECTOR_MASK mask with VSHUFPSY instruction.
3275static unsigned getShuffleVSHUFPSYImmediate(SDNode *N) {
3276 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3277 EVT VT = SVOp->getValueType(0);
3278 int NumElems = VT.getVectorNumElements();
3279
3280 assert(NumElems == 8 && VT.getSizeInBits() == 256 &&
3281 "Only supports v8i32 and v8f32 types");
3282
3283 int HalfSize = NumElems/2;
3284 unsigned Mask = 0;
3285 for (int i = 0; i != NumElems ; ++i) {
3286 if (SVOp->getMaskElt(i) < 0)
3287 continue;
3288 // The mask of the first half must be equal to the second one.
3289 unsigned Shamt = (i%HalfSize)*2;
3290 unsigned Elt = SVOp->getMaskElt(i) % HalfSize;
3291 Mask |= Elt << Shamt;
3292 }
3293
3294 return Mask;
3295}
3296
3297/// isVSHUFPDYMask - Return true if the specified VECTOR_SHUFFLE operand
3298/// specifies a shuffle of elements that is suitable for input to 256-bit
3299/// VSHUFPDY. This shuffle doesn't have the same restriction as the PS
3300/// version and the mask of the second half isn't binded with the first
3301/// one.
3302static bool isVSHUFPDYMask(const SmallVectorImpl<int> &Mask, EVT VT,
3303 const X86Subtarget *Subtarget) {
3304 int NumElems = VT.getVectorNumElements();
3305
3306 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256)
3307 return false;
3308
3309 if (NumElems != 4)
3310 return false;
3311
3312 // VSHUFPSY divides the resulting vector into 4 chunks.
3313 // The sources are also splitted into 4 chunks, and each destination
3314 // chunk must come from a different source chunk.
3315 //
3316 // SRC1 => X3 X2 X1 X0
3317 // SRC2 => Y3 Y2 Y1 Y0
3318 //
3319 // DST => Y2..Y3, X2..X3, Y1..Y0, X1..X0
3320 //
3321 int QuarterSize = NumElems/4;
3322 int HalfSize = QuarterSize*2;
3323 for (int i = 0; i < QuarterSize; ++i)
3324 if (!isUndefOrInRange(Mask[i], 0, HalfSize))
3325 return false;
3326 for (int i = QuarterSize; i < QuarterSize*2; ++i)
3327 if (!isUndefOrInRange(Mask[i], NumElems, NumElems+HalfSize))
3328 return false;
3329 for (int i = QuarterSize*2; i < QuarterSize*3; ++i)
3330 if (!isUndefOrInRange(Mask[i], HalfSize, NumElems))
3331 return false;
3332 for (int i = QuarterSize*3; i < NumElems; ++i)
3333 if (!isUndefOrInRange(Mask[i], NumElems+HalfSize, NumElems*2))
3334 return false;
3335
3336 return true;
3337}
3338
3339/// getShuffleVSHUFPDYImmediate - Return the appropriate immediate to shuffle
3340/// the specified VECTOR_MASK mask with VSHUFPDY instruction.
3341static unsigned getShuffleVSHUFPDYImmediate(SDNode *N) {
3342 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3343 EVT VT = SVOp->getValueType(0);
3344 int NumElems = VT.getVectorNumElements();
3345
3346 assert(NumElems == 4 && VT.getSizeInBits() == 256 &&
3347 "Only supports v4i64 and v4f64 types");
3348
3349 int HalfSize = NumElems/2;
3350 unsigned Mask = 0;
3351 for (int i = 0; i != NumElems ; ++i) {
3352 if (SVOp->getMaskElt(i) < 0)
3353 continue;
3354 int Elt = SVOp->getMaskElt(i) % HalfSize;
3355 Mask |= Elt << i;
3356 }
3357
3358 return Mask;
3359}
3360
Evan Cheng14aed5e2006-03-24 01:18:28 +00003361/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopesaf002d82011-08-24 23:17:55 +00003362/// specifies a shuffle of elements that is suitable for input to 128-bit
3363/// SHUFPS and SHUFPD.
Owen Andersone50ed302009-08-10 22:56:29 +00003364static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003365 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesaf002d82011-08-24 23:17:55 +00003366
3367 if (VT.getSizeInBits() != 128)
3368 return false;
3369
Nate Begeman9008ca62009-04-27 18:41:29 +00003370 if (NumElems != 2 && NumElems != 4)
3371 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003372
Nate Begeman9008ca62009-04-27 18:41:29 +00003373 int Half = NumElems / 2;
3374 for (int i = 0; i < Half; ++i)
3375 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003376 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003377 for (int i = Half; i < NumElems; ++i)
3378 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003379 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003380
Evan Cheng14aed5e2006-03-24 01:18:28 +00003381 return true;
3382}
3383
Nate Begeman9008ca62009-04-27 18:41:29 +00003384bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
3385 SmallVector<int, 8> M;
3386 N->getMask(M);
3387 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003388}
3389
Evan Cheng213d2cf2007-05-17 18:45:50 +00003390/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00003391/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
3392/// half elements to come from vector 1 (which would equal the dest.) and
3393/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00003394static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003395 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003396
3397 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00003398 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003399
Nate Begeman9008ca62009-04-27 18:41:29 +00003400 int Half = NumElems / 2;
3401 for (int i = 0; i < Half; ++i)
3402 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003403 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003404 for (int i = Half; i < NumElems; ++i)
3405 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003406 return false;
3407 return true;
3408}
3409
Nate Begeman9008ca62009-04-27 18:41:29 +00003410static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
3411 SmallVector<int, 8> M;
3412 N->getMask(M);
3413 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003414}
3415
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003416/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3417/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00003418bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003419 EVT VT = N->getValueType(0);
3420 unsigned NumElems = VT.getVectorNumElements();
3421
3422 if (VT.getSizeInBits() != 128)
3423 return false;
3424
3425 if (NumElems != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003426 return false;
3427
Evan Cheng2064a2b2006-03-28 06:50:32 +00003428 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00003429 return isUndefOrEqual(N->getMaskElt(0), 6) &&
3430 isUndefOrEqual(N->getMaskElt(1), 7) &&
3431 isUndefOrEqual(N->getMaskElt(2), 2) &&
3432 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003433}
3434
Nate Begeman0b10b912009-11-07 23:17:15 +00003435/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3436/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3437/// <2, 3, 2, 3>
3438bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003439 EVT VT = N->getValueType(0);
3440 unsigned NumElems = VT.getVectorNumElements();
3441
3442 if (VT.getSizeInBits() != 128)
3443 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003444
Nate Begeman0b10b912009-11-07 23:17:15 +00003445 if (NumElems != 4)
3446 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003447
Nate Begeman0b10b912009-11-07 23:17:15 +00003448 return isUndefOrEqual(N->getMaskElt(0), 2) &&
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003449 isUndefOrEqual(N->getMaskElt(1), 3) &&
3450 isUndefOrEqual(N->getMaskElt(2), 2) &&
3451 isUndefOrEqual(N->getMaskElt(3), 3);
Nate Begeman0b10b912009-11-07 23:17:15 +00003452}
3453
Evan Cheng5ced1d82006-04-06 23:23:56 +00003454/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3455/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003456bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3457 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003458
Evan Cheng5ced1d82006-04-06 23:23:56 +00003459 if (NumElems != 2 && NumElems != 4)
3460 return false;
3461
Evan Chengc5cdff22006-04-07 21:53:05 +00003462 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003463 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003464 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003465
Evan Chengc5cdff22006-04-07 21:53:05 +00003466 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003467 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003468 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003469
3470 return true;
3471}
3472
Nate Begeman0b10b912009-11-07 23:17:15 +00003473/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3474/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3475bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003476 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003477
David Greenea20244d2011-03-02 17:23:43 +00003478 if ((NumElems != 2 && NumElems != 4)
3479 || N->getValueType(0).getSizeInBits() > 128)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003480 return false;
3481
Evan Chengc5cdff22006-04-07 21:53:05 +00003482 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003483 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003484 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003485
Nate Begeman9008ca62009-04-27 18:41:29 +00003486 for (unsigned i = 0; i < NumElems/2; ++i)
3487 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003488 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003489
3490 return true;
3491}
3492
Evan Cheng0038e592006-03-28 00:39:58 +00003493/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3494/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003495static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003496 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003497 int NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003498
3499 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3500 "Unsupported vector type for unpckh");
3501
3502 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8)
Evan Cheng0038e592006-03-28 00:39:58 +00003503 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003504
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003505 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3506 // independently on 128-bit lanes.
3507 unsigned NumLanes = VT.getSizeInBits()/128;
3508 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003509
3510 unsigned Start = 0;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003511 unsigned End = NumLaneElts;
3512 for (unsigned s = 0; s < NumLanes; ++s) {
3513 for (unsigned i = Start, j = s * NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003514 i != End;
3515 i += 2, ++j) {
3516 int BitI = Mask[i];
3517 int BitI1 = Mask[i+1];
3518 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003519 return false;
David Greenea20244d2011-03-02 17:23:43 +00003520 if (V2IsSplat) {
3521 if (!isUndefOrEqual(BitI1, NumElts))
3522 return false;
3523 } else {
3524 if (!isUndefOrEqual(BitI1, j + NumElts))
3525 return false;
3526 }
Evan Cheng39623da2006-04-20 08:58:49 +00003527 }
David Greenea20244d2011-03-02 17:23:43 +00003528 // Process the next 128 bits.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003529 Start += NumLaneElts;
3530 End += NumLaneElts;
Evan Cheng0038e592006-03-28 00:39:58 +00003531 }
David Greenea20244d2011-03-02 17:23:43 +00003532
Evan Cheng0038e592006-03-28 00:39:58 +00003533 return true;
3534}
3535
Nate Begeman9008ca62009-04-27 18:41:29 +00003536bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3537 SmallVector<int, 8> M;
3538 N->getMask(M);
3539 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003540}
3541
Evan Cheng4fcb9222006-03-28 02:43:26 +00003542/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3543/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003544static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003545 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003546 int NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003547
3548 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3549 "Unsupported vector type for unpckh");
3550
3551 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8)
Evan Cheng4fcb9222006-03-28 02:43:26 +00003552 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003553
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003554 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3555 // independently on 128-bit lanes.
3556 unsigned NumLanes = VT.getSizeInBits()/128;
3557 unsigned NumLaneElts = NumElts/NumLanes;
3558
3559 unsigned Start = 0;
3560 unsigned End = NumLaneElts;
3561 for (unsigned l = 0; l != NumLanes; ++l) {
3562 for (unsigned i = Start, j = (l*NumLaneElts)+NumLaneElts/2;
3563 i != End; i += 2, ++j) {
3564 int BitI = Mask[i];
3565 int BitI1 = Mask[i+1];
3566 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003567 return false;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003568 if (V2IsSplat) {
3569 if (isUndefOrEqual(BitI1, NumElts))
3570 return false;
3571 } else {
3572 if (!isUndefOrEqual(BitI1, j+NumElts))
3573 return false;
3574 }
Evan Cheng39623da2006-04-20 08:58:49 +00003575 }
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003576 // Process the next 128 bits.
3577 Start += NumLaneElts;
3578 End += NumLaneElts;
Evan Cheng4fcb9222006-03-28 02:43:26 +00003579 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003580 return true;
3581}
3582
Nate Begeman9008ca62009-04-27 18:41:29 +00003583bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3584 SmallVector<int, 8> M;
3585 N->getMask(M);
3586 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003587}
3588
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003589/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3590/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3591/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003592static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003593 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003594 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003595 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003596
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003597 // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern
3598 // FIXME: Need a better way to get rid of this, there's no latency difference
3599 // between UNPCKLPD and MOVDDUP, the later should always be checked first and
3600 // the former later. We should also remove the "_undef" special mask.
3601 if (NumElems == 4 && VT.getSizeInBits() == 256)
3602 return false;
3603
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003604 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3605 // independently on 128-bit lanes.
3606 unsigned NumLanes = VT.getSizeInBits() / 128;
3607 unsigned NumLaneElts = NumElems / NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003608
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003609 for (unsigned s = 0; s < NumLanes; ++s) {
3610 for (unsigned i = s * NumLaneElts, j = s * NumLaneElts;
3611 i != NumLaneElts * (s + 1);
David Greenea20244d2011-03-02 17:23:43 +00003612 i += 2, ++j) {
3613 int BitI = Mask[i];
3614 int BitI1 = Mask[i+1];
3615
3616 if (!isUndefOrEqual(BitI, j))
3617 return false;
3618 if (!isUndefOrEqual(BitI1, j))
3619 return false;
3620 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003621 }
David Greenea20244d2011-03-02 17:23:43 +00003622
Rafael Espindola15684b22009-04-24 12:40:33 +00003623 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003624}
3625
Nate Begeman9008ca62009-04-27 18:41:29 +00003626bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3627 SmallVector<int, 8> M;
3628 N->getMask(M);
3629 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3630}
3631
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003632/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3633/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3634/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003635static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003636 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003637 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3638 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003639
Nate Begeman9008ca62009-04-27 18:41:29 +00003640 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3641 int BitI = Mask[i];
3642 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003643 if (!isUndefOrEqual(BitI, j))
3644 return false;
3645 if (!isUndefOrEqual(BitI1, j))
3646 return false;
3647 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003648 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003649}
3650
Nate Begeman9008ca62009-04-27 18:41:29 +00003651bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3652 SmallVector<int, 8> M;
3653 N->getMask(M);
3654 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3655}
3656
Evan Cheng017dcc62006-04-21 01:05:10 +00003657/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3658/// specifies a shuffle of elements that is suitable for input to MOVSS,
3659/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003660static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003661 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003662 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003663
3664 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003665
Nate Begeman9008ca62009-04-27 18:41:29 +00003666 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003667 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003668
Nate Begeman9008ca62009-04-27 18:41:29 +00003669 for (int i = 1; i < NumElts; ++i)
3670 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003671 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003672
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003673 return true;
3674}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003675
Nate Begeman9008ca62009-04-27 18:41:29 +00003676bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3677 SmallVector<int, 8> M;
3678 N->getMask(M);
3679 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003680}
3681
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003682/// isVPERM2F128Mask - Match 256-bit shuffles where the elements are considered
3683/// as permutations between 128-bit chunks or halves. As an example: this
3684/// shuffle bellow:
3685/// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
3686/// The first half comes from the second half of V1 and the second half from the
3687/// the second half of V2.
3688static bool isVPERM2F128Mask(const SmallVectorImpl<int> &Mask, EVT VT,
3689 const X86Subtarget *Subtarget) {
3690 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256)
3691 return false;
3692
3693 // The shuffle result is divided into half A and half B. In total the two
3694 // sources have 4 halves, namely: C, D, E, F. The final values of A and
3695 // B must come from C, D, E or F.
3696 int HalfSize = VT.getVectorNumElements()/2;
3697 bool MatchA = false, MatchB = false;
3698
3699 // Check if A comes from one of C, D, E, F.
3700 for (int Half = 0; Half < 4; ++Half) {
3701 if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
3702 MatchA = true;
3703 break;
3704 }
3705 }
3706
3707 // Check if B comes from one of C, D, E, F.
3708 for (int Half = 0; Half < 4; ++Half) {
3709 if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) {
3710 MatchB = true;
3711 break;
3712 }
3713 }
3714
3715 return MatchA && MatchB;
3716}
3717
3718/// getShuffleVPERM2F128Immediate - Return the appropriate immediate to shuffle
3719/// the specified VECTOR_MASK mask with VPERM2F128 instructions.
3720static unsigned getShuffleVPERM2F128Immediate(SDNode *N) {
3721 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3722 EVT VT = SVOp->getValueType(0);
3723
3724 int HalfSize = VT.getVectorNumElements()/2;
3725
3726 int FstHalf = 0, SndHalf = 0;
3727 for (int i = 0; i < HalfSize; ++i) {
3728 if (SVOp->getMaskElt(i) > 0) {
3729 FstHalf = SVOp->getMaskElt(i)/HalfSize;
3730 break;
3731 }
3732 }
3733 for (int i = HalfSize; i < HalfSize*2; ++i) {
3734 if (SVOp->getMaskElt(i) > 0) {
3735 SndHalf = SVOp->getMaskElt(i)/HalfSize;
3736 break;
3737 }
3738 }
3739
3740 return (FstHalf | (SndHalf << 4));
3741}
3742
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003743/// isVPERMILPDMask - Return true if the specified VECTOR_SHUFFLE operand
3744/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
3745/// Note that VPERMIL mask matching is different depending whether theunderlying
3746/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3747/// to the same elements of the low, but to the higher half of the source.
3748/// In VPERMILPD the two lanes could be shuffled independently of each other
3749/// with the same restriction that lanes can't be crossed.
3750static bool isVPERMILPDMask(const SmallVectorImpl<int> &Mask, EVT VT,
3751 const X86Subtarget *Subtarget) {
3752 int NumElts = VT.getVectorNumElements();
3753 int NumLanes = VT.getSizeInBits()/128;
3754
3755 if (!Subtarget->hasAVX())
3756 return false;
3757
3758 // Match any permutation of 128-bit vector with 64-bit types
3759 if (NumLanes == 1 && NumElts != 2)
3760 return false;
3761
3762 // Only match 256-bit with 32 types
3763 if (VT.getSizeInBits() == 256 && NumElts != 4)
3764 return false;
3765
3766 // The mask on the high lane is independent of the low. Both can match
3767 // any element in inside its own lane, but can't cross.
3768 int LaneSize = NumElts/NumLanes;
3769 for (int l = 0; l < NumLanes; ++l)
3770 for (int i = l*LaneSize; i < LaneSize*(l+1); ++i) {
3771 int LaneStart = l*LaneSize;
3772 if (!isUndefOrInRange(Mask[i], LaneStart, LaneStart+LaneSize))
3773 return false;
3774 }
3775
3776 return true;
3777}
3778
3779/// isVPERMILPSMask - Return true if the specified VECTOR_SHUFFLE operand
3780/// specifies a shuffle of elements that is suitable for input to VPERMILPS*.
3781/// Note that VPERMIL mask matching is different depending whether theunderlying
3782/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3783/// to the same elements of the low, but to the higher half of the source.
3784/// In VPERMILPD the two lanes could be shuffled independently of each other
3785/// with the same restriction that lanes can't be crossed.
3786static bool isVPERMILPSMask(const SmallVectorImpl<int> &Mask, EVT VT,
3787 const X86Subtarget *Subtarget) {
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003788 unsigned NumElts = VT.getVectorNumElements();
3789 unsigned NumLanes = VT.getSizeInBits()/128;
3790
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003791 if (!Subtarget->hasAVX())
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003792 return false;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003793
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003794 // Match any permutation of 128-bit vector with 32-bit types
3795 if (NumLanes == 1 && NumElts != 4)
3796 return false;
3797
3798 // Only match 256-bit with 32 types
3799 if (VT.getSizeInBits() == 256 && NumElts != 8)
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003800 return false;
3801
3802 // The mask on the high lane should be the same as the low. Actually,
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003803 // they can differ if any of the corresponding index in a lane is undef
3804 // and the other stays in range.
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003805 int LaneSize = NumElts/NumLanes;
3806 for (int i = 0; i < LaneSize; ++i) {
3807 int HighElt = i+LaneSize;
Bruno Cardoso Lopes155a92a2011-08-10 01:54:17 +00003808 bool HighValid = isUndefOrInRange(Mask[HighElt], LaneSize, NumElts);
3809 bool LowValid = isUndefOrInRange(Mask[i], 0, LaneSize);
3810
3811 if (!HighValid || !LowValid)
3812 return false;
3813 if (Mask[i] < 0 || Mask[HighElt] < 0)
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003814 continue;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003815 if (Mask[HighElt]-Mask[i] != LaneSize)
3816 return false;
3817 }
3818
3819 return true;
3820}
3821
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003822/// getShuffleVPERMILPSImmediate - Return the appropriate immediate to shuffle
3823/// the specified VECTOR_MASK mask with VPERMILPS* instructions.
3824static unsigned getShuffleVPERMILPSImmediate(SDNode *N) {
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003825 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3826 EVT VT = SVOp->getValueType(0);
3827
3828 int NumElts = VT.getVectorNumElements();
3829 int NumLanes = VT.getSizeInBits()/128;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003830 int LaneSize = NumElts/NumLanes;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003831
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003832 // Although the mask is equal for both lanes do it twice to get the cases
3833 // where a mask will match because the same mask element is undef on the
3834 // first half but valid on the second. This would get pathological cases
3835 // such as: shuffle <u, 0, 1, 2, 4, 4, 5, 6>, which is completely valid.
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003836 unsigned Mask = 0;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003837 for (int l = 0; l < NumLanes; ++l) {
3838 for (int i = 0; i < LaneSize; ++i) {
3839 int MaskElt = SVOp->getMaskElt(i+(l*LaneSize));
3840 if (MaskElt < 0)
3841 continue;
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003842 if (MaskElt >= LaneSize)
3843 MaskElt -= LaneSize;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003844 Mask |= MaskElt << (i*2);
3845 }
Bruno Cardoso Lopes377baa52011-07-29 01:31:04 +00003846 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003847
3848 return Mask;
3849}
3850
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003851/// getShuffleVPERMILPDImmediate - Return the appropriate immediate to shuffle
3852/// the specified VECTOR_MASK mask with VPERMILPD* instructions.
3853static unsigned getShuffleVPERMILPDImmediate(SDNode *N) {
3854 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3855 EVT VT = SVOp->getValueType(0);
3856
3857 int NumElts = VT.getVectorNumElements();
3858 int NumLanes = VT.getSizeInBits()/128;
3859
3860 unsigned Mask = 0;
3861 int LaneSize = NumElts/NumLanes;
3862 for (int l = 0; l < NumLanes; ++l)
Bruno Cardoso Lopes377baa52011-07-29 01:31:04 +00003863 for (int i = l*LaneSize; i < LaneSize*(l+1); ++i) {
3864 int MaskElt = SVOp->getMaskElt(i);
3865 if (MaskElt < 0)
3866 continue;
3867 Mask |= (MaskElt-l*LaneSize) << i;
3868 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003869
3870 return Mask;
3871}
3872
Evan Cheng017dcc62006-04-21 01:05:10 +00003873/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3874/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003875/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003876static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003877 bool V2IsSplat = false, bool V2IsUndef = false) {
3878 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003879 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003880 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003881
Nate Begeman9008ca62009-04-27 18:41:29 +00003882 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003883 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003884
Nate Begeman9008ca62009-04-27 18:41:29 +00003885 for (int i = 1; i < NumOps; ++i)
3886 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3887 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3888 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003889 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003890
Evan Cheng39623da2006-04-20 08:58:49 +00003891 return true;
3892}
3893
Nate Begeman9008ca62009-04-27 18:41:29 +00003894static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003895 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003896 SmallVector<int, 8> M;
3897 N->getMask(M);
3898 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003899}
3900
Evan Chengd9539472006-04-14 21:59:03 +00003901/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3902/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003903/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
3904bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N,
3905 const X86Subtarget *Subtarget) {
3906 if (!Subtarget->hasSSE3() && !Subtarget->hasAVX())
Evan Chengd9539472006-04-14 21:59:03 +00003907 return false;
3908
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003909 // The second vector must be undef
3910 if (N->getOperand(1).getOpcode() != ISD::UNDEF)
3911 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003912
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003913 EVT VT = N->getValueType(0);
3914 unsigned NumElems = VT.getVectorNumElements();
3915
3916 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3917 (VT.getSizeInBits() == 256 && NumElems != 8))
3918 return false;
3919
3920 // "i+1" is the value the indexed mask element must have
3921 for (unsigned i = 0; i < NumElems; i += 2)
3922 if (!isUndefOrEqual(N->getMaskElt(i), i+1) ||
3923 !isUndefOrEqual(N->getMaskElt(i+1), i+1))
Nate Begeman9008ca62009-04-27 18:41:29 +00003924 return false;
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003925
3926 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003927}
3928
3929/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3930/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003931/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
3932bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N,
3933 const X86Subtarget *Subtarget) {
3934 if (!Subtarget->hasSSE3() && !Subtarget->hasAVX())
Evan Chengd9539472006-04-14 21:59:03 +00003935 return false;
3936
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003937 // The second vector must be undef
3938 if (N->getOperand(1).getOpcode() != ISD::UNDEF)
3939 return false;
3940
3941 EVT VT = N->getValueType(0);
3942 unsigned NumElems = VT.getVectorNumElements();
3943
3944 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3945 (VT.getSizeInBits() == 256 && NumElems != 8))
3946 return false;
3947
3948 // "i" is the value the indexed mask element must have
3949 for (unsigned i = 0; i < NumElems; i += 2)
3950 if (!isUndefOrEqual(N->getMaskElt(i), i) ||
3951 !isUndefOrEqual(N->getMaskElt(i+1), i))
Nate Begeman9008ca62009-04-27 18:41:29 +00003952 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003953
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003954 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003955}
3956
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003957/// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand
3958/// specifies a shuffle of elements that is suitable for input to 256-bit
3959/// version of MOVDDUP.
3960static bool isMOVDDUPYMask(ShuffleVectorSDNode *N,
3961 const X86Subtarget *Subtarget) {
3962 EVT VT = N->getValueType(0);
3963 int NumElts = VT.getVectorNumElements();
3964 bool V2IsUndef = N->getOperand(1).getOpcode() == ISD::UNDEF;
3965
3966 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256 ||
3967 !V2IsUndef || NumElts != 4)
3968 return false;
3969
3970 for (int i = 0; i != NumElts/2; ++i)
3971 if (!isUndefOrEqual(N->getMaskElt(i), 0))
3972 return false;
3973 for (int i = NumElts/2; i != NumElts; ++i)
3974 if (!isUndefOrEqual(N->getMaskElt(i), NumElts/2))
3975 return false;
3976 return true;
3977}
3978
Evan Cheng0b457f02008-09-25 20:50:48 +00003979/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003980/// specifies a shuffle of elements that is suitable for input to 128-bit
3981/// version of MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003982bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003983 EVT VT = N->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00003984
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003985 if (VT.getSizeInBits() != 128)
3986 return false;
3987
3988 int e = VT.getVectorNumElements() / 2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003989 for (int i = 0; i < e; ++i)
3990 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003991 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003992 for (int i = 0; i < e; ++i)
3993 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003994 return false;
3995 return true;
3996}
3997
David Greenec38a03e2011-02-03 15:50:00 +00003998/// isVEXTRACTF128Index - Return true if the specified
3999/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
4000/// suitable for input to VEXTRACTF128.
4001bool X86::isVEXTRACTF128Index(SDNode *N) {
4002 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4003 return false;
4004
4005 // The index should be aligned on a 128-bit boundary.
4006 uint64_t Index =
4007 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4008
4009 unsigned VL = N->getValueType(0).getVectorNumElements();
4010 unsigned VBits = N->getValueType(0).getSizeInBits();
4011 unsigned ElSize = VBits / VL;
4012 bool Result = (Index * ElSize) % 128 == 0;
4013
4014 return Result;
4015}
4016
David Greeneccacdc12011-02-04 16:08:29 +00004017/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
4018/// operand specifies a subvector insert that is suitable for input to
4019/// VINSERTF128.
4020bool X86::isVINSERTF128Index(SDNode *N) {
4021 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4022 return false;
4023
4024 // The index should be aligned on a 128-bit boundary.
4025 uint64_t Index =
4026 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
4027
4028 unsigned VL = N->getValueType(0).getVectorNumElements();
4029 unsigned VBits = N->getValueType(0).getSizeInBits();
4030 unsigned ElSize = VBits / VL;
4031 bool Result = (Index * ElSize) % 128 == 0;
4032
4033 return Result;
4034}
4035
Evan Cheng63d33002006-03-22 08:01:21 +00004036/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004037/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00004038unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004039 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
4040 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
4041
Evan Chengb9df0ca2006-03-22 02:53:00 +00004042 unsigned Shift = (NumOperands == 4) ? 2 : 1;
4043 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00004044 for (int i = 0; i < NumOperands; ++i) {
4045 int Val = SVOp->getMaskElt(NumOperands-i-1);
4046 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00004047 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00004048 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00004049 if (i != NumOperands - 1)
4050 Mask <<= Shift;
4051 }
Evan Cheng63d33002006-03-22 08:01:21 +00004052 return Mask;
4053}
4054
Evan Cheng506d3df2006-03-29 23:07:14 +00004055/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004056/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00004057unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004058 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00004059 unsigned Mask = 0;
4060 // 8 nodes, but we only care about the last 4.
4061 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004062 int Val = SVOp->getMaskElt(i);
4063 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00004064 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00004065 if (i != 4)
4066 Mask <<= 2;
4067 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004068 return Mask;
4069}
4070
4071/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004072/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00004073unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004074 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00004075 unsigned Mask = 0;
4076 // 8 nodes, but we only care about the first 4.
4077 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004078 int Val = SVOp->getMaskElt(i);
4079 if (Val >= 0)
4080 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00004081 if (i != 0)
4082 Mask <<= 2;
4083 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004084 return Mask;
4085}
4086
Nate Begemana09008b2009-10-19 02:17:23 +00004087/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
4088/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
4089unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
4090 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
4091 EVT VVT = N->getValueType(0);
4092 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
4093 int Val = 0;
4094
4095 unsigned i, e;
4096 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
4097 Val = SVOp->getMaskElt(i);
4098 if (Val >= 0)
4099 break;
4100 }
Eli Friedman63f8dde2011-07-25 21:36:45 +00004101 assert(Val - i > 0 && "PALIGNR imm should be positive");
Nate Begemana09008b2009-10-19 02:17:23 +00004102 return (Val - i) * EltSize;
4103}
4104
David Greenec38a03e2011-02-03 15:50:00 +00004105/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
4106/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
4107/// instructions.
4108unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
4109 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4110 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
4111
4112 uint64_t Index =
4113 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4114
4115 EVT VecVT = N->getOperand(0).getValueType();
4116 EVT ElVT = VecVT.getVectorElementType();
4117
4118 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004119 return Index / NumElemsPerChunk;
4120}
4121
David Greeneccacdc12011-02-04 16:08:29 +00004122/// getInsertVINSERTF128Immediate - Return the appropriate immediate
4123/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
4124/// instructions.
4125unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
4126 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4127 llvm_unreachable("Illegal insert subvector for VINSERTF128");
4128
4129 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00004130 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00004131
4132 EVT VecVT = N->getValueType(0);
4133 EVT ElVT = VecVT.getVectorElementType();
4134
4135 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004136 return Index / NumElemsPerChunk;
4137}
4138
Evan Cheng37b73872009-07-30 08:33:02 +00004139/// isZeroNode - Returns true if Elt is a constant zero or a floating point
4140/// constant +0.0.
4141bool X86::isZeroNode(SDValue Elt) {
4142 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00004143 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00004144 (isa<ConstantFPSDNode>(Elt) &&
4145 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
4146}
4147
Nate Begeman9008ca62009-04-27 18:41:29 +00004148/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
4149/// their permute mask.
4150static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
4151 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004152 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004153 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004154 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00004155
Nate Begeman5a5ca152009-04-29 05:20:52 +00004156 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004157 int idx = SVOp->getMaskElt(i);
4158 if (idx < 0)
4159 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004160 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00004161 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004162 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004163 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004164 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004165 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
4166 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004167}
4168
Evan Cheng779ccea2007-12-07 21:30:01 +00004169/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
4170/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00004171static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00004172 unsigned NumElems = VT.getVectorNumElements();
4173 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004174 int idx = Mask[i];
4175 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004176 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004177 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00004178 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004179 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004180 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004181 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004182}
4183
Evan Cheng533a0aa2006-04-19 20:35:22 +00004184/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
4185/// match movhlps. The lower half elements should come from upper half of
4186/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004187/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00004188static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004189 EVT VT = Op->getValueType(0);
4190 if (VT.getSizeInBits() != 128)
4191 return false;
4192 if (VT.getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00004193 return false;
4194 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004195 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004196 return false;
4197 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004198 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004199 return false;
4200 return true;
4201}
4202
Evan Cheng5ced1d82006-04-06 23:23:56 +00004203/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00004204/// is promoted to a vector. It also returns the LoadSDNode by reference if
4205/// required.
4206static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00004207 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
4208 return false;
4209 N = N->getOperand(0).getNode();
4210 if (!ISD::isNON_EXTLoad(N))
4211 return false;
4212 if (LD)
4213 *LD = cast<LoadSDNode>(N);
4214 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004215}
4216
Evan Cheng533a0aa2006-04-19 20:35:22 +00004217/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4218/// match movlp{s|d}. The lower half elements should come from lower half of
4219/// V1 (and in order), and the upper half elements should come from the upper
4220/// half of V2 (and in order). And since V1 will become the source of the
4221/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004222static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
4223 ShuffleVectorSDNode *Op) {
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004224 EVT VT = Op->getValueType(0);
4225 if (VT.getSizeInBits() != 128)
4226 return false;
4227
Evan Cheng466685d2006-10-09 20:57:25 +00004228 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004229 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00004230 // Is V2 is a vector load, don't do this transformation. We will try to use
4231 // load folding shufps op.
4232 if (ISD::isNON_EXTLoad(V2))
4233 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004234
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004235 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004236
Evan Cheng533a0aa2006-04-19 20:35:22 +00004237 if (NumElems != 2 && NumElems != 4)
4238 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004239 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004240 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004241 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004242 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004243 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004244 return false;
4245 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004246}
4247
Evan Cheng39623da2006-04-20 08:58:49 +00004248/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4249/// all the same.
4250static bool isSplatVector(SDNode *N) {
4251 if (N->getOpcode() != ISD::BUILD_VECTOR)
4252 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004253
Dan Gohman475871a2008-07-27 21:46:04 +00004254 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00004255 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4256 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00004257 return false;
4258 return true;
4259}
4260
Evan Cheng213d2cf2007-05-17 18:45:50 +00004261/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00004262/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00004263/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00004264static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00004265 SDValue V1 = N->getOperand(0);
4266 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004267 unsigned NumElems = N->getValueType(0).getVectorNumElements();
4268 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004269 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004270 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004271 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00004272 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4273 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004274 if (Opc != ISD::BUILD_VECTOR ||
4275 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00004276 return false;
4277 } else if (Idx >= 0) {
4278 unsigned Opc = V1.getOpcode();
4279 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4280 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004281 if (Opc != ISD::BUILD_VECTOR ||
4282 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00004283 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00004284 }
4285 }
4286 return true;
4287}
4288
4289/// getZeroVector - Returns a vector of specified type with all zero elements.
4290///
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004291static SDValue getZeroVector(EVT VT, bool HasXMMInt, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00004292 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004293 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004294
Dale Johannesen0488fb62010-09-30 23:57:10 +00004295 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004296 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00004297 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00004298 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004299 if (HasXMMInt) { // SSE2
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004300 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4301 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4302 } else { // SSE1
4303 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4304 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4305 }
4306 } else if (VT.getSizeInBits() == 256) { // AVX
4307 // 256-bit logic and arithmetic instructions in AVX are
4308 // all floating-point, no support for integer ops. Default
4309 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00004310 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004311 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4312 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00004313 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004314 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004315}
4316
Chris Lattner8a594482007-11-25 00:24:49 +00004317/// getOnesVector - Returns a vector of specified type with all bits set.
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004318/// Always build ones vectors as <4 x i32>. For 256-bit types, use two
4319/// <4 x i32> inserted in a <8 x i32> appropriately. Then bitcast to their
4320/// original type, ensuring they get CSE'd.
Owen Andersone50ed302009-08-10 22:56:29 +00004321static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004322 assert(VT.isVector() && "Expected a vector type");
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004323 assert((VT.is128BitVector() || VT.is256BitVector())
4324 && "Expected a 128-bit or 256-bit vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004325
Owen Anderson825b72b2009-08-11 20:47:22 +00004326 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004327 SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
4328 Cst, Cst, Cst, Cst);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004329
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004330 if (VT.is256BitVector()) {
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004331 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, MVT::v8i32),
4332 Vec, DAG.getConstant(0, MVT::i32), DAG, dl);
4333 Vec = Insert128BitVector(InsV, Vec,
4334 DAG.getConstant(4 /* NumElems/2 */, MVT::i32), DAG, dl);
4335 }
4336
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004337 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00004338}
4339
Evan Cheng39623da2006-04-20 08:58:49 +00004340/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4341/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00004342static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004343 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004344 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004345
Evan Cheng39623da2006-04-20 08:58:49 +00004346 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00004347 SmallVector<int, 8> MaskVec;
4348 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00004349
Nate Begeman5a5ca152009-04-29 05:20:52 +00004350 for (unsigned i = 0; i != NumElems; ++i) {
4351 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004352 MaskVec[i] = NumElems;
4353 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00004354 }
Evan Cheng39623da2006-04-20 08:58:49 +00004355 }
Evan Cheng39623da2006-04-20 08:58:49 +00004356 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00004357 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
4358 SVOp->getOperand(1), &MaskVec[0]);
4359 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00004360}
4361
Evan Cheng017dcc62006-04-21 01:05:10 +00004362/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4363/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00004364static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004365 SDValue V2) {
4366 unsigned NumElems = VT.getVectorNumElements();
4367 SmallVector<int, 8> Mask;
4368 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00004369 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004370 Mask.push_back(i);
4371 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00004372}
4373
Nate Begeman9008ca62009-04-27 18:41:29 +00004374/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004375static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004376 SDValue V2) {
4377 unsigned NumElems = VT.getVectorNumElements();
4378 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00004379 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004380 Mask.push_back(i);
4381 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00004382 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004383 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00004384}
4385
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004386/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004387static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004388 SDValue V2) {
4389 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00004390 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00004391 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00004392 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004393 Mask.push_back(i + Half);
4394 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00004395 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004396 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004397}
4398
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004399// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004400// a generic shuffle instruction because the target has no such instructions.
4401// Generate shuffles which repeat i16 and i8 several times until they can be
4402// represented by v4f32 and then be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004403static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004404 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004405 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004406 DebugLoc dl = V.getDebugLoc();
Rafael Espindola15684b22009-04-24 12:40:33 +00004407
Nate Begeman9008ca62009-04-27 18:41:29 +00004408 while (NumElems > 4) {
4409 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004410 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004411 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004412 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004413 EltNo -= NumElems/2;
4414 }
4415 NumElems >>= 1;
4416 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004417 return V;
4418}
Eric Christopherfd179292009-08-27 18:07:15 +00004419
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004420/// getLegalSplat - Generate a legal splat with supported x86 shuffles
4421static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
4422 EVT VT = V.getValueType();
4423 DebugLoc dl = V.getDebugLoc();
4424 assert((VT.getSizeInBits() == 128 || VT.getSizeInBits() == 256)
4425 && "Vector size not supported");
4426
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004427 if (VT.getSizeInBits() == 128) {
4428 V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004429 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004430 V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
4431 &SplatMask[0]);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004432 } else {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004433 // To use VPERMILPS to splat scalars, the second half of indicies must
4434 // refer to the higher part, which is a duplication of the lower one,
4435 // because VPERMILPS can only handle in-lane permutations.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004436 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
4437 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004438
4439 V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
4440 V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
4441 &SplatMask[0]);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004442 }
4443
4444 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4445}
4446
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004447/// PromoteSplat - Splat is promoted to target supported vector shuffles.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004448static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4449 EVT SrcVT = SV->getValueType(0);
4450 SDValue V1 = SV->getOperand(0);
4451 DebugLoc dl = SV->getDebugLoc();
4452
4453 int EltNo = SV->getSplatIndex();
4454 int NumElems = SrcVT.getVectorNumElements();
4455 unsigned Size = SrcVT.getSizeInBits();
4456
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004457 assert(((Size == 128 && NumElems > 4) || Size == 256) &&
4458 "Unknown how to promote splat for type");
4459
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004460 // Extract the 128-bit part containing the splat element and update
4461 // the splat element index when it refers to the higher register.
4462 if (Size == 256) {
4463 unsigned Idx = (EltNo > NumElems/2) ? NumElems/2 : 0;
4464 V1 = Extract128BitVector(V1, DAG.getConstant(Idx, MVT::i32), DAG, dl);
4465 if (Idx > 0)
4466 EltNo -= NumElems/2;
4467 }
4468
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004469 // All i16 and i8 vector types can't be used directly by a generic shuffle
4470 // instruction because the target has no such instruction. Generate shuffles
4471 // which repeat i16 and i8 several times until they fit in i32, and then can
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004472 // be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004473 EVT EltVT = SrcVT.getVectorElementType();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004474 if (EltVT == MVT::i8 || EltVT == MVT::i16)
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004475 V1 = PromoteSplati8i16(V1, DAG, EltNo);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004476
4477 // Recreate the 256-bit vector and place the same 128-bit vector
4478 // into the low and high part. This is necessary because we want
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004479 // to use VPERM* to shuffle the vectors
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004480 if (Size == 256) {
4481 SDValue InsV = Insert128BitVector(DAG.getUNDEF(SrcVT), V1,
4482 DAG.getConstant(0, MVT::i32), DAG, dl);
4483 V1 = Insert128BitVector(InsV, V1,
4484 DAG.getConstant(NumElems/2, MVT::i32), DAG, dl);
4485 }
4486
4487 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004488}
4489
Evan Chengba05f722006-04-21 23:03:30 +00004490/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004491/// vector of zero or undef vector. This produces a shuffle where the low
4492/// element of V2 is swizzled into the zero/undef vector, landing at element
4493/// 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 +00004494static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004495 bool isZero, bool HasXMMInt,
4496 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004497 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00004498 SDValue V1 = isZero
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004499 ? getZeroVector(VT, HasXMMInt, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00004500 unsigned NumElems = VT.getVectorNumElements();
4501 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004502 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004503 // If this is the insertion idx, put the low elt of V2 here.
4504 MaskVec.push_back(i == Idx ? NumElems : i);
4505 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004506}
4507
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004508/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4509/// element of the result of the vector shuffle.
Benjamin Kramer050db522011-03-26 12:38:19 +00004510static SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
4511 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004512 if (Depth == 6)
4513 return SDValue(); // Limit search depth.
4514
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004515 SDValue V = SDValue(N, 0);
4516 EVT VT = V.getValueType();
4517 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004518
4519 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4520 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
4521 Index = SV->getMaskElt(Index);
4522
4523 if (Index < 0)
4524 return DAG.getUNDEF(VT.getVectorElementType());
4525
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004526 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004527 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004528 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004529 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004530
4531 // Recurse into target specific vector shuffles to find scalars.
4532 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004533 int NumElems = VT.getVectorNumElements();
4534 SmallVector<unsigned, 16> ShuffleMask;
4535 SDValue ImmN;
4536
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004537 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004538 case X86ISD::SHUFPS:
4539 case X86ISD::SHUFPD:
4540 ImmN = N->getOperand(N->getNumOperands()-1);
4541 DecodeSHUFPSMask(NumElems,
4542 cast<ConstantSDNode>(ImmN)->getZExtValue(),
4543 ShuffleMask);
4544 break;
4545 case X86ISD::PUNPCKHBW:
4546 case X86ISD::PUNPCKHWD:
4547 case X86ISD::PUNPCKHDQ:
4548 case X86ISD::PUNPCKHQDQ:
4549 DecodePUNPCKHMask(NumElems, ShuffleMask);
4550 break;
4551 case X86ISD::UNPCKHPS:
4552 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00004553 case X86ISD::VUNPCKHPSY:
4554 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004555 DecodeUNPCKHPMask(NumElems, ShuffleMask);
4556 break;
4557 case X86ISD::PUNPCKLBW:
4558 case X86ISD::PUNPCKLWD:
4559 case X86ISD::PUNPCKLDQ:
4560 case X86ISD::PUNPCKLQDQ:
David Greenec4db4e52011-02-28 19:06:56 +00004561 DecodePUNPCKLMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004562 break;
4563 case X86ISD::UNPCKLPS:
4564 case X86ISD::UNPCKLPD:
David Greenec4db4e52011-02-28 19:06:56 +00004565 case X86ISD::VUNPCKLPSY:
4566 case X86ISD::VUNPCKLPDY:
4567 DecodeUNPCKLPMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004568 break;
4569 case X86ISD::MOVHLPS:
4570 DecodeMOVHLPSMask(NumElems, ShuffleMask);
4571 break;
4572 case X86ISD::MOVLHPS:
4573 DecodeMOVLHPSMask(NumElems, ShuffleMask);
4574 break;
4575 case X86ISD::PSHUFD:
4576 ImmN = N->getOperand(N->getNumOperands()-1);
4577 DecodePSHUFMask(NumElems,
4578 cast<ConstantSDNode>(ImmN)->getZExtValue(),
4579 ShuffleMask);
4580 break;
4581 case X86ISD::PSHUFHW:
4582 ImmN = N->getOperand(N->getNumOperands()-1);
4583 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4584 ShuffleMask);
4585 break;
4586 case X86ISD::PSHUFLW:
4587 ImmN = N->getOperand(N->getNumOperands()-1);
4588 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4589 ShuffleMask);
4590 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004591 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004592 case X86ISD::MOVSD: {
4593 // The index 0 always comes from the first element of the second source,
4594 // this is why MOVSS and MOVSD are used in the first place. The other
4595 // elements come from the other positions of the first source vector.
4596 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004597 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
4598 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004599 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00004600 case X86ISD::VPERMILPS:
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00004601 ImmN = N->getOperand(N->getNumOperands()-1);
Bruno Cardoso Lopes2eb4c2b2011-07-29 01:31:11 +00004602 DecodeVPERMILPSMask(4, cast<ConstantSDNode>(ImmN)->getZExtValue(),
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00004603 ShuffleMask);
Bruno Cardoso Lopes2eb4c2b2011-07-29 01:31:11 +00004604 break;
4605 case X86ISD::VPERMILPSY:
4606 ImmN = N->getOperand(N->getNumOperands()-1);
4607 DecodeVPERMILPSMask(8, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4608 ShuffleMask);
4609 break;
4610 case X86ISD::VPERMILPD:
4611 ImmN = N->getOperand(N->getNumOperands()-1);
4612 DecodeVPERMILPDMask(2, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4613 ShuffleMask);
4614 break;
4615 case X86ISD::VPERMILPDY:
4616 ImmN = N->getOperand(N->getNumOperands()-1);
4617 DecodeVPERMILPDMask(4, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4618 ShuffleMask);
4619 break;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00004620 case X86ISD::VPERM2F128:
4621 ImmN = N->getOperand(N->getNumOperands()-1);
4622 DecodeVPERM2F128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4623 ShuffleMask);
4624 break;
Eli Friedman106f6e72011-09-10 02:01:42 +00004625 case X86ISD::MOVDDUP:
4626 case X86ISD::MOVLHPD:
4627 case X86ISD::MOVLPD:
4628 case X86ISD::MOVLPS:
4629 case X86ISD::MOVSHDUP:
4630 case X86ISD::MOVSLDUP:
4631 case X86ISD::PALIGN:
4632 return SDValue(); // Not yet implemented.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004633 default:
Eli Friedman106f6e72011-09-10 02:01:42 +00004634 assert(0 && "unknown target shuffle node");
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004635 return SDValue();
4636 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004637
4638 Index = ShuffleMask[Index];
4639 if (Index < 0)
4640 return DAG.getUNDEF(VT.getVectorElementType());
4641
4642 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
4643 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
4644 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004645 }
4646
4647 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004648 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004649 V = V.getOperand(0);
4650 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004651 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004652
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004653 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004654 return SDValue();
4655 }
4656
4657 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4658 return (Index == 0) ? V.getOperand(0)
4659 : DAG.getUNDEF(VT.getVectorElementType());
4660
4661 if (V.getOpcode() == ISD::BUILD_VECTOR)
4662 return V.getOperand(Index);
4663
4664 return SDValue();
4665}
4666
4667/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4668/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004669/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004670static
4671unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
4672 bool ZerosFromLeft, SelectionDAG &DAG) {
4673 int i = 0;
4674
4675 while (i < NumElems) {
4676 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004677 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004678 if (!(Elt.getNode() &&
4679 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4680 break;
4681 ++i;
4682 }
4683
4684 return i;
4685}
4686
4687/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
4688/// MaskE correspond consecutively to elements from one of the vector operands,
4689/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4690static
4691bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
4692 int OpIdx, int NumElems, unsigned &OpNum) {
4693 bool SeenV1 = false;
4694 bool SeenV2 = false;
4695
4696 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
4697 int Idx = SVOp->getMaskElt(i);
4698 // Ignore undef indicies
4699 if (Idx < 0)
4700 continue;
4701
4702 if (Idx < NumElems)
4703 SeenV1 = true;
4704 else
4705 SeenV2 = true;
4706
4707 // Only accept consecutive elements from the same vector
4708 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4709 return false;
4710 }
4711
4712 OpNum = SeenV1 ? 0 : 1;
4713 return true;
4714}
4715
4716/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4717/// logical left shift of a vector.
4718static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4719 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4720 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4721 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4722 false /* check zeros from right */, DAG);
4723 unsigned OpSrc;
4724
4725 if (!NumZeros)
4726 return false;
4727
4728 // Considering the elements in the mask that are not consecutive zeros,
4729 // check if they consecutively come from only one of the source vectors.
4730 //
4731 // V1 = {X, A, B, C} 0
4732 // \ \ \ /
4733 // vector_shuffle V1, V2 <1, 2, 3, X>
4734 //
4735 if (!isShuffleMaskConsecutive(SVOp,
4736 0, // Mask Start Index
4737 NumElems-NumZeros-1, // Mask End Index
4738 NumZeros, // Where to start looking in the src vector
4739 NumElems, // Number of elements in vector
4740 OpSrc)) // Which source operand ?
4741 return false;
4742
4743 isLeft = false;
4744 ShAmt = NumZeros;
4745 ShVal = SVOp->getOperand(OpSrc);
4746 return true;
4747}
4748
4749/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4750/// logical left shift of a vector.
4751static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4752 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4753 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4754 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4755 true /* check zeros from left */, DAG);
4756 unsigned OpSrc;
4757
4758 if (!NumZeros)
4759 return false;
4760
4761 // Considering the elements in the mask that are not consecutive zeros,
4762 // check if they consecutively come from only one of the source vectors.
4763 //
4764 // 0 { A, B, X, X } = V2
4765 // / \ / /
4766 // vector_shuffle V1, V2 <X, X, 4, 5>
4767 //
4768 if (!isShuffleMaskConsecutive(SVOp,
4769 NumZeros, // Mask Start Index
4770 NumElems-1, // Mask End Index
4771 0, // Where to start looking in the src vector
4772 NumElems, // Number of elements in vector
4773 OpSrc)) // Which source operand ?
4774 return false;
4775
4776 isLeft = true;
4777 ShAmt = NumZeros;
4778 ShVal = SVOp->getOperand(OpSrc);
4779 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004780}
4781
4782/// isVectorShift - Returns true if the shuffle can be implemented as a
4783/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004784static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004785 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004786 // Although the logic below support any bitwidth size, there are no
4787 // shift instructions which handle more than 128-bit vectors.
4788 if (SVOp->getValueType(0).getSizeInBits() > 128)
4789 return false;
4790
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004791 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4792 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4793 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004794
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004795 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004796}
4797
Evan Chengc78d3b42006-04-24 18:01:45 +00004798/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4799///
Dan Gohman475871a2008-07-27 21:46:04 +00004800static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004801 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004802 SelectionDAG &DAG,
4803 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004804 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004805 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004806
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004807 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004808 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004809 bool First = true;
4810 for (unsigned i = 0; i < 16; ++i) {
4811 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4812 if (ThisIsNonZero && First) {
4813 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004814 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004815 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004816 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004817 First = false;
4818 }
4819
4820 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004821 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004822 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4823 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004824 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004825 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004826 }
4827 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004828 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4829 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4830 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004831 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004832 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004833 } else
4834 ThisElt = LastElt;
4835
Gabor Greifba36cb52008-08-28 21:40:38 +00004836 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004837 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004838 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004839 }
4840 }
4841
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004842 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004843}
4844
Bill Wendlinga348c562007-03-22 18:42:45 +00004845/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004846///
Dan Gohman475871a2008-07-27 21:46:04 +00004847static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004848 unsigned NumNonZero, unsigned NumZero,
4849 SelectionDAG &DAG,
4850 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004851 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004852 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004853
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004854 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004855 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004856 bool First = true;
4857 for (unsigned i = 0; i < 8; ++i) {
4858 bool isNonZero = (NonZeros & (1 << i)) != 0;
4859 if (isNonZero) {
4860 if (First) {
4861 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004862 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004863 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004864 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004865 First = false;
4866 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004867 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004868 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004869 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004870 }
4871 }
4872
4873 return V;
4874}
4875
Evan Chengf26ffe92008-05-29 08:22:04 +00004876/// getVShift - Return a vector logical shift node.
4877///
Owen Andersone50ed302009-08-10 22:56:29 +00004878static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004879 unsigned NumBits, SelectionDAG &DAG,
4880 const TargetLowering &TLI, DebugLoc dl) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004881 assert(VT.getSizeInBits() == 128 && "Unknown type for VShift");
Dale Johannesen0488fb62010-09-30 23:57:10 +00004882 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00004883 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004884 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4885 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004886 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004887 DAG.getConstant(NumBits,
4888 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004889}
4890
Dan Gohman475871a2008-07-27 21:46:04 +00004891SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004892X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004893 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004894
Evan Chengc3630942009-12-09 21:00:30 +00004895 // Check if the scalar load can be widened into a vector load. And if
4896 // the address is "base + cst" see if the cst can be "absorbed" into
4897 // the shuffle mask.
4898 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4899 SDValue Ptr = LD->getBasePtr();
4900 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4901 return SDValue();
4902 EVT PVT = LD->getValueType(0);
4903 if (PVT != MVT::i32 && PVT != MVT::f32)
4904 return SDValue();
4905
4906 int FI = -1;
4907 int64_t Offset = 0;
4908 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4909 FI = FINode->getIndex();
4910 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004911 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004912 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4913 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4914 Offset = Ptr.getConstantOperandVal(1);
4915 Ptr = Ptr.getOperand(0);
4916 } else {
4917 return SDValue();
4918 }
4919
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004920 // FIXME: 256-bit vector instructions don't require a strict alignment,
4921 // improve this code to support it better.
4922 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00004923 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004924 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00004925 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004926 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00004927 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004928 // Can't change the alignment. FIXME: It's possible to compute
4929 // the exact stack offset and reference FI + adjust offset instead.
4930 // If someone *really* cares about this. That's the way to implement it.
4931 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004932 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004933 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00004934 }
4935 }
4936
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004937 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00004938 // Ptr + (Offset & ~15).
4939 if (Offset < 0)
4940 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004941 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00004942 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004943 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00004944 if (StartOffset)
4945 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4946 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4947
4948 int EltNo = (Offset - StartOffset) >> 2;
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004949 int NumElems = VT.getVectorNumElements();
4950
4951 EVT CanonVT = VT.getSizeInBits() == 128 ? MVT::v4i32 : MVT::v8i32;
4952 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
4953 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00004954 LD->getPointerInfo().getWithOffset(StartOffset),
David Greene67c9d422010-02-15 16:53:33 +00004955 false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004956
4957 // Canonicalize it to a v4i32 or v8i32 shuffle.
4958 SmallVector<int, 8> Mask;
4959 for (int i = 0; i < NumElems; ++i)
4960 Mask.push_back(EltNo);
4961
4962 V1 = DAG.getNode(ISD::BITCAST, dl, CanonVT, V1);
4963 return DAG.getNode(ISD::BITCAST, dl, NVT,
4964 DAG.getVectorShuffle(CanonVT, dl, V1,
4965 DAG.getUNDEF(CanonVT),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00004966 }
4967
4968 return SDValue();
4969}
4970
Michael J. Spencerec38de22010-10-10 22:04:20 +00004971/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4972/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004973/// load which has the same value as a build_vector whose operands are 'elts'.
4974///
4975/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004976///
Nate Begeman1449f292010-03-24 22:19:06 +00004977/// FIXME: we'd also like to handle the case where the last elements are zero
4978/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4979/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004980static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004981 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004982 EVT EltVT = VT.getVectorElementType();
4983 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004984
Nate Begemanfdea31a2010-03-24 20:49:50 +00004985 LoadSDNode *LDBase = NULL;
4986 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004987
Nate Begeman1449f292010-03-24 22:19:06 +00004988 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004989 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004990 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004991 for (unsigned i = 0; i < NumElems; ++i) {
4992 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004993
Nate Begemanfdea31a2010-03-24 20:49:50 +00004994 if (!Elt.getNode() ||
4995 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4996 return SDValue();
4997 if (!LDBase) {
4998 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4999 return SDValue();
5000 LDBase = cast<LoadSDNode>(Elt.getNode());
5001 LastLoadedElt = i;
5002 continue;
5003 }
5004 if (Elt.getOpcode() == ISD::UNDEF)
5005 continue;
5006
5007 LoadSDNode *LD = cast<LoadSDNode>(Elt);
5008 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
5009 return SDValue();
5010 LastLoadedElt = i;
5011 }
Nate Begeman1449f292010-03-24 22:19:06 +00005012
5013 // If we have found an entire vector of loads and undefs, then return a large
5014 // load of the entire vector width starting at the base pointer. If we found
5015 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005016 if (LastLoadedElt == NumElems - 1) {
5017 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00005018 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005019 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00005020 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00005021 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005022 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00005023 LDBase->isVolatile(), LDBase->isNonTemporal(),
5024 LDBase->getAlignment());
Eli Friedman61cc47e2011-07-26 21:02:58 +00005025 } else if (NumElems == 4 && LastLoadedElt == 1 &&
5026 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005027 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
5028 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Eli Friedman322ea082011-09-14 23:42:45 +00005029 SDValue ResNode =
5030 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys, Ops, 2, MVT::i64,
5031 LDBase->getPointerInfo(),
5032 LDBase->getAlignment(),
5033 false/*isVolatile*/, true/*ReadMem*/,
5034 false/*WriteMem*/);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005035 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00005036 }
5037 return SDValue();
5038}
5039
Evan Chengc3630942009-12-09 21:00:30 +00005040SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005041X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005042 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00005043
David Greenef125a292011-02-08 19:04:41 +00005044 EVT VT = Op.getValueType();
5045 EVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00005046 unsigned NumElems = Op.getNumOperands();
5047
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005048 // Vectors containing all zeros can be matched by pxor and xorps later
5049 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5050 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
5051 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00005052 if (Op.getValueType() == MVT::v4i32 ||
5053 Op.getValueType() == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00005054 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005055
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005056 return getZeroVector(Op.getValueType(), Subtarget->hasXMMInt(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00005057 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005058
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005059 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
5060 // vectors or broken into v4i32 operations on 256-bit vectors.
5061 if (ISD::isBuildVectorAllOnes(Op.getNode())) {
5062 if (Op.getValueType() == MVT::v4i32)
5063 return Op;
5064
5065 return getOnesVector(Op.getValueType(), DAG, dl);
5066 }
5067
Owen Andersone50ed302009-08-10 22:56:29 +00005068 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005069
Evan Cheng0db9fe62006-04-25 20:13:52 +00005070 unsigned NumZero = 0;
5071 unsigned NumNonZero = 0;
5072 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005073 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005074 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005075 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005076 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00005077 if (Elt.getOpcode() == ISD::UNDEF)
5078 continue;
5079 Values.insert(Elt);
5080 if (Elt.getOpcode() != ISD::Constant &&
5081 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005082 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00005083 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00005084 NumZero++;
5085 else {
5086 NonZeros |= (1 << i);
5087 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005088 }
5089 }
5090
Chris Lattner97a2a562010-08-26 05:24:29 +00005091 // All undef vector. Return an UNDEF. All zero vectors were handled above.
5092 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00005093 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005094
Chris Lattner67f453a2008-03-09 05:42:06 +00005095 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00005096 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005097 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00005098 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005099
Chris Lattner62098042008-03-09 01:05:04 +00005100 // If this is an insertion of an i64 value on x86-32, and if the top bits of
5101 // the value are obviously zero, truncate the value to i32 and do the
5102 // insertion that way. Only do this if the value is non-constant or if the
5103 // value is a constant being inserted into element 0. It is cheaper to do
5104 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00005105 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00005106 (!IsAllConstants || Idx == 0)) {
5107 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00005108 // Handle SSE only.
5109 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
5110 EVT VecVT = MVT::v4i32;
5111 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00005112
Chris Lattner62098042008-03-09 01:05:04 +00005113 // Truncate the value (which may itself be a constant) to i32, and
5114 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00005115 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00005116 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00005117 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005118 Subtarget->hasXMMInt(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005119
Chris Lattner62098042008-03-09 01:05:04 +00005120 // Now we have our 32-bit value zero extended in the low element of
5121 // a vector. If Idx != 0, swizzle it into place.
5122 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005123 SmallVector<int, 4> Mask;
5124 Mask.push_back(Idx);
5125 for (unsigned i = 1; i != VecElts; ++i)
5126 Mask.push_back(i);
5127 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00005128 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00005129 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00005130 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005131 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00005132 }
5133 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005134
Chris Lattner19f79692008-03-08 22:59:52 +00005135 // If we have a constant or non-constant insertion into the low element of
5136 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5137 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00005138 // depending on what the source datatype is.
5139 if (Idx == 0) {
5140 if (NumZero == 0) {
5141 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00005142 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
5143 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00005144 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5145 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005146 return getShuffleVectorZeroOrUndef(Item, 0, true,Subtarget->hasXMMInt(),
Eli Friedman10415532009-06-06 06:05:10 +00005147 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00005148 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
5149 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005150 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
5151 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00005152 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
5153 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005154 Subtarget->hasXMMInt(), DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005155 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00005156 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005157 }
Evan Chengf26ffe92008-05-29 08:22:04 +00005158
5159 // Is it a vector logical left shift?
5160 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00005161 X86::isZeroNode(Op.getOperand(0)) &&
5162 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005163 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00005164 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00005165 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005166 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00005167 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005168 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005169
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005170 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00005171 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005172
Chris Lattner19f79692008-03-08 22:59:52 +00005173 // Otherwise, if this is a vector with i32 or f32 elements, and the element
5174 // is a non-constant being inserted into an element other than the low one,
5175 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
5176 // movd/movss) to move this into the low element, then shuffle it into
5177 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005178 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00005179 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00005180
Evan Cheng0db9fe62006-04-25 20:13:52 +00005181 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00005182 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005183 Subtarget->hasXMMInt(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00005184 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005185 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00005186 MaskVec.push_back(i == Idx ? 0 : 1);
5187 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005188 }
5189 }
5190
Chris Lattner67f453a2008-03-09 05:42:06 +00005191 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00005192 if (Values.size() == 1) {
5193 if (EVTBits == 32) {
5194 // Instead of a shuffle like this:
5195 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5196 // Check if it's possible to issue this instead.
5197 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
5198 unsigned Idx = CountTrailingZeros_32(NonZeros);
5199 SDValue Item = Op.getOperand(Idx);
5200 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5201 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5202 }
Dan Gohman475871a2008-07-27 21:46:04 +00005203 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005204 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005205
Dan Gohmana3941172007-07-24 22:55:08 +00005206 // A vector full of immediates; various special cases are already
5207 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005208 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00005209 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00005210
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005211 // For AVX-length vectors, build the individual 128-bit pieces and use
5212 // shuffles to put them in place.
5213 if (VT.getSizeInBits() == 256 && !ISD::isBuildVectorAllZeros(Op.getNode())) {
5214 SmallVector<SDValue, 32> V;
5215 for (unsigned i = 0; i < NumElems; ++i)
5216 V.push_back(Op.getOperand(i));
5217
5218 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
5219
5220 // Build both the lower and upper subvector.
5221 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
5222 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
5223 NumElems/2);
5224
5225 // Recreate the wider vector with the lower and upper part.
Bruno Cardoso Lopes15d03fb2011-07-28 01:26:53 +00005226 SDValue Vec = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), Lower,
5227 DAG.getConstant(0, MVT::i32), DAG, dl);
5228 return Insert128BitVector(Vec, Upper, DAG.getConstant(NumElems/2, MVT::i32),
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005229 DAG, dl);
5230 }
5231
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005232 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005233 if (EVTBits == 64) {
5234 if (NumNonZero == 1) {
5235 // One half is zero or undef.
5236 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005237 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005238 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00005239 return getShuffleVectorZeroOrUndef(V2, Idx, true,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005240 Subtarget->hasXMMInt(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005241 }
Dan Gohman475871a2008-07-27 21:46:04 +00005242 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005243 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005244
5245 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005246 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005247 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00005248 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005249 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005250 }
5251
Bill Wendling826f36f2007-03-28 00:57:11 +00005252 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005253 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00005254 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005255 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005256 }
5257
5258 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00005259 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00005260 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005261 if (NumElems == 4 && NumZero > 0) {
5262 for (unsigned i = 0; i < 4; ++i) {
5263 bool isZero = !(NonZeros & (1 << i));
5264 if (isZero)
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005265 V[i] = getZeroVector(VT, Subtarget->hasXMMInt(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005266 else
Dale Johannesenace16102009-02-03 19:33:06 +00005267 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005268 }
5269
5270 for (unsigned i = 0; i < 2; ++i) {
5271 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5272 default: break;
5273 case 0:
5274 V[i] = V[i*2]; // Must be a zero vector.
5275 break;
5276 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005277 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005278 break;
5279 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005280 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005281 break;
5282 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005283 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005284 break;
5285 }
5286 }
5287
Nate Begeman9008ca62009-04-27 18:41:29 +00005288 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005289 bool Reverse = (NonZeros & 0x3) == 2;
5290 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005291 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005292 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5293 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005294 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
5295 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005296 }
5297
Nate Begemanfdea31a2010-03-24 20:49:50 +00005298 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
5299 // Check for a build vector of consecutive loads.
5300 for (unsigned i = 0; i < NumElems; ++i)
5301 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005302
Nate Begemanfdea31a2010-03-24 20:49:50 +00005303 // Check for elements which are consecutive loads.
5304 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5305 if (LD.getNode())
5306 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005307
5308 // For SSE 4.1, use insertps to put the high elements into the low element.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005309 if (getSubtarget()->hasSSE41() || getSubtarget()->hasAVX()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005310 SDValue Result;
5311 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5312 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5313 else
5314 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005315
Chris Lattner24faf612010-08-28 17:59:08 +00005316 for (unsigned i = 1; i < NumElems; ++i) {
5317 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5318 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005319 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005320 }
5321 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005322 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005323
Chris Lattner6e80e442010-08-28 17:15:43 +00005324 // Otherwise, expand into a number of unpckl*, start by extending each of
5325 // our (non-undef) elements to the full vector width with the element in the
5326 // bottom slot of the vector (which generates no code for SSE).
5327 for (unsigned i = 0; i < NumElems; ++i) {
5328 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5329 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5330 else
5331 V[i] = DAG.getUNDEF(VT);
5332 }
5333
5334 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005335 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5336 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5337 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005338 unsigned EltStride = NumElems >> 1;
5339 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005340 for (unsigned i = 0; i < EltStride; ++i) {
5341 // If V[i+EltStride] is undef and this is the first round of mixing,
5342 // then it is safe to just drop this shuffle: V[i] is already in the
5343 // right place, the one element (since it's the first round) being
5344 // inserted as undef can be dropped. This isn't safe for successive
5345 // rounds because they will permute elements within both vectors.
5346 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5347 EltStride == NumElems/2)
5348 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005349
Chris Lattner6e80e442010-08-28 17:15:43 +00005350 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005351 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005352 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005353 }
5354 return V[0];
5355 }
Dan Gohman475871a2008-07-27 21:46:04 +00005356 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005357}
5358
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005359// LowerMMXCONCAT_VECTORS - We support concatenate two MMX registers and place
5360// them in a MMX register. This is better than doing a stack convert.
5361static SDValue LowerMMXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005362 DebugLoc dl = Op.getDebugLoc();
5363 EVT ResVT = Op.getValueType();
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005364
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005365 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
5366 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
5367 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005368 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005369 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
5370 InVec = Op.getOperand(1);
5371 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5372 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005373 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005374 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
5375 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
5376 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005377 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005378 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
5379 Mask[0] = 0; Mask[1] = 2;
5380 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
5381 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005382 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005383}
5384
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005385// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5386// to create 256-bit vectors from two other 128-bit ones.
5387static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5388 DebugLoc dl = Op.getDebugLoc();
5389 EVT ResVT = Op.getValueType();
5390
5391 assert(ResVT.getSizeInBits() == 256 && "Value type must be 256-bit wide");
5392
5393 SDValue V1 = Op.getOperand(0);
5394 SDValue V2 = Op.getOperand(1);
5395 unsigned NumElems = ResVT.getVectorNumElements();
5396
5397 SDValue V = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, ResVT), V1,
5398 DAG.getConstant(0, MVT::i32), DAG, dl);
5399 return Insert128BitVector(V, V2, DAG.getConstant(NumElems/2, MVT::i32),
5400 DAG, dl);
5401}
5402
5403SDValue
5404X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005405 EVT ResVT = Op.getValueType();
5406
5407 assert(Op.getNumOperands() == 2);
5408 assert((ResVT.getSizeInBits() == 128 || ResVT.getSizeInBits() == 256) &&
5409 "Unsupported CONCAT_VECTORS for value type");
5410
5411 // We support concatenate two MMX registers and place them in a MMX register.
5412 // This is better than doing a stack convert.
5413 if (ResVT.is128BitVector())
5414 return LowerMMXCONCAT_VECTORS(Op, DAG);
5415
5416 // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
5417 // from two other 128-bit ones.
5418 return LowerAVXCONCAT_VECTORS(Op, DAG);
5419}
5420
Nate Begemanb9a47b82009-02-23 08:49:38 +00005421// v8i16 shuffles - Prefer shuffles in the following order:
5422// 1. [all] pshuflw, pshufhw, optional move
5423// 2. [ssse3] 1 x pshufb
5424// 3. [ssse3] 2 x pshufb + 1 x por
5425// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005426SDValue
5427X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
5428 SelectionDAG &DAG) const {
5429 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00005430 SDValue V1 = SVOp->getOperand(0);
5431 SDValue V2 = SVOp->getOperand(1);
5432 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005433 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00005434
Nate Begemanb9a47b82009-02-23 08:49:38 +00005435 // Determine if more than 1 of the words in each of the low and high quadwords
5436 // of the result come from the same quadword of one of the two inputs. Undef
5437 // mask values count as coming from any quadword, for better codegen.
5438 SmallVector<unsigned, 4> LoQuad(4);
5439 SmallVector<unsigned, 4> HiQuad(4);
5440 BitVector InputQuads(4);
5441 for (unsigned i = 0; i < 8; ++i) {
5442 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00005443 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005444 MaskVals.push_back(EltIdx);
5445 if (EltIdx < 0) {
5446 ++Quad[0];
5447 ++Quad[1];
5448 ++Quad[2];
5449 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00005450 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005451 }
5452 ++Quad[EltIdx / 4];
5453 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00005454 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005455
Nate Begemanb9a47b82009-02-23 08:49:38 +00005456 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005457 unsigned MaxQuad = 1;
5458 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005459 if (LoQuad[i] > MaxQuad) {
5460 BestLoQuad = i;
5461 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005462 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005463 }
5464
Nate Begemanb9a47b82009-02-23 08:49:38 +00005465 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005466 MaxQuad = 1;
5467 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005468 if (HiQuad[i] > MaxQuad) {
5469 BestHiQuad = i;
5470 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005471 }
5472 }
5473
Nate Begemanb9a47b82009-02-23 08:49:38 +00005474 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00005475 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00005476 // single pshufb instruction is necessary. If There are more than 2 input
5477 // quads, disable the next transformation since it does not help SSSE3.
5478 bool V1Used = InputQuads[0] || InputQuads[1];
5479 bool V2Used = InputQuads[2] || InputQuads[3];
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005480 if (Subtarget->hasSSSE3() || Subtarget->hasAVX()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005481 if (InputQuads.count() == 2 && V1Used && V2Used) {
5482 BestLoQuad = InputQuads.find_first();
5483 BestHiQuad = InputQuads.find_next(BestLoQuad);
5484 }
5485 if (InputQuads.count() > 2) {
5486 BestLoQuad = -1;
5487 BestHiQuad = -1;
5488 }
5489 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005490
Nate Begemanb9a47b82009-02-23 08:49:38 +00005491 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5492 // the shuffle mask. If a quad is scored as -1, that means that it contains
5493 // words from all 4 input quadwords.
5494 SDValue NewV;
5495 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005496 SmallVector<int, 8> MaskV;
5497 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
5498 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00005499 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005500 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
5501 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
5502 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005503
Nate Begemanb9a47b82009-02-23 08:49:38 +00005504 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5505 // source words for the shuffle, to aid later transformations.
5506 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00005507 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00005508 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005509 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00005510 if (idx != (int)i)
5511 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005512 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00005513 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005514 AllWordsInNewV = false;
5515 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00005516 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005517
Nate Begemanb9a47b82009-02-23 08:49:38 +00005518 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5519 if (AllWordsInNewV) {
5520 for (int i = 0; i != 8; ++i) {
5521 int idx = MaskVals[i];
5522 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005523 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005524 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005525 if ((idx != i) && idx < 4)
5526 pshufhw = false;
5527 if ((idx != i) && idx > 3)
5528 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00005529 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005530 V1 = NewV;
5531 V2Used = false;
5532 BestLoQuad = 0;
5533 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005534 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005535
Nate Begemanb9a47b82009-02-23 08:49:38 +00005536 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5537 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005538 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005539 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5540 unsigned TargetMask = 0;
5541 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005542 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005543 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
5544 X86::getShufflePSHUFLWImmediate(NewV.getNode());
5545 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005546 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005547 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005548 }
Eric Christopherfd179292009-08-27 18:07:15 +00005549
Nate Begemanb9a47b82009-02-23 08:49:38 +00005550 // If we have SSSE3, and all words of the result are from 1 input vector,
5551 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5552 // is present, fall back to case 4.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005553 if (Subtarget->hasSSSE3() || Subtarget->hasAVX()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005554 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005555
Nate Begemanb9a47b82009-02-23 08:49:38 +00005556 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005557 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005558 // mask, and elements that come from V1 in the V2 mask, so that the two
5559 // results can be OR'd together.
5560 bool TwoInputs = V1Used && V2Used;
5561 for (unsigned i = 0; i != 8; ++i) {
5562 int EltIdx = MaskVals[i] * 2;
5563 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005564 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5565 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005566 continue;
5567 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005568 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
5569 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005570 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005571 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005572 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005573 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005574 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005575 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005576 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005577
Nate Begemanb9a47b82009-02-23 08:49:38 +00005578 // Calculate the shuffle mask for the second input, shuffle it, and
5579 // OR it with the first shuffled input.
5580 pshufbMask.clear();
5581 for (unsigned i = 0; i != 8; ++i) {
5582 int EltIdx = MaskVals[i] * 2;
5583 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005584 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5585 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005586 continue;
5587 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005588 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
5589 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005590 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005591 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005592 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005593 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005594 MVT::v16i8, &pshufbMask[0], 16));
5595 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005596 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005597 }
5598
5599 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5600 // and update MaskVals with new element order.
5601 BitVector InOrder(8);
5602 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005603 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005604 for (int i = 0; i != 4; ++i) {
5605 int idx = MaskVals[i];
5606 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005607 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005608 InOrder.set(i);
5609 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005610 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005611 InOrder.set(i);
5612 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005613 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005614 }
5615 }
5616 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005617 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00005618 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005619 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005620
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005621 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE &&
5622 (Subtarget->hasSSSE3() || Subtarget->hasAVX()))
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005623 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
5624 NewV.getOperand(0),
5625 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
5626 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005627 }
Eric Christopherfd179292009-08-27 18:07:15 +00005628
Nate Begemanb9a47b82009-02-23 08:49:38 +00005629 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5630 // and update MaskVals with the new element order.
5631 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005632 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005633 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005634 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005635 for (unsigned i = 4; i != 8; ++i) {
5636 int idx = MaskVals[i];
5637 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005638 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005639 InOrder.set(i);
5640 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005641 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005642 InOrder.set(i);
5643 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005644 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005645 }
5646 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005647 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005648 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005649
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005650 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE &&
5651 (Subtarget->hasSSSE3() || Subtarget->hasAVX()))
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005652 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
5653 NewV.getOperand(0),
5654 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
5655 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005656 }
Eric Christopherfd179292009-08-27 18:07:15 +00005657
Nate Begemanb9a47b82009-02-23 08:49:38 +00005658 // In case BestHi & BestLo were both -1, which means each quadword has a word
5659 // from each of the four input quadwords, calculate the InOrder bitvector now
5660 // before falling through to the insert/extract cleanup.
5661 if (BestLoQuad == -1 && BestHiQuad == -1) {
5662 NewV = V1;
5663 for (int i = 0; i != 8; ++i)
5664 if (MaskVals[i] < 0 || MaskVals[i] == i)
5665 InOrder.set(i);
5666 }
Eric Christopherfd179292009-08-27 18:07:15 +00005667
Nate Begemanb9a47b82009-02-23 08:49:38 +00005668 // The other elements are put in the right place using pextrw and pinsrw.
5669 for (unsigned i = 0; i != 8; ++i) {
5670 if (InOrder[i])
5671 continue;
5672 int EltIdx = MaskVals[i];
5673 if (EltIdx < 0)
5674 continue;
5675 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00005676 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005677 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00005678 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005679 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005680 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005681 DAG.getIntPtrConstant(i));
5682 }
5683 return NewV;
5684}
5685
5686// v16i8 shuffles - Prefer shuffles in the following order:
5687// 1. [ssse3] 1 x pshufb
5688// 2. [ssse3] 2 x pshufb + 1 x por
5689// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5690static
Nate Begeman9008ca62009-04-27 18:41:29 +00005691SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005692 SelectionDAG &DAG,
5693 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005694 SDValue V1 = SVOp->getOperand(0);
5695 SDValue V2 = SVOp->getOperand(1);
5696 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005697 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00005698 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00005699
Nate Begemanb9a47b82009-02-23 08:49:38 +00005700 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005701 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005702 // present, fall back to case 3.
5703 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
5704 bool V1Only = true;
5705 bool V2Only = true;
5706 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005707 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00005708 if (EltIdx < 0)
5709 continue;
5710 if (EltIdx < 16)
5711 V2Only = false;
5712 else
5713 V1Only = false;
5714 }
Eric Christopherfd179292009-08-27 18:07:15 +00005715
Nate Begemanb9a47b82009-02-23 08:49:38 +00005716 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005717 if (TLI.getSubtarget()->hasSSSE3() || TLI.getSubtarget()->hasAVX()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005718 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005719
Nate Begemanb9a47b82009-02-23 08:49:38 +00005720 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005721 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005722 //
5723 // Otherwise, we have elements from both input vectors, and must zero out
5724 // elements that come from V2 in the first mask, and V1 in the second mask
5725 // so that we can OR them together.
5726 bool TwoInputs = !(V1Only || V2Only);
5727 for (unsigned i = 0; i != 16; ++i) {
5728 int EltIdx = MaskVals[i];
5729 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005730 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005731 continue;
5732 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005733 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005734 }
5735 // If all the elements are from V2, assign it to V1 and return after
5736 // building the first pshufb.
5737 if (V2Only)
5738 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00005739 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005740 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005741 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005742 if (!TwoInputs)
5743 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005744
Nate Begemanb9a47b82009-02-23 08:49:38 +00005745 // Calculate the shuffle mask for the second input, shuffle it, and
5746 // OR it with the first shuffled input.
5747 pshufbMask.clear();
5748 for (unsigned i = 0; i != 16; ++i) {
5749 int EltIdx = MaskVals[i];
5750 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005751 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005752 continue;
5753 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005754 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005755 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005756 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005757 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005758 MVT::v16i8, &pshufbMask[0], 16));
5759 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005760 }
Eric Christopherfd179292009-08-27 18:07:15 +00005761
Nate Begemanb9a47b82009-02-23 08:49:38 +00005762 // No SSSE3 - Calculate in place words and then fix all out of place words
5763 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5764 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005765 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5766 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005767 SDValue NewV = V2Only ? V2 : V1;
5768 for (int i = 0; i != 8; ++i) {
5769 int Elt0 = MaskVals[i*2];
5770 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005771
Nate Begemanb9a47b82009-02-23 08:49:38 +00005772 // This word of the result is all undef, skip it.
5773 if (Elt0 < 0 && Elt1 < 0)
5774 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005775
Nate Begemanb9a47b82009-02-23 08:49:38 +00005776 // This word of the result is already in the correct place, skip it.
5777 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
5778 continue;
5779 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
5780 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005781
Nate Begemanb9a47b82009-02-23 08:49:38 +00005782 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5783 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5784 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005785
5786 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5787 // using a single extract together, load it and store it.
5788 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005789 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005790 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005791 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005792 DAG.getIntPtrConstant(i));
5793 continue;
5794 }
5795
Nate Begemanb9a47b82009-02-23 08:49:38 +00005796 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00005797 // source byte is not also odd, shift the extracted word left 8 bits
5798 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005799 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005800 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005801 DAG.getIntPtrConstant(Elt1 / 2));
5802 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005803 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00005804 DAG.getConstant(8,
5805 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005806 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005807 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
5808 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005809 }
5810 // If Elt0 is defined, extract it from the appropriate source. If the
5811 // source byte is not also even, shift the extracted word right 8 bits. If
5812 // Elt1 was also defined, OR the extracted values together before
5813 // inserting them in the result.
5814 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005815 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005816 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
5817 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005818 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00005819 DAG.getConstant(8,
5820 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005821 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005822 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
5823 DAG.getConstant(0x00FF, MVT::i16));
5824 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00005825 : InsElt0;
5826 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005827 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005828 DAG.getIntPtrConstant(i));
5829 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005830 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005831}
5832
Evan Cheng7a831ce2007-12-15 03:00:47 +00005833/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005834/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00005835/// done when every pair / quad of shuffle mask elements point to elements in
5836/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005837/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00005838static
Nate Begeman9008ca62009-04-27 18:41:29 +00005839SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005840 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00005841 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00005842 SDValue V1 = SVOp->getOperand(0);
5843 SDValue V2 = SVOp->getOperand(1);
5844 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00005845 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005846 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00005847 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005848 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005849 case MVT::v4f32: NewVT = MVT::v2f64; break;
5850 case MVT::v4i32: NewVT = MVT::v2i64; break;
5851 case MVT::v8i16: NewVT = MVT::v4i32; break;
5852 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00005853 }
5854
Nate Begeman9008ca62009-04-27 18:41:29 +00005855 int Scale = NumElems / NewWidth;
5856 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00005857 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005858 int StartIdx = -1;
5859 for (int j = 0; j < Scale; ++j) {
5860 int EltIdx = SVOp->getMaskElt(i+j);
5861 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005862 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00005863 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00005864 StartIdx = EltIdx - (EltIdx % Scale);
5865 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00005866 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005867 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005868 if (StartIdx == -1)
5869 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00005870 else
Nate Begeman9008ca62009-04-27 18:41:29 +00005871 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005872 }
5873
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005874 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
5875 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00005876 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005877}
5878
Evan Chengd880b972008-05-09 21:53:03 +00005879/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005880///
Owen Andersone50ed302009-08-10 22:56:29 +00005881static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00005882 SDValue SrcOp, SelectionDAG &DAG,
5883 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005884 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005885 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00005886 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00005887 LD = dyn_cast<LoadSDNode>(SrcOp);
5888 if (!LD) {
5889 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
5890 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00005891 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00005892 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00005893 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005894 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00005895 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005896 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00005897 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005898 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005899 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
5900 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
5901 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00005902 SrcOp.getOperand(0)
5903 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005904 }
5905 }
5906 }
5907
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005908 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005909 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005910 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00005911 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005912}
5913
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00005914/// areShuffleHalvesWithinDisjointLanes - Check whether each half of a vector
5915/// shuffle node referes to only one lane in the sources.
5916static bool areShuffleHalvesWithinDisjointLanes(ShuffleVectorSDNode *SVOp) {
5917 EVT VT = SVOp->getValueType(0);
5918 int NumElems = VT.getVectorNumElements();
5919 int HalfSize = NumElems/2;
5920 SmallVector<int, 16> M;
5921 SVOp->getMask(M);
5922 bool MatchA = false, MatchB = false;
5923
5924 for (int l = 0; l < NumElems*2; l += HalfSize) {
5925 if (isUndefOrInRange(M, 0, HalfSize, l, l+HalfSize)) {
5926 MatchA = true;
5927 break;
5928 }
5929 }
5930
5931 for (int l = 0; l < NumElems*2; l += HalfSize) {
5932 if (isUndefOrInRange(M, HalfSize, HalfSize, l, l+HalfSize)) {
5933 MatchB = true;
5934 break;
5935 }
5936 }
5937
5938 return MatchA && MatchB;
5939}
5940
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00005941/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
5942/// which could not be matched by any known target speficic shuffle
5943static SDValue
5944LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00005945 if (areShuffleHalvesWithinDisjointLanes(SVOp)) {
5946 // If each half of a vector shuffle node referes to only one lane in the
5947 // source vectors, extract each used 128-bit lane and shuffle them using
5948 // 128-bit shuffles. Then, concatenate the results. Otherwise leave
5949 // the work to the legalizer.
5950 DebugLoc dl = SVOp->getDebugLoc();
5951 EVT VT = SVOp->getValueType(0);
5952 int NumElems = VT.getVectorNumElements();
5953 int HalfSize = NumElems/2;
5954
5955 // Extract the reference for each half
5956 int FstVecExtractIdx = 0, SndVecExtractIdx = 0;
5957 int FstVecOpNum = 0, SndVecOpNum = 0;
5958 for (int i = 0; i < HalfSize; ++i) {
5959 int Elt = SVOp->getMaskElt(i);
5960 if (SVOp->getMaskElt(i) < 0)
5961 continue;
5962 FstVecOpNum = Elt/NumElems;
5963 FstVecExtractIdx = Elt % NumElems < HalfSize ? 0 : HalfSize;
5964 break;
5965 }
5966 for (int i = HalfSize; i < NumElems; ++i) {
5967 int Elt = SVOp->getMaskElt(i);
5968 if (SVOp->getMaskElt(i) < 0)
5969 continue;
5970 SndVecOpNum = Elt/NumElems;
5971 SndVecExtractIdx = Elt % NumElems < HalfSize ? 0 : HalfSize;
5972 break;
5973 }
5974
5975 // Extract the subvectors
5976 SDValue V1 = Extract128BitVector(SVOp->getOperand(FstVecOpNum),
5977 DAG.getConstant(FstVecExtractIdx, MVT::i32), DAG, dl);
5978 SDValue V2 = Extract128BitVector(SVOp->getOperand(SndVecOpNum),
5979 DAG.getConstant(SndVecExtractIdx, MVT::i32), DAG, dl);
5980
5981 // Generate 128-bit shuffles
5982 SmallVector<int, 16> MaskV1, MaskV2;
5983 for (int i = 0; i < HalfSize; ++i) {
5984 int Elt = SVOp->getMaskElt(i);
5985 MaskV1.push_back(Elt < 0 ? Elt : Elt % HalfSize);
5986 }
5987 for (int i = HalfSize; i < NumElems; ++i) {
5988 int Elt = SVOp->getMaskElt(i);
5989 MaskV2.push_back(Elt < 0 ? Elt : Elt % HalfSize);
5990 }
5991
5992 EVT NVT = V1.getValueType();
5993 V1 = DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &MaskV1[0]);
5994 V2 = DAG.getVectorShuffle(NVT, dl, V2, DAG.getUNDEF(NVT), &MaskV2[0]);
5995
5996 // Concatenate the result back
5997 SDValue V = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), V1,
5998 DAG.getConstant(0, MVT::i32), DAG, dl);
5999 return Insert128BitVector(V, V2, DAG.getConstant(NumElems/2, MVT::i32),
6000 DAG, dl);
6001 }
6002
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006003 return SDValue();
6004}
6005
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006006/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
6007/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00006008static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006009LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006010 SDValue V1 = SVOp->getOperand(0);
6011 SDValue V2 = SVOp->getOperand(1);
6012 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006013 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00006014
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006015 assert(VT.getSizeInBits() == 128 && "Unsupported vector size");
6016
Evan Chengace3c172008-07-22 21:13:36 +00006017 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00006018 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00006019 SmallVector<int, 8> Mask1(4U, -1);
6020 SmallVector<int, 8> PermMask;
6021 SVOp->getMask(PermMask);
6022
Evan Chengace3c172008-07-22 21:13:36 +00006023 unsigned NumHi = 0;
6024 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00006025 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006026 int Idx = PermMask[i];
6027 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006028 Locs[i] = std::make_pair(-1, -1);
6029 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006030 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
6031 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006032 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00006033 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006034 NumLo++;
6035 } else {
6036 Locs[i] = std::make_pair(1, NumHi);
6037 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00006038 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006039 NumHi++;
6040 }
6041 }
6042 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006043
Evan Chengace3c172008-07-22 21:13:36 +00006044 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006045 // If no more than two elements come from either vector. This can be
6046 // implemented with two shuffles. First shuffle gather the elements.
6047 // The second shuffle, which takes the first shuffle as both of its
6048 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00006049 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006050
Nate Begeman9008ca62009-04-27 18:41:29 +00006051 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00006052
Evan Chengace3c172008-07-22 21:13:36 +00006053 for (unsigned i = 0; i != 4; ++i) {
6054 if (Locs[i].first == -1)
6055 continue;
6056 else {
6057 unsigned Idx = (i < 2) ? 0 : 4;
6058 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006059 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006060 }
6061 }
6062
Nate Begeman9008ca62009-04-27 18:41:29 +00006063 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006064 } else if (NumLo == 3 || NumHi == 3) {
6065 // Otherwise, we must have three elements from one vector, call it X, and
6066 // one element from the other, call it Y. First, use a shufps to build an
6067 // intermediate vector with the one element from Y and the element from X
6068 // that will be in the same half in the final destination (the indexes don't
6069 // matter). Then, use a shufps to build the final vector, taking the half
6070 // containing the element from Y from the intermediate, and the other half
6071 // from X.
6072 if (NumHi == 3) {
6073 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00006074 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006075 std::swap(V1, V2);
6076 }
6077
6078 // Find the element from V2.
6079 unsigned HiIndex;
6080 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006081 int Val = PermMask[HiIndex];
6082 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006083 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006084 if (Val >= 4)
6085 break;
6086 }
6087
Nate Begeman9008ca62009-04-27 18:41:29 +00006088 Mask1[0] = PermMask[HiIndex];
6089 Mask1[1] = -1;
6090 Mask1[2] = PermMask[HiIndex^1];
6091 Mask1[3] = -1;
6092 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006093
6094 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006095 Mask1[0] = PermMask[0];
6096 Mask1[1] = PermMask[1];
6097 Mask1[2] = HiIndex & 1 ? 6 : 4;
6098 Mask1[3] = HiIndex & 1 ? 4 : 6;
6099 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006100 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006101 Mask1[0] = HiIndex & 1 ? 2 : 0;
6102 Mask1[1] = HiIndex & 1 ? 0 : 2;
6103 Mask1[2] = PermMask[2];
6104 Mask1[3] = PermMask[3];
6105 if (Mask1[2] >= 0)
6106 Mask1[2] += 4;
6107 if (Mask1[3] >= 0)
6108 Mask1[3] += 4;
6109 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006110 }
Evan Chengace3c172008-07-22 21:13:36 +00006111 }
6112
6113 // Break it into (shuffle shuffle_hi, shuffle_lo).
6114 Locs.clear();
David Greenec4db4e52011-02-28 19:06:56 +00006115 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00006116 SmallVector<int,8> LoMask(4U, -1);
6117 SmallVector<int,8> HiMask(4U, -1);
6118
6119 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00006120 unsigned MaskIdx = 0;
6121 unsigned LoIdx = 0;
6122 unsigned HiIdx = 2;
6123 for (unsigned i = 0; i != 4; ++i) {
6124 if (i == 2) {
6125 MaskPtr = &HiMask;
6126 MaskIdx = 1;
6127 LoIdx = 0;
6128 HiIdx = 2;
6129 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006130 int Idx = PermMask[i];
6131 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006132 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00006133 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006134 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00006135 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006136 LoIdx++;
6137 } else {
6138 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00006139 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006140 HiIdx++;
6141 }
6142 }
6143
Nate Begeman9008ca62009-04-27 18:41:29 +00006144 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6145 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
6146 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00006147 for (unsigned i = 0; i != 4; ++i) {
6148 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006149 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00006150 } else {
6151 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006152 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00006153 }
6154 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006155 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006156}
6157
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006158static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006159 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006160 V = V.getOperand(0);
6161 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6162 V = V.getOperand(0);
6163 if (MayFoldLoad(V))
6164 return true;
6165 return false;
6166}
6167
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006168// FIXME: the version above should always be used. Since there's
6169// a bug where several vector shuffles can't be folded because the
6170// DAG is not updated during lowering and a node claims to have two
6171// uses while it only has one, use this version, and let isel match
6172// another instruction if the load really happens to have more than
6173// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00006174// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006175static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006176 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006177 V = V.getOperand(0);
6178 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6179 V = V.getOperand(0);
6180 if (ISD::isNormalLoad(V.getNode()))
6181 return true;
6182 return false;
6183}
6184
6185/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
6186/// a vector extract, and if both can be later optimized into a single load.
6187/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
6188/// here because otherwise a target specific shuffle node is going to be
6189/// emitted for this shuffle, and the optimization not done.
6190/// FIXME: This is probably not the best approach, but fix the problem
6191/// until the right path is decided.
6192static
6193bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
6194 const TargetLowering &TLI) {
6195 EVT VT = V.getValueType();
6196 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
6197
6198 // Be sure that the vector shuffle is present in a pattern like this:
6199 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
6200 if (!V.hasOneUse())
6201 return false;
6202
6203 SDNode *N = *V.getNode()->use_begin();
6204 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
6205 return false;
6206
6207 SDValue EltNo = N->getOperand(1);
6208 if (!isa<ConstantSDNode>(EltNo))
6209 return false;
6210
6211 // If the bit convert changed the number of elements, it is unsafe
6212 // to examine the mask.
6213 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006214 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006215 EVT SrcVT = V.getOperand(0).getValueType();
6216 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
6217 return false;
6218 V = V.getOperand(0);
6219 HasShuffleIntoBitcast = true;
6220 }
6221
6222 // Select the input vector, guarding against out of range extract vector.
6223 unsigned NumElems = VT.getVectorNumElements();
6224 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
6225 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
6226 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
6227
6228 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006229 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006230 V = V.getOperand(0);
6231
6232 if (ISD::isNormalLoad(V.getNode())) {
6233 // Is the original load suitable?
6234 LoadSDNode *LN0 = cast<LoadSDNode>(V);
6235
6236 // FIXME: avoid the multi-use bug that is preventing lots of
6237 // of foldings to be detected, this is still wrong of course, but
6238 // give the temporary desired behavior, and if it happens that
6239 // the load has real more uses, during isel it will not fold, and
6240 // will generate poor code.
6241 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
6242 return false;
6243
6244 if (!HasShuffleIntoBitcast)
6245 return true;
6246
6247 // If there's a bitcast before the shuffle, check if the load type and
6248 // alignment is valid.
6249 unsigned Align = LN0->getAlignment();
6250 unsigned NewAlign =
6251 TLI.getTargetData()->getABITypeAlignment(
6252 VT.getTypeForEVT(*DAG.getContext()));
6253
6254 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
6255 return false;
6256 }
6257
6258 return true;
6259}
6260
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006261static
Evan Cheng835580f2010-10-07 20:50:20 +00006262SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
6263 EVT VT = Op.getValueType();
6264
6265 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006266 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6267 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006268 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6269 V1, DAG));
6270}
6271
6272static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006273SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006274 bool HasXMMInt) {
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006275 SDValue V1 = Op.getOperand(0);
6276 SDValue V2 = Op.getOperand(1);
6277 EVT VT = Op.getValueType();
6278
6279 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6280
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006281 if (HasXMMInt && VT == MVT::v2f64)
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006282 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6283
Evan Cheng0899f5c2011-08-31 02:05:24 +00006284 // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
6285 return DAG.getNode(ISD::BITCAST, dl, VT,
6286 getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
6287 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
6288 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006289}
6290
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006291static
6292SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
6293 SDValue V1 = Op.getOperand(0);
6294 SDValue V2 = Op.getOperand(1);
6295 EVT VT = Op.getValueType();
6296
6297 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6298 "unsupported shuffle type");
6299
6300 if (V2.getOpcode() == ISD::UNDEF)
6301 V2 = V1;
6302
6303 // v4i32 or v4f32
6304 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6305}
6306
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006307static inline unsigned getSHUFPOpcode(EVT VT) {
6308 switch(VT.getSimpleVT().SimpleTy) {
6309 case MVT::v8i32: // Use fp unit for int unpack.
6310 case MVT::v8f32:
6311 case MVT::v4i32: // Use fp unit for int unpack.
6312 case MVT::v4f32: return X86ISD::SHUFPS;
6313 case MVT::v4i64: // Use fp unit for int unpack.
6314 case MVT::v4f64:
6315 case MVT::v2i64: // Use fp unit for int unpack.
6316 case MVT::v2f64: return X86ISD::SHUFPD;
6317 default:
6318 llvm_unreachable("Unknown type for shufp*");
6319 }
6320 return 0;
6321}
6322
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006323static
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006324SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasXMMInt) {
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006325 SDValue V1 = Op.getOperand(0);
6326 SDValue V2 = Op.getOperand(1);
6327 EVT VT = Op.getValueType();
6328 unsigned NumElems = VT.getVectorNumElements();
6329
6330 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6331 // operand of these instructions is only memory, so check if there's a
6332 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6333 // same masks.
6334 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006335
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006336 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006337 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006338 CanFoldLoad = true;
6339
6340 // When V1 is a load, it can be folded later into a store in isel, example:
6341 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6342 // turns into:
6343 // (MOVLPSmr addr:$src1, VR128:$src2)
6344 // So, recognize this potential and also use MOVLPS or MOVLPD
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006345 if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006346 CanFoldLoad = true;
6347
Eric Christopher893a8822011-02-20 05:04:42 +00006348 // Both of them can't be memory operations though.
6349 if (MayFoldVectorLoad(V1) && MayFoldVectorLoad(V2))
6350 CanFoldLoad = false;
Owen Anderson95771af2011-02-25 21:41:48 +00006351
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006352 if (CanFoldLoad) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006353 if (HasXMMInt && NumElems == 2)
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006354 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6355
6356 if (NumElems == 4)
6357 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
6358 }
6359
6360 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6361 // movl and movlp will both match v2i64, but v2i64 is never matched by
6362 // movl earlier because we make it strict to avoid messing with the movlp load
6363 // folding logic (see the code above getMOVLP call). Match it here then,
6364 // this is horrible, but will stay like this until we move all shuffle
6365 // matching to x86 specific nodes. Note that for the 1st condition all
6366 // types are matched with movsd.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006367 if (HasXMMInt) {
Bruno Cardoso Lopes5ca0d142011-09-14 02:36:14 +00006368 // FIXME: isMOVLMask should be checked and matched before getMOVLP,
6369 // as to remove this logic from here, as much as possible
6370 if (NumElems == 2 || !X86::isMOVLMask(SVOp))
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006371 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006372 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006373 }
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006374
6375 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6376
6377 // Invert the operand order and use SHUFPS to match it.
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006378 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V2, V1,
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006379 X86::getShuffleSHUFImmediate(SVOp), DAG);
6380}
6381
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006382static inline unsigned getUNPCKLOpcode(EVT VT) {
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006383 switch(VT.getSimpleVT().SimpleTy) {
6384 case MVT::v4i32: return X86ISD::PUNPCKLDQ;
6385 case MVT::v2i64: return X86ISD::PUNPCKLQDQ;
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00006386 case MVT::v4f32: return X86ISD::UNPCKLPS;
6387 case MVT::v2f64: return X86ISD::UNPCKLPD;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006388 case MVT::v8i32: // Use fp unit for int unpack.
David Greenec4db4e52011-02-28 19:06:56 +00006389 case MVT::v8f32: return X86ISD::VUNPCKLPSY;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006390 case MVT::v4i64: // Use fp unit for int unpack.
David Greenec4db4e52011-02-28 19:06:56 +00006391 case MVT::v4f64: return X86ISD::VUNPCKLPDY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006392 case MVT::v16i8: return X86ISD::PUNPCKLBW;
6393 case MVT::v8i16: return X86ISD::PUNPCKLWD;
6394 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00006395 llvm_unreachable("Unknown type for unpckl");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006396 }
6397 return 0;
6398}
6399
6400static inline unsigned getUNPCKHOpcode(EVT VT) {
6401 switch(VT.getSimpleVT().SimpleTy) {
6402 case MVT::v4i32: return X86ISD::PUNPCKHDQ;
6403 case MVT::v2i64: return X86ISD::PUNPCKHQDQ;
6404 case MVT::v4f32: return X86ISD::UNPCKHPS;
6405 case MVT::v2f64: return X86ISD::UNPCKHPD;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006406 case MVT::v8i32: // Use fp unit for int unpack.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00006407 case MVT::v8f32: return X86ISD::VUNPCKHPSY;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006408 case MVT::v4i64: // Use fp unit for int unpack.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00006409 case MVT::v4f64: return X86ISD::VUNPCKHPDY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006410 case MVT::v16i8: return X86ISD::PUNPCKHBW;
6411 case MVT::v8i16: return X86ISD::PUNPCKHWD;
6412 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00006413 llvm_unreachable("Unknown type for unpckh");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006414 }
6415 return 0;
6416}
6417
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006418static inline unsigned getVPERMILOpcode(EVT VT) {
6419 switch(VT.getSimpleVT().SimpleTy) {
6420 case MVT::v4i32:
6421 case MVT::v4f32: return X86ISD::VPERMILPS;
6422 case MVT::v2i64:
6423 case MVT::v2f64: return X86ISD::VPERMILPD;
6424 case MVT::v8i32:
6425 case MVT::v8f32: return X86ISD::VPERMILPSY;
6426 case MVT::v4i64:
6427 case MVT::v4f64: return X86ISD::VPERMILPDY;
6428 default:
6429 llvm_unreachable("Unknown type for vpermil");
6430 }
6431 return 0;
6432}
6433
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006434/// isVectorBroadcast - Check if the node chain is suitable to be xformed to
6435/// a vbroadcast node. The nodes are suitable whenever we can fold a load coming
6436/// from a 32 or 64 bit scalar. Update Op to the desired load to be folded.
6437static bool isVectorBroadcast(SDValue &Op) {
6438 EVT VT = Op.getValueType();
6439 bool Is256 = VT.getSizeInBits() == 256;
6440
6441 assert((VT.getSizeInBits() == 128 || Is256) &&
6442 "Unsupported type for vbroadcast node");
6443
6444 SDValue V = Op;
6445 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
6446 V = V.getOperand(0);
6447
6448 if (Is256 && !(V.hasOneUse() &&
6449 V.getOpcode() == ISD::INSERT_SUBVECTOR &&
6450 V.getOperand(0).getOpcode() == ISD::UNDEF))
6451 return false;
6452
6453 if (Is256)
6454 V = V.getOperand(1);
Bruno Cardoso Lopesa39ccdb2011-09-01 18:15:06 +00006455
6456 if (!V.hasOneUse())
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006457 return false;
6458
6459 // Check the source scalar_to_vector type. 256-bit broadcasts are
6460 // supported for 32/64-bit sizes, while 128-bit ones are only supported
6461 // for 32-bit scalars.
Bruno Cardoso Lopesa39ccdb2011-09-01 18:15:06 +00006462 if (V.getOpcode() != ISD::SCALAR_TO_VECTOR)
6463 return false;
6464
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006465 unsigned ScalarSize = V.getOperand(0).getValueType().getSizeInBits();
6466 if (ScalarSize != 32 && ScalarSize != 64)
6467 return false;
6468 if (!Is256 && ScalarSize == 64)
6469 return false;
6470
6471 V = V.getOperand(0);
6472 if (!MayFoldLoad(V))
6473 return false;
6474
6475 // Return the load node
6476 Op = V;
6477 return true;
6478}
6479
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006480static
6481SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006482 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006483 const X86Subtarget *Subtarget) {
6484 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6485 EVT VT = Op.getValueType();
6486 DebugLoc dl = Op.getDebugLoc();
6487 SDValue V1 = Op.getOperand(0);
6488 SDValue V2 = Op.getOperand(1);
6489
6490 if (isZeroShuffle(SVOp))
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006491 return getZeroVector(VT, Subtarget->hasXMMInt(), DAG, dl);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006492
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006493 // Handle splat operations
6494 if (SVOp->isSplat()) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006495 unsigned NumElem = VT.getVectorNumElements();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006496 int Size = VT.getSizeInBits();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006497 // Special case, this is the only place now where it's allowed to return
6498 // a vector_shuffle operation without using a target specific node, because
6499 // *hopefully* it will be optimized away by the dag combiner. FIXME: should
6500 // this be moved to DAGCombine instead?
6501 if (NumElem <= 4 && CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006502 return Op;
6503
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006504 // Use vbroadcast whenever the splat comes from a foldable load
6505 if (Subtarget->hasAVX() && isVectorBroadcast(V1))
6506 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, V1);
6507
Bruno Cardoso Lopes6a32adc2011-07-25 23:05:25 +00006508 // Handle splats by matching through known shuffle masks
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006509 if ((Size == 128 && NumElem <= 4) ||
6510 (Size == 256 && NumElem < 8))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006511 return SDValue();
6512
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00006513 // All remaning splats are promoted to target supported vector shuffles.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006514 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006515 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006516
6517 // If the shuffle can be profitably rewritten as a narrower shuffle, then
6518 // do it!
6519 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
6520 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6521 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006522 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006523 } else if ((VT == MVT::v4i32 ||
6524 (VT == MVT::v4f32 && Subtarget->hasXMMInt()))) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006525 // FIXME: Figure out a cleaner way to do this.
6526 // Try to make use of movq to zero out the top part.
6527 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
6528 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6529 if (NewOp.getNode()) {
6530 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
6531 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
6532 DAG, Subtarget, dl);
6533 }
6534 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
6535 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6536 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
6537 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
6538 DAG, Subtarget, dl);
6539 }
6540 }
6541 return SDValue();
6542}
6543
Dan Gohman475871a2008-07-27 21:46:04 +00006544SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006545X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00006546 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00006547 SDValue V1 = Op.getOperand(0);
6548 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006549 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006550 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006551 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006552 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006553 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
6554 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00006555 bool V1IsSplat = false;
6556 bool V2IsSplat = false;
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006557 bool HasXMMInt = Subtarget->hasXMMInt();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006558 MachineFunction &MF = DAG.getMachineFunction();
6559 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006560
Dale Johannesen0488fb62010-09-30 23:57:10 +00006561 // Shuffle operations on MMX not supported.
6562 if (isMMX)
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00006563 return Op;
6564
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006565 // Vector shuffle lowering takes 3 steps:
6566 //
6567 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
6568 // narrowing and commutation of operands should be handled.
6569 // 2) Matching of shuffles with known shuffle masks to x86 target specific
6570 // shuffle nodes.
6571 // 3) Rewriting of unmatched masks into new generic shuffle operations,
6572 // so the shuffle can be broken into other shuffles and the legalizer can
6573 // try the lowering again.
6574 //
6575 // The general ideia is that no vector_shuffle operation should be left to
6576 // be matched during isel, all of them must be converted to a target specific
6577 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00006578
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006579 // Normalize the input vectors. Here splats, zeroed vectors, profitable
6580 // narrowing and commutation of operands should be handled. The actual code
6581 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006582 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006583 if (NewOp.getNode())
6584 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00006585
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006586 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
6587 // unpckh_undef). Only use pshufd if speed is more important than size.
6588 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006589 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006590 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
Rafael Espindola23e31012011-07-22 18:56:05 +00006591 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00006592
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006593 if (X86::isMOVDDUPMask(SVOp) &&
6594 (Subtarget->hasSSE3() || Subtarget->hasAVX()) &&
6595 V2IsUndef && RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00006596 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006597
Dale Johannesen0488fb62010-09-30 23:57:10 +00006598 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006599 return getMOVHighToLow(Op, dl, DAG);
6600
6601 // Use to match splats
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006602 if (HasXMMInt && X86::isUNPCKHMask(SVOp) && V2IsUndef &&
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006603 (VT == MVT::v2f64 || VT == MVT::v2i64))
6604 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
6605
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006606 if (X86::isPSHUFDMask(SVOp)) {
6607 // The actual implementation will match the mask in the if above and then
6608 // during isel it can match several different instructions, not only pshufd
6609 // as its name says, sad but true, emulate the behavior for now...
6610 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
6611 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
6612
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006613 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
6614
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006615 if (HasXMMInt && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006616 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
6617
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006618 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V1,
6619 TargetMask, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006620 }
Eric Christopherfd179292009-08-27 18:07:15 +00006621
Evan Chengf26ffe92008-05-29 08:22:04 +00006622 // Check if this can be converted into a logical shift.
6623 bool isLeft = false;
6624 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00006625 SDValue ShVal;
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006626 bool isShift = getSubtarget()->hasXMMInt() &&
6627 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00006628 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006629 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00006630 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006631 EVT EltVT = VT.getVectorElementType();
6632 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006633 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006634 }
Eric Christopherfd179292009-08-27 18:07:15 +00006635
Nate Begeman9008ca62009-04-27 18:41:29 +00006636 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006637 if (V1IsUndef)
6638 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00006639 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00006640 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00006641 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006642 if (HasXMMInt && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006643 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6644
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006645 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006646 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6647 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00006648 }
Eric Christopherfd179292009-08-27 18:07:15 +00006649
Nate Begeman9008ca62009-04-27 18:41:29 +00006650 // FIXME: fold these into legal mask.
Dale Johannesen0488fb62010-09-30 23:57:10 +00006651 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp))
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006652 return getMOVLowToHigh(Op, dl, DAG, HasXMMInt);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006653
Dale Johannesen0488fb62010-09-30 23:57:10 +00006654 if (X86::isMOVHLPSMask(SVOp))
6655 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006656
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00006657 if (X86::isMOVSHDUPMask(SVOp, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006658 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006659
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00006660 if (X86::isMOVSLDUPMask(SVOp, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006661 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00006662
Dale Johannesen0488fb62010-09-30 23:57:10 +00006663 if (X86::isMOVLPMask(SVOp))
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006664 return getMOVLP(Op, dl, DAG, HasXMMInt);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006665
Nate Begeman9008ca62009-04-27 18:41:29 +00006666 if (ShouldXformToMOVHLPS(SVOp) ||
6667 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
6668 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006669
Evan Chengf26ffe92008-05-29 08:22:04 +00006670 if (isShift) {
6671 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006672 EVT EltVT = VT.getVectorElementType();
6673 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006674 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006675 }
Eric Christopherfd179292009-08-27 18:07:15 +00006676
Evan Cheng9eca5e82006-10-25 21:49:50 +00006677 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00006678 // FIXME: This should also accept a bitcast of a splat? Be careful, not
6679 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00006680 V1IsSplat = isSplatVector(V1.getNode());
6681 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006682
Chris Lattner8a594482007-11-25 00:24:49 +00006683 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00006684 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006685 Op = CommuteVectorShuffle(SVOp, DAG);
6686 SVOp = cast<ShuffleVectorSDNode>(Op);
6687 V1 = SVOp->getOperand(0);
6688 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00006689 std::swap(V1IsSplat, V2IsSplat);
6690 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006691 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00006692 }
6693
Nate Begeman9008ca62009-04-27 18:41:29 +00006694 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
6695 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00006696 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00006697 return V1;
6698 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6699 // the instruction selector will not match, so get a canonical MOVL with
6700 // swapped operands to undo the commute.
6701 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006702 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006703
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006704 if (X86::isUNPCKLMask(SVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006705 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006706
6707 if (X86::isUNPCKHMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006708 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006709
Evan Cheng9bbbb982006-10-25 20:48:19 +00006710 if (V2IsSplat) {
6711 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006712 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00006713 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00006714 SDValue NewMask = NormalizeMask(SVOp, DAG);
6715 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
6716 if (NSVOp != SVOp) {
6717 if (X86::isUNPCKLMask(NSVOp, true)) {
6718 return NewMask;
6719 } else if (X86::isUNPCKHMask(NSVOp, true)) {
6720 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006721 }
6722 }
6723 }
6724
Evan Cheng9eca5e82006-10-25 21:49:50 +00006725 if (Commuted) {
6726 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006727 // FIXME: this seems wrong.
6728 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
6729 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006730
6731 if (X86::isUNPCKLMask(NewSVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006732 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V2, V1, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006733
6734 if (X86::isUNPCKHMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006735 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V2, V1, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006736 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006737
Nate Begeman9008ca62009-04-27 18:41:29 +00006738 // Normalize the node to match x86 shuffle ops if needed
Dale Johannesen0488fb62010-09-30 23:57:10 +00006739 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
Nate Begeman9008ca62009-04-27 18:41:29 +00006740 return CommuteVectorShuffle(SVOp, DAG);
6741
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006742 // The checks below are all present in isShuffleMaskLegal, but they are
6743 // inlined here right now to enable us to directly emit target specific
6744 // nodes, and remove one by one until they don't return Op anymore.
6745 SmallVector<int, 16> M;
6746 SVOp->getMask(M);
6747
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006748 if (isPALIGNRMask(M, VT, Subtarget->hasSSSE3() || Subtarget->hasAVX()))
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006749 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
6750 X86::getShufflePALIGNRImmediate(SVOp),
6751 DAG);
6752
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006753 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6754 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00006755 if (VT == MVT::v2f64)
6756 return getTargetShuffleNode(X86ISD::UNPCKLPD, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006757 if (VT == MVT::v2i64)
6758 return getTargetShuffleNode(X86ISD::PUNPCKLQDQ, dl, VT, V1, V1, DAG);
6759 }
6760
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006761 if (isPSHUFHWMask(M, VT))
6762 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
6763 X86::getShufflePSHUFHWImmediate(SVOp),
6764 DAG);
6765
6766 if (isPSHUFLWMask(M, VT))
6767 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
6768 X86::getShufflePSHUFLWImmediate(SVOp),
6769 DAG);
6770
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006771 if (isSHUFPMask(M, VT))
6772 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6773 X86::getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00006774
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006775 if (X86::isUNPCKL_v_undef_Mask(SVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006776 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006777 if (X86::isUNPCKH_v_undef_Mask(SVOp))
Rafael Espindola23e31012011-07-22 18:56:05 +00006778 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006779
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006780 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006781 // Generate target specific nodes for 128 or 256-bit shuffles only
6782 // supported in the AVX instruction set.
6783 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006784
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006785 // Handle VMOVDDUPY permutations
6786 if (isMOVDDUPYMask(SVOp, Subtarget))
6787 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
6788
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006789 // Handle VPERMILPS* permutations
6790 if (isVPERMILPSMask(M, VT, Subtarget))
6791 return getTargetShuffleNode(getVPERMILOpcode(VT), dl, VT, V1,
6792 getShuffleVPERMILPSImmediate(SVOp), DAG);
6793
6794 // Handle VPERMILPD* permutations
6795 if (isVPERMILPDMask(M, VT, Subtarget))
6796 return getTargetShuffleNode(getVPERMILOpcode(VT), dl, VT, V1,
6797 getShuffleVPERMILPDImmediate(SVOp), DAG);
6798
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00006799 // Handle VPERM2F128 permutations
6800 if (isVPERM2F128Mask(M, VT, Subtarget))
6801 return getTargetShuffleNode(X86ISD::VPERM2F128, dl, VT, V1, V2,
6802 getShuffleVPERM2F128Immediate(SVOp), DAG);
6803
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006804 // Handle VSHUFPSY permutations
6805 if (isVSHUFPSYMask(M, VT, Subtarget))
6806 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6807 getShuffleVSHUFPSYImmediate(SVOp), DAG);
6808
6809 // Handle VSHUFPDY permutations
6810 if (isVSHUFPDYMask(M, VT, Subtarget))
6811 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6812 getShuffleVSHUFPDYImmediate(SVOp), DAG);
6813
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006814 //===--------------------------------------------------------------------===//
6815 // Since no target specific shuffle was selected for this generic one,
6816 // lower it into other known shuffles. FIXME: this isn't true yet, but
6817 // this is the plan.
6818 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00006819
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006820 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
6821 if (VT == MVT::v8i16) {
6822 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
6823 if (NewOp.getNode())
6824 return NewOp;
6825 }
6826
6827 if (VT == MVT::v16i8) {
6828 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
6829 if (NewOp.getNode())
6830 return NewOp;
6831 }
6832
6833 // Handle all 128-bit wide vectors with 4 elements, and match them with
6834 // several different shuffle types.
6835 if (NumElems == 4 && VT.getSizeInBits() == 128)
6836 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
6837
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006838 // Handle general 256-bit shuffles
6839 if (VT.is256BitVector())
6840 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
6841
Dan Gohman475871a2008-07-27 21:46:04 +00006842 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006843}
6844
Dan Gohman475871a2008-07-27 21:46:04 +00006845SDValue
6846X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00006847 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006848 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006849 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006850
6851 if (Op.getOperand(0).getValueType().getSizeInBits() != 128)
6852 return SDValue();
6853
Duncan Sands83ec4b62008-06-06 12:08:01 +00006854 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006855 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006856 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006857 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006858 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006859 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006860 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00006861 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
6862 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
6863 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006864 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6865 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006866 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006867 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00006868 Op.getOperand(0)),
6869 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006870 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006871 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006872 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006873 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006874 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00006875 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00006876 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
6877 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006878 // result has a single use which is a store or a bitcast to i32. And in
6879 // the case of a store, it's not worth it if the index is a constant 0,
6880 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00006881 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00006882 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00006883 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006884 if ((User->getOpcode() != ISD::STORE ||
6885 (isa<ConstantSDNode>(Op.getOperand(1)) &&
6886 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006887 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00006888 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00006889 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00006890 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006891 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00006892 Op.getOperand(0)),
6893 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006894 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Owen Anderson825b72b2009-08-11 20:47:22 +00006895 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00006896 // ExtractPS works with constant index.
6897 if (isa<ConstantSDNode>(Op.getOperand(1)))
6898 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006899 }
Dan Gohman475871a2008-07-27 21:46:04 +00006900 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006901}
6902
6903
Dan Gohman475871a2008-07-27 21:46:04 +00006904SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006905X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
6906 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006907 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00006908 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006909
David Greene74a579d2011-02-10 16:57:36 +00006910 SDValue Vec = Op.getOperand(0);
6911 EVT VecVT = Vec.getValueType();
6912
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006913 // If this is a 256-bit vector result, first extract the 128-bit vector and
6914 // then extract the element from the 128-bit vector.
6915 if (VecVT.getSizeInBits() == 256) {
David Greene74a579d2011-02-10 16:57:36 +00006916 DebugLoc dl = Op.getNode()->getDebugLoc();
6917 unsigned NumElems = VecVT.getVectorNumElements();
6918 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00006919 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
6920
6921 // Get the 128-bit vector.
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006922 bool Upper = IdxVal >= NumElems/2;
6923 Vec = Extract128BitVector(Vec,
6924 DAG.getConstant(Upper ? NumElems/2 : 0, MVT::i32), DAG, dl);
David Greene74a579d2011-02-10 16:57:36 +00006925
David Greene74a579d2011-02-10 16:57:36 +00006926 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006927 Upper ? DAG.getConstant(IdxVal-NumElems/2, MVT::i32) : Idx);
David Greene74a579d2011-02-10 16:57:36 +00006928 }
6929
6930 assert(Vec.getValueSizeInBits() <= 128 && "Unexpected vector length");
6931
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006932 if (Subtarget->hasSSE41() || Subtarget->hasAVX()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006933 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006934 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00006935 return Res;
6936 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00006937
Owen Andersone50ed302009-08-10 22:56:29 +00006938 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006939 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006940 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006941 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00006942 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006943 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006944 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006945 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6946 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006947 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006948 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00006949 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006950 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00006951 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00006952 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006953 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00006954 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006955 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006956 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006957 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006958 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006959 if (Idx == 0)
6960 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00006961
Evan Cheng0db9fe62006-04-25 20:13:52 +00006962 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00006963 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006964 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006965 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006966 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006967 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006968 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00006969 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006970 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
6971 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
6972 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006973 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006974 if (Idx == 0)
6975 return Op;
6976
6977 // UNPCKHPD the element to the lowest double word, then movsd.
6978 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
6979 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00006980 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006981 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006982 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006983 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006984 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006985 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006986 }
6987
Dan Gohman475871a2008-07-27 21:46:04 +00006988 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006989}
6990
Dan Gohman475871a2008-07-27 21:46:04 +00006991SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006992X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
6993 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006994 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006995 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006996 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006997
Dan Gohman475871a2008-07-27 21:46:04 +00006998 SDValue N0 = Op.getOperand(0);
6999 SDValue N1 = Op.getOperand(1);
7000 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00007001
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007002 if (VT.getSizeInBits() == 256)
7003 return SDValue();
7004
Dan Gohman8a55ce42009-09-23 21:02:20 +00007005 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00007006 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007007 unsigned Opc;
7008 if (VT == MVT::v8i16)
7009 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007010 else if (VT == MVT::v16i8)
7011 Opc = X86ISD::PINSRB;
7012 else
7013 Opc = X86ISD::PINSRB;
7014
Nate Begeman14d12ca2008-02-11 04:19:36 +00007015 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
7016 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007017 if (N1.getValueType() != MVT::i32)
7018 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7019 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007020 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00007021 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00007022 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007023 // Bits [7:6] of the constant are the source select. This will always be
7024 // zero here. The DAG Combiner may combine an extract_elt index into these
7025 // bits. For example (insert (extract, 3), 2) could be matched by putting
7026 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00007027 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00007028 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00007029 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00007030 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007031 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00007032 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00007033 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00007034 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00007035 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00007036 // PINSR* works with constant index.
7037 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007038 }
Dan Gohman475871a2008-07-27 21:46:04 +00007039 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007040}
7041
Dan Gohman475871a2008-07-27 21:46:04 +00007042SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007043X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007044 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007045 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007046
David Greene6b381262011-02-09 15:32:06 +00007047 DebugLoc dl = Op.getDebugLoc();
7048 SDValue N0 = Op.getOperand(0);
7049 SDValue N1 = Op.getOperand(1);
7050 SDValue N2 = Op.getOperand(2);
7051
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007052 // If this is a 256-bit vector result, first extract the 128-bit vector,
7053 // insert the element into the extracted half and then place it back.
7054 if (VT.getSizeInBits() == 256) {
David Greene6b381262011-02-09 15:32:06 +00007055 if (!isa<ConstantSDNode>(N2))
7056 return SDValue();
7057
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007058 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00007059 unsigned NumElems = VT.getVectorNumElements();
7060 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007061 bool Upper = IdxVal >= NumElems/2;
7062 SDValue Ins128Idx = DAG.getConstant(Upper ? NumElems/2 : 0, MVT::i32);
7063 SDValue V = Extract128BitVector(N0, Ins128Idx, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007064
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007065 // Insert the element into the desired half.
7066 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V,
7067 N1, Upper ? DAG.getConstant(IdxVal-NumElems/2, MVT::i32) : N2);
David Greene6b381262011-02-09 15:32:06 +00007068
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007069 // Insert the changed part back to the 256-bit vector
7070 return Insert128BitVector(N0, V, Ins128Idx, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007071 }
7072
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007073 if (Subtarget->hasSSE41() || Subtarget->hasAVX())
Nate Begeman14d12ca2008-02-11 04:19:36 +00007074 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
7075
Dan Gohman8a55ce42009-09-23 21:02:20 +00007076 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00007077 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00007078
Dan Gohman8a55ce42009-09-23 21:02:20 +00007079 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00007080 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
7081 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007082 if (N1.getValueType() != MVT::i32)
7083 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7084 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007085 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00007086 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007087 }
Dan Gohman475871a2008-07-27 21:46:04 +00007088 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007089}
7090
Dan Gohman475871a2008-07-27 21:46:04 +00007091SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007092X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007093 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007094 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00007095 EVT OpVT = Op.getValueType();
7096
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007097 // If this is a 256-bit vector result, first insert into a 128-bit
7098 // vector and then insert into the 256-bit vector.
7099 if (OpVT.getSizeInBits() > 128) {
7100 // Insert into a 128-bit vector.
7101 EVT VT128 = EVT::getVectorVT(*Context,
7102 OpVT.getVectorElementType(),
7103 OpVT.getVectorNumElements() / 2);
7104
7105 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
7106
7107 // Insert the 128-bit vector.
7108 return Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, OpVT), Op,
7109 DAG.getConstant(0, MVT::i32),
7110 DAG, dl);
7111 }
7112
Chris Lattnerf172ecd2010-07-04 23:07:25 +00007113 if (Op.getValueType() == MVT::v1i64 &&
7114 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00007115 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00007116
Owen Anderson825b72b2009-08-11 20:47:22 +00007117 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00007118 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
7119 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007120 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00007121 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007122}
7123
David Greene91585092011-01-26 15:38:49 +00007124// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
7125// a simple subregister reference or explicit instructions to grab
7126// upper bits of a vector.
7127SDValue
7128X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7129 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00007130 DebugLoc dl = Op.getNode()->getDebugLoc();
7131 SDValue Vec = Op.getNode()->getOperand(0);
7132 SDValue Idx = Op.getNode()->getOperand(1);
7133
7134 if (Op.getNode()->getValueType(0).getSizeInBits() == 128
7135 && Vec.getNode()->getValueType(0).getSizeInBits() == 256) {
7136 return Extract128BitVector(Vec, Idx, DAG, dl);
7137 }
David Greene91585092011-01-26 15:38:49 +00007138 }
7139 return SDValue();
7140}
7141
David Greenecfe33c42011-01-26 19:13:22 +00007142// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
7143// simple superregister reference or explicit instructions to insert
7144// the upper bits of a vector.
7145SDValue
7146X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7147 if (Subtarget->hasAVX()) {
7148 DebugLoc dl = Op.getNode()->getDebugLoc();
7149 SDValue Vec = Op.getNode()->getOperand(0);
7150 SDValue SubVec = Op.getNode()->getOperand(1);
7151 SDValue Idx = Op.getNode()->getOperand(2);
7152
7153 if (Op.getNode()->getValueType(0).getSizeInBits() == 256
7154 && SubVec.getNode()->getValueType(0).getSizeInBits() == 128) {
David Greenea5f26012011-02-07 19:36:54 +00007155 return Insert128BitVector(Vec, SubVec, Idx, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00007156 }
7157 }
7158 return SDValue();
7159}
7160
Bill Wendling056292f2008-09-16 21:48:12 +00007161// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7162// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7163// one of the above mentioned nodes. It has to be wrapped because otherwise
7164// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7165// be used to form addressing mode. These wrapped nodes will be selected
7166// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00007167SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007168X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007169 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007170
Chris Lattner41621a22009-06-26 19:22:52 +00007171 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7172 // global base reg.
7173 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007174 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007175 CodeModel::Model M = getTargetMachine().getCodeModel();
7176
Chris Lattner4f066492009-07-11 20:29:19 +00007177 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007178 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007179 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007180 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007181 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007182 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007183 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007184
Evan Cheng1606e8e2009-03-13 07:51:59 +00007185 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00007186 CP->getAlignment(),
7187 CP->getOffset(), OpFlag);
7188 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00007189 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007190 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00007191 if (OpFlag) {
7192 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007193 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007194 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007195 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007196 }
7197
7198 return Result;
7199}
7200
Dan Gohmand858e902010-04-17 15:26:15 +00007201SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007202 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007203
Chris Lattner18c59872009-06-27 04:16:01 +00007204 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7205 // global base reg.
7206 unsigned char OpFlag = 0;
7207 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007208 CodeModel::Model M = getTargetMachine().getCodeModel();
7209
Chris Lattner4f066492009-07-11 20:29:19 +00007210 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007211 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007212 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007213 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007214 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007215 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007216 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007217
Chris Lattner18c59872009-06-27 04:16:01 +00007218 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7219 OpFlag);
7220 DebugLoc DL = JT->getDebugLoc();
7221 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007222
Chris Lattner18c59872009-06-27 04:16:01 +00007223 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00007224 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00007225 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7226 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007227 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007228 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007229
Chris Lattner18c59872009-06-27 04:16:01 +00007230 return Result;
7231}
7232
7233SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007234X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007235 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00007236
Chris Lattner18c59872009-06-27 04:16:01 +00007237 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7238 // global base reg.
7239 unsigned char OpFlag = 0;
7240 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007241 CodeModel::Model M = getTargetMachine().getCodeModel();
7242
Chris Lattner4f066492009-07-11 20:29:19 +00007243 if (Subtarget->isPICStyleRIPRel() &&
Eli Friedman586272d2011-08-11 01:48:05 +00007244 (M == CodeModel::Small || M == CodeModel::Kernel)) {
7245 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
7246 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00007247 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00007248 } else if (Subtarget->isPICStyleGOT()) {
7249 OpFlag = X86II::MO_GOT;
7250 } else if (Subtarget->isPICStyleStubPIC()) {
7251 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
7252 } else if (Subtarget->isPICStyleStubNoDynamic()) {
7253 OpFlag = X86II::MO_DARWIN_NONLAZY;
7254 }
Eric Christopherfd179292009-08-27 18:07:15 +00007255
Chris Lattner18c59872009-06-27 04:16:01 +00007256 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00007257
Chris Lattner18c59872009-06-27 04:16:01 +00007258 DebugLoc DL = Op.getDebugLoc();
7259 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007260
7261
Chris Lattner18c59872009-06-27 04:16:01 +00007262 // With PIC, the address is actually $g + Offset.
7263 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007264 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007265 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7266 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007267 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007268 Result);
7269 }
Eric Christopherfd179292009-08-27 18:07:15 +00007270
Eli Friedman586272d2011-08-11 01:48:05 +00007271 // For symbols that require a load from a stub to get the address, emit the
7272 // load.
7273 if (isGlobalStubReference(OpFlag))
7274 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
7275 MachinePointerInfo::getGOT(), false, false, 0);
7276
Chris Lattner18c59872009-06-27 04:16:01 +00007277 return Result;
7278}
7279
Dan Gohman475871a2008-07-27 21:46:04 +00007280SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007281X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007282 // Create the TargetBlockAddressAddress node.
7283 unsigned char OpFlags =
7284 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007285 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007286 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00007287 DebugLoc dl = Op.getDebugLoc();
7288 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
7289 /*isTarget=*/true, OpFlags);
7290
Dan Gohmanf705adb2009-10-30 01:28:02 +00007291 if (Subtarget->isPICStyleRIPRel() &&
7292 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007293 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7294 else
7295 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007296
Dan Gohman29cbade2009-11-20 23:18:13 +00007297 // With PIC, the address is actually $g + Offset.
7298 if (isGlobalRelativeToPICBase(OpFlags)) {
7299 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7300 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7301 Result);
7302 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007303
7304 return Result;
7305}
7306
7307SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00007308X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00007309 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00007310 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007311 // Create the TargetGlobalAddress node, folding in the constant
7312 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007313 unsigned char OpFlags =
7314 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007315 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007316 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007317 if (OpFlags == X86II::MO_NO_FLAG &&
7318 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007319 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007320 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007321 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007322 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007323 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007324 }
Eric Christopherfd179292009-08-27 18:07:15 +00007325
Chris Lattner4f066492009-07-11 20:29:19 +00007326 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007327 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007328 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7329 else
7330 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007331
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007332 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007333 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007334 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7335 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007336 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007337 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007338
Chris Lattner36c25012009-07-10 07:34:39 +00007339 // For globals that require a load from a stub to get the address, emit the
7340 // load.
7341 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007342 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00007343 MachinePointerInfo::getGOT(), false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007344
Dan Gohman6520e202008-10-18 02:06:02 +00007345 // If there was a non-zero offset that we didn't fold, create an explicit
7346 // addition for it.
7347 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007348 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007349 DAG.getConstant(Offset, getPointerTy()));
7350
Evan Cheng0db9fe62006-04-25 20:13:52 +00007351 return Result;
7352}
7353
Evan Chengda43bcf2008-09-24 00:05:32 +00007354SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007355X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007356 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007357 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007358 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007359}
7360
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007361static SDValue
7362GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007363 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00007364 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007365 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007366 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007367 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007368 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007369 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007370 GA->getOffset(),
7371 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007372 if (InFlag) {
7373 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00007374 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007375 } else {
7376 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00007377 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007378 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007379
7380 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007381 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007382
Rafael Espindola15f1b662009-04-24 12:59:40 +00007383 SDValue Flag = Chain.getValue(1);
7384 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007385}
7386
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007387// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007388static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007389LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007390 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007391 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00007392 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
7393 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007394 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007395 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007396 InFlag = Chain.getValue(1);
7397
Chris Lattnerb903bed2009-06-26 21:20:29 +00007398 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007399}
7400
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007401// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007402static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007403LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007404 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007405 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7406 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007407}
7408
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007409// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
7410// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007411static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007412 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00007413 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007414 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00007415
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007416 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
7417 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
7418 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00007419
Michael J. Spencerec38de22010-10-10 22:04:20 +00007420 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007421 DAG.getIntPtrConstant(0),
7422 MachinePointerInfo(Ptr), false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00007423
Chris Lattnerb903bed2009-06-26 21:20:29 +00007424 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007425 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
7426 // initialexec.
7427 unsigned WrapperKind = X86ISD::Wrapper;
7428 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007429 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00007430 } else if (is64Bit) {
7431 assert(model == TLSModel::InitialExec);
7432 OperandFlags = X86II::MO_GOTTPOFF;
7433 WrapperKind = X86ISD::WrapperRIP;
7434 } else {
7435 assert(model == TLSModel::InitialExec);
7436 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00007437 }
Eric Christopherfd179292009-08-27 18:07:15 +00007438
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007439 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
7440 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00007441 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00007442 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007443 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007444 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007445
Rafael Espindola9a580232009-02-27 13:37:18 +00007446 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007447 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00007448 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007449
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007450 // The address of the thread local variable is the add of the thread
7451 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00007452 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007453}
7454
Dan Gohman475871a2008-07-27 21:46:04 +00007455SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007456X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00007457
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007458 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00007459 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00007460
Eric Christopher30ef0e52010-06-03 04:07:48 +00007461 if (Subtarget->isTargetELF()) {
7462 // TODO: implement the "local dynamic" model
7463 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00007464
Eric Christopher30ef0e52010-06-03 04:07:48 +00007465 // If GV is an alias then use the aliasee for determining
7466 // thread-localness.
7467 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
7468 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007469
7470 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00007471 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00007472
Eric Christopher30ef0e52010-06-03 04:07:48 +00007473 switch (model) {
7474 case TLSModel::GeneralDynamic:
7475 case TLSModel::LocalDynamic: // not implemented
7476 if (Subtarget->is64Bit())
7477 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
7478 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00007479
Eric Christopher30ef0e52010-06-03 04:07:48 +00007480 case TLSModel::InitialExec:
7481 case TLSModel::LocalExec:
7482 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
7483 Subtarget->is64Bit());
7484 }
7485 } else if (Subtarget->isTargetDarwin()) {
7486 // Darwin only has one model of TLS. Lower to that.
7487 unsigned char OpFlag = 0;
7488 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7489 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007490
Eric Christopher30ef0e52010-06-03 04:07:48 +00007491 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7492 // global base reg.
7493 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7494 !Subtarget->is64Bit();
7495 if (PIC32)
7496 OpFlag = X86II::MO_TLVP_PIC_BASE;
7497 else
7498 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007499 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007500 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00007501 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007502 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007503 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007504
Eric Christopher30ef0e52010-06-03 04:07:48 +00007505 // With PIC32, the address is actually $g + Offset.
7506 if (PIC32)
7507 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7508 DAG.getNode(X86ISD::GlobalBaseReg,
7509 DebugLoc(), getPointerTy()),
7510 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007511
Eric Christopher30ef0e52010-06-03 04:07:48 +00007512 // Lowering the machine isd will make sure everything is in the right
7513 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007514 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007515 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00007516 SDValue Args[] = { Chain, Offset };
7517 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007518
Eric Christopher30ef0e52010-06-03 04:07:48 +00007519 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7520 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7521 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007522
Eric Christopher30ef0e52010-06-03 04:07:48 +00007523 // And our return value (tls address) is in the standard call return value
7524 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007525 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
7526 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007527 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007528
Eric Christopher30ef0e52010-06-03 04:07:48 +00007529 assert(false &&
7530 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00007531
Torok Edwinc23197a2009-07-14 16:55:14 +00007532 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00007533 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007534}
7535
Evan Cheng0db9fe62006-04-25 20:13:52 +00007536
Nadav Rotem43012222011-05-11 08:12:09 +00007537/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00007538/// take a 2 x i32 value to shift plus a shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +00007539SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00007540 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00007541 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007542 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007543 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007544 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00007545 SDValue ShOpLo = Op.getOperand(0);
7546 SDValue ShOpHi = Op.getOperand(1);
7547 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00007548 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00007549 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00007550 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00007551
Dan Gohman475871a2008-07-27 21:46:04 +00007552 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007553 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007554 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
7555 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007556 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007557 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
7558 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007559 }
Evan Chenge3413162006-01-09 18:33:28 +00007560
Owen Anderson825b72b2009-08-11 20:47:22 +00007561 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
7562 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00007563 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00007564 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00007565
Dan Gohman475871a2008-07-27 21:46:04 +00007566 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00007567 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00007568 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
7569 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00007570
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007571 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007572 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7573 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007574 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007575 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7576 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007577 }
7578
Dan Gohman475871a2008-07-27 21:46:04 +00007579 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00007580 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007581}
Evan Chenga3195e82006-01-12 22:54:21 +00007582
Dan Gohmand858e902010-04-17 15:26:15 +00007583SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
7584 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007585 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00007586
Dale Johannesen0488fb62010-09-30 23:57:10 +00007587 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007588 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007589
Owen Anderson825b72b2009-08-11 20:47:22 +00007590 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00007591 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007592
Eli Friedman36df4992009-05-27 00:47:34 +00007593 // These are really Legal; return the operand so the caller accepts it as
7594 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007595 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00007596 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00007597 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00007598 Subtarget->is64Bit()) {
7599 return Op;
7600 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007601
Bruno Cardoso Lopesa511b8e2011-08-09 17:39:01 +00007602 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007603 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007604 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00007605 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007606 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00007607 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00007608 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007609 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007610 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007611 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
7612}
Evan Cheng0db9fe62006-04-25 20:13:52 +00007613
Owen Andersone50ed302009-08-10 22:56:29 +00007614SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007615 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00007616 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007617 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00007618 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00007619 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00007620 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007621 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007622 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00007623 else
Owen Anderson825b72b2009-08-11 20:47:22 +00007624 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007625
Chris Lattner492a43e2010-09-22 01:28:21 +00007626 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007627
Stuart Hastings84be9582011-06-02 15:57:11 +00007628 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
7629 MachineMemOperand *MMO;
7630 if (FI) {
7631 int SSFI = FI->getIndex();
7632 MMO =
7633 DAG.getMachineFunction()
7634 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7635 MachineMemOperand::MOLoad, ByteSize, ByteSize);
7636 } else {
7637 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
7638 StackSlot = StackSlot.getOperand(1);
7639 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007640 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007641 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
7642 X86ISD::FILD, DL,
7643 Tys, Ops, array_lengthof(Ops),
7644 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007645
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007646 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007647 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00007648 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007649
7650 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
7651 // shouldn't be necessary except that RFP cannot be live across
7652 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007653 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007654 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
7655 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007656 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00007657 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007658 SDValue Ops[] = {
7659 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
7660 };
Chris Lattner492a43e2010-09-22 01:28:21 +00007661 MachineMemOperand *MMO =
7662 DAG.getMachineFunction()
7663 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007664 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007665
Chris Lattner492a43e2010-09-22 01:28:21 +00007666 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
7667 Ops, array_lengthof(Ops),
7668 Op.getValueType(), MMO);
7669 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007670 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007671 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007672 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007673
Evan Cheng0db9fe62006-04-25 20:13:52 +00007674 return Result;
7675}
7676
Bill Wendling8b8a6362009-01-17 03:56:04 +00007677// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007678SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
7679 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00007680 // This algorithm is not obvious. Here it is in C code, more or less:
7681 /*
7682 double uint64_to_double( uint32_t hi, uint32_t lo ) {
7683 static const __m128i exp = { 0x4330000045300000ULL, 0 };
7684 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00007685
Bill Wendling8b8a6362009-01-17 03:56:04 +00007686 // Copy ints to xmm registers.
7687 __m128i xh = _mm_cvtsi32_si128( hi );
7688 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00007689
Bill Wendling8b8a6362009-01-17 03:56:04 +00007690 // Combine into low half of a single xmm register.
7691 __m128i x = _mm_unpacklo_epi32( xh, xl );
7692 __m128d d;
7693 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00007694
Bill Wendling8b8a6362009-01-17 03:56:04 +00007695 // Merge in appropriate exponents to give the integer bits the right
7696 // magnitude.
7697 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00007698
Bill Wendling8b8a6362009-01-17 03:56:04 +00007699 // Subtract away the biases to deal with the IEEE-754 double precision
7700 // implicit 1.
7701 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00007702
Bill Wendling8b8a6362009-01-17 03:56:04 +00007703 // All conversions up to here are exact. The correctly rounded result is
7704 // calculated using the current rounding mode using the following
7705 // horizontal add.
7706 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
7707 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
7708 // store doesn't really need to be here (except
7709 // maybe to zero the other double)
7710 return sd;
7711 }
7712 */
Dale Johannesen040225f2008-10-21 23:07:49 +00007713
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007714 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00007715 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00007716
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007717 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00007718 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00007719 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
7720 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
7721 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
7722 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00007723 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007724 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007725
Bill Wendling8b8a6362009-01-17 03:56:04 +00007726 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00007727 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007728 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00007729 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007730 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00007731 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007732 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007733
Owen Anderson825b72b2009-08-11 20:47:22 +00007734 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7735 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00007736 Op.getOperand(0),
7737 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007738 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7739 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00007740 Op.getOperand(0),
7741 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007742 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
7743 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00007744 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007745 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00007746 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007747 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
Owen Anderson825b72b2009-08-11 20:47:22 +00007748 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00007749 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007750 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00007751 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007752
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007753 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00007754 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00007755 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
7756 DAG.getUNDEF(MVT::v2f64), ShufMask);
7757 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
7758 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007759 DAG.getIntPtrConstant(0));
7760}
7761
Bill Wendling8b8a6362009-01-17 03:56:04 +00007762// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007763SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
7764 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007765 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007766 // FP constant to bias correct the final result.
7767 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00007768 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007769
7770 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00007771 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00007772 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007773
Eli Friedmanf3704762011-08-29 21:15:46 +00007774 // Zero out the upper parts of the register.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00007775 Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget->hasXMMInt(),
7776 DAG);
Eli Friedmanf3704762011-08-29 21:15:46 +00007777
Owen Anderson825b72b2009-08-11 20:47:22 +00007778 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007779 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007780 DAG.getIntPtrConstant(0));
7781
7782 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007783 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007784 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007785 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007786 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007787 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007788 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007789 MVT::v2f64, Bias)));
7790 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007791 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007792 DAG.getIntPtrConstant(0));
7793
7794 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007795 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007796
7797 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00007798 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00007799
Owen Anderson825b72b2009-08-11 20:47:22 +00007800 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007801 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00007802 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007803 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007804 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00007805 }
7806
7807 // Handle final rounding.
7808 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00007809}
7810
Dan Gohmand858e902010-04-17 15:26:15 +00007811SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
7812 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00007813 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007814 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007815
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007816 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00007817 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
7818 // the optimization here.
7819 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00007820 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00007821
Owen Andersone50ed302009-08-10 22:56:29 +00007822 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007823 EVT DstVT = Op.getValueType();
7824 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007825 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007826 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007827 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00007828
7829 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00007830 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007831 if (SrcVT == MVT::i32) {
7832 SDValue WordOff = DAG.getConstant(4, getPointerTy());
7833 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
7834 getPointerTy(), StackSlot, WordOff);
7835 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007836 StackSlot, MachinePointerInfo(),
7837 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007838 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007839 OffsetSlot, MachinePointerInfo(),
7840 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007841 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
7842 return Fild;
7843 }
7844
7845 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
7846 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007847 StackSlot, MachinePointerInfo(),
7848 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007849 // For i64 source, we need to add the appropriate power of 2 if the input
7850 // was negative. This is the same as the optimization in
7851 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
7852 // we must be careful to do the computation in x87 extended precision, not
7853 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00007854 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
7855 MachineMemOperand *MMO =
7856 DAG.getMachineFunction()
7857 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7858 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007859
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007860 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
7861 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007862 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
7863 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007864
7865 APInt FF(32, 0x5F800000ULL);
7866
7867 // Check whether the sign bit is set.
7868 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
7869 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
7870 ISD::SETLT);
7871
7872 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
7873 SDValue FudgePtr = DAG.getConstantPool(
7874 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
7875 getPointerTy());
7876
7877 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
7878 SDValue Zero = DAG.getIntPtrConstant(0);
7879 SDValue Four = DAG.getIntPtrConstant(4);
7880 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
7881 Zero, Four);
7882 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
7883
7884 // Load the value out, extending it from f32 to f80.
7885 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00007886 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00007887 FudgePtr, MachinePointerInfo::getConstantPool(),
7888 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007889 // Extend everything to 80 bits to force it to be done on x87.
7890 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
7891 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007892}
7893
Dan Gohman475871a2008-07-27 21:46:04 +00007894std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00007895FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00007896 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00007897
Owen Andersone50ed302009-08-10 22:56:29 +00007898 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00007899
7900 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007901 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
7902 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00007903 }
7904
Owen Anderson825b72b2009-08-11 20:47:22 +00007905 assert(DstTy.getSimpleVT() <= MVT::i64 &&
7906 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00007907 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00007908
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007909 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007910 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00007911 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007912 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00007913 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00007914 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00007915 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007916 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007917
Evan Cheng87c89352007-10-15 20:11:21 +00007918 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
7919 // stack slot.
7920 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00007921 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00007922 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007923 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00007924
Michael J. Spencerec38de22010-10-10 22:04:20 +00007925
7926
Evan Cheng0db9fe62006-04-25 20:13:52 +00007927 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00007928 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007929 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007930 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
7931 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
7932 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007933 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007934
Dan Gohman475871a2008-07-27 21:46:04 +00007935 SDValue Chain = DAG.getEntryNode();
7936 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00007937 EVT TheVT = Op.getOperand(0).getValueType();
7938 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007939 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00007940 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007941 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007942 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007943 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00007944 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00007945 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00007946 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00007947
Chris Lattner492a43e2010-09-22 01:28:21 +00007948 MachineMemOperand *MMO =
7949 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7950 MachineMemOperand::MOLoad, MemSize, MemSize);
7951 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
7952 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007953 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00007954 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007955 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
7956 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007957
Chris Lattner07290932010-09-22 01:05:16 +00007958 MachineMemOperand *MMO =
7959 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7960 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007961
Evan Cheng0db9fe62006-04-25 20:13:52 +00007962 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00007963 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00007964 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
7965 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00007966
Chris Lattner27a6c732007-11-24 07:07:01 +00007967 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007968}
7969
Dan Gohmand858e902010-04-17 15:26:15 +00007970SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
7971 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00007972 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007973 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007974
Eli Friedman948e95a2009-05-23 09:59:16 +00007975 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00007976 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00007977 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
7978 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00007979
Chris Lattner27a6c732007-11-24 07:07:01 +00007980 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007981 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007982 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00007983}
7984
Dan Gohmand858e902010-04-17 15:26:15 +00007985SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
7986 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00007987 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
7988 SDValue FIST = Vals.first, StackSlot = Vals.second;
7989 assert(FIST.getNode() && "Unexpected failure");
7990
7991 // Load the result.
7992 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007993 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007994}
7995
Dan Gohmand858e902010-04-17 15:26:15 +00007996SDValue X86TargetLowering::LowerFABS(SDValue Op,
7997 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007998 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007999 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008000 EVT VT = Op.getValueType();
8001 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008002 if (VT.isVector())
8003 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008004 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008005 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008006 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00008007 CV.push_back(C);
8008 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008009 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008010 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00008011 CV.push_back(C);
8012 CV.push_back(C);
8013 CV.push_back(C);
8014 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008015 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008016 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008017 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008018 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008019 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00008020 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008021 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008022}
8023
Dan Gohmand858e902010-04-17 15:26:15 +00008024SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008025 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008026 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008027 EVT VT = Op.getValueType();
8028 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00008029 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00008030 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008031 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008032 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008033 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00008034 CV.push_back(C);
8035 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008036 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008037 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00008038 CV.push_back(C);
8039 CV.push_back(C);
8040 CV.push_back(C);
8041 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008042 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008043 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008044 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008045 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008046 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00008047 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00008048 if (VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008049 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008050 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008051 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00008052 Op.getOperand(0)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008053 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00008054 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00008055 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00008056 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008057}
8058
Dan Gohmand858e902010-04-17 15:26:15 +00008059SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008060 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00008061 SDValue Op0 = Op.getOperand(0);
8062 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008063 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008064 EVT VT = Op.getValueType();
8065 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00008066
8067 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008068 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008069 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008070 SrcVT = VT;
8071 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008072 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008073 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008074 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008075 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008076 }
8077
8078 // At this point the operands and the result should have the same
8079 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00008080
Evan Cheng68c47cb2007-01-05 07:55:56 +00008081 // First get the sign bit of second operand.
8082 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008083 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008084 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
8085 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008086 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008087 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
8088 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8089 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8090 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008091 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008092 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008093 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008094 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008095 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00008096 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008097 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008098
8099 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008100 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008101 // Op0 is MVT::f32, Op1 is MVT::f64.
8102 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
8103 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
8104 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008105 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00008106 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00008107 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008108 }
8109
Evan Cheng73d6cf12007-01-05 21:37:56 +00008110 // Clear first operand sign bit.
8111 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00008112 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008113 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
8114 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008115 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008116 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
8117 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8118 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8119 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008120 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008121 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008122 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008123 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008124 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00008125 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008126 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008127
8128 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00008129 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008130}
8131
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008132SDValue X86TargetLowering::LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) const {
8133 SDValue N0 = Op.getOperand(0);
8134 DebugLoc dl = Op.getDebugLoc();
8135 EVT VT = Op.getValueType();
8136
8137 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
8138 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
8139 DAG.getConstant(1, VT));
8140 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
8141}
8142
Dan Gohman076aee32009-03-04 19:44:21 +00008143/// Emit nodes that will be selected as "test Op0,Op0", or something
8144/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008145SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008146 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008147 DebugLoc dl = Op.getDebugLoc();
8148
Dan Gohman31125812009-03-07 01:58:32 +00008149 // CF and OF aren't always set the way we want. Determine which
8150 // of these we need.
8151 bool NeedCF = false;
8152 bool NeedOF = false;
8153 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008154 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00008155 case X86::COND_A: case X86::COND_AE:
8156 case X86::COND_B: case X86::COND_BE:
8157 NeedCF = true;
8158 break;
8159 case X86::COND_G: case X86::COND_GE:
8160 case X86::COND_L: case X86::COND_LE:
8161 case X86::COND_O: case X86::COND_NO:
8162 NeedOF = true;
8163 break;
Dan Gohman31125812009-03-07 01:58:32 +00008164 }
8165
Dan Gohman076aee32009-03-04 19:44:21 +00008166 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00008167 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
8168 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008169 if (Op.getResNo() != 0 || NeedOF || NeedCF)
8170 // Emit a CMP with 0, which is the TEST pattern.
8171 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8172 DAG.getConstant(0, Op.getValueType()));
8173
8174 unsigned Opcode = 0;
8175 unsigned NumOperands = 0;
8176 switch (Op.getNode()->getOpcode()) {
8177 case ISD::ADD:
8178 // Due to an isel shortcoming, be conservative if this add is likely to be
8179 // selected as part of a load-modify-store instruction. When the root node
8180 // in a match is a store, isel doesn't know how to remap non-chain non-flag
8181 // uses of other nodes in the match, such as the ADD in this case. This
8182 // leads to the ADD being left around and reselected, with the result being
8183 // two adds in the output. Alas, even if none our users are stores, that
8184 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
8185 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
8186 // climbing the DAG back to the root, and it doesn't seem to be worth the
8187 // effort.
8188 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Dan Gohman076aee32009-03-04 19:44:21 +00008189 UE = Op.getNode()->use_end(); UI != UE; ++UI)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008190 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
8191 goto default_case;
8192
8193 if (ConstantSDNode *C =
8194 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
8195 // An add of one will be selected as an INC.
8196 if (C->getAPIntValue() == 1) {
8197 Opcode = X86ISD::INC;
8198 NumOperands = 1;
8199 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00008200 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008201
8202 // An add of negative one (subtract of one) will be selected as a DEC.
8203 if (C->getAPIntValue().isAllOnesValue()) {
8204 Opcode = X86ISD::DEC;
8205 NumOperands = 1;
8206 break;
8207 }
Dan Gohman076aee32009-03-04 19:44:21 +00008208 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008209
8210 // Otherwise use a regular EFLAGS-setting add.
8211 Opcode = X86ISD::ADD;
8212 NumOperands = 2;
8213 break;
8214 case ISD::AND: {
8215 // If the primary and result isn't used, don't bother using X86ISD::AND,
8216 // because a TEST instruction will be better.
8217 bool NonFlagUse = false;
8218 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8219 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
8220 SDNode *User = *UI;
8221 unsigned UOpNo = UI.getOperandNo();
8222 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
8223 // Look pass truncate.
8224 UOpNo = User->use_begin().getOperandNo();
8225 User = *User->use_begin();
8226 }
8227
8228 if (User->getOpcode() != ISD::BRCOND &&
8229 User->getOpcode() != ISD::SETCC &&
8230 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
8231 NonFlagUse = true;
8232 break;
8233 }
Dan Gohman076aee32009-03-04 19:44:21 +00008234 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008235
8236 if (!NonFlagUse)
8237 break;
8238 }
8239 // FALL THROUGH
8240 case ISD::SUB:
8241 case ISD::OR:
8242 case ISD::XOR:
8243 // Due to the ISEL shortcoming noted above, be conservative if this op is
8244 // likely to be selected as part of a load-modify-store instruction.
8245 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8246 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8247 if (UI->getOpcode() == ISD::STORE)
8248 goto default_case;
8249
8250 // Otherwise use a regular EFLAGS-setting instruction.
8251 switch (Op.getNode()->getOpcode()) {
8252 default: llvm_unreachable("unexpected operator!");
8253 case ISD::SUB: Opcode = X86ISD::SUB; break;
8254 case ISD::OR: Opcode = X86ISD::OR; break;
8255 case ISD::XOR: Opcode = X86ISD::XOR; break;
8256 case ISD::AND: Opcode = X86ISD::AND; break;
8257 }
8258
8259 NumOperands = 2;
8260 break;
8261 case X86ISD::ADD:
8262 case X86ISD::SUB:
8263 case X86ISD::INC:
8264 case X86ISD::DEC:
8265 case X86ISD::OR:
8266 case X86ISD::XOR:
8267 case X86ISD::AND:
8268 return SDValue(Op.getNode(), 1);
8269 default:
8270 default_case:
8271 break;
Dan Gohman076aee32009-03-04 19:44:21 +00008272 }
8273
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008274 if (Opcode == 0)
8275 // Emit a CMP with 0, which is the TEST pattern.
8276 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8277 DAG.getConstant(0, Op.getValueType()));
8278
8279 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
8280 SmallVector<SDValue, 4> Ops;
8281 for (unsigned i = 0; i != NumOperands; ++i)
8282 Ops.push_back(Op.getOperand(i));
8283
8284 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
8285 DAG.ReplaceAllUsesWith(Op, New);
8286 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00008287}
8288
8289/// Emit nodes that will be selected as "cmp Op0,Op1", or something
8290/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008291SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008292 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008293 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
8294 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00008295 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00008296
8297 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00008298 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00008299}
8300
Evan Chengd40d03e2010-01-06 19:38:29 +00008301/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
8302/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00008303SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
8304 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008305 SDValue Op0 = And.getOperand(0);
8306 SDValue Op1 = And.getOperand(1);
8307 if (Op0.getOpcode() == ISD::TRUNCATE)
8308 Op0 = Op0.getOperand(0);
8309 if (Op1.getOpcode() == ISD::TRUNCATE)
8310 Op1 = Op1.getOperand(0);
8311
Evan Chengd40d03e2010-01-06 19:38:29 +00008312 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008313 if (Op1.getOpcode() == ISD::SHL)
8314 std::swap(Op0, Op1);
8315 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008316 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
8317 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008318 // If we looked past a truncate, check that it's only truncating away
8319 // known zeros.
8320 unsigned BitWidth = Op0.getValueSizeInBits();
8321 unsigned AndBitWidth = And.getValueSizeInBits();
8322 if (BitWidth > AndBitWidth) {
8323 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
8324 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
8325 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
8326 return SDValue();
8327 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008328 LHS = Op1;
8329 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00008330 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008331 } else if (Op1.getOpcode() == ISD::Constant) {
8332 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
8333 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00008334 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
8335 LHS = AndLHS.getOperand(0);
8336 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008337 }
Evan Chengd40d03e2010-01-06 19:38:29 +00008338 }
Evan Cheng0488db92007-09-25 01:57:46 +00008339
Evan Chengd40d03e2010-01-06 19:38:29 +00008340 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00008341 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00008342 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00008343 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00008344 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00008345 // Also promote i16 to i32 for performance / code size reason.
8346 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00008347 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00008348 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00008349
Evan Chengd40d03e2010-01-06 19:38:29 +00008350 // If the operand types disagree, extend the shift amount to match. Since
8351 // BT ignores high bits (like shifts) we can use anyextend.
8352 if (LHS.getValueType() != RHS.getValueType())
8353 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008354
Evan Chengd40d03e2010-01-06 19:38:29 +00008355 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
8356 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
8357 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8358 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00008359 }
8360
Evan Cheng54de3ea2010-01-05 06:52:31 +00008361 return SDValue();
8362}
8363
Dan Gohmand858e902010-04-17 15:26:15 +00008364SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Duncan Sands28b77e92011-09-06 19:07:46 +00008365
8366 if (Op.getValueType().isVector()) return LowerVSETCC(Op, DAG);
8367
Evan Cheng54de3ea2010-01-05 06:52:31 +00008368 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
8369 SDValue Op0 = Op.getOperand(0);
8370 SDValue Op1 = Op.getOperand(1);
8371 DebugLoc dl = Op.getDebugLoc();
8372 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
8373
8374 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00008375 // Lower (X & (1 << N)) == 0 to BT(X, N).
8376 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
8377 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00008378 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008379 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00008380 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008381 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
8382 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
8383 if (NewSetCC.getNode())
8384 return NewSetCC;
8385 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00008386
Chris Lattner481eebc2010-12-19 21:23:48 +00008387 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
8388 // these.
8389 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00008390 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00008391 cast<ConstantSDNode>(Op1)->isNullValue()) &&
8392 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008393
Chris Lattner481eebc2010-12-19 21:23:48 +00008394 // If the input is a setcc, then reuse the input setcc or use a new one with
8395 // the inverted condition.
8396 if (Op0.getOpcode() == X86ISD::SETCC) {
8397 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
8398 bool Invert = (CC == ISD::SETNE) ^
8399 cast<ConstantSDNode>(Op1)->isNullValue();
8400 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008401
Evan Cheng2c755ba2010-02-27 07:36:59 +00008402 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00008403 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8404 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
8405 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008406 }
8407
Evan Chenge5b51ac2010-04-17 06:13:15 +00008408 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00008409 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008410 if (X86CC == X86::COND_INVALID)
8411 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008412
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008413 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00008414 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008415 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00008416}
8417
Craig Topper89af15e2011-09-18 08:03:58 +00008418// Lower256IntVSETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008419// ones, and then concatenate the result back.
Craig Topper89af15e2011-09-18 08:03:58 +00008420static SDValue Lower256IntVSETCC(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008421 EVT VT = Op.getValueType();
8422
Duncan Sands28b77e92011-09-06 19:07:46 +00008423 assert(VT.getSizeInBits() == 256 && Op.getOpcode() == ISD::SETCC &&
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008424 "Unsupported value type for operation");
8425
8426 int NumElems = VT.getVectorNumElements();
8427 DebugLoc dl = Op.getDebugLoc();
8428 SDValue CC = Op.getOperand(2);
8429 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
8430 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
8431
8432 // Extract the LHS vectors
8433 SDValue LHS = Op.getOperand(0);
8434 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
8435 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
8436
8437 // Extract the RHS vectors
8438 SDValue RHS = Op.getOperand(1);
8439 SDValue RHS1 = Extract128BitVector(RHS, Idx0, DAG, dl);
8440 SDValue RHS2 = Extract128BitVector(RHS, Idx1, DAG, dl);
8441
8442 // Issue the operation on the smaller types and concatenate the result back
8443 MVT EltVT = VT.getVectorElementType().getSimpleVT();
8444 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
8445 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
8446 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
8447 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
8448}
8449
8450
Dan Gohmand858e902010-04-17 15:26:15 +00008451SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008452 SDValue Cond;
8453 SDValue Op0 = Op.getOperand(0);
8454 SDValue Op1 = Op.getOperand(1);
8455 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00008456 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00008457 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
8458 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008459 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00008460
8461 if (isFP) {
8462 unsigned SSECC = 8;
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008463 EVT EltVT = Op0.getValueType().getVectorElementType();
8464 assert(EltVT == MVT::f32 || EltVT == MVT::f64);
8465
8466 unsigned Opc = EltVT == MVT::f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00008467 bool Swap = false;
8468
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008469 // SSE Condition code mapping:
8470 // 0 - EQ
8471 // 1 - LT
8472 // 2 - LE
8473 // 3 - UNORD
8474 // 4 - NEQ
8475 // 5 - NLT
8476 // 6 - NLE
8477 // 7 - ORD
Nate Begeman30a0de92008-07-17 16:51:19 +00008478 switch (SetCCOpcode) {
8479 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008480 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00008481 case ISD::SETEQ: SSECC = 0; break;
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008482 case ISD::SETOGT:
8483 case ISD::SETGT: Swap = true; // Fallthrough
Bruno Cardoso Lopes457d53d2011-09-12 21:24:07 +00008484 case ISD::SETLT:
8485 case ISD::SETOLT: SSECC = 1; break;
8486 case ISD::SETOGE:
8487 case ISD::SETGE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008488 case ISD::SETLE:
8489 case ISD::SETOLE: SSECC = 2; break;
8490 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008491 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00008492 case ISD::SETNE: SSECC = 4; break;
8493 case ISD::SETULE: Swap = true;
8494 case ISD::SETUGE: SSECC = 5; break;
8495 case ISD::SETULT: Swap = true;
8496 case ISD::SETUGT: SSECC = 6; break;
8497 case ISD::SETO: SSECC = 7; break;
8498 }
8499 if (Swap)
8500 std::swap(Op0, Op1);
8501
Nate Begemanfb8ead02008-07-25 19:05:58 +00008502 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00008503 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00008504 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00008505 SDValue UNORD, EQ;
Bruno Cardoso Lopes809f17f2011-09-13 19:33:00 +00008506 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
8507 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00008508 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00008509 }
8510 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00008511 SDValue ORD, NEQ;
Bruno Cardoso Lopes809f17f2011-09-13 19:33:00 +00008512 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
8513 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00008514 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00008515 }
Torok Edwinc23197a2009-07-14 16:55:14 +00008516 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00008517 }
8518 // Handle all other FP comparisons here.
Bruno Cardoso Lopes809f17f2011-09-13 19:33:00 +00008519 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00008520 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008521
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008522 // Break 256-bit integer vector compare into smaller ones.
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008523 if (!isFP && VT.getSizeInBits() == 256)
Craig Topper89af15e2011-09-18 08:03:58 +00008524 return Lower256IntVSETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008525
Nate Begeman30a0de92008-07-17 16:51:19 +00008526 // We are handling one of the integer comparisons here. Since SSE only has
8527 // GT and EQ comparisons for integer, swapping operands and multiple
8528 // operations may be required for some comparisons.
8529 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
8530 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00008531
Owen Anderson825b72b2009-08-11 20:47:22 +00008532 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00008533 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00008534 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00008535 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00008536 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
8537 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008538 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008539
Nate Begeman30a0de92008-07-17 16:51:19 +00008540 switch (SetCCOpcode) {
8541 default: break;
8542 case ISD::SETNE: Invert = true;
8543 case ISD::SETEQ: Opc = EQOpc; break;
8544 case ISD::SETLT: Swap = true;
8545 case ISD::SETGT: Opc = GTOpc; break;
8546 case ISD::SETGE: Swap = true;
8547 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
8548 case ISD::SETULT: Swap = true;
8549 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
8550 case ISD::SETUGE: Swap = true;
8551 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
8552 }
8553 if (Swap)
8554 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008555
Nate Begeman30a0de92008-07-17 16:51:19 +00008556 // Since SSE has no unsigned integer comparisons, we need to flip the sign
8557 // bits of the inputs before performing those operations.
8558 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00008559 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00008560 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
8561 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00008562 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00008563 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
8564 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00008565 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
8566 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00008567 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008568
Dale Johannesenace16102009-02-03 19:33:06 +00008569 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00008570
8571 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00008572 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00008573 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00008574
Nate Begeman30a0de92008-07-17 16:51:19 +00008575 return Result;
8576}
Evan Cheng0488db92007-09-25 01:57:46 +00008577
Evan Cheng370e5342008-12-03 08:38:43 +00008578// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00008579static bool isX86LogicalCmp(SDValue Op) {
8580 unsigned Opc = Op.getNode()->getOpcode();
8581 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
8582 return true;
8583 if (Op.getResNo() == 1 &&
8584 (Opc == X86ISD::ADD ||
8585 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00008586 Opc == X86ISD::ADC ||
8587 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00008588 Opc == X86ISD::SMUL ||
8589 Opc == X86ISD::UMUL ||
8590 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00008591 Opc == X86ISD::DEC ||
8592 Opc == X86ISD::OR ||
8593 Opc == X86ISD::XOR ||
8594 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00008595 return true;
8596
Chris Lattner9637d5b2010-12-05 07:49:54 +00008597 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
8598 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008599
Dan Gohman076aee32009-03-04 19:44:21 +00008600 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00008601}
8602
Chris Lattnera2b56002010-12-05 01:23:24 +00008603static bool isZero(SDValue V) {
8604 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8605 return C && C->isNullValue();
8606}
8607
Chris Lattner96908b12010-12-05 02:00:51 +00008608static bool isAllOnes(SDValue V) {
8609 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8610 return C && C->isAllOnesValue();
8611}
8612
Dan Gohmand858e902010-04-17 15:26:15 +00008613SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008614 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008615 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00008616 SDValue Op1 = Op.getOperand(1);
8617 SDValue Op2 = Op.getOperand(2);
8618 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008619 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00008620
Dan Gohman1a492952009-10-20 16:22:37 +00008621 if (Cond.getOpcode() == ISD::SETCC) {
8622 SDValue NewCond = LowerSETCC(Cond, DAG);
8623 if (NewCond.getNode())
8624 Cond = NewCond;
8625 }
Evan Cheng734503b2006-09-11 02:19:56 +00008626
Chris Lattnera2b56002010-12-05 01:23:24 +00008627 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008628 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00008629 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008630 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008631 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00008632 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
8633 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008634 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008635
Chris Lattnera2b56002010-12-05 01:23:24 +00008636 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008637
8638 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00008639 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
8640 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00008641
8642 SDValue CmpOp0 = Cmp.getOperand(0);
8643 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
8644 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008645
Chris Lattner96908b12010-12-05 02:00:51 +00008646 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00008647 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8648 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008649
Chris Lattner96908b12010-12-05 02:00:51 +00008650 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
8651 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008652
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008653 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00008654 if (N2C == 0 || !N2C->isNullValue())
8655 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
8656 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008657 }
8658 }
8659
Chris Lattnera2b56002010-12-05 01:23:24 +00008660 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00008661 if (Cond.getOpcode() == ISD::AND &&
8662 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8663 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008664 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008665 Cond = Cond.getOperand(0);
8666 }
8667
Evan Cheng3f41d662007-10-08 22:16:29 +00008668 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8669 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00008670 if (Cond.getOpcode() == X86ISD::SETCC ||
8671 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008672 CC = Cond.getOperand(0);
8673
Dan Gohman475871a2008-07-27 21:46:04 +00008674 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008675 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00008676 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00008677
Evan Cheng3f41d662007-10-08 22:16:29 +00008678 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008679 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00008680 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00008681 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00008682
Chris Lattnerd1980a52009-03-12 06:52:53 +00008683 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
8684 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00008685 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008686 addTest = false;
8687 }
8688 }
8689
8690 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008691 // Look pass the truncate.
8692 if (Cond.getOpcode() == ISD::TRUNCATE)
8693 Cond = Cond.getOperand(0);
8694
8695 // We know the result of AND is compared against zero. Try to match
8696 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00008697 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00008698 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00008699 if (NewSetCC.getNode()) {
8700 CC = NewSetCC.getOperand(0);
8701 Cond = NewSetCC.getOperand(1);
8702 addTest = false;
8703 }
8704 }
8705 }
8706
8707 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008708 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00008709 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00008710 }
8711
Benjamin Kramere915ff32010-12-22 23:09:28 +00008712 // a < b ? -1 : 0 -> RES = ~setcc_carry
8713 // a < b ? 0 : -1 -> RES = setcc_carry
8714 // a >= b ? -1 : 0 -> RES = setcc_carry
8715 // a >= b ? 0 : -1 -> RES = ~setcc_carry
8716 if (Cond.getOpcode() == X86ISD::CMP) {
8717 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
8718
8719 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
8720 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
8721 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8722 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
8723 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
8724 return DAG.getNOT(DL, Res, Res.getValueType());
8725 return Res;
8726 }
8727 }
8728
Evan Cheng0488db92007-09-25 01:57:46 +00008729 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
8730 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008731 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008732 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00008733 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00008734}
8735
Evan Cheng370e5342008-12-03 08:38:43 +00008736// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
8737// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
8738// from the AND / OR.
8739static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
8740 Opc = Op.getOpcode();
8741 if (Opc != ISD::OR && Opc != ISD::AND)
8742 return false;
8743 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8744 Op.getOperand(0).hasOneUse() &&
8745 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
8746 Op.getOperand(1).hasOneUse());
8747}
8748
Evan Cheng961d6d42009-02-02 08:19:07 +00008749// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
8750// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00008751static bool isXor1OfSetCC(SDValue Op) {
8752 if (Op.getOpcode() != ISD::XOR)
8753 return false;
8754 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
8755 if (N1C && N1C->getAPIntValue() == 1) {
8756 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8757 Op.getOperand(0).hasOneUse();
8758 }
8759 return false;
8760}
8761
Dan Gohmand858e902010-04-17 15:26:15 +00008762SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008763 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008764 SDValue Chain = Op.getOperand(0);
8765 SDValue Cond = Op.getOperand(1);
8766 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008767 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008768 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00008769
Dan Gohman1a492952009-10-20 16:22:37 +00008770 if (Cond.getOpcode() == ISD::SETCC) {
8771 SDValue NewCond = LowerSETCC(Cond, DAG);
8772 if (NewCond.getNode())
8773 Cond = NewCond;
8774 }
Chris Lattnere55484e2008-12-25 05:34:37 +00008775#if 0
8776 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00008777 else if (Cond.getOpcode() == X86ISD::ADD ||
8778 Cond.getOpcode() == X86ISD::SUB ||
8779 Cond.getOpcode() == X86ISD::SMUL ||
8780 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00008781 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00008782#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00008783
Evan Chengad9c0a32009-12-15 00:53:42 +00008784 // Look pass (and (setcc_carry (cmp ...)), 1).
8785 if (Cond.getOpcode() == ISD::AND &&
8786 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8787 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008788 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008789 Cond = Cond.getOperand(0);
8790 }
8791
Evan Cheng3f41d662007-10-08 22:16:29 +00008792 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8793 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00008794 if (Cond.getOpcode() == X86ISD::SETCC ||
8795 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008796 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008797
Dan Gohman475871a2008-07-27 21:46:04 +00008798 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008799 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00008800 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00008801 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00008802 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008803 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00008804 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00008805 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00008806 default: break;
8807 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00008808 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00008809 // These can only come from an arithmetic instruction with overflow,
8810 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00008811 Cond = Cond.getNode()->getOperand(1);
8812 addTest = false;
8813 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00008814 }
Evan Cheng0488db92007-09-25 01:57:46 +00008815 }
Evan Cheng370e5342008-12-03 08:38:43 +00008816 } else {
8817 unsigned CondOpc;
8818 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
8819 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00008820 if (CondOpc == ISD::OR) {
8821 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
8822 // two branches instead of an explicit OR instruction with a
8823 // separate test.
8824 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00008825 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00008826 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008827 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00008828 Chain, Dest, CC, Cmp);
8829 CC = Cond.getOperand(1).getOperand(0);
8830 Cond = Cmp;
8831 addTest = false;
8832 }
8833 } else { // ISD::AND
8834 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
8835 // two branches instead of an explicit AND instruction with a
8836 // separate test. However, we only do this if this block doesn't
8837 // have a fall-through edge, because this requires an explicit
8838 // jmp when the condition is false.
8839 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00008840 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00008841 Op.getNode()->hasOneUse()) {
8842 X86::CondCode CCode =
8843 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
8844 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008845 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00008846 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00008847 // Look for an unconditional branch following this conditional branch.
8848 // We need this because we need to reverse the successors in order
8849 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00008850 if (User->getOpcode() == ISD::BR) {
8851 SDValue FalseBB = User->getOperand(1);
8852 SDNode *NewBR =
8853 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00008854 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00008855 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00008856 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00008857
Dale Johannesene4d209d2009-02-03 20:21:25 +00008858 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00008859 Chain, Dest, CC, Cmp);
8860 X86::CondCode CCode =
8861 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
8862 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008863 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00008864 Cond = Cmp;
8865 addTest = false;
8866 }
8867 }
Dan Gohman279c22e2008-10-21 03:29:32 +00008868 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00008869 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
8870 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
8871 // It should be transformed during dag combiner except when the condition
8872 // is set by a arithmetics with overflow node.
8873 X86::CondCode CCode =
8874 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
8875 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008876 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00008877 Cond = Cond.getOperand(0).getOperand(1);
8878 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00008879 }
Evan Cheng0488db92007-09-25 01:57:46 +00008880 }
8881
8882 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008883 // Look pass the truncate.
8884 if (Cond.getOpcode() == ISD::TRUNCATE)
8885 Cond = Cond.getOperand(0);
8886
8887 // We know the result of AND is compared against zero. Try to match
8888 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00008889 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008890 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
8891 if (NewSetCC.getNode()) {
8892 CC = NewSetCC.getOperand(0);
8893 Cond = NewSetCC.getOperand(1);
8894 addTest = false;
8895 }
8896 }
8897 }
8898
8899 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008900 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00008901 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00008902 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00008903 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00008904 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00008905}
8906
Anton Korobeynikove060b532007-04-17 19:34:00 +00008907
8908// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
8909// Calls to _alloca is needed to probe the stack when allocating more than 4k
8910// bytes in one go. Touching the stack at 4K increments is necessary to ensure
8911// that the guard pages used by the OS virtual memory manager are allocated in
8912// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00008913SDValue
8914X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008915 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008916 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
8917 EnableSegmentedStacks) &&
8918 "This should be used only on Windows targets or when segmented stacks "
Rafael Espindola96428ce2011-09-06 18:43:08 +00008919 "are being used");
8920 assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008921 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008922
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008923 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00008924 SDValue Chain = Op.getOperand(0);
8925 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008926 // FIXME: Ensure alignment here
8927
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008928 bool Is64Bit = Subtarget->is64Bit();
8929 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008930
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008931 if (EnableSegmentedStacks) {
8932 MachineFunction &MF = DAG.getMachineFunction();
8933 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008934
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008935 if (Is64Bit) {
8936 // The 64 bit implementation of segmented stacks needs to clobber both r10
Rafael Espindola96428ce2011-09-06 18:43:08 +00008937 // r11. This makes it impossible to use it along with nested parameters.
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008938 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00008939
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008940 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
8941 I != E; I++)
8942 if (I->hasNestAttr())
8943 report_fatal_error("Cannot use segmented stacks with functions that "
8944 "have nested arguments.");
8945 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00008946
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008947 const TargetRegisterClass *AddrRegClass =
8948 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
8949 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
8950 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
8951 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
8952 DAG.getRegister(Vreg, SPTy));
8953 SDValue Ops1[2] = { Value, Chain };
8954 return DAG.getMergeValues(Ops1, 2, dl);
8955 } else {
8956 SDValue Flag;
8957 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008958
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008959 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
8960 Flag = Chain.getValue(1);
8961 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008962
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008963 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
8964 Flag = Chain.getValue(1);
8965
8966 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
8967
8968 SDValue Ops1[2] = { Chain.getValue(0), Chain };
8969 return DAG.getMergeValues(Ops1, 2, dl);
8970 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008971}
8972
Dan Gohmand858e902010-04-17 15:26:15 +00008973SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00008974 MachineFunction &MF = DAG.getMachineFunction();
8975 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
8976
Dan Gohman69de1932008-02-06 22:27:42 +00008977 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00008978 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00008979
Anton Korobeynikove7beda12010-10-03 22:52:07 +00008980 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00008981 // vastart just stores the address of the VarArgsFrameIndex slot into the
8982 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00008983 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
8984 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00008985 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
8986 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008987 }
8988
8989 // __va_list_tag:
8990 // gp_offset (0 - 6 * 8)
8991 // fp_offset (48 - 48 + 8 * 16)
8992 // overflow_arg_area (point to parameters coming in memory).
8993 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00008994 SmallVector<SDValue, 8> MemOps;
8995 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00008996 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00008997 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00008998 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
8999 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009000 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009001 MemOps.push_back(Store);
9002
9003 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009004 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009005 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009006 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009007 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
9008 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009009 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009010 MemOps.push_back(Store);
9011
9012 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00009013 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009014 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00009015 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9016 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009017 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
9018 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00009019 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009020 MemOps.push_back(Store);
9021
9022 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00009023 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009024 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00009025 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
9026 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009027 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
9028 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009029 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009030 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00009031 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00009032}
9033
Dan Gohmand858e902010-04-17 15:26:15 +00009034SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00009035 assert(Subtarget->is64Bit() &&
9036 "LowerVAARG only handles 64-bit va_arg!");
9037 assert((Subtarget->isTargetLinux() ||
9038 Subtarget->isTargetDarwin()) &&
9039 "Unhandled target in LowerVAARG");
9040 assert(Op.getNode()->getNumOperands() == 4);
9041 SDValue Chain = Op.getOperand(0);
9042 SDValue SrcPtr = Op.getOperand(1);
9043 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
9044 unsigned Align = Op.getConstantOperandVal(3);
9045 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00009046
Dan Gohman320afb82010-10-12 18:00:49 +00009047 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009048 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Dan Gohman320afb82010-10-12 18:00:49 +00009049 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
9050 uint8_t ArgMode;
9051
9052 // Decide which area this value should be read from.
9053 // TODO: Implement the AMD64 ABI in its entirety. This simple
9054 // selection mechanism works only for the basic types.
9055 if (ArgVT == MVT::f80) {
9056 llvm_unreachable("va_arg for f80 not yet implemented");
9057 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
9058 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
9059 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
9060 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
9061 } else {
9062 llvm_unreachable("Unhandled argument type in LowerVAARG");
9063 }
9064
9065 if (ArgMode == 2) {
9066 // Sanity Check: Make sure using fp_offset makes sense.
Michael J. Spencer87b86652010-10-19 07:32:42 +00009067 assert(!UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00009068 !(DAG.getMachineFunction()
9069 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00009070 Subtarget->hasXMM());
Dan Gohman320afb82010-10-12 18:00:49 +00009071 }
9072
9073 // Insert VAARG_64 node into the DAG
9074 // VAARG_64 returns two values: Variable Argument Address, Chain
9075 SmallVector<SDValue, 11> InstOps;
9076 InstOps.push_back(Chain);
9077 InstOps.push_back(SrcPtr);
9078 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
9079 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
9080 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
9081 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
9082 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
9083 VTs, &InstOps[0], InstOps.size(),
9084 MVT::i64,
9085 MachinePointerInfo(SV),
9086 /*Align=*/0,
9087 /*Volatile=*/false,
9088 /*ReadMem=*/true,
9089 /*WriteMem=*/true);
9090 Chain = VAARG.getValue(1);
9091
9092 // Load the next argument and return it
9093 return DAG.getLoad(ArgVT, dl,
9094 Chain,
9095 VAARG,
9096 MachinePointerInfo(),
9097 false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00009098}
9099
Dan Gohmand858e902010-04-17 15:26:15 +00009100SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00009101 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00009102 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00009103 SDValue Chain = Op.getOperand(0);
9104 SDValue DstPtr = Op.getOperand(1);
9105 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00009106 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
9107 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00009108 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00009109
Chris Lattnere72f2022010-09-21 05:40:29 +00009110 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00009111 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00009112 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00009113 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00009114}
9115
Dan Gohman475871a2008-07-27 21:46:04 +00009116SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00009117X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009118 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009119 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00009120 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00009121 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00009122 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00009123 case Intrinsic::x86_sse_comieq_ss:
9124 case Intrinsic::x86_sse_comilt_ss:
9125 case Intrinsic::x86_sse_comile_ss:
9126 case Intrinsic::x86_sse_comigt_ss:
9127 case Intrinsic::x86_sse_comige_ss:
9128 case Intrinsic::x86_sse_comineq_ss:
9129 case Intrinsic::x86_sse_ucomieq_ss:
9130 case Intrinsic::x86_sse_ucomilt_ss:
9131 case Intrinsic::x86_sse_ucomile_ss:
9132 case Intrinsic::x86_sse_ucomigt_ss:
9133 case Intrinsic::x86_sse_ucomige_ss:
9134 case Intrinsic::x86_sse_ucomineq_ss:
9135 case Intrinsic::x86_sse2_comieq_sd:
9136 case Intrinsic::x86_sse2_comilt_sd:
9137 case Intrinsic::x86_sse2_comile_sd:
9138 case Intrinsic::x86_sse2_comigt_sd:
9139 case Intrinsic::x86_sse2_comige_sd:
9140 case Intrinsic::x86_sse2_comineq_sd:
9141 case Intrinsic::x86_sse2_ucomieq_sd:
9142 case Intrinsic::x86_sse2_ucomilt_sd:
9143 case Intrinsic::x86_sse2_ucomile_sd:
9144 case Intrinsic::x86_sse2_ucomigt_sd:
9145 case Intrinsic::x86_sse2_ucomige_sd:
9146 case Intrinsic::x86_sse2_ucomineq_sd: {
9147 unsigned Opc = 0;
9148 ISD::CondCode CC = ISD::SETCC_INVALID;
9149 switch (IntNo) {
9150 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009151 case Intrinsic::x86_sse_comieq_ss:
9152 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009153 Opc = X86ISD::COMI;
9154 CC = ISD::SETEQ;
9155 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009156 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009157 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009158 Opc = X86ISD::COMI;
9159 CC = ISD::SETLT;
9160 break;
9161 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009162 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009163 Opc = X86ISD::COMI;
9164 CC = ISD::SETLE;
9165 break;
9166 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009167 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009168 Opc = X86ISD::COMI;
9169 CC = ISD::SETGT;
9170 break;
9171 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009172 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009173 Opc = X86ISD::COMI;
9174 CC = ISD::SETGE;
9175 break;
9176 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009177 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009178 Opc = X86ISD::COMI;
9179 CC = ISD::SETNE;
9180 break;
9181 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009182 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009183 Opc = X86ISD::UCOMI;
9184 CC = ISD::SETEQ;
9185 break;
9186 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009187 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009188 Opc = X86ISD::UCOMI;
9189 CC = ISD::SETLT;
9190 break;
9191 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009192 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009193 Opc = X86ISD::UCOMI;
9194 CC = ISD::SETLE;
9195 break;
9196 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009197 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009198 Opc = X86ISD::UCOMI;
9199 CC = ISD::SETGT;
9200 break;
9201 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009202 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009203 Opc = X86ISD::UCOMI;
9204 CC = ISD::SETGE;
9205 break;
9206 case Intrinsic::x86_sse_ucomineq_ss:
9207 case Intrinsic::x86_sse2_ucomineq_sd:
9208 Opc = X86ISD::UCOMI;
9209 CC = ISD::SETNE;
9210 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009211 }
Evan Cheng734503b2006-09-11 02:19:56 +00009212
Dan Gohman475871a2008-07-27 21:46:04 +00009213 SDValue LHS = Op.getOperand(1);
9214 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00009215 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00009216 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00009217 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
9218 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9219 DAG.getConstant(X86CC, MVT::i8), Cond);
9220 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00009221 }
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009222 // ptest and testp intrinsics. The intrinsic these come from are designed to
9223 // return an integer value, not just an instruction so lower it to the ptest
9224 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00009225 case Intrinsic::x86_sse41_ptestz:
9226 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009227 case Intrinsic::x86_sse41_ptestnzc:
9228 case Intrinsic::x86_avx_ptestz_256:
9229 case Intrinsic::x86_avx_ptestc_256:
9230 case Intrinsic::x86_avx_ptestnzc_256:
9231 case Intrinsic::x86_avx_vtestz_ps:
9232 case Intrinsic::x86_avx_vtestc_ps:
9233 case Intrinsic::x86_avx_vtestnzc_ps:
9234 case Intrinsic::x86_avx_vtestz_pd:
9235 case Intrinsic::x86_avx_vtestc_pd:
9236 case Intrinsic::x86_avx_vtestnzc_pd:
9237 case Intrinsic::x86_avx_vtestz_ps_256:
9238 case Intrinsic::x86_avx_vtestc_ps_256:
9239 case Intrinsic::x86_avx_vtestnzc_ps_256:
9240 case Intrinsic::x86_avx_vtestz_pd_256:
9241 case Intrinsic::x86_avx_vtestc_pd_256:
9242 case Intrinsic::x86_avx_vtestnzc_pd_256: {
9243 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00009244 unsigned X86CC = 0;
9245 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00009246 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009247 case Intrinsic::x86_avx_vtestz_ps:
9248 case Intrinsic::x86_avx_vtestz_pd:
9249 case Intrinsic::x86_avx_vtestz_ps_256:
9250 case Intrinsic::x86_avx_vtestz_pd_256:
9251 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009252 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009253 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009254 // ZF = 1
9255 X86CC = X86::COND_E;
9256 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009257 case Intrinsic::x86_avx_vtestc_ps:
9258 case Intrinsic::x86_avx_vtestc_pd:
9259 case Intrinsic::x86_avx_vtestc_ps_256:
9260 case Intrinsic::x86_avx_vtestc_pd_256:
9261 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009262 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009263 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009264 // CF = 1
9265 X86CC = X86::COND_B;
9266 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009267 case Intrinsic::x86_avx_vtestnzc_ps:
9268 case Intrinsic::x86_avx_vtestnzc_pd:
9269 case Intrinsic::x86_avx_vtestnzc_ps_256:
9270 case Intrinsic::x86_avx_vtestnzc_pd_256:
9271 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00009272 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009273 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009274 // ZF and CF = 0
9275 X86CC = X86::COND_A;
9276 break;
9277 }
Eric Christopherfd179292009-08-27 18:07:15 +00009278
Eric Christopher71c67532009-07-29 00:28:05 +00009279 SDValue LHS = Op.getOperand(1);
9280 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009281 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
9282 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00009283 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
9284 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
9285 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00009286 }
Evan Cheng5759f972008-05-04 09:15:50 +00009287
9288 // Fix vector shift instructions where the last operand is a non-immediate
9289 // i32 value.
9290 case Intrinsic::x86_sse2_pslli_w:
9291 case Intrinsic::x86_sse2_pslli_d:
9292 case Intrinsic::x86_sse2_pslli_q:
9293 case Intrinsic::x86_sse2_psrli_w:
9294 case Intrinsic::x86_sse2_psrli_d:
9295 case Intrinsic::x86_sse2_psrli_q:
9296 case Intrinsic::x86_sse2_psrai_w:
9297 case Intrinsic::x86_sse2_psrai_d:
9298 case Intrinsic::x86_mmx_pslli_w:
9299 case Intrinsic::x86_mmx_pslli_d:
9300 case Intrinsic::x86_mmx_pslli_q:
9301 case Intrinsic::x86_mmx_psrli_w:
9302 case Intrinsic::x86_mmx_psrli_d:
9303 case Intrinsic::x86_mmx_psrli_q:
9304 case Intrinsic::x86_mmx_psrai_w:
9305 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00009306 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00009307 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00009308 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00009309
9310 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00009311 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00009312 switch (IntNo) {
9313 case Intrinsic::x86_sse2_pslli_w:
9314 NewIntNo = Intrinsic::x86_sse2_psll_w;
9315 break;
9316 case Intrinsic::x86_sse2_pslli_d:
9317 NewIntNo = Intrinsic::x86_sse2_psll_d;
9318 break;
9319 case Intrinsic::x86_sse2_pslli_q:
9320 NewIntNo = Intrinsic::x86_sse2_psll_q;
9321 break;
9322 case Intrinsic::x86_sse2_psrli_w:
9323 NewIntNo = Intrinsic::x86_sse2_psrl_w;
9324 break;
9325 case Intrinsic::x86_sse2_psrli_d:
9326 NewIntNo = Intrinsic::x86_sse2_psrl_d;
9327 break;
9328 case Intrinsic::x86_sse2_psrli_q:
9329 NewIntNo = Intrinsic::x86_sse2_psrl_q;
9330 break;
9331 case Intrinsic::x86_sse2_psrai_w:
9332 NewIntNo = Intrinsic::x86_sse2_psra_w;
9333 break;
9334 case Intrinsic::x86_sse2_psrai_d:
9335 NewIntNo = Intrinsic::x86_sse2_psra_d;
9336 break;
9337 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00009338 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00009339 switch (IntNo) {
9340 case Intrinsic::x86_mmx_pslli_w:
9341 NewIntNo = Intrinsic::x86_mmx_psll_w;
9342 break;
9343 case Intrinsic::x86_mmx_pslli_d:
9344 NewIntNo = Intrinsic::x86_mmx_psll_d;
9345 break;
9346 case Intrinsic::x86_mmx_pslli_q:
9347 NewIntNo = Intrinsic::x86_mmx_psll_q;
9348 break;
9349 case Intrinsic::x86_mmx_psrli_w:
9350 NewIntNo = Intrinsic::x86_mmx_psrl_w;
9351 break;
9352 case Intrinsic::x86_mmx_psrli_d:
9353 NewIntNo = Intrinsic::x86_mmx_psrl_d;
9354 break;
9355 case Intrinsic::x86_mmx_psrli_q:
9356 NewIntNo = Intrinsic::x86_mmx_psrl_q;
9357 break;
9358 case Intrinsic::x86_mmx_psrai_w:
9359 NewIntNo = Intrinsic::x86_mmx_psra_w;
9360 break;
9361 case Intrinsic::x86_mmx_psrai_d:
9362 NewIntNo = Intrinsic::x86_mmx_psra_d;
9363 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00009364 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00009365 }
9366 break;
9367 }
9368 }
Mon P Wangefa42202009-09-03 19:56:25 +00009369
9370 // The vector shift intrinsics with scalars uses 32b shift amounts but
9371 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
9372 // to be zero.
9373 SDValue ShOps[4];
9374 ShOps[0] = ShAmt;
9375 ShOps[1] = DAG.getConstant(0, MVT::i32);
9376 if (ShAmtVT == MVT::v4i32) {
9377 ShOps[2] = DAG.getUNDEF(MVT::i32);
9378 ShOps[3] = DAG.getUNDEF(MVT::i32);
9379 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
9380 } else {
9381 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00009382// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00009383 }
9384
Owen Andersone50ed302009-08-10 22:56:29 +00009385 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009386 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009387 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009388 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00009389 Op.getOperand(1), ShAmt);
9390 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00009391 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00009392}
Evan Cheng72261582005-12-20 06:22:03 +00009393
Dan Gohmand858e902010-04-17 15:26:15 +00009394SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
9395 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00009396 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9397 MFI->setReturnAddressIsTaken(true);
9398
Bill Wendling64e87322009-01-16 19:25:27 +00009399 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009400 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00009401
9402 if (Depth > 0) {
9403 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
9404 SDValue Offset =
9405 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00009406 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009407 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00009408 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009409 FrameAddr, Offset),
Chris Lattner51abfe42010-09-21 06:02:19 +00009410 MachinePointerInfo(), false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00009411 }
9412
9413 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00009414 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00009415 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Chris Lattner51abfe42010-09-21 06:02:19 +00009416 RetAddrFI, MachinePointerInfo(), false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00009417}
9418
Dan Gohmand858e902010-04-17 15:26:15 +00009419SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00009420 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9421 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00009422
Owen Andersone50ed302009-08-10 22:56:29 +00009423 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009424 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00009425 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
9426 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00009427 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00009428 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00009429 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
9430 MachinePointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +00009431 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00009432 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00009433}
9434
Dan Gohman475871a2008-07-27 21:46:04 +00009435SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009436 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00009437 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009438}
9439
Dan Gohmand858e902010-04-17 15:26:15 +00009440SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009441 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00009442 SDValue Chain = Op.getOperand(0);
9443 SDValue Offset = Op.getOperand(1);
9444 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009445 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009446
Dan Gohmand8816272010-08-11 18:14:00 +00009447 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
9448 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
9449 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009450 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009451
Dan Gohmand8816272010-08-11 18:14:00 +00009452 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
9453 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009454 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009455 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
9456 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00009457 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009458 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009459
Dale Johannesene4d209d2009-02-03 20:21:25 +00009460 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00009461 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009462 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009463}
9464
Duncan Sands4a544a72011-09-06 13:37:06 +00009465SDValue X86TargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op,
9466 SelectionDAG &DAG) const {
9467 return Op.getOperand(0);
9468}
9469
9470SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
9471 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00009472 SDValue Root = Op.getOperand(0);
9473 SDValue Trmp = Op.getOperand(1); // trampoline
9474 SDValue FPtr = Op.getOperand(2); // nested function
9475 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009476 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009477
Dan Gohman69de1932008-02-06 22:27:42 +00009478 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009479
9480 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00009481 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00009482
9483 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00009484 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
9485 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00009486
Evan Cheng0e6a0522011-07-18 20:57:22 +00009487 const unsigned char N86R10 = X86_MC::getX86RegNum(X86::R10);
9488 const unsigned char N86R11 = X86_MC::getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00009489
9490 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
9491
9492 // Load the pointer to the nested function into R11.
9493 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00009494 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00009495 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009496 Addr, MachinePointerInfo(TrmpAddr),
9497 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009498
Owen Anderson825b72b2009-08-11 20:47:22 +00009499 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9500 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009501 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
9502 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00009503 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00009504
9505 // Load the 'nest' parameter value into R10.
9506 // R10 is specified in X86CallingConv.td
9507 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00009508 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9509 DAG.getConstant(10, MVT::i64));
9510 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009511 Addr, MachinePointerInfo(TrmpAddr, 10),
9512 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009513
Owen Anderson825b72b2009-08-11 20:47:22 +00009514 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9515 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009516 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
9517 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00009518 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00009519
9520 // Jump to the nested function.
9521 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00009522 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9523 DAG.getConstant(20, MVT::i64));
9524 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009525 Addr, MachinePointerInfo(TrmpAddr, 20),
9526 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009527
9528 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00009529 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9530 DAG.getConstant(22, MVT::i64));
9531 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00009532 MachinePointerInfo(TrmpAddr, 22),
9533 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009534
Duncan Sands4a544a72011-09-06 13:37:06 +00009535 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009536 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00009537 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00009538 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00009539 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00009540 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009541
9542 switch (CC) {
9543 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00009544 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00009545 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00009546 case CallingConv::X86_StdCall: {
9547 // Pass 'nest' parameter in ECX.
9548 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00009549 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009550
9551 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009552 FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00009553 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009554
Chris Lattner58d74912008-03-12 17:45:29 +00009555 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00009556 unsigned InRegCount = 0;
9557 unsigned Idx = 1;
9558
9559 for (FunctionType::param_iterator I = FTy->param_begin(),
9560 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00009561 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00009562 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00009563 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009564
9565 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00009566 report_fatal_error("Nest register in use - reduce number of inreg"
9567 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00009568 }
9569 }
9570 break;
9571 }
9572 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00009573 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00009574 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00009575 // Pass 'nest' parameter in EAX.
9576 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00009577 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009578 break;
9579 }
9580
Dan Gohman475871a2008-07-27 21:46:04 +00009581 SDValue OutChains[4];
9582 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009583
Owen Anderson825b72b2009-08-11 20:47:22 +00009584 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9585 DAG.getConstant(10, MVT::i32));
9586 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009587
Chris Lattnera62fe662010-02-05 19:20:30 +00009588 // This is storing the opcode for MOV32ri.
9589 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Evan Cheng0e6a0522011-07-18 20:57:22 +00009590 const unsigned char N86Reg = X86_MC::getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009591 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00009592 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009593 Trmp, MachinePointerInfo(TrmpAddr),
9594 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009595
Owen Anderson825b72b2009-08-11 20:47:22 +00009596 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9597 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009598 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
9599 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00009600 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009601
Chris Lattnera62fe662010-02-05 19:20:30 +00009602 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00009603 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9604 DAG.getConstant(5, MVT::i32));
9605 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00009606 MachinePointerInfo(TrmpAddr, 5),
9607 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009608
Owen Anderson825b72b2009-08-11 20:47:22 +00009609 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9610 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009611 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
9612 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00009613 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009614
Duncan Sands4a544a72011-09-06 13:37:06 +00009615 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009616 }
9617}
9618
Dan Gohmand858e902010-04-17 15:26:15 +00009619SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
9620 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009621 /*
9622 The rounding mode is in bits 11:10 of FPSR, and has the following
9623 settings:
9624 00 Round to nearest
9625 01 Round to -inf
9626 10 Round to +inf
9627 11 Round to 0
9628
9629 FLT_ROUNDS, on the other hand, expects the following:
9630 -1 Undefined
9631 0 Round to 0
9632 1 Round to nearest
9633 2 Round to +inf
9634 3 Round to -inf
9635
9636 To perform the conversion, we do:
9637 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
9638 */
9639
9640 MachineFunction &MF = DAG.getMachineFunction();
9641 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00009642 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009643 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00009644 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00009645 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009646
9647 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00009648 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00009649 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009650
Michael J. Spencerec38de22010-10-10 22:04:20 +00009651
Chris Lattner2156b792010-09-22 01:11:26 +00009652 MachineMemOperand *MMO =
9653 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
9654 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009655
Chris Lattner2156b792010-09-22 01:11:26 +00009656 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
9657 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
9658 DAG.getVTList(MVT::Other),
9659 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009660
9661 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00009662 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Chris Lattner51abfe42010-09-21 06:02:19 +00009663 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009664
9665 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00009666 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00009667 DAG.getNode(ISD::SRL, DL, MVT::i16,
9668 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00009669 CWD, DAG.getConstant(0x800, MVT::i16)),
9670 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00009671 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00009672 DAG.getNode(ISD::SRL, DL, MVT::i16,
9673 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00009674 CWD, DAG.getConstant(0x400, MVT::i16)),
9675 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009676
Dan Gohman475871a2008-07-27 21:46:04 +00009677 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00009678 DAG.getNode(ISD::AND, DL, MVT::i16,
9679 DAG.getNode(ISD::ADD, DL, MVT::i16,
9680 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00009681 DAG.getConstant(1, MVT::i16)),
9682 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009683
9684
Duncan Sands83ec4b62008-06-06 12:08:01 +00009685 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00009686 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009687}
9688
Dan Gohmand858e902010-04-17 15:26:15 +00009689SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009690 EVT VT = Op.getValueType();
9691 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009692 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009693 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00009694
9695 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009696 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00009697 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00009698 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00009699 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009700 }
Evan Cheng18efe262007-12-14 02:13:44 +00009701
Evan Cheng152804e2007-12-14 08:30:15 +00009702 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00009703 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009704 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00009705
9706 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00009707 SDValue Ops[] = {
9708 Op,
9709 DAG.getConstant(NumBits+NumBits-1, OpVT),
9710 DAG.getConstant(X86::COND_E, MVT::i8),
9711 Op.getValue(1)
9712 };
9713 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00009714
9715 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00009716 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00009717
Owen Anderson825b72b2009-08-11 20:47:22 +00009718 if (VT == MVT::i8)
9719 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009720 return Op;
9721}
9722
Dan Gohmand858e902010-04-17 15:26:15 +00009723SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009724 EVT VT = Op.getValueType();
9725 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009726 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009727 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00009728
9729 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009730 if (VT == MVT::i8) {
9731 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00009732 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009733 }
Evan Cheng152804e2007-12-14 08:30:15 +00009734
9735 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00009736 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009737 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00009738
9739 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00009740 SDValue Ops[] = {
9741 Op,
9742 DAG.getConstant(NumBits, OpVT),
9743 DAG.getConstant(X86::COND_E, MVT::i8),
9744 Op.getValue(1)
9745 };
9746 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00009747
Owen Anderson825b72b2009-08-11 20:47:22 +00009748 if (VT == MVT::i8)
9749 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009750 return Op;
9751}
9752
Craig Topper13894fa2011-08-24 06:14:18 +00009753// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
9754// ones, and then concatenate the result back.
9755static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00009756 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +00009757
9758 assert(VT.getSizeInBits() == 256 && VT.isInteger() &&
9759 "Unsupported value type for operation");
9760
9761 int NumElems = VT.getVectorNumElements();
9762 DebugLoc dl = Op.getDebugLoc();
9763 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
9764 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
9765
9766 // Extract the LHS vectors
9767 SDValue LHS = Op.getOperand(0);
9768 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
9769 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
9770
9771 // Extract the RHS vectors
9772 SDValue RHS = Op.getOperand(1);
9773 SDValue RHS1 = Extract128BitVector(RHS, Idx0, DAG, dl);
9774 SDValue RHS2 = Extract128BitVector(RHS, Idx1, DAG, dl);
9775
9776 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9777 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
9778
9779 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
9780 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
9781 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
9782}
9783
9784SDValue X86TargetLowering::LowerADD(SDValue Op, SelectionDAG &DAG) const {
9785 assert(Op.getValueType().getSizeInBits() == 256 &&
9786 Op.getValueType().isInteger() &&
9787 "Only handle AVX 256-bit vector integer operation");
9788 return Lower256IntArith(Op, DAG);
9789}
9790
9791SDValue X86TargetLowering::LowerSUB(SDValue Op, SelectionDAG &DAG) const {
9792 assert(Op.getValueType().getSizeInBits() == 256 &&
9793 Op.getValueType().isInteger() &&
9794 "Only handle AVX 256-bit vector integer operation");
9795 return Lower256IntArith(Op, DAG);
9796}
9797
9798SDValue X86TargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
9799 EVT VT = Op.getValueType();
9800
9801 // Decompose 256-bit ops into smaller 128-bit ops.
9802 if (VT.getSizeInBits() == 256)
9803 return Lower256IntArith(Op, DAG);
9804
Owen Anderson825b72b2009-08-11 20:47:22 +00009805 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009806 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00009807
Mon P Wangaf9b9522008-12-18 21:42:19 +00009808 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
9809 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
9810 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
9811 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
9812 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
9813 //
9814 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
9815 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
9816 // return AloBlo + AloBhi + AhiBlo;
9817
9818 SDValue A = Op.getOperand(0);
9819 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009820
Dale Johannesene4d209d2009-02-03 20:21:25 +00009821 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009822 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
9823 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009824 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009825 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
9826 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009827 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009828 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00009829 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009830 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009831 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00009832 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009833 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009834 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00009835 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009836 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009837 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
9838 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009839 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009840 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
9841 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009842 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
9843 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00009844 return Res;
9845}
9846
Nadav Rotem43012222011-05-11 08:12:09 +00009847SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
9848
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009849 EVT VT = Op.getValueType();
9850 DebugLoc dl = Op.getDebugLoc();
9851 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +00009852 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009853 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009854
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00009855 if (!Subtarget->hasXMMInt())
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00009856 return SDValue();
9857
9858 // Decompose 256-bit shifts into smaller 128-bit shifts.
9859 if (VT.getSizeInBits() == 256) {
9860 int NumElems = VT.getVectorNumElements();
9861 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9862 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
9863
9864 // Extract the two vectors
9865 SDValue V1 = Extract128BitVector(R, DAG.getConstant(0, MVT::i32), DAG, dl);
9866 SDValue V2 = Extract128BitVector(R, DAG.getConstant(NumElems/2, MVT::i32),
9867 DAG, dl);
9868
9869 // Recreate the shift amount vectors
Bruno Cardoso Lopes0dd80b02011-08-17 22:12:20 +00009870 SDValue Amt1, Amt2;
9871 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
9872 // Constant shift amount
9873 SmallVector<SDValue, 4> Amt1Csts;
9874 SmallVector<SDValue, 4> Amt2Csts;
9875 for (int i = 0; i < NumElems/2; ++i)
9876 Amt1Csts.push_back(Amt->getOperand(i));
9877 for (int i = NumElems/2; i < NumElems; ++i)
9878 Amt2Csts.push_back(Amt->getOperand(i));
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00009879
Bruno Cardoso Lopes0dd80b02011-08-17 22:12:20 +00009880 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
9881 &Amt1Csts[0], NumElems/2);
9882 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
9883 &Amt2Csts[0], NumElems/2);
9884 } else {
9885 // Variable shift amount
9886 Amt1 = Extract128BitVector(Amt, DAG.getConstant(0, MVT::i32), DAG, dl);
9887 Amt2 = Extract128BitVector(Amt, DAG.getConstant(NumElems/2, MVT::i32),
9888 DAG, dl);
9889 }
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00009890
9891 // Issue new vector shifts for the smaller types
9892 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
9893 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
9894
9895 // Concatenate the result back
9896 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
9897 }
Nate Begeman51409212010-07-28 00:21:48 +00009898
Nadav Rotem43012222011-05-11 08:12:09 +00009899 // Optimize shl/srl/sra with constant shift amount.
9900 if (isSplatVector(Amt.getNode())) {
9901 SDValue SclrAmt = Amt->getOperand(0);
9902 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
9903 uint64_t ShiftAmt = C->getZExtValue();
9904
9905 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SHL)
9906 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9907 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
9908 R, DAG.getConstant(ShiftAmt, MVT::i32));
9909
9910 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SHL)
9911 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9912 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
9913 R, DAG.getConstant(ShiftAmt, MVT::i32));
9914
9915 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SHL)
9916 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9917 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
9918 R, DAG.getConstant(ShiftAmt, MVT::i32));
9919
9920 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SRL)
9921 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9922 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
9923 R, DAG.getConstant(ShiftAmt, MVT::i32));
9924
9925 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRL)
9926 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9927 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
9928 R, DAG.getConstant(ShiftAmt, MVT::i32));
9929
9930 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRL)
9931 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9932 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
9933 R, DAG.getConstant(ShiftAmt, MVT::i32));
9934
9935 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRA)
9936 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9937 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
9938 R, DAG.getConstant(ShiftAmt, MVT::i32));
9939
9940 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRA)
9941 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9942 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
9943 R, DAG.getConstant(ShiftAmt, MVT::i32));
9944 }
9945 }
9946
9947 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +00009948 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +00009949 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9950 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
9951 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
9952
9953 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009954
Nate Begeman51409212010-07-28 00:21:48 +00009955 std::vector<Constant*> CV(4, CI);
9956 Constant *C = ConstantVector::get(CV);
9957 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
9958 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00009959 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00009960 false, false, 16);
9961
9962 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009963 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +00009964 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
9965 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
9966 }
Nadav Rotem43012222011-05-11 08:12:09 +00009967 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +00009968 // a = a << 5;
9969 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9970 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
9971 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
9972
9973 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
9974 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
9975
9976 std::vector<Constant*> CVM1(16, CM1);
9977 std::vector<Constant*> CVM2(16, CM2);
9978 Constant *C = ConstantVector::get(CVM1);
9979 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
9980 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00009981 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00009982 false, false, 16);
9983
9984 // r = pblendv(r, psllw(r & (char16)15, 4), a);
9985 M = DAG.getNode(ISD::AND, dl, VT, R, M);
9986 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9987 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
9988 DAG.getConstant(4, MVT::i32));
Nadav Rotemfbad25e2011-09-11 15:02:23 +00009989 R = DAG.getNode(ISD::VSELECT, dl, VT, Op, R, M);
Nate Begeman51409212010-07-28 00:21:48 +00009990 // a += a
9991 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009992
Nate Begeman51409212010-07-28 00:21:48 +00009993 C = ConstantVector::get(CVM2);
9994 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
9995 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00009996 MachinePointerInfo::getConstantPool(),
Chris Lattner51abfe42010-09-21 06:02:19 +00009997 false, false, 16);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009998
Nate Begeman51409212010-07-28 00:21:48 +00009999 // r = pblendv(r, psllw(r & (char16)63, 2), a);
10000 M = DAG.getNode(ISD::AND, dl, VT, R, M);
10001 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10002 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
10003 DAG.getConstant(2, MVT::i32));
Nadav Rotemfbad25e2011-09-11 15:02:23 +000010004 R = DAG.getNode(ISD::VSELECT, dl, VT, Op, R, M);
Nate Begeman51409212010-07-28 00:21:48 +000010005 // a += a
10006 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010007
Nate Begeman51409212010-07-28 00:21:48 +000010008 // return pblendv(r, r+r, a);
Nadav Rotemfbad25e2011-09-11 15:02:23 +000010009 R = DAG.getNode(ISD::VSELECT, dl, VT, Op,
10010 R, DAG.getNode(ISD::ADD, dl, VT, R, R));
Nate Begeman51409212010-07-28 00:21:48 +000010011 return R;
10012 }
10013 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010014}
Mon P Wangaf9b9522008-12-18 21:42:19 +000010015
Dan Gohmand858e902010-04-17 15:26:15 +000010016SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +000010017 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
10018 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +000010019 // looks for this combo and may remove the "setcc" instruction if the "setcc"
10020 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +000010021 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +000010022 SDValue LHS = N->getOperand(0);
10023 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +000010024 unsigned BaseOp = 0;
10025 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010026 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +000010027 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010028 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +000010029 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +000010030 // A subtract of one will be selected as a INC. Note that INC doesn't
10031 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010032 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10033 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010034 BaseOp = X86ISD::INC;
10035 Cond = X86::COND_O;
10036 break;
10037 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010038 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +000010039 Cond = X86::COND_O;
10040 break;
10041 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010042 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +000010043 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010044 break;
10045 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +000010046 // A subtract of one will be selected as a DEC. Note that DEC doesn't
10047 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010048 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10049 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010050 BaseOp = X86ISD::DEC;
10051 Cond = X86::COND_O;
10052 break;
10053 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010054 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +000010055 Cond = X86::COND_O;
10056 break;
10057 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010058 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +000010059 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010060 break;
10061 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000010062 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000010063 Cond = X86::COND_O;
10064 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010065 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
10066 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
10067 MVT::i32);
10068 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010069
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010070 SDValue SetCC =
10071 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10072 DAG.getConstant(X86::COND_O, MVT::i32),
10073 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010074
Dan Gohman6e5fda22011-07-22 18:45:15 +000010075 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010076 }
Bill Wendling74c37652008-12-09 22:08:41 +000010077 }
Bill Wendling3fafd932008-11-26 22:37:40 +000010078
Bill Wendling61edeb52008-12-02 01:06:39 +000010079 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010080 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010081 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000010082
Bill Wendling61edeb52008-12-02 01:06:39 +000010083 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010084 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
10085 DAG.getConstant(Cond, MVT::i32),
10086 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000010087
Dan Gohman6e5fda22011-07-22 18:45:15 +000010088 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000010089}
10090
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010091SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op, SelectionDAG &DAG) const{
10092 DebugLoc dl = Op.getDebugLoc();
10093 SDNode* Node = Op.getNode();
10094 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
10095 EVT VT = Node->getValueType(0);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010096 if (Subtarget->hasXMMInt() && VT.isVector()) {
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010097 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
10098 ExtraVT.getScalarType().getSizeInBits();
10099 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
10100
10101 unsigned SHLIntrinsicsID = 0;
10102 unsigned SRAIntrinsicsID = 0;
10103 switch (VT.getSimpleVT().SimpleTy) {
10104 default:
10105 return SDValue();
10106 case MVT::v2i64: {
10107 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_q;
10108 SRAIntrinsicsID = 0;
10109 break;
10110 }
10111 case MVT::v4i32: {
10112 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_d;
10113 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_d;
10114 break;
10115 }
10116 case MVT::v8i16: {
10117 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_w;
10118 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_w;
10119 break;
10120 }
10121 }
10122
10123 SDValue Tmp1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10124 DAG.getConstant(SHLIntrinsicsID, MVT::i32),
10125 Node->getOperand(0), ShAmt);
10126
10127 // In case of 1 bit sext, no need to shr
10128 if (ExtraVT.getScalarType().getSizeInBits() == 1) return Tmp1;
10129
10130 if (SRAIntrinsicsID) {
10131 Tmp1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10132 DAG.getConstant(SRAIntrinsicsID, MVT::i32),
10133 Tmp1, ShAmt);
10134 }
10135 return Tmp1;
10136 }
10137
10138 return SDValue();
10139}
10140
10141
Eric Christopher9a9d2752010-07-22 02:48:34 +000010142SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
10143 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010144
Eric Christopher77ed1352011-07-08 00:04:56 +000010145 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
10146 // There isn't any reason to disable it if the target processor supports it.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010147 if (!Subtarget->hasXMMInt() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +000010148 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +000010149 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +000010150 SDValue Ops[] = {
10151 DAG.getRegister(X86::ESP, MVT::i32), // Base
10152 DAG.getTargetConstant(1, MVT::i8), // Scale
10153 DAG.getRegister(0, MVT::i32), // Index
10154 DAG.getTargetConstant(0, MVT::i32), // Disp
10155 DAG.getRegister(0, MVT::i32), // Segment.
10156 Zero,
10157 Chain
10158 };
Michael J. Spencerec38de22010-10-10 22:04:20 +000010159 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +000010160 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
10161 array_lengthof(Ops));
10162 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +000010163 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010164
Eric Christopher9a9d2752010-07-22 02:48:34 +000010165 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +000010166 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +000010167 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010168
Chris Lattner132929a2010-08-14 17:26:09 +000010169 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10170 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
10171 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
10172 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010173
Chris Lattner132929a2010-08-14 17:26:09 +000010174 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
10175 if (!Op1 && !Op2 && !Op3 && Op4)
10176 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010177
Chris Lattner132929a2010-08-14 17:26:09 +000010178 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
10179 if (Op1 && !Op2 && !Op3 && !Op4)
10180 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010181
10182 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +000010183 // (MFENCE)>;
10184 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +000010185}
10186
Eli Friedman14648462011-07-27 22:21:52 +000010187SDValue X86TargetLowering::LowerATOMIC_FENCE(SDValue Op,
10188 SelectionDAG &DAG) const {
10189 DebugLoc dl = Op.getDebugLoc();
10190 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
10191 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
10192 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
10193 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
10194
10195 // The only fence that needs an instruction is a sequentially-consistent
10196 // cross-thread fence.
10197 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
10198 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
10199 // no-sse2). There isn't any reason to disable it if the target processor
10200 // supports it.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010201 if (Subtarget->hasXMMInt() || Subtarget->is64Bit())
Eli Friedman14648462011-07-27 22:21:52 +000010202 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
10203
10204 SDValue Chain = Op.getOperand(0);
10205 SDValue Zero = DAG.getConstant(0, MVT::i32);
10206 SDValue Ops[] = {
10207 DAG.getRegister(X86::ESP, MVT::i32), // Base
10208 DAG.getTargetConstant(1, MVT::i8), // Scale
10209 DAG.getRegister(0, MVT::i32), // Index
10210 DAG.getTargetConstant(0, MVT::i32), // Disp
10211 DAG.getRegister(0, MVT::i32), // Segment.
10212 Zero,
10213 Chain
10214 };
10215 SDNode *Res =
10216 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
10217 array_lengthof(Ops));
10218 return SDValue(Res, 0);
10219 }
10220
10221 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
10222 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
10223}
10224
10225
Dan Gohmand858e902010-04-17 15:26:15 +000010226SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010227 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010228 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000010229 unsigned Reg = 0;
10230 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000010231 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000010232 default:
10233 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000010234 case MVT::i8: Reg = X86::AL; size = 1; break;
10235 case MVT::i16: Reg = X86::AX; size = 2; break;
10236 case MVT::i32: Reg = X86::EAX; size = 4; break;
10237 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000010238 assert(Subtarget->is64Bit() && "Node not type legal!");
10239 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000010240 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000010241 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010242 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000010243 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000010244 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000010245 Op.getOperand(1),
10246 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000010247 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000010248 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010249 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010250 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
10251 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
10252 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000010253 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010254 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000010255 return cpOut;
10256}
10257
Duncan Sands1607f052008-12-01 11:39:25 +000010258SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010259 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +000010260 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010261 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000010262 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010263 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +000010264 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010265 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
10266 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000010267 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000010268 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
10269 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000010270 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000010271 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000010272 rdx.getValue(1)
10273 };
Dale Johannesene4d209d2009-02-03 20:21:25 +000010274 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010275}
10276
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010277SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +000010278 SelectionDAG &DAG) const {
10279 EVT SrcVT = Op.getOperand(0).getValueType();
10280 EVT DstVT = Op.getValueType();
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010281 assert(Subtarget->is64Bit() && !Subtarget->hasXMMInt() &&
Chris Lattner2a786eb2010-12-19 20:19:20 +000010282 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000010283 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000010284 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010285 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000010286 // i64 <=> MMX conversions are Legal.
10287 if (SrcVT==MVT::i64 && DstVT.isVector())
10288 return Op;
10289 if (DstVT==MVT::i64 && SrcVT.isVector())
10290 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000010291 // MMX <=> MMX conversions are Legal.
10292 if (SrcVT.isVector() && DstVT.isVector())
10293 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000010294 // All other conversions need to be expanded.
10295 return SDValue();
10296}
Chris Lattner5b856542010-12-20 00:59:46 +000010297
Dan Gohmand858e902010-04-17 15:26:15 +000010298SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000010299 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +000010300 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000010301 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010302 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000010303 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010304 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010305 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000010306 Node->getOperand(0),
10307 Node->getOperand(1), negOp,
10308 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000010309 cast<AtomicSDNode>(Node)->getAlignment(),
10310 cast<AtomicSDNode>(Node)->getOrdering(),
10311 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000010312}
10313
Eli Friedman327236c2011-08-24 20:50:09 +000010314static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
10315 SDNode *Node = Op.getNode();
10316 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010317 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000010318
10319 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010320 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
10321 // FIXME: On 32-bit, store -> fist or movq would be more efficient
10322 // (The only way to get a 16-byte store is cmpxchg16b)
10323 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
10324 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
10325 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000010326 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
10327 cast<AtomicSDNode>(Node)->getMemoryVT(),
10328 Node->getOperand(0),
10329 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010330 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000010331 cast<AtomicSDNode>(Node)->getOrdering(),
10332 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000010333 return Swap.getValue(1);
10334 }
10335 // Other atomic stores have a simple pattern.
10336 return Op;
10337}
10338
Chris Lattner5b856542010-12-20 00:59:46 +000010339static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
10340 EVT VT = Op.getNode()->getValueType(0);
10341
10342 // Let legalize expand this if it isn't a legal type yet.
10343 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
10344 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010345
Chris Lattner5b856542010-12-20 00:59:46 +000010346 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010347
Chris Lattner5b856542010-12-20 00:59:46 +000010348 unsigned Opc;
10349 bool ExtraOp = false;
10350 switch (Op.getOpcode()) {
10351 default: assert(0 && "Invalid code");
10352 case ISD::ADDC: Opc = X86ISD::ADD; break;
10353 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
10354 case ISD::SUBC: Opc = X86ISD::SUB; break;
10355 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
10356 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010357
Chris Lattner5b856542010-12-20 00:59:46 +000010358 if (!ExtraOp)
10359 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
10360 Op.getOperand(1));
10361 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
10362 Op.getOperand(1), Op.getOperand(2));
10363}
10364
Evan Cheng0db9fe62006-04-25 20:13:52 +000010365/// LowerOperation - Provide custom lowering hooks for some operations.
10366///
Dan Gohmand858e902010-04-17 15:26:15 +000010367SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000010368 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010369 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010370 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Eric Christopher9a9d2752010-07-22 02:48:34 +000010371 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Eli Friedman14648462011-07-27 22:21:52 +000010372 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010373 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
10374 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000010375 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010376 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000010377 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010378 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
10379 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
10380 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +000010381 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +000010382 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010383 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
10384 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
10385 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000010386 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000010387 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000010388 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010389 case ISD::SHL_PARTS:
10390 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000010391 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010392 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000010393 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010394 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000010395 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010396 case ISD::FABS: return LowerFABS(Op, DAG);
10397 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000010398 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000010399 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000010400 case ISD::SETCC: return LowerSETCC(Op, DAG);
10401 case ISD::SELECT: return LowerSELECT(Op, DAG);
10402 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010403 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010404 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000010405 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +000010406 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010407 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000010408 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
10409 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010410 case ISD::FRAME_TO_ARGS_OFFSET:
10411 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010412 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010413 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +000010414 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
10415 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000010416 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000010417 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
10418 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000010419 case ISD::MUL: return LowerMUL(Op, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000010420 case ISD::SRA:
10421 case ISD::SRL:
10422 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000010423 case ISD::SADDO:
10424 case ISD::UADDO:
10425 case ISD::SSUBO:
10426 case ISD::USUBO:
10427 case ISD::SMULO:
10428 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +000010429 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010430 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000010431 case ISD::ADDC:
10432 case ISD::ADDE:
10433 case ISD::SUBC:
10434 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000010435 case ISD::ADD: return LowerADD(Op, DAG);
10436 case ISD::SUB: return LowerSUB(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010437 }
Chris Lattner27a6c732007-11-24 07:07:01 +000010438}
10439
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010440static void ReplaceATOMIC_LOAD(SDNode *Node,
10441 SmallVectorImpl<SDValue> &Results,
10442 SelectionDAG &DAG) {
10443 DebugLoc dl = Node->getDebugLoc();
10444 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
10445
10446 // Convert wide load -> cmpxchg8b/cmpxchg16b
10447 // FIXME: On 32-bit, load -> fild or movq would be more efficient
10448 // (The only way to get a 16-byte load is cmpxchg16b)
10449 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000010450 SDValue Zero = DAG.getConstant(0, VT);
10451 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010452 Node->getOperand(0),
10453 Node->getOperand(1), Zero, Zero,
10454 cast<AtomicSDNode>(Node)->getMemOperand(),
10455 cast<AtomicSDNode>(Node)->getOrdering(),
10456 cast<AtomicSDNode>(Node)->getSynchScope());
10457 Results.push_back(Swap.getValue(0));
10458 Results.push_back(Swap.getValue(1));
10459}
10460
Duncan Sands1607f052008-12-01 11:39:25 +000010461void X86TargetLowering::
10462ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000010463 SelectionDAG &DAG, unsigned NewOp) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010464 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010465 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +000010466 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000010467
10468 SDValue Chain = Node->getOperand(0);
10469 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010470 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010471 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000010472 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010473 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000010474 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000010475 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000010476 SDValue Result =
10477 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
10478 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000010479 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000010480 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010481 Results.push_back(Result.getValue(2));
10482}
10483
Duncan Sands126d9072008-07-04 11:47:58 +000010484/// ReplaceNodeResults - Replace a node with an illegal result type
10485/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000010486void X86TargetLowering::ReplaceNodeResults(SDNode *N,
10487 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000010488 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010489 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +000010490 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000010491 default:
Duncan Sands1607f052008-12-01 11:39:25 +000010492 assert(false && "Do not know how to custom type legalize this operation!");
10493 return;
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010494 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000010495 case ISD::ADDC:
10496 case ISD::ADDE:
10497 case ISD::SUBC:
10498 case ISD::SUBE:
10499 // We don't want to expand or promote these.
10500 return;
Duncan Sands1607f052008-12-01 11:39:25 +000010501 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +000010502 std::pair<SDValue,SDValue> Vals =
10503 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +000010504 SDValue FIST = Vals.first, StackSlot = Vals.second;
10505 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000010506 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000010507 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +000010508 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
10509 MachinePointerInfo(), false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +000010510 }
10511 return;
10512 }
10513 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010514 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000010515 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010516 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010517 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000010518 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000010519 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010520 eax.getValue(2));
10521 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
10522 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +000010523 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010524 Results.push_back(edx.getValue(1));
10525 return;
10526 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010527 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000010528 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000010529 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000010530 bool Regs64bit = T == MVT::i128;
10531 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000010532 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000010533 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
10534 DAG.getConstant(0, HalfT));
10535 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
10536 DAG.getConstant(1, HalfT));
10537 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
10538 Regs64bit ? X86::RAX : X86::EAX,
10539 cpInL, SDValue());
10540 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
10541 Regs64bit ? X86::RDX : X86::EDX,
10542 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000010543 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000010544 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
10545 DAG.getConstant(0, HalfT));
10546 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
10547 DAG.getConstant(1, HalfT));
10548 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
10549 Regs64bit ? X86::RBX : X86::EBX,
10550 swapInL, cpInH.getValue(1));
10551 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
10552 Regs64bit ? X86::RCX : X86::ECX,
10553 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000010554 SDValue Ops[] = { swapInH.getValue(0),
10555 N->getOperand(1),
10556 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010557 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000010558 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000010559 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
10560 X86ISD::LCMPXCHG8_DAG;
10561 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000010562 Ops, 3, T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000010563 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
10564 Regs64bit ? X86::RAX : X86::EAX,
10565 HalfT, Result.getValue(1));
10566 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
10567 Regs64bit ? X86::RDX : X86::EDX,
10568 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000010569 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000010570 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010571 Results.push_back(cpOutH.getValue(1));
10572 return;
10573 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010574 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +000010575 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
10576 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010577 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +000010578 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
10579 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010580 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +000010581 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
10582 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010583 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +000010584 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
10585 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010586 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +000010587 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
10588 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010589 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +000010590 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
10591 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010592 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +000010593 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
10594 return;
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010595 case ISD::ATOMIC_LOAD:
10596 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000010597 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000010598}
10599
Evan Cheng72261582005-12-20 06:22:03 +000010600const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
10601 switch (Opcode) {
10602 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000010603 case X86ISD::BSF: return "X86ISD::BSF";
10604 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000010605 case X86ISD::SHLD: return "X86ISD::SHLD";
10606 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000010607 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000010608 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000010609 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000010610 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000010611 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000010612 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000010613 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
10614 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
10615 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000010616 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000010617 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000010618 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000010619 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000010620 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000010621 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000010622 case X86ISD::COMI: return "X86ISD::COMI";
10623 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000010624 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000010625 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000010626 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
10627 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000010628 case X86ISD::CMOV: return "X86ISD::CMOV";
10629 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000010630 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000010631 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
10632 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000010633 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000010634 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000010635 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000010636 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000010637 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000010638 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
10639 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000010640 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000010641 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000010642 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Nate Begemanb65c1752010-12-17 22:55:37 +000010643 case X86ISD::PSIGNB: return "X86ISD::PSIGNB";
10644 case X86ISD::PSIGNW: return "X86ISD::PSIGNW";
10645 case X86ISD::PSIGND: return "X86ISD::PSIGND";
Evan Cheng8ca29322006-11-10 21:43:37 +000010646 case X86ISD::FMAX: return "X86ISD::FMAX";
10647 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +000010648 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
10649 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000010650 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000010651 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010652 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000010653 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010654 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +000010655 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
10656 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010657 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
10658 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
10659 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
10660 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
10661 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
10662 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000010663 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
10664 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +000010665 case X86ISD::VSHL: return "X86ISD::VSHL";
10666 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +000010667 case X86ISD::CMPPD: return "X86ISD::CMPPD";
10668 case X86ISD::CMPPS: return "X86ISD::CMPPS";
10669 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
10670 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
10671 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
10672 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
10673 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
10674 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
10675 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
10676 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010677 case X86ISD::ADD: return "X86ISD::ADD";
10678 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000010679 case X86ISD::ADC: return "X86ISD::ADC";
10680 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000010681 case X86ISD::SMUL: return "X86ISD::SMUL";
10682 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000010683 case X86ISD::INC: return "X86ISD::INC";
10684 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000010685 case X86ISD::OR: return "X86ISD::OR";
10686 case X86ISD::XOR: return "X86ISD::XOR";
10687 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +000010688 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000010689 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010690 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010691 case X86ISD::PALIGN: return "X86ISD::PALIGN";
10692 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
10693 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
10694 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
10695 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
10696 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
10697 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
10698 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
10699 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010700 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000010701 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010702 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000010703 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
10704 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010705 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
10706 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
10707 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
10708 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
10709 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
10710 case X86ISD::MOVSD: return "X86ISD::MOVSD";
10711 case X86ISD::MOVSS: return "X86ISD::MOVSS";
10712 case X86ISD::UNPCKLPS: return "X86ISD::UNPCKLPS";
10713 case X86ISD::UNPCKLPD: return "X86ISD::UNPCKLPD";
David Greenefbf05d32011-02-22 23:31:46 +000010714 case X86ISD::VUNPCKLPDY: return "X86ISD::VUNPCKLPDY";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010715 case X86ISD::UNPCKHPS: return "X86ISD::UNPCKHPS";
10716 case X86ISD::UNPCKHPD: return "X86ISD::UNPCKHPD";
10717 case X86ISD::PUNPCKLBW: return "X86ISD::PUNPCKLBW";
10718 case X86ISD::PUNPCKLWD: return "X86ISD::PUNPCKLWD";
10719 case X86ISD::PUNPCKLDQ: return "X86ISD::PUNPCKLDQ";
10720 case X86ISD::PUNPCKLQDQ: return "X86ISD::PUNPCKLQDQ";
10721 case X86ISD::PUNPCKHBW: return "X86ISD::PUNPCKHBW";
10722 case X86ISD::PUNPCKHWD: return "X86ISD::PUNPCKHWD";
10723 case X86ISD::PUNPCKHDQ: return "X86ISD::PUNPCKHDQ";
10724 case X86ISD::PUNPCKHQDQ: return "X86ISD::PUNPCKHQDQ";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000010725 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +000010726 case X86ISD::VPERMILPS: return "X86ISD::VPERMILPS";
10727 case X86ISD::VPERMILPSY: return "X86ISD::VPERMILPSY";
10728 case X86ISD::VPERMILPD: return "X86ISD::VPERMILPD";
10729 case X86ISD::VPERMILPDY: return "X86ISD::VPERMILPDY";
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +000010730 case X86ISD::VPERM2F128: return "X86ISD::VPERM2F128";
Dan Gohmand6708ea2009-08-15 01:38:56 +000010731 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000010732 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010733 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000010734 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000010735 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +000010736 }
10737}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010738
Chris Lattnerc9addb72007-03-30 23:15:24 +000010739// isLegalAddressingMode - Return true if the addressing mode represented
10740// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000010741bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010742 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000010743 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010744 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000010745 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000010746
Chris Lattnerc9addb72007-03-30 23:15:24 +000010747 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010748 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000010749 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000010750
Chris Lattnerc9addb72007-03-30 23:15:24 +000010751 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000010752 unsigned GVFlags =
10753 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010754
Chris Lattnerdfed4132009-07-10 07:38:24 +000010755 // If a reference to this global requires an extra load, we can't fold it.
10756 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000010757 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010758
Chris Lattnerdfed4132009-07-10 07:38:24 +000010759 // If BaseGV requires a register for the PIC base, we cannot also have a
10760 // BaseReg specified.
10761 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000010762 return false;
Evan Cheng52787842007-08-01 23:46:47 +000010763
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010764 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000010765 if ((M != CodeModel::Small || R != Reloc::Static) &&
10766 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010767 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000010768 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010769
Chris Lattnerc9addb72007-03-30 23:15:24 +000010770 switch (AM.Scale) {
10771 case 0:
10772 case 1:
10773 case 2:
10774 case 4:
10775 case 8:
10776 // These scales always work.
10777 break;
10778 case 3:
10779 case 5:
10780 case 9:
10781 // These scales are formed with basereg+scalereg. Only accept if there is
10782 // no basereg yet.
10783 if (AM.HasBaseReg)
10784 return false;
10785 break;
10786 default: // Other stuff never works.
10787 return false;
10788 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010789
Chris Lattnerc9addb72007-03-30 23:15:24 +000010790 return true;
10791}
10792
10793
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010794bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000010795 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000010796 return false;
Evan Chenge127a732007-10-29 07:57:50 +000010797 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
10798 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000010799 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +000010800 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000010801 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +000010802}
10803
Owen Andersone50ed302009-08-10 22:56:29 +000010804bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000010805 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000010806 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000010807 unsigned NumBits1 = VT1.getSizeInBits();
10808 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000010809 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +000010810 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000010811 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000010812}
Evan Cheng2bd122c2007-10-26 01:56:11 +000010813
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010814bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000010815 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000010816 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000010817}
10818
Owen Andersone50ed302009-08-10 22:56:29 +000010819bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000010820 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000010821 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000010822}
10823
Owen Andersone50ed302009-08-10 22:56:29 +000010824bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000010825 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000010826 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000010827}
10828
Evan Cheng60c07e12006-07-05 22:17:51 +000010829/// isShuffleMaskLegal - Targets can use this to indicate that they only
10830/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
10831/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
10832/// are assumed to be legal.
10833bool
Eric Christopherfd179292009-08-27 18:07:15 +000010834X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000010835 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000010836 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000010837 if (VT.getSizeInBits() == 64)
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010838 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3() || Subtarget->hasAVX());
Nate Begeman9008ca62009-04-27 18:41:29 +000010839
Nate Begemana09008b2009-10-19 02:17:23 +000010840 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000010841 return (VT.getVectorNumElements() == 2 ||
10842 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
10843 isMOVLMask(M, VT) ||
10844 isSHUFPMask(M, VT) ||
10845 isPSHUFDMask(M, VT) ||
10846 isPSHUFHWMask(M, VT) ||
10847 isPSHUFLWMask(M, VT) ||
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010848 isPALIGNRMask(M, VT, Subtarget->hasSSSE3() || Subtarget->hasAVX()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000010849 isUNPCKLMask(M, VT) ||
10850 isUNPCKHMask(M, VT) ||
10851 isUNPCKL_v_undef_Mask(M, VT) ||
10852 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +000010853}
10854
Dan Gohman7d8143f2008-04-09 20:09:42 +000010855bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000010856X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000010857 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000010858 unsigned NumElts = VT.getVectorNumElements();
10859 // FIXME: This collection of masks seems suspect.
10860 if (NumElts == 2)
10861 return true;
10862 if (NumElts == 4 && VT.getSizeInBits() == 128) {
10863 return (isMOVLMask(Mask, VT) ||
10864 isCommutedMOVLMask(Mask, VT, true) ||
10865 isSHUFPMask(Mask, VT) ||
10866 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +000010867 }
10868 return false;
10869}
10870
10871//===----------------------------------------------------------------------===//
10872// X86 Scheduler Hooks
10873//===----------------------------------------------------------------------===//
10874
Mon P Wang63307c32008-05-05 19:05:59 +000010875// private utility function
10876MachineBasicBlock *
10877X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
10878 MachineBasicBlock *MBB,
10879 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010880 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010881 unsigned LoadOpc,
10882 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010883 unsigned notOpc,
10884 unsigned EAXreg,
10885 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000010886 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000010887 // For the atomic bitwise operator, we generate
10888 // thisMBB:
10889 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000010890 // ld t1 = [bitinstr.addr]
10891 // op t2 = t1, [bitinstr.val]
10892 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000010893 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
10894 // bz newMBB
10895 // fallthrough -->nextMBB
10896 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10897 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000010898 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000010899 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000010900
Mon P Wang63307c32008-05-05 19:05:59 +000010901 /// First build the CFG
10902 MachineFunction *F = MBB->getParent();
10903 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000010904 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
10905 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
10906 F->insert(MBBIter, newMBB);
10907 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010908
Dan Gohman14152b42010-07-06 20:24:04 +000010909 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
10910 nextMBB->splice(nextMBB->begin(), thisMBB,
10911 llvm::next(MachineBasicBlock::iterator(bInstr)),
10912 thisMBB->end());
10913 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010914
Mon P Wang63307c32008-05-05 19:05:59 +000010915 // Update thisMBB to fall through to newMBB
10916 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010917
Mon P Wang63307c32008-05-05 19:05:59 +000010918 // newMBB jumps to itself and fall through to nextMBB
10919 newMBB->addSuccessor(nextMBB);
10920 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010921
Mon P Wang63307c32008-05-05 19:05:59 +000010922 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010923 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000010924 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +000010925 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000010926 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010927 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000010928 int numArgs = bInstr->getNumOperands() - 1;
10929 for (int i=0; i < numArgs; ++i)
10930 argOpers[i] = &bInstr->getOperand(i+1);
10931
10932 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010933 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000010934 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000010935
Dale Johannesen140be2d2008-08-19 18:47:28 +000010936 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010937 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000010938 for (int i=0; i <= lastAddrIndx; ++i)
10939 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010940
Dale Johannesen140be2d2008-08-19 18:47:28 +000010941 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010942 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010943 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010944 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010945 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010946 tt = t1;
10947
Dale Johannesen140be2d2008-08-19 18:47:28 +000010948 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +000010949 assert((argOpers[valArgIndx]->isReg() ||
10950 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000010951 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +000010952 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000010953 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000010954 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000010955 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010956 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +000010957 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010958
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010959 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +000010960 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +000010961
Dale Johannesene4d209d2009-02-03 20:21:25 +000010962 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +000010963 for (int i=0; i <= lastAddrIndx; ++i)
10964 (*MIB).addOperand(*argOpers[i]);
10965 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +000010966 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000010967 (*MIB).setMemRefs(bInstr->memoperands_begin(),
10968 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +000010969
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010970 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +000010971 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +000010972
Mon P Wang63307c32008-05-05 19:05:59 +000010973 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000010974 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000010975
Dan Gohman14152b42010-07-06 20:24:04 +000010976 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000010977 return nextMBB;
10978}
10979
Dale Johannesen1b54c7f2008-10-03 19:41:08 +000010980// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +000010981MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010982X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
10983 MachineBasicBlock *MBB,
10984 unsigned regOpcL,
10985 unsigned regOpcH,
10986 unsigned immOpcL,
10987 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000010988 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010989 // For the atomic bitwise operator, we generate
10990 // thisMBB (instructions are in pairs, except cmpxchg8b)
10991 // ld t1,t2 = [bitinstr.addr]
10992 // newMBB:
10993 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
10994 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +000010995 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010996 // mov ECX, EBX <- t5, t6
10997 // mov EAX, EDX <- t1, t2
10998 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
10999 // mov t3, t4 <- EAX, EDX
11000 // bz newMBB
11001 // result in out1, out2
11002 // fallthrough -->nextMBB
11003
11004 const TargetRegisterClass *RC = X86::GR32RegisterClass;
11005 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011006 const unsigned NotOpc = X86::NOT32r;
11007 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11008 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11009 MachineFunction::iterator MBBIter = MBB;
11010 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011011
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011012 /// First build the CFG
11013 MachineFunction *F = MBB->getParent();
11014 MachineBasicBlock *thisMBB = MBB;
11015 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11016 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11017 F->insert(MBBIter, newMBB);
11018 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011019
Dan Gohman14152b42010-07-06 20:24:04 +000011020 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11021 nextMBB->splice(nextMBB->begin(), thisMBB,
11022 llvm::next(MachineBasicBlock::iterator(bInstr)),
11023 thisMBB->end());
11024 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011025
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011026 // Update thisMBB to fall through to newMBB
11027 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011028
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011029 // newMBB jumps to itself and fall through to nextMBB
11030 newMBB->addSuccessor(nextMBB);
11031 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011032
Dale Johannesene4d209d2009-02-03 20:21:25 +000011033 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011034 // Insert instructions into newMBB based on incoming instruction
11035 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011036 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011037 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011038 MachineOperand& dest1Oper = bInstr->getOperand(0);
11039 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011040 MachineOperand* argOpers[2 + X86::AddrNumOperands];
11041 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011042 argOpers[i] = &bInstr->getOperand(i+2);
11043
Dan Gohman71ea4e52010-05-14 21:01:44 +000011044 // We use some of the operands multiple times, so conservatively just
11045 // clear any kill flags that might be present.
11046 if (argOpers[i]->isReg() && argOpers[i]->isUse())
11047 argOpers[i]->setIsKill(false);
11048 }
11049
Evan Chengad5b52f2010-01-08 19:14:57 +000011050 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011051 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +000011052
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011053 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011054 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011055 for (int i=0; i <= lastAddrIndx; ++i)
11056 (*MIB).addOperand(*argOpers[i]);
11057 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011058 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +000011059 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +000011060 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011061 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +000011062 MachineOperand newOp3 = *(argOpers[3]);
11063 if (newOp3.isImm())
11064 newOp3.setImm(newOp3.getImm()+4);
11065 else
11066 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011067 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +000011068 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011069
11070 // t3/4 are defined later, at the bottom of the loop
11071 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
11072 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011073 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011074 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011075 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011076 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
11077
Evan Cheng306b4ca2010-01-08 23:41:50 +000011078 // The subsequent operations should be using the destination registers of
11079 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +000011080 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +000011081 t1 = F->getRegInfo().createVirtualRegister(RC);
11082 t2 = F->getRegInfo().createVirtualRegister(RC);
11083 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
11084 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011085 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +000011086 t1 = dest1Oper.getReg();
11087 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011088 }
11089
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011090 int valArgIndx = lastAddrIndx + 1;
11091 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +000011092 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011093 "invalid operand");
11094 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
11095 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011096 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011097 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011098 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011099 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +000011100 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011101 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011102 (*MIB).addOperand(*argOpers[valArgIndx]);
11103 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011104 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011105 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011106 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011107 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011108 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011109 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011110 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +000011111 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011112 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011113 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011114
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011115 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011116 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011117 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011118 MIB.addReg(t2);
11119
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011120 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011121 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011122 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011123 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +000011124
Dale Johannesene4d209d2009-02-03 20:21:25 +000011125 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011126 for (int i=0; i <= lastAddrIndx; ++i)
11127 (*MIB).addOperand(*argOpers[i]);
11128
11129 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011130 (*MIB).setMemRefs(bInstr->memoperands_begin(),
11131 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011132
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011133 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011134 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011135 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011136 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000011137
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011138 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011139 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011140
Dan Gohman14152b42010-07-06 20:24:04 +000011141 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011142 return nextMBB;
11143}
11144
11145// private utility function
11146MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +000011147X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
11148 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011149 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000011150 // For the atomic min/max operator, we generate
11151 // thisMBB:
11152 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000011153 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +000011154 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +000011155 // cmp t1, t2
11156 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +000011157 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000011158 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
11159 // bz newMBB
11160 // fallthrough -->nextMBB
11161 //
11162 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11163 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011164 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000011165 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011166
Mon P Wang63307c32008-05-05 19:05:59 +000011167 /// First build the CFG
11168 MachineFunction *F = MBB->getParent();
11169 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011170 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11171 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11172 F->insert(MBBIter, newMBB);
11173 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011174
Dan Gohman14152b42010-07-06 20:24:04 +000011175 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11176 nextMBB->splice(nextMBB->begin(), thisMBB,
11177 llvm::next(MachineBasicBlock::iterator(mInstr)),
11178 thisMBB->end());
11179 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011180
Mon P Wang63307c32008-05-05 19:05:59 +000011181 // Update thisMBB to fall through to newMBB
11182 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011183
Mon P Wang63307c32008-05-05 19:05:59 +000011184 // newMBB jumps to newMBB and fall through to nextMBB
11185 newMBB->addSuccessor(nextMBB);
11186 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011187
Dale Johannesene4d209d2009-02-03 20:21:25 +000011188 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000011189 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011190 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011191 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +000011192 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011193 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000011194 int numArgs = mInstr->getNumOperands() - 1;
11195 for (int i=0; i < numArgs; ++i)
11196 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +000011197
Mon P Wang63307c32008-05-05 19:05:59 +000011198 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011199 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011200 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000011201
Mon P Wangab3e7472008-05-05 22:56:23 +000011202 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011203 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011204 for (int i=0; i <= lastAddrIndx; ++i)
11205 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +000011206
Mon P Wang63307c32008-05-05 19:05:59 +000011207 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +000011208 assert((argOpers[valArgIndx]->isReg() ||
11209 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000011210 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +000011211
11212 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +000011213 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011214 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +000011215 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011216 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000011217 (*MIB).addOperand(*argOpers[valArgIndx]);
11218
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011219 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +000011220 MIB.addReg(t1);
11221
Dale Johannesene4d209d2009-02-03 20:21:25 +000011222 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +000011223 MIB.addReg(t1);
11224 MIB.addReg(t2);
11225
11226 // Generate movc
11227 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011228 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +000011229 MIB.addReg(t2);
11230 MIB.addReg(t1);
11231
11232 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +000011233 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +000011234 for (int i=0; i <= lastAddrIndx; ++i)
11235 (*MIB).addOperand(*argOpers[i]);
11236 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +000011237 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011238 (*MIB).setMemRefs(mInstr->memoperands_begin(),
11239 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +000011240
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011241 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +000011242 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +000011243
Mon P Wang63307c32008-05-05 19:05:59 +000011244 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011245 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000011246
Dan Gohman14152b42010-07-06 20:24:04 +000011247 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000011248 return nextMBB;
11249}
11250
Eric Christopherf83a5de2009-08-27 18:08:16 +000011251// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011252// or XMM0_V32I8 in AVX all of this code can be replaced with that
11253// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +000011254MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +000011255X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +000011256 unsigned numArgs, bool memArg) const {
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011257 assert((Subtarget->hasSSE42() || Subtarget->hasAVX()) &&
11258 "Target must have SSE4.2 or AVX features enabled");
11259
Eric Christopherb120ab42009-08-18 22:50:32 +000011260 DebugLoc dl = MI->getDebugLoc();
11261 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +000011262 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011263 if (!Subtarget->hasAVX()) {
11264 if (memArg)
11265 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
11266 else
11267 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
11268 } else {
11269 if (memArg)
11270 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
11271 else
11272 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
11273 }
Eric Christopherb120ab42009-08-18 22:50:32 +000011274
Eric Christopher41c902f2010-11-30 08:20:21 +000011275 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +000011276 for (unsigned i = 0; i < numArgs; ++i) {
11277 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +000011278 if (!(Op.isReg() && Op.isImplicit()))
11279 MIB.addOperand(Op);
11280 }
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011281 BuildMI(*BB, MI, dl,
11282 TII->get(Subtarget->hasAVX() ? X86::VMOVAPSrr : X86::MOVAPSrr),
11283 MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000011284 .addReg(X86::XMM0);
11285
Dan Gohman14152b42010-07-06 20:24:04 +000011286 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000011287 return BB;
11288}
11289
11290MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +000011291X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000011292 DebugLoc dl = MI->getDebugLoc();
11293 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011294
Eric Christopher228232b2010-11-30 07:20:12 +000011295 // Address into RAX/EAX, other two args into ECX, EDX.
11296 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
11297 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
11298 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
11299 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000011300 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011301
Eric Christopher228232b2010-11-30 07:20:12 +000011302 unsigned ValOps = X86::AddrNumOperands;
11303 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
11304 .addReg(MI->getOperand(ValOps).getReg());
11305 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
11306 .addReg(MI->getOperand(ValOps+1).getReg());
11307
11308 // The instruction doesn't actually take any operands though.
11309 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011310
Eric Christopher228232b2010-11-30 07:20:12 +000011311 MI->eraseFromParent(); // The pseudo is gone now.
11312 return BB;
11313}
11314
11315MachineBasicBlock *
11316X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000011317 DebugLoc dl = MI->getDebugLoc();
11318 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011319
Eric Christopher228232b2010-11-30 07:20:12 +000011320 // First arg in ECX, the second in EAX.
11321 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
11322 .addReg(MI->getOperand(0).getReg());
11323 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
11324 .addReg(MI->getOperand(1).getReg());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011325
Eric Christopher228232b2010-11-30 07:20:12 +000011326 // The instruction doesn't actually take any operands though.
11327 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011328
Eric Christopher228232b2010-11-30 07:20:12 +000011329 MI->eraseFromParent(); // The pseudo is gone now.
11330 return BB;
11331}
11332
11333MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000011334X86TargetLowering::EmitVAARG64WithCustomInserter(
11335 MachineInstr *MI,
11336 MachineBasicBlock *MBB) const {
11337 // Emit va_arg instruction on X86-64.
11338
11339 // Operands to this pseudo-instruction:
11340 // 0 ) Output : destination address (reg)
11341 // 1-5) Input : va_list address (addr, i64mem)
11342 // 6 ) ArgSize : Size (in bytes) of vararg type
11343 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
11344 // 8 ) Align : Alignment of type
11345 // 9 ) EFLAGS (implicit-def)
11346
11347 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
11348 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
11349
11350 unsigned DestReg = MI->getOperand(0).getReg();
11351 MachineOperand &Base = MI->getOperand(1);
11352 MachineOperand &Scale = MI->getOperand(2);
11353 MachineOperand &Index = MI->getOperand(3);
11354 MachineOperand &Disp = MI->getOperand(4);
11355 MachineOperand &Segment = MI->getOperand(5);
11356 unsigned ArgSize = MI->getOperand(6).getImm();
11357 unsigned ArgMode = MI->getOperand(7).getImm();
11358 unsigned Align = MI->getOperand(8).getImm();
11359
11360 // Memory Reference
11361 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
11362 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
11363 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
11364
11365 // Machine Information
11366 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11367 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
11368 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
11369 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
11370 DebugLoc DL = MI->getDebugLoc();
11371
11372 // struct va_list {
11373 // i32 gp_offset
11374 // i32 fp_offset
11375 // i64 overflow_area (address)
11376 // i64 reg_save_area (address)
11377 // }
11378 // sizeof(va_list) = 24
11379 // alignment(va_list) = 8
11380
11381 unsigned TotalNumIntRegs = 6;
11382 unsigned TotalNumXMMRegs = 8;
11383 bool UseGPOffset = (ArgMode == 1);
11384 bool UseFPOffset = (ArgMode == 2);
11385 unsigned MaxOffset = TotalNumIntRegs * 8 +
11386 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
11387
11388 /* Align ArgSize to a multiple of 8 */
11389 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
11390 bool NeedsAlign = (Align > 8);
11391
11392 MachineBasicBlock *thisMBB = MBB;
11393 MachineBasicBlock *overflowMBB;
11394 MachineBasicBlock *offsetMBB;
11395 MachineBasicBlock *endMBB;
11396
11397 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
11398 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
11399 unsigned OffsetReg = 0;
11400
11401 if (!UseGPOffset && !UseFPOffset) {
11402 // If we only pull from the overflow region, we don't create a branch.
11403 // We don't need to alter control flow.
11404 OffsetDestReg = 0; // unused
11405 OverflowDestReg = DestReg;
11406
11407 offsetMBB = NULL;
11408 overflowMBB = thisMBB;
11409 endMBB = thisMBB;
11410 } else {
11411 // First emit code to check if gp_offset (or fp_offset) is below the bound.
11412 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
11413 // If not, pull from overflow_area. (branch to overflowMBB)
11414 //
11415 // thisMBB
11416 // | .
11417 // | .
11418 // offsetMBB overflowMBB
11419 // | .
11420 // | .
11421 // endMBB
11422
11423 // Registers for the PHI in endMBB
11424 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
11425 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
11426
11427 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11428 MachineFunction *MF = MBB->getParent();
11429 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11430 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11431 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11432
11433 MachineFunction::iterator MBBIter = MBB;
11434 ++MBBIter;
11435
11436 // Insert the new basic blocks
11437 MF->insert(MBBIter, offsetMBB);
11438 MF->insert(MBBIter, overflowMBB);
11439 MF->insert(MBBIter, endMBB);
11440
11441 // Transfer the remainder of MBB and its successor edges to endMBB.
11442 endMBB->splice(endMBB->begin(), thisMBB,
11443 llvm::next(MachineBasicBlock::iterator(MI)),
11444 thisMBB->end());
11445 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
11446
11447 // Make offsetMBB and overflowMBB successors of thisMBB
11448 thisMBB->addSuccessor(offsetMBB);
11449 thisMBB->addSuccessor(overflowMBB);
11450
11451 // endMBB is a successor of both offsetMBB and overflowMBB
11452 offsetMBB->addSuccessor(endMBB);
11453 overflowMBB->addSuccessor(endMBB);
11454
11455 // Load the offset value into a register
11456 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
11457 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
11458 .addOperand(Base)
11459 .addOperand(Scale)
11460 .addOperand(Index)
11461 .addDisp(Disp, UseFPOffset ? 4 : 0)
11462 .addOperand(Segment)
11463 .setMemRefs(MMOBegin, MMOEnd);
11464
11465 // Check if there is enough room left to pull this argument.
11466 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
11467 .addReg(OffsetReg)
11468 .addImm(MaxOffset + 8 - ArgSizeA8);
11469
11470 // Branch to "overflowMBB" if offset >= max
11471 // Fall through to "offsetMBB" otherwise
11472 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
11473 .addMBB(overflowMBB);
11474 }
11475
11476 // In offsetMBB, emit code to use the reg_save_area.
11477 if (offsetMBB) {
11478 assert(OffsetReg != 0);
11479
11480 // Read the reg_save_area address.
11481 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
11482 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
11483 .addOperand(Base)
11484 .addOperand(Scale)
11485 .addOperand(Index)
11486 .addDisp(Disp, 16)
11487 .addOperand(Segment)
11488 .setMemRefs(MMOBegin, MMOEnd);
11489
11490 // Zero-extend the offset
11491 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
11492 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
11493 .addImm(0)
11494 .addReg(OffsetReg)
11495 .addImm(X86::sub_32bit);
11496
11497 // Add the offset to the reg_save_area to get the final address.
11498 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
11499 .addReg(OffsetReg64)
11500 .addReg(RegSaveReg);
11501
11502 // Compute the offset for the next argument
11503 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
11504 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
11505 .addReg(OffsetReg)
11506 .addImm(UseFPOffset ? 16 : 8);
11507
11508 // Store it back into the va_list.
11509 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
11510 .addOperand(Base)
11511 .addOperand(Scale)
11512 .addOperand(Index)
11513 .addDisp(Disp, UseFPOffset ? 4 : 0)
11514 .addOperand(Segment)
11515 .addReg(NextOffsetReg)
11516 .setMemRefs(MMOBegin, MMOEnd);
11517
11518 // Jump to endMBB
11519 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
11520 .addMBB(endMBB);
11521 }
11522
11523 //
11524 // Emit code to use overflow area
11525 //
11526
11527 // Load the overflow_area address into a register.
11528 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
11529 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
11530 .addOperand(Base)
11531 .addOperand(Scale)
11532 .addOperand(Index)
11533 .addDisp(Disp, 8)
11534 .addOperand(Segment)
11535 .setMemRefs(MMOBegin, MMOEnd);
11536
11537 // If we need to align it, do so. Otherwise, just copy the address
11538 // to OverflowDestReg.
11539 if (NeedsAlign) {
11540 // Align the overflow address
11541 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
11542 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
11543
11544 // aligned_addr = (addr + (align-1)) & ~(align-1)
11545 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
11546 .addReg(OverflowAddrReg)
11547 .addImm(Align-1);
11548
11549 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
11550 .addReg(TmpReg)
11551 .addImm(~(uint64_t)(Align-1));
11552 } else {
11553 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
11554 .addReg(OverflowAddrReg);
11555 }
11556
11557 // Compute the next overflow address after this argument.
11558 // (the overflow address should be kept 8-byte aligned)
11559 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
11560 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
11561 .addReg(OverflowDestReg)
11562 .addImm(ArgSizeA8);
11563
11564 // Store the new overflow address.
11565 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
11566 .addOperand(Base)
11567 .addOperand(Scale)
11568 .addOperand(Index)
11569 .addDisp(Disp, 8)
11570 .addOperand(Segment)
11571 .addReg(NextAddrReg)
11572 .setMemRefs(MMOBegin, MMOEnd);
11573
11574 // If we branched, emit the PHI to the front of endMBB.
11575 if (offsetMBB) {
11576 BuildMI(*endMBB, endMBB->begin(), DL,
11577 TII->get(X86::PHI), DestReg)
11578 .addReg(OffsetDestReg).addMBB(offsetMBB)
11579 .addReg(OverflowDestReg).addMBB(overflowMBB);
11580 }
11581
11582 // Erase the pseudo instruction
11583 MI->eraseFromParent();
11584
11585 return endMBB;
11586}
11587
11588MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000011589X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
11590 MachineInstr *MI,
11591 MachineBasicBlock *MBB) const {
11592 // Emit code to save XMM registers to the stack. The ABI says that the
11593 // number of registers to save is given in %al, so it's theoretically
11594 // possible to do an indirect jump trick to avoid saving all of them,
11595 // however this code takes a simpler approach and just executes all
11596 // of the stores if %al is non-zero. It's less code, and it's probably
11597 // easier on the hardware branch predictor, and stores aren't all that
11598 // expensive anyway.
11599
11600 // Create the new basic blocks. One block contains all the XMM stores,
11601 // and one block is the final destination regardless of whether any
11602 // stores were performed.
11603 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11604 MachineFunction *F = MBB->getParent();
11605 MachineFunction::iterator MBBIter = MBB;
11606 ++MBBIter;
11607 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
11608 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
11609 F->insert(MBBIter, XMMSaveMBB);
11610 F->insert(MBBIter, EndMBB);
11611
Dan Gohman14152b42010-07-06 20:24:04 +000011612 // Transfer the remainder of MBB and its successor edges to EndMBB.
11613 EndMBB->splice(EndMBB->begin(), MBB,
11614 llvm::next(MachineBasicBlock::iterator(MI)),
11615 MBB->end());
11616 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
11617
Dan Gohmand6708ea2009-08-15 01:38:56 +000011618 // The original block will now fall through to the XMM save block.
11619 MBB->addSuccessor(XMMSaveMBB);
11620 // The XMMSaveMBB will fall through to the end block.
11621 XMMSaveMBB->addSuccessor(EndMBB);
11622
11623 // Now add the instructions.
11624 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11625 DebugLoc DL = MI->getDebugLoc();
11626
11627 unsigned CountReg = MI->getOperand(0).getReg();
11628 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
11629 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
11630
11631 if (!Subtarget->isTargetWin64()) {
11632 // If %al is 0, branch around the XMM save block.
11633 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011634 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000011635 MBB->addSuccessor(EndMBB);
11636 }
11637
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011638 unsigned MOVOpc = Subtarget->hasAVX() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000011639 // In the XMM save block, save all the XMM argument registers.
11640 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
11641 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000011642 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000011643 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000011644 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000011645 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000011646 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011647 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000011648 .addFrameIndex(RegSaveFrameIndex)
11649 .addImm(/*Scale=*/1)
11650 .addReg(/*IndexReg=*/0)
11651 .addImm(/*Disp=*/Offset)
11652 .addReg(/*Segment=*/0)
11653 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000011654 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000011655 }
11656
Dan Gohman14152b42010-07-06 20:24:04 +000011657 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000011658
11659 return EndMBB;
11660}
Mon P Wang63307c32008-05-05 19:05:59 +000011661
Evan Cheng60c07e12006-07-05 22:17:51 +000011662MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000011663X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000011664 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000011665 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11666 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000011667
Chris Lattner52600972009-09-02 05:57:00 +000011668 // To "insert" a SELECT_CC instruction, we actually have to insert the
11669 // diamond control-flow pattern. The incoming instruction knows the
11670 // destination vreg to set, the condition code register to branch on, the
11671 // true/false values to select between, and a branch opcode to use.
11672 const BasicBlock *LLVM_BB = BB->getBasicBlock();
11673 MachineFunction::iterator It = BB;
11674 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000011675
Chris Lattner52600972009-09-02 05:57:00 +000011676 // thisMBB:
11677 // ...
11678 // TrueVal = ...
11679 // cmpTY ccX, r1, r2
11680 // bCC copy1MBB
11681 // fallthrough --> copy0MBB
11682 MachineBasicBlock *thisMBB = BB;
11683 MachineFunction *F = BB->getParent();
11684 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
11685 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000011686 F->insert(It, copy0MBB);
11687 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000011688
Bill Wendling730c07e2010-06-25 20:48:10 +000011689 // If the EFLAGS register isn't dead in the terminator, then claim that it's
11690 // live into the sink and copy blocks.
Jakob Stoklund Olesen4a1b9d82011-09-02 23:52:49 +000011691 if (!MI->killsRegister(X86::EFLAGS)) {
11692 copy0MBB->addLiveIn(X86::EFLAGS);
11693 sinkMBB->addLiveIn(X86::EFLAGS);
Bill Wendling730c07e2010-06-25 20:48:10 +000011694 }
11695
Dan Gohman14152b42010-07-06 20:24:04 +000011696 // Transfer the remainder of BB and its successor edges to sinkMBB.
11697 sinkMBB->splice(sinkMBB->begin(), BB,
11698 llvm::next(MachineBasicBlock::iterator(MI)),
11699 BB->end());
11700 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
11701
11702 // Add the true and fallthrough blocks as its successors.
11703 BB->addSuccessor(copy0MBB);
11704 BB->addSuccessor(sinkMBB);
11705
11706 // Create the conditional branch instruction.
11707 unsigned Opc =
11708 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
11709 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
11710
Chris Lattner52600972009-09-02 05:57:00 +000011711 // copy0MBB:
11712 // %FalseValue = ...
11713 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000011714 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000011715
Chris Lattner52600972009-09-02 05:57:00 +000011716 // sinkMBB:
11717 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
11718 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000011719 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
11720 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000011721 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
11722 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
11723
Dan Gohman14152b42010-07-06 20:24:04 +000011724 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000011725 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000011726}
11727
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011728MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000011729X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
11730 bool Is64Bit) const {
11731 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11732 DebugLoc DL = MI->getDebugLoc();
11733 MachineFunction *MF = BB->getParent();
11734 const BasicBlock *LLVM_BB = BB->getBasicBlock();
11735
11736 assert(EnableSegmentedStacks);
11737
11738 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
11739 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
11740
11741 // BB:
11742 // ... [Till the alloca]
11743 // If stacklet is not large enough, jump to mallocMBB
11744 //
11745 // bumpMBB:
11746 // Allocate by subtracting from RSP
11747 // Jump to continueMBB
11748 //
11749 // mallocMBB:
11750 // Allocate by call to runtime
11751 //
11752 // continueMBB:
11753 // ...
11754 // [rest of original BB]
11755 //
11756
11757 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11758 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11759 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11760
11761 MachineRegisterInfo &MRI = MF->getRegInfo();
11762 const TargetRegisterClass *AddrRegClass =
11763 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
11764
11765 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
11766 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
11767 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
11768 sizeVReg = MI->getOperand(1).getReg(),
11769 physSPReg = Is64Bit ? X86::RSP : X86::ESP;
11770
11771 MachineFunction::iterator MBBIter = BB;
11772 ++MBBIter;
11773
11774 MF->insert(MBBIter, bumpMBB);
11775 MF->insert(MBBIter, mallocMBB);
11776 MF->insert(MBBIter, continueMBB);
11777
11778 continueMBB->splice(continueMBB->begin(), BB, llvm::next
11779 (MachineBasicBlock::iterator(MI)), BB->end());
11780 continueMBB->transferSuccessorsAndUpdatePHIs(BB);
11781
11782 // Add code to the main basic block to check if the stack limit has been hit,
11783 // and if so, jump to mallocMBB otherwise to bumpMBB.
11784 BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
11785 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), tmpSPVReg)
11786 .addReg(tmpSPVReg).addReg(sizeVReg);
11787 BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
11788 .addReg(0).addImm(0).addReg(0).addImm(TlsOffset).addReg(TlsReg)
11789 .addReg(tmpSPVReg);
11790 BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
11791
11792 // bumpMBB simply decreases the stack pointer, since we know the current
11793 // stacklet has enough space.
11794 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
11795 .addReg(tmpSPVReg);
11796 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
11797 .addReg(tmpSPVReg);
11798 BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
11799
11800 // Calls into a routine in libgcc to allocate more space from the heap.
11801 if (Is64Bit) {
11802 BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
11803 .addReg(sizeVReg);
11804 BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
11805 .addExternalSymbol("__morestack_allocate_stack_space").addReg(X86::RDI);
11806 } else {
11807 BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
11808 .addImm(12);
11809 BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
11810 BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
11811 .addExternalSymbol("__morestack_allocate_stack_space");
11812 }
11813
11814 if (!Is64Bit)
11815 BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
11816 .addImm(16);
11817
11818 BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
11819 .addReg(Is64Bit ? X86::RAX : X86::EAX);
11820 BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
11821
11822 // Set up the CFG correctly.
11823 BB->addSuccessor(bumpMBB);
11824 BB->addSuccessor(mallocMBB);
11825 mallocMBB->addSuccessor(continueMBB);
11826 bumpMBB->addSuccessor(continueMBB);
11827
11828 // Take care of the PHI nodes.
11829 BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
11830 MI->getOperand(0).getReg())
11831 .addReg(mallocPtrVReg).addMBB(mallocMBB)
11832 .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
11833
11834 // Delete the original pseudo instruction.
11835 MI->eraseFromParent();
11836
11837 // And we're done.
11838 return continueMBB;
11839}
11840
11841MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000011842X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000011843 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011844 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11845 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011846
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000011847 assert(!Subtarget->isTargetEnvMacho());
11848
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011849 // The lowering is pretty easy: we're just emitting the call to _alloca. The
11850 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011851
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000011852 if (Subtarget->isTargetWin64()) {
11853 if (Subtarget->isTargetCygMing()) {
11854 // ___chkstk(Mingw64):
11855 // Clobbers R10, R11, RAX and EFLAGS.
11856 // Updates RSP.
11857 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
11858 .addExternalSymbol("___chkstk")
11859 .addReg(X86::RAX, RegState::Implicit)
11860 .addReg(X86::RSP, RegState::Implicit)
11861 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
11862 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
11863 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
11864 } else {
11865 // __chkstk(MSVCRT): does not update stack pointer.
11866 // Clobbers R10, R11 and EFLAGS.
11867 // FIXME: RAX(allocated size) might be reused and not killed.
11868 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
11869 .addExternalSymbol("__chkstk")
11870 .addReg(X86::RAX, RegState::Implicit)
11871 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
11872 // RAX has the offset to subtracted from RSP.
11873 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
11874 .addReg(X86::RSP)
11875 .addReg(X86::RAX);
11876 }
11877 } else {
11878 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000011879 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
11880
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000011881 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
11882 .addExternalSymbol(StackProbeSymbol)
11883 .addReg(X86::EAX, RegState::Implicit)
11884 .addReg(X86::ESP, RegState::Implicit)
11885 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
11886 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
11887 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
11888 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011889
Dan Gohman14152b42010-07-06 20:24:04 +000011890 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011891 return BB;
11892}
Chris Lattner52600972009-09-02 05:57:00 +000011893
11894MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000011895X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
11896 MachineBasicBlock *BB) const {
11897 // This is pretty easy. We're taking the value that we received from
11898 // our load from the relocation, sticking it in either RDI (x86-64)
11899 // or EAX and doing an indirect call. The return value will then
11900 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000011901 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000011902 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000011903 DebugLoc DL = MI->getDebugLoc();
11904 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000011905
11906 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000011907 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000011908
Eric Christopher30ef0e52010-06-03 04:07:48 +000011909 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000011910 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
11911 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000011912 .addReg(X86::RIP)
11913 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000011914 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000011915 MI->getOperand(3).getTargetFlags())
11916 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000011917 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000011918 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +000011919 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000011920 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
11921 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000011922 .addReg(0)
11923 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000011924 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000011925 MI->getOperand(3).getTargetFlags())
11926 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000011927 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000011928 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000011929 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000011930 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
11931 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000011932 .addReg(TII->getGlobalBaseReg(F))
11933 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000011934 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000011935 MI->getOperand(3).getTargetFlags())
11936 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000011937 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000011938 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000011939 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000011940
Dan Gohman14152b42010-07-06 20:24:04 +000011941 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000011942 return BB;
11943}
11944
11945MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000011946X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000011947 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000011948 switch (MI->getOpcode()) {
Richard Trieu23946fc2011-09-21 03:09:09 +000011949 default: assert(0 && "Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000011950 case X86::TAILJMPd64:
11951 case X86::TAILJMPr64:
11952 case X86::TAILJMPm64:
Richard Trieu23946fc2011-09-21 03:09:09 +000011953 assert(0 && "TAILJMP64 would not be touched here.");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000011954 case X86::TCRETURNdi64:
11955 case X86::TCRETURNri64:
11956 case X86::TCRETURNmi64:
11957 // Defs of TCRETURNxx64 has Win64's callee-saved registers, as subset.
11958 // On AMD64, additional defs should be added before register allocation.
11959 if (!Subtarget->isTargetWin64()) {
11960 MI->addRegisterDefined(X86::RSI);
11961 MI->addRegisterDefined(X86::RDI);
11962 MI->addRegisterDefined(X86::XMM6);
11963 MI->addRegisterDefined(X86::XMM7);
11964 MI->addRegisterDefined(X86::XMM8);
11965 MI->addRegisterDefined(X86::XMM9);
11966 MI->addRegisterDefined(X86::XMM10);
11967 MI->addRegisterDefined(X86::XMM11);
11968 MI->addRegisterDefined(X86::XMM12);
11969 MI->addRegisterDefined(X86::XMM13);
11970 MI->addRegisterDefined(X86::XMM14);
11971 MI->addRegisterDefined(X86::XMM15);
11972 }
11973 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000011974 case X86::WIN_ALLOCA:
11975 return EmitLoweredWinAlloca(MI, BB);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000011976 case X86::SEG_ALLOCA_32:
11977 return EmitLoweredSegAlloca(MI, BB, false);
11978 case X86::SEG_ALLOCA_64:
11979 return EmitLoweredSegAlloca(MI, BB, true);
Eric Christopher30ef0e52010-06-03 04:07:48 +000011980 case X86::TLSCall_32:
11981 case X86::TLSCall_64:
11982 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000011983 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000011984 case X86::CMOV_FR32:
11985 case X86::CMOV_FR64:
11986 case X86::CMOV_V4F32:
11987 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000011988 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000011989 case X86::CMOV_V8F32:
11990 case X86::CMOV_V4F64:
11991 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000011992 case X86::CMOV_GR16:
11993 case X86::CMOV_GR32:
11994 case X86::CMOV_RFP32:
11995 case X86::CMOV_RFP64:
11996 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000011997 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000011998
Dale Johannesen849f2142007-07-03 00:53:03 +000011999 case X86::FP32_TO_INT16_IN_MEM:
12000 case X86::FP32_TO_INT32_IN_MEM:
12001 case X86::FP32_TO_INT64_IN_MEM:
12002 case X86::FP64_TO_INT16_IN_MEM:
12003 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000012004 case X86::FP64_TO_INT64_IN_MEM:
12005 case X86::FP80_TO_INT16_IN_MEM:
12006 case X86::FP80_TO_INT32_IN_MEM:
12007 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000012008 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12009 DebugLoc DL = MI->getDebugLoc();
12010
Evan Cheng60c07e12006-07-05 22:17:51 +000012011 // Change the floating point control register to use "round towards zero"
12012 // mode when truncating to an integer value.
12013 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000012014 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000012015 addFrameReference(BuildMI(*BB, MI, DL,
12016 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012017
12018 // Load the old value of the high byte of the control word...
12019 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000012020 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000012021 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000012022 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012023
12024 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000012025 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012026 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000012027
12028 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000012029 addFrameReference(BuildMI(*BB, MI, DL,
12030 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012031
12032 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000012033 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012034 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000012035
12036 // Get the X86 opcode to use.
12037 unsigned Opc;
12038 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000012039 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000012040 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
12041 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
12042 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
12043 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
12044 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
12045 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000012046 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
12047 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
12048 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000012049 }
12050
12051 X86AddressMode AM;
12052 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000012053 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012054 AM.BaseType = X86AddressMode::RegBase;
12055 AM.Base.Reg = Op.getReg();
12056 } else {
12057 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000012058 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000012059 }
12060 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000012061 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012062 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012063 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000012064 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012065 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012066 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000012067 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012068 AM.GV = Op.getGlobal();
12069 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000012070 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012071 }
Dan Gohman14152b42010-07-06 20:24:04 +000012072 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000012073 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000012074
12075 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000012076 addFrameReference(BuildMI(*BB, MI, DL,
12077 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012078
Dan Gohman14152b42010-07-06 20:24:04 +000012079 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000012080 return BB;
12081 }
Eric Christopherb120ab42009-08-18 22:50:32 +000012082 // String/text processing lowering.
12083 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012084 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000012085 return EmitPCMP(MI, BB, 3, false /* in-mem */);
12086 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012087 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000012088 return EmitPCMP(MI, BB, 3, true /* in-mem */);
12089 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012090 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000012091 return EmitPCMP(MI, BB, 5, false /* in mem */);
12092 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012093 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000012094 return EmitPCMP(MI, BB, 5, true /* in mem */);
12095
Eric Christopher228232b2010-11-30 07:20:12 +000012096 // Thread synchronization.
12097 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012098 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000012099 case X86::MWAIT:
12100 return EmitMwait(MI, BB);
12101
Eric Christopherb120ab42009-08-18 22:50:32 +000012102 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000012103 case X86::ATOMAND32:
12104 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012105 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012106 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012107 X86::NOT32r, X86::EAX,
12108 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000012109 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000012110 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
12111 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012112 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012113 X86::NOT32r, X86::EAX,
12114 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000012115 case X86::ATOMXOR32:
12116 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012117 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012118 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012119 X86::NOT32r, X86::EAX,
12120 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000012121 case X86::ATOMNAND32:
12122 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012123 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012124 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012125 X86::NOT32r, X86::EAX,
12126 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000012127 case X86::ATOMMIN32:
12128 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
12129 case X86::ATOMMAX32:
12130 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
12131 case X86::ATOMUMIN32:
12132 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
12133 case X86::ATOMUMAX32:
12134 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000012135
12136 case X86::ATOMAND16:
12137 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
12138 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012139 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012140 X86::NOT16r, X86::AX,
12141 X86::GR16RegisterClass);
12142 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000012143 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012144 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012145 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012146 X86::NOT16r, X86::AX,
12147 X86::GR16RegisterClass);
12148 case X86::ATOMXOR16:
12149 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
12150 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012151 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012152 X86::NOT16r, X86::AX,
12153 X86::GR16RegisterClass);
12154 case X86::ATOMNAND16:
12155 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
12156 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012157 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012158 X86::NOT16r, X86::AX,
12159 X86::GR16RegisterClass, true);
12160 case X86::ATOMMIN16:
12161 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
12162 case X86::ATOMMAX16:
12163 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
12164 case X86::ATOMUMIN16:
12165 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
12166 case X86::ATOMUMAX16:
12167 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
12168
12169 case X86::ATOMAND8:
12170 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
12171 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012172 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012173 X86::NOT8r, X86::AL,
12174 X86::GR8RegisterClass);
12175 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000012176 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012177 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012178 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012179 X86::NOT8r, X86::AL,
12180 X86::GR8RegisterClass);
12181 case X86::ATOMXOR8:
12182 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
12183 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012184 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012185 X86::NOT8r, X86::AL,
12186 X86::GR8RegisterClass);
12187 case X86::ATOMNAND8:
12188 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
12189 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012190 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012191 X86::NOT8r, X86::AL,
12192 X86::GR8RegisterClass, true);
12193 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012194 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000012195 case X86::ATOMAND64:
12196 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012197 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012198 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012199 X86::NOT64r, X86::RAX,
12200 X86::GR64RegisterClass);
12201 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000012202 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
12203 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012204 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012205 X86::NOT64r, X86::RAX,
12206 X86::GR64RegisterClass);
12207 case X86::ATOMXOR64:
12208 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012209 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012210 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012211 X86::NOT64r, X86::RAX,
12212 X86::GR64RegisterClass);
12213 case X86::ATOMNAND64:
12214 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
12215 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012216 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012217 X86::NOT64r, X86::RAX,
12218 X86::GR64RegisterClass, true);
12219 case X86::ATOMMIN64:
12220 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
12221 case X86::ATOMMAX64:
12222 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
12223 case X86::ATOMUMIN64:
12224 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
12225 case X86::ATOMUMAX64:
12226 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012227
12228 // This group does 64-bit operations on a 32-bit host.
12229 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012230 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012231 X86::AND32rr, X86::AND32rr,
12232 X86::AND32ri, X86::AND32ri,
12233 false);
12234 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012235 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012236 X86::OR32rr, X86::OR32rr,
12237 X86::OR32ri, X86::OR32ri,
12238 false);
12239 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012240 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012241 X86::XOR32rr, X86::XOR32rr,
12242 X86::XOR32ri, X86::XOR32ri,
12243 false);
12244 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012245 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012246 X86::AND32rr, X86::AND32rr,
12247 X86::AND32ri, X86::AND32ri,
12248 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012249 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012250 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012251 X86::ADD32rr, X86::ADC32rr,
12252 X86::ADD32ri, X86::ADC32ri,
12253 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012254 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012255 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012256 X86::SUB32rr, X86::SBB32rr,
12257 X86::SUB32ri, X86::SBB32ri,
12258 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000012259 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012260 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000012261 X86::MOV32rr, X86::MOV32rr,
12262 X86::MOV32ri, X86::MOV32ri,
12263 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012264 case X86::VASTART_SAVE_XMM_REGS:
12265 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000012266
12267 case X86::VAARG_64:
12268 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000012269 }
12270}
12271
12272//===----------------------------------------------------------------------===//
12273// X86 Optimization Hooks
12274//===----------------------------------------------------------------------===//
12275
Dan Gohman475871a2008-07-27 21:46:04 +000012276void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000012277 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000012278 APInt &KnownZero,
12279 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000012280 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000012281 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012282 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000012283 assert((Opc >= ISD::BUILTIN_OP_END ||
12284 Opc == ISD::INTRINSIC_WO_CHAIN ||
12285 Opc == ISD::INTRINSIC_W_CHAIN ||
12286 Opc == ISD::INTRINSIC_VOID) &&
12287 "Should use MaskedValueIsZero if you don't know whether Op"
12288 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012289
Dan Gohmanf4f92f52008-02-13 23:07:24 +000012290 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012291 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000012292 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012293 case X86ISD::ADD:
12294 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000012295 case X86ISD::ADC:
12296 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012297 case X86ISD::SMUL:
12298 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000012299 case X86ISD::INC:
12300 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000012301 case X86ISD::OR:
12302 case X86ISD::XOR:
12303 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012304 // These nodes' second result is a boolean.
12305 if (Op.getResNo() == 0)
12306 break;
12307 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012308 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000012309 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
12310 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000012311 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012312 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012313}
Chris Lattner259e97c2006-01-31 19:43:35 +000012314
Owen Andersonbc146b02010-09-21 20:42:50 +000012315unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
12316 unsigned Depth) const {
12317 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
12318 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
12319 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000012320
Owen Andersonbc146b02010-09-21 20:42:50 +000012321 // Fallback case.
12322 return 1;
12323}
12324
Evan Cheng206ee9d2006-07-07 08:33:52 +000012325/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000012326/// node is a GlobalAddress + offset.
12327bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000012328 const GlobalValue* &GA,
12329 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000012330 if (N->getOpcode() == X86ISD::Wrapper) {
12331 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000012332 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000012333 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000012334 return true;
12335 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000012336 }
Evan Chengad4196b2008-05-12 19:56:52 +000012337 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000012338}
12339
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012340/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
12341/// same as extracting the high 128-bit part of 256-bit vector and then
12342/// inserting the result into the low part of a new 256-bit vector
12343static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
12344 EVT VT = SVOp->getValueType(0);
12345 int NumElems = VT.getVectorNumElements();
12346
12347 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
12348 for (int i = 0, j = NumElems/2; i < NumElems/2; ++i, ++j)
12349 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
12350 SVOp->getMaskElt(j) >= 0)
12351 return false;
12352
12353 return true;
12354}
12355
12356/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
12357/// same as extracting the low 128-bit part of 256-bit vector and then
12358/// inserting the result into the high part of a new 256-bit vector
12359static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
12360 EVT VT = SVOp->getValueType(0);
12361 int NumElems = VT.getVectorNumElements();
12362
12363 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
12364 for (int i = NumElems/2, j = 0; i < NumElems; ++i, ++j)
12365 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
12366 SVOp->getMaskElt(j) >= 0)
12367 return false;
12368
12369 return true;
12370}
12371
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012372/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
12373static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
12374 TargetLowering::DAGCombinerInfo &DCI) {
12375 DebugLoc dl = N->getDebugLoc();
12376 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
12377 SDValue V1 = SVOp->getOperand(0);
12378 SDValue V2 = SVOp->getOperand(1);
12379 EVT VT = SVOp->getValueType(0);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012380 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012381
12382 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
12383 V2.getOpcode() == ISD::CONCAT_VECTORS) {
12384 //
12385 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000012386 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012387 // V UNDEF BUILD_VECTOR UNDEF
12388 // \ / \ /
12389 // CONCAT_VECTOR CONCAT_VECTOR
12390 // \ /
12391 // \ /
12392 // RESULT: V + zero extended
12393 //
12394 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
12395 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
12396 V1.getOperand(1).getOpcode() != ISD::UNDEF)
12397 return SDValue();
12398
12399 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
12400 return SDValue();
12401
12402 // To match the shuffle mask, the first half of the mask should
12403 // be exactly the first vector, and all the rest a splat with the
12404 // first element of the second one.
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012405 for (int i = 0; i < NumElems/2; ++i)
12406 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
12407 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
12408 return SDValue();
12409
12410 // Emit a zeroed vector and insert the desired subvector on its
12411 // first half.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000012412 SDValue Zeros = getZeroVector(VT, true /* HasXMMInt */, DAG, dl);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012413 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0),
12414 DAG.getConstant(0, MVT::i32), DAG, dl);
12415 return DCI.CombineTo(N, InsV);
12416 }
12417
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012418 //===--------------------------------------------------------------------===//
12419 // Combine some shuffles into subvector extracts and inserts:
12420 //
12421
12422 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
12423 if (isShuffleHigh128VectorInsertLow(SVOp)) {
12424 SDValue V = Extract128BitVector(V1, DAG.getConstant(NumElems/2, MVT::i32),
12425 DAG, dl);
12426 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT),
12427 V, DAG.getConstant(0, MVT::i32), DAG, dl);
12428 return DCI.CombineTo(N, InsV);
12429 }
12430
12431 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
12432 if (isShuffleLow128VectorInsertHigh(SVOp)) {
12433 SDValue V = Extract128BitVector(V1, DAG.getConstant(0, MVT::i32), DAG, dl);
12434 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT),
12435 V, DAG.getConstant(NumElems/2, MVT::i32), DAG, dl);
12436 return DCI.CombineTo(N, InsV);
12437 }
12438
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012439 return SDValue();
12440}
12441
12442/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000012443static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000012444 TargetLowering::DAGCombinerInfo &DCI,
12445 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000012446 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000012447 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000012448
Mon P Wanga0fd0d52010-12-19 23:55:53 +000012449 // Don't create instructions with illegal types after legalize types has run.
12450 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12451 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
12452 return SDValue();
12453
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000012454 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
12455 if (Subtarget->hasAVX() && VT.getSizeInBits() == 256 &&
12456 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012457 return PerformShuffleCombine256(N, DAG, DCI);
12458
12459 // Only handle 128 wide vector from here on.
12460 if (VT.getSizeInBits() != 128)
12461 return SDValue();
12462
12463 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
12464 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
12465 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000012466 SmallVector<SDValue, 16> Elts;
12467 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012468 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000012469
Nate Begemanfdea31a2010-03-24 20:49:50 +000012470 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000012471}
Evan Chengd880b972008-05-09 21:53:03 +000012472
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000012473/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
12474/// generation and convert it from being a bunch of shuffles and extracts
12475/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012476static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
12477 const TargetLowering &TLI) {
12478 SDValue InputVector = N->getOperand(0);
12479
12480 // Only operate on vectors of 4 elements, where the alternative shuffling
12481 // gets to be more expensive.
12482 if (InputVector.getValueType() != MVT::v4i32)
12483 return SDValue();
12484
12485 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
12486 // single use which is a sign-extend or zero-extend, and all elements are
12487 // used.
12488 SmallVector<SDNode *, 4> Uses;
12489 unsigned ExtractedElements = 0;
12490 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
12491 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
12492 if (UI.getUse().getResNo() != InputVector.getResNo())
12493 return SDValue();
12494
12495 SDNode *Extract = *UI;
12496 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
12497 return SDValue();
12498
12499 if (Extract->getValueType(0) != MVT::i32)
12500 return SDValue();
12501 if (!Extract->hasOneUse())
12502 return SDValue();
12503 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
12504 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
12505 return SDValue();
12506 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
12507 return SDValue();
12508
12509 // Record which element was extracted.
12510 ExtractedElements |=
12511 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
12512
12513 Uses.push_back(Extract);
12514 }
12515
12516 // If not all the elements were used, this may not be worthwhile.
12517 if (ExtractedElements != 15)
12518 return SDValue();
12519
12520 // Ok, we've now decided to do the transformation.
12521 DebugLoc dl = InputVector.getDebugLoc();
12522
12523 // Store the value to a temporary stack slot.
12524 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000012525 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
12526 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012527
12528 // Replace each use (extract) with a load of the appropriate element.
12529 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
12530 UE = Uses.end(); UI != UE; ++UI) {
12531 SDNode *Extract = *UI;
12532
Nadav Rotem86694292011-05-17 08:31:57 +000012533 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012534 SDValue Idx = Extract->getOperand(1);
12535 unsigned EltSize =
12536 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
12537 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
12538 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
12539
Nadav Rotem86694292011-05-17 08:31:57 +000012540 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000012541 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012542
12543 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000012544 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000012545 ScalarAddr, MachinePointerInfo(),
12546 false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012547
12548 // Replace the exact with the load.
12549 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
12550 }
12551
12552 // The replacement was made in place; don't return anything.
12553 return SDValue();
12554}
12555
Duncan Sands6bcd2192011-09-17 16:49:39 +000012556/// PerformSELECTCombine - Do target-specific dag combines on SELECT and VSELECT
12557/// nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000012558static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000012559 const X86Subtarget *Subtarget) {
12560 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000012561 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000012562 // Get the LHS/RHS of the select.
12563 SDValue LHS = N->getOperand(1);
12564 SDValue RHS = N->getOperand(2);
Bruno Cardoso Lopes149f29f2011-09-20 22:34:45 +000012565 EVT VT = LHS.getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +000012566
Dan Gohman670e5392009-09-21 18:03:22 +000012567 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000012568 // instructions match the semantics of the common C idiom x<y?x:y but not
12569 // x<=y?x:y, because of how they handle negative zero (which can be
12570 // ignored in unsafe-math mode).
Bruno Cardoso Lopes149f29f2011-09-20 22:34:45 +000012571 if (Cond.getOpcode() == ISD::SETCC && VT.isFloatingPoint() &&
12572 VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000012573 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012574
Chris Lattner47b4ce82009-03-11 05:48:52 +000012575 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000012576 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000012577 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
12578 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000012579 switch (CC) {
12580 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000012581 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000012582 // Converting this to a min would handle NaNs incorrectly, and swapping
12583 // the operands would cause it to handle comparisons between positive
12584 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000012585 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000012586 if (!UnsafeFPMath &&
12587 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
12588 break;
12589 std::swap(LHS, RHS);
12590 }
Dan Gohman670e5392009-09-21 18:03:22 +000012591 Opcode = X86ISD::FMIN;
12592 break;
12593 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000012594 // Converting this to a min would handle comparisons between positive
12595 // and negative zero incorrectly.
12596 if (!UnsafeFPMath &&
12597 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
12598 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012599 Opcode = X86ISD::FMIN;
12600 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000012601 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000012602 // Converting this to a min would handle both negative zeros and NaNs
12603 // incorrectly, but we can swap the operands to fix both.
12604 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012605 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012606 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000012607 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012608 Opcode = X86ISD::FMIN;
12609 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012610
Dan Gohman670e5392009-09-21 18:03:22 +000012611 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012612 // Converting this to a max would handle comparisons between positive
12613 // and negative zero incorrectly.
12614 if (!UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000012615 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012616 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012617 Opcode = X86ISD::FMAX;
12618 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000012619 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000012620 // Converting this to a max would handle NaNs incorrectly, and swapping
12621 // the operands would cause it to handle comparisons between positive
12622 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000012623 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000012624 if (!UnsafeFPMath &&
12625 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
12626 break;
12627 std::swap(LHS, RHS);
12628 }
Dan Gohman670e5392009-09-21 18:03:22 +000012629 Opcode = X86ISD::FMAX;
12630 break;
12631 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012632 // Converting this to a max would handle both negative zeros and NaNs
12633 // incorrectly, but we can swap the operands to fix both.
12634 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012635 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012636 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012637 case ISD::SETGE:
12638 Opcode = X86ISD::FMAX;
12639 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000012640 }
Dan Gohman670e5392009-09-21 18:03:22 +000012641 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000012642 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
12643 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000012644 switch (CC) {
12645 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000012646 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012647 // Converting this to a min would handle comparisons between positive
12648 // and negative zero incorrectly, and swapping the operands would
12649 // cause it to handle NaNs incorrectly.
12650 if (!UnsafeFPMath &&
12651 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000012652 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012653 break;
12654 std::swap(LHS, RHS);
12655 }
Dan Gohman670e5392009-09-21 18:03:22 +000012656 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000012657 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012658 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000012659 // Converting this to a min would handle NaNs incorrectly.
12660 if (!UnsafeFPMath &&
12661 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
12662 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012663 Opcode = X86ISD::FMIN;
12664 break;
12665 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012666 // Converting this to a min would handle both negative zeros and NaNs
12667 // incorrectly, but we can swap the operands to fix both.
12668 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012669 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012670 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012671 case ISD::SETGE:
12672 Opcode = X86ISD::FMIN;
12673 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012674
Dan Gohman670e5392009-09-21 18:03:22 +000012675 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000012676 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000012677 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012678 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012679 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000012680 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012681 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000012682 // Converting this to a max would handle comparisons between positive
12683 // and negative zero incorrectly, and swapping the operands would
12684 // cause it to handle NaNs incorrectly.
12685 if (!UnsafeFPMath &&
12686 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000012687 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012688 break;
12689 std::swap(LHS, RHS);
12690 }
Dan Gohman670e5392009-09-21 18:03:22 +000012691 Opcode = X86ISD::FMAX;
12692 break;
12693 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000012694 // Converting this to a max would handle both negative zeros and NaNs
12695 // incorrectly, but we can swap the operands to fix both.
12696 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012697 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012698 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000012699 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012700 Opcode = X86ISD::FMAX;
12701 break;
12702 }
Chris Lattner83e6c992006-10-04 06:57:07 +000012703 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012704
Chris Lattner47b4ce82009-03-11 05:48:52 +000012705 if (Opcode)
12706 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000012707 }
Eric Christopherfd179292009-08-27 18:07:15 +000012708
Chris Lattnerd1980a52009-03-12 06:52:53 +000012709 // If this is a select between two integer constants, try to do some
12710 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000012711 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
12712 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000012713 // Don't do this for crazy integer types.
12714 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
12715 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000012716 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000012717 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000012718
Chris Lattnercee56e72009-03-13 05:53:31 +000012719 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000012720 // Efficiently invertible.
12721 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
12722 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
12723 isa<ConstantSDNode>(Cond.getOperand(1))))) {
12724 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000012725 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000012726 }
Eric Christopherfd179292009-08-27 18:07:15 +000012727
Chris Lattnerd1980a52009-03-12 06:52:53 +000012728 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000012729 if (FalseC->getAPIntValue() == 0 &&
12730 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000012731 if (NeedsCondInvert) // Invert the condition if needed.
12732 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
12733 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000012734
Chris Lattnerd1980a52009-03-12 06:52:53 +000012735 // Zero extend the condition if needed.
12736 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000012737
Chris Lattnercee56e72009-03-13 05:53:31 +000012738 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000012739 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000012740 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000012741 }
Eric Christopherfd179292009-08-27 18:07:15 +000012742
Chris Lattner97a29a52009-03-13 05:22:11 +000012743 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000012744 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000012745 if (NeedsCondInvert) // Invert the condition if needed.
12746 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
12747 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000012748
Chris Lattner97a29a52009-03-13 05:22:11 +000012749 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000012750 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
12751 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000012752 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000012753 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000012754 }
Eric Christopherfd179292009-08-27 18:07:15 +000012755
Chris Lattnercee56e72009-03-13 05:53:31 +000012756 // Optimize cases that will turn into an LEA instruction. This requires
12757 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000012758 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000012759 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000012760 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000012761
Chris Lattnercee56e72009-03-13 05:53:31 +000012762 bool isFastMultiplier = false;
12763 if (Diff < 10) {
12764 switch ((unsigned char)Diff) {
12765 default: break;
12766 case 1: // result = add base, cond
12767 case 2: // result = lea base( , cond*2)
12768 case 3: // result = lea base(cond, cond*2)
12769 case 4: // result = lea base( , cond*4)
12770 case 5: // result = lea base(cond, cond*4)
12771 case 8: // result = lea base( , cond*8)
12772 case 9: // result = lea base(cond, cond*8)
12773 isFastMultiplier = true;
12774 break;
12775 }
12776 }
Eric Christopherfd179292009-08-27 18:07:15 +000012777
Chris Lattnercee56e72009-03-13 05:53:31 +000012778 if (isFastMultiplier) {
12779 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
12780 if (NeedsCondInvert) // Invert the condition if needed.
12781 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
12782 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000012783
Chris Lattnercee56e72009-03-13 05:53:31 +000012784 // Zero extend the condition if needed.
12785 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
12786 Cond);
12787 // Scale the condition by the difference.
12788 if (Diff != 1)
12789 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
12790 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000012791
Chris Lattnercee56e72009-03-13 05:53:31 +000012792 // Add the base if non-zero.
12793 if (FalseC->getAPIntValue() != 0)
12794 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
12795 SDValue(FalseC, 0));
12796 return Cond;
12797 }
Eric Christopherfd179292009-08-27 18:07:15 +000012798 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000012799 }
12800 }
Eric Christopherfd179292009-08-27 18:07:15 +000012801
Dan Gohman475871a2008-07-27 21:46:04 +000012802 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000012803}
12804
Chris Lattnerd1980a52009-03-12 06:52:53 +000012805/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
12806static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
12807 TargetLowering::DAGCombinerInfo &DCI) {
12808 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000012809
Chris Lattnerd1980a52009-03-12 06:52:53 +000012810 // If the flag operand isn't dead, don't touch this CMOV.
12811 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
12812 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000012813
Evan Chengb5a55d92011-05-24 01:48:22 +000012814 SDValue FalseOp = N->getOperand(0);
12815 SDValue TrueOp = N->getOperand(1);
12816 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
12817 SDValue Cond = N->getOperand(3);
12818 if (CC == X86::COND_E || CC == X86::COND_NE) {
12819 switch (Cond.getOpcode()) {
12820 default: break;
12821 case X86ISD::BSR:
12822 case X86ISD::BSF:
12823 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
12824 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
12825 return (CC == X86::COND_E) ? FalseOp : TrueOp;
12826 }
12827 }
12828
Chris Lattnerd1980a52009-03-12 06:52:53 +000012829 // If this is a select between two integer constants, try to do some
12830 // optimizations. Note that the operands are ordered the opposite of SELECT
12831 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000012832 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
12833 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000012834 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
12835 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000012836 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
12837 CC = X86::GetOppositeBranchCondition(CC);
12838 std::swap(TrueC, FalseC);
12839 }
Eric Christopherfd179292009-08-27 18:07:15 +000012840
Chris Lattnerd1980a52009-03-12 06:52:53 +000012841 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000012842 // This is efficient for any integer data type (including i8/i16) and
12843 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000012844 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012845 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
12846 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000012847
Chris Lattnerd1980a52009-03-12 06:52:53 +000012848 // Zero extend the condition if needed.
12849 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000012850
Chris Lattnerd1980a52009-03-12 06:52:53 +000012851 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
12852 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000012853 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000012854 if (N->getNumValues() == 2) // Dead flag value?
12855 return DCI.CombineTo(N, Cond, SDValue());
12856 return Cond;
12857 }
Eric Christopherfd179292009-08-27 18:07:15 +000012858
Chris Lattnercee56e72009-03-13 05:53:31 +000012859 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
12860 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000012861 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012862 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
12863 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000012864
Chris Lattner97a29a52009-03-13 05:22:11 +000012865 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000012866 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
12867 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000012868 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
12869 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000012870
Chris Lattner97a29a52009-03-13 05:22:11 +000012871 if (N->getNumValues() == 2) // Dead flag value?
12872 return DCI.CombineTo(N, Cond, SDValue());
12873 return Cond;
12874 }
Eric Christopherfd179292009-08-27 18:07:15 +000012875
Chris Lattnercee56e72009-03-13 05:53:31 +000012876 // Optimize cases that will turn into an LEA instruction. This requires
12877 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000012878 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000012879 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000012880 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000012881
Chris Lattnercee56e72009-03-13 05:53:31 +000012882 bool isFastMultiplier = false;
12883 if (Diff < 10) {
12884 switch ((unsigned char)Diff) {
12885 default: break;
12886 case 1: // result = add base, cond
12887 case 2: // result = lea base( , cond*2)
12888 case 3: // result = lea base(cond, cond*2)
12889 case 4: // result = lea base( , cond*4)
12890 case 5: // result = lea base(cond, cond*4)
12891 case 8: // result = lea base( , cond*8)
12892 case 9: // result = lea base(cond, cond*8)
12893 isFastMultiplier = true;
12894 break;
12895 }
12896 }
Eric Christopherfd179292009-08-27 18:07:15 +000012897
Chris Lattnercee56e72009-03-13 05:53:31 +000012898 if (isFastMultiplier) {
12899 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000012900 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
12901 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000012902 // Zero extend the condition if needed.
12903 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
12904 Cond);
12905 // Scale the condition by the difference.
12906 if (Diff != 1)
12907 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
12908 DAG.getConstant(Diff, Cond.getValueType()));
12909
12910 // Add the base if non-zero.
12911 if (FalseC->getAPIntValue() != 0)
12912 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
12913 SDValue(FalseC, 0));
12914 if (N->getNumValues() == 2) // Dead flag value?
12915 return DCI.CombineTo(N, Cond, SDValue());
12916 return Cond;
12917 }
Eric Christopherfd179292009-08-27 18:07:15 +000012918 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000012919 }
12920 }
12921 return SDValue();
12922}
12923
12924
Evan Cheng0b0cd912009-03-28 05:57:29 +000012925/// PerformMulCombine - Optimize a single multiply with constant into two
12926/// in order to implement it with two cheaper instructions, e.g.
12927/// LEA + SHL, LEA + LEA.
12928static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
12929 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000012930 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
12931 return SDValue();
12932
Owen Andersone50ed302009-08-10 22:56:29 +000012933 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012934 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000012935 return SDValue();
12936
12937 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
12938 if (!C)
12939 return SDValue();
12940 uint64_t MulAmt = C->getZExtValue();
12941 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
12942 return SDValue();
12943
12944 uint64_t MulAmt1 = 0;
12945 uint64_t MulAmt2 = 0;
12946 if ((MulAmt % 9) == 0) {
12947 MulAmt1 = 9;
12948 MulAmt2 = MulAmt / 9;
12949 } else if ((MulAmt % 5) == 0) {
12950 MulAmt1 = 5;
12951 MulAmt2 = MulAmt / 5;
12952 } else if ((MulAmt % 3) == 0) {
12953 MulAmt1 = 3;
12954 MulAmt2 = MulAmt / 3;
12955 }
12956 if (MulAmt2 &&
12957 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
12958 DebugLoc DL = N->getDebugLoc();
12959
12960 if (isPowerOf2_64(MulAmt2) &&
12961 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
12962 // If second multiplifer is pow2, issue it first. We want the multiply by
12963 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
12964 // is an add.
12965 std::swap(MulAmt1, MulAmt2);
12966
12967 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000012968 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000012969 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000012970 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000012971 else
Evan Cheng73f24c92009-03-30 21:36:47 +000012972 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000012973 DAG.getConstant(MulAmt1, VT));
12974
Eric Christopherfd179292009-08-27 18:07:15 +000012975 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000012976 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000012977 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000012978 else
Evan Cheng73f24c92009-03-30 21:36:47 +000012979 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000012980 DAG.getConstant(MulAmt2, VT));
12981
12982 // Do not add new nodes to DAG combiner worklist.
12983 DCI.CombineTo(N, NewMul, false);
12984 }
12985 return SDValue();
12986}
12987
Evan Chengad9c0a32009-12-15 00:53:42 +000012988static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
12989 SDValue N0 = N->getOperand(0);
12990 SDValue N1 = N->getOperand(1);
12991 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
12992 EVT VT = N0.getValueType();
12993
12994 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
12995 // since the result of setcc_c is all zero's or all ones.
12996 if (N1C && N0.getOpcode() == ISD::AND &&
12997 N0.getOperand(1).getOpcode() == ISD::Constant) {
12998 SDValue N00 = N0.getOperand(0);
12999 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
13000 ((N00.getOpcode() == ISD::ANY_EXTEND ||
13001 N00.getOpcode() == ISD::ZERO_EXTEND) &&
13002 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
13003 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
13004 APInt ShAmt = N1C->getAPIntValue();
13005 Mask = Mask.shl(ShAmt);
13006 if (Mask != 0)
13007 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
13008 N00, DAG.getConstant(Mask, VT));
13009 }
13010 }
13011
13012 return SDValue();
13013}
Evan Cheng0b0cd912009-03-28 05:57:29 +000013014
Nate Begeman740ab032009-01-26 00:52:55 +000013015/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
13016/// when possible.
13017static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
13018 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000013019 EVT VT = N->getValueType(0);
13020 if (!VT.isVector() && VT.isInteger() &&
13021 N->getOpcode() == ISD::SHL)
13022 return PerformSHLCombine(N, DAG);
13023
Nate Begeman740ab032009-01-26 00:52:55 +000013024 // On X86 with SSE2 support, we can transform this to a vector shift if
13025 // all elements are shifted by the same amount. We can't do this in legalize
13026 // because the a constant vector is typically transformed to a constant pool
13027 // so we have no knowledge of the shift amount.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013028 if (!Subtarget->hasXMMInt())
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013029 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000013030
Owen Anderson825b72b2009-08-11 20:47:22 +000013031 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013032 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000013033
Mon P Wang3becd092009-01-28 08:12:05 +000013034 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000013035 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000013036 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000013037 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000013038 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
13039 unsigned NumElts = VT.getVectorNumElements();
13040 unsigned i = 0;
13041 for (; i != NumElts; ++i) {
13042 SDValue Arg = ShAmtOp.getOperand(i);
13043 if (Arg.getOpcode() == ISD::UNDEF) continue;
13044 BaseShAmt = Arg;
13045 break;
13046 }
13047 for (; i != NumElts; ++i) {
13048 SDValue Arg = ShAmtOp.getOperand(i);
13049 if (Arg.getOpcode() == ISD::UNDEF) continue;
13050 if (Arg != BaseShAmt) {
13051 return SDValue();
13052 }
13053 }
13054 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000013055 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000013056 SDValue InVec = ShAmtOp.getOperand(0);
13057 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
13058 unsigned NumElts = InVec.getValueType().getVectorNumElements();
13059 unsigned i = 0;
13060 for (; i != NumElts; ++i) {
13061 SDValue Arg = InVec.getOperand(i);
13062 if (Arg.getOpcode() == ISD::UNDEF) continue;
13063 BaseShAmt = Arg;
13064 break;
13065 }
13066 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
13067 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000013068 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000013069 if (C->getZExtValue() == SplatIdx)
13070 BaseShAmt = InVec.getOperand(1);
13071 }
13072 }
13073 if (BaseShAmt.getNode() == 0)
13074 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
13075 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000013076 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013077 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000013078
Mon P Wangefa42202009-09-03 19:56:25 +000013079 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000013080 if (EltVT.bitsGT(MVT::i32))
13081 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
13082 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000013083 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000013084
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013085 // The shift amount is identical so we can do a vector shift.
13086 SDValue ValOp = N->getOperand(0);
13087 switch (N->getOpcode()) {
13088 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000013089 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013090 break;
13091 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000013092 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013093 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013094 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013095 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013096 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013097 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013098 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013099 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013100 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013101 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013102 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013103 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013104 break;
13105 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000013106 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013107 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013108 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013109 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013110 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013111 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013112 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013113 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013114 break;
13115 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000013116 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013117 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013118 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013119 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013120 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013121 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013122 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013123 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013124 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013125 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013126 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013127 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013128 break;
Nate Begeman740ab032009-01-26 00:52:55 +000013129 }
13130 return SDValue();
13131}
13132
Nate Begemanb65c1752010-12-17 22:55:37 +000013133
Stuart Hastings865f0932011-06-03 23:53:54 +000013134// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
13135// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
13136// and friends. Likewise for OR -> CMPNEQSS.
13137static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
13138 TargetLowering::DAGCombinerInfo &DCI,
13139 const X86Subtarget *Subtarget) {
13140 unsigned opcode;
13141
13142 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
13143 // we're requiring SSE2 for both.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013144 if (Subtarget->hasXMMInt() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
Stuart Hastings865f0932011-06-03 23:53:54 +000013145 SDValue N0 = N->getOperand(0);
13146 SDValue N1 = N->getOperand(1);
13147 SDValue CMP0 = N0->getOperand(1);
13148 SDValue CMP1 = N1->getOperand(1);
13149 DebugLoc DL = N->getDebugLoc();
13150
13151 // The SETCCs should both refer to the same CMP.
13152 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
13153 return SDValue();
13154
13155 SDValue CMP00 = CMP0->getOperand(0);
13156 SDValue CMP01 = CMP0->getOperand(1);
13157 EVT VT = CMP00.getValueType();
13158
13159 if (VT == MVT::f32 || VT == MVT::f64) {
13160 bool ExpectingFlags = false;
13161 // Check for any users that want flags:
13162 for (SDNode::use_iterator UI = N->use_begin(),
13163 UE = N->use_end();
13164 !ExpectingFlags && UI != UE; ++UI)
13165 switch (UI->getOpcode()) {
13166 default:
13167 case ISD::BR_CC:
13168 case ISD::BRCOND:
13169 case ISD::SELECT:
13170 ExpectingFlags = true;
13171 break;
13172 case ISD::CopyToReg:
13173 case ISD::SIGN_EXTEND:
13174 case ISD::ZERO_EXTEND:
13175 case ISD::ANY_EXTEND:
13176 break;
13177 }
13178
13179 if (!ExpectingFlags) {
13180 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
13181 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
13182
13183 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
13184 X86::CondCode tmp = cc0;
13185 cc0 = cc1;
13186 cc1 = tmp;
13187 }
13188
13189 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
13190 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
13191 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
13192 X86ISD::NodeType NTOperator = is64BitFP ?
13193 X86ISD::FSETCCsd : X86ISD::FSETCCss;
13194 // FIXME: need symbolic constants for these magic numbers.
13195 // See X86ATTInstPrinter.cpp:printSSECC().
13196 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
13197 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
13198 DAG.getConstant(x86cc, MVT::i8));
13199 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
13200 OnesOrZeroesF);
13201 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
13202 DAG.getConstant(1, MVT::i32));
13203 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
13204 return OneBitOfTruth;
13205 }
13206 }
13207 }
13208 }
13209 return SDValue();
13210}
13211
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013212/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
13213/// so it can be folded inside ANDNP.
13214static bool CanFoldXORWithAllOnes(const SDNode *N) {
13215 EVT VT = N->getValueType(0);
13216
13217 // Match direct AllOnes for 128 and 256-bit vectors
13218 if (ISD::isBuildVectorAllOnes(N))
13219 return true;
13220
13221 // Look through a bit convert.
13222 if (N->getOpcode() == ISD::BITCAST)
13223 N = N->getOperand(0).getNode();
13224
13225 // Sometimes the operand may come from a insert_subvector building a 256-bit
13226 // allones vector
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013227 if (VT.getSizeInBits() == 256 &&
Bill Wendling456a9252011-08-04 00:32:58 +000013228 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
13229 SDValue V1 = N->getOperand(0);
13230 SDValue V2 = N->getOperand(1);
13231
13232 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
13233 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
13234 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
13235 ISD::isBuildVectorAllOnes(V2.getNode()))
13236 return true;
13237 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013238
13239 return false;
13240}
13241
Nate Begemanb65c1752010-12-17 22:55:37 +000013242static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
13243 TargetLowering::DAGCombinerInfo &DCI,
13244 const X86Subtarget *Subtarget) {
13245 if (DCI.isBeforeLegalizeOps())
13246 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013247
Stuart Hastings865f0932011-06-03 23:53:54 +000013248 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
13249 if (R.getNode())
13250 return R;
13251
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000013252 // Want to form ANDNP nodes:
13253 // 1) In the hopes of then easily combining them with OR and AND nodes
13254 // to form PBLEND/PSIGN.
13255 // 2) To match ANDN packed intrinsics
Nate Begemanb65c1752010-12-17 22:55:37 +000013256 EVT VT = N->getValueType(0);
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000013257 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000013258 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013259
Nate Begemanb65c1752010-12-17 22:55:37 +000013260 SDValue N0 = N->getOperand(0);
13261 SDValue N1 = N->getOperand(1);
13262 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013263
Nate Begemanb65c1752010-12-17 22:55:37 +000013264 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013265 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013266 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
13267 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013268 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000013269
13270 // Check RHS for vnot
13271 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013272 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
13273 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013274 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013275
Nate Begemanb65c1752010-12-17 22:55:37 +000013276 return SDValue();
13277}
13278
Evan Cheng760d1942010-01-04 21:22:48 +000013279static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000013280 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000013281 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000013282 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000013283 return SDValue();
13284
Stuart Hastings865f0932011-06-03 23:53:54 +000013285 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
13286 if (R.getNode())
13287 return R;
13288
Evan Cheng760d1942010-01-04 21:22:48 +000013289 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000013290 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64 && VT != MVT::v2i64)
Evan Cheng760d1942010-01-04 21:22:48 +000013291 return SDValue();
13292
Evan Cheng760d1942010-01-04 21:22:48 +000013293 SDValue N0 = N->getOperand(0);
13294 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013295
Nate Begemanb65c1752010-12-17 22:55:37 +000013296 // look for psign/blend
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013297 if (Subtarget->hasSSSE3() || Subtarget->hasAVX()) {
Nate Begemanb65c1752010-12-17 22:55:37 +000013298 if (VT == MVT::v2i64) {
13299 // Canonicalize pandn to RHS
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013300 if (N0.getOpcode() == X86ISD::ANDNP)
Nate Begemanb65c1752010-12-17 22:55:37 +000013301 std::swap(N0, N1);
13302 // or (and (m, x), (pandn m, y))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013303 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
Nate Begemanb65c1752010-12-17 22:55:37 +000013304 SDValue Mask = N1.getOperand(0);
13305 SDValue X = N1.getOperand(1);
13306 SDValue Y;
13307 if (N0.getOperand(0) == Mask)
13308 Y = N0.getOperand(1);
13309 if (N0.getOperand(1) == Mask)
13310 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013311
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013312 // Check to see if the mask appeared in both the AND and ANDNP and
Nate Begemanb65c1752010-12-17 22:55:37 +000013313 if (!Y.getNode())
13314 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013315
Nate Begemanb65c1752010-12-17 22:55:37 +000013316 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
13317 if (Mask.getOpcode() != ISD::BITCAST ||
13318 X.getOpcode() != ISD::BITCAST ||
13319 Y.getOpcode() != ISD::BITCAST)
13320 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013321
Nate Begemanb65c1752010-12-17 22:55:37 +000013322 // Look through mask bitcast.
13323 Mask = Mask.getOperand(0);
13324 EVT MaskVT = Mask.getValueType();
13325
13326 // Validate that the Mask operand is a vector sra node. The sra node
13327 // will be an intrinsic.
13328 if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
13329 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013330
Nate Begemanb65c1752010-12-17 22:55:37 +000013331 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
13332 // there is no psrai.b
13333 switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
13334 case Intrinsic::x86_sse2_psrai_w:
13335 case Intrinsic::x86_sse2_psrai_d:
13336 break;
13337 default: return SDValue();
13338 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013339
Nate Begemanb65c1752010-12-17 22:55:37 +000013340 // Check that the SRA is all signbits.
13341 SDValue SraC = Mask.getOperand(2);
13342 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
13343 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
13344 if ((SraAmt + 1) != EltBits)
13345 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013346
Nate Begemanb65c1752010-12-17 22:55:37 +000013347 DebugLoc DL = N->getDebugLoc();
13348
13349 // Now we know we at least have a plendvb with the mask val. See if
13350 // we can form a psignb/w/d.
13351 // psign = x.type == y.type == mask.type && y = sub(0, x);
13352 X = X.getOperand(0);
13353 Y = Y.getOperand(0);
13354 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
13355 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
13356 X.getValueType() == MaskVT && X.getValueType() == Y.getValueType()){
13357 unsigned Opc = 0;
13358 switch (EltBits) {
13359 case 8: Opc = X86ISD::PSIGNB; break;
13360 case 16: Opc = X86ISD::PSIGNW; break;
13361 case 32: Opc = X86ISD::PSIGND; break;
13362 default: break;
13363 }
13364 if (Opc) {
13365 SDValue Sign = DAG.getNode(Opc, DL, MaskVT, X, Mask.getOperand(1));
13366 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Sign);
13367 }
13368 }
13369 // PBLENDVB only available on SSE 4.1
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013370 if (!(Subtarget->hasSSE41() || Subtarget->hasAVX()))
Nate Begemanb65c1752010-12-17 22:55:37 +000013371 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013372
Nate Begemanb65c1752010-12-17 22:55:37 +000013373 X = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, X);
13374 Y = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Y);
13375 Mask = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Mask);
Nadav Rotemfbad25e2011-09-11 15:02:23 +000013376 Mask = DAG.getNode(ISD::VSELECT, DL, MVT::v16i8, Mask, X, Y);
Nate Begemanb65c1752010-12-17 22:55:37 +000013377 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Mask);
13378 }
13379 }
13380 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013381
Nate Begemanb65c1752010-12-17 22:55:37 +000013382 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000013383 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
13384 std::swap(N0, N1);
13385 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
13386 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000013387 if (!N0.hasOneUse() || !N1.hasOneUse())
13388 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000013389
13390 SDValue ShAmt0 = N0.getOperand(1);
13391 if (ShAmt0.getValueType() != MVT::i8)
13392 return SDValue();
13393 SDValue ShAmt1 = N1.getOperand(1);
13394 if (ShAmt1.getValueType() != MVT::i8)
13395 return SDValue();
13396 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
13397 ShAmt0 = ShAmt0.getOperand(0);
13398 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
13399 ShAmt1 = ShAmt1.getOperand(0);
13400
13401 DebugLoc DL = N->getDebugLoc();
13402 unsigned Opc = X86ISD::SHLD;
13403 SDValue Op0 = N0.getOperand(0);
13404 SDValue Op1 = N1.getOperand(0);
13405 if (ShAmt0.getOpcode() == ISD::SUB) {
13406 Opc = X86ISD::SHRD;
13407 std::swap(Op0, Op1);
13408 std::swap(ShAmt0, ShAmt1);
13409 }
13410
Evan Cheng8b1190a2010-04-28 01:18:01 +000013411 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000013412 if (ShAmt1.getOpcode() == ISD::SUB) {
13413 SDValue Sum = ShAmt1.getOperand(0);
13414 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000013415 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
13416 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
13417 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
13418 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000013419 return DAG.getNode(Opc, DL, VT,
13420 Op0, Op1,
13421 DAG.getNode(ISD::TRUNCATE, DL,
13422 MVT::i8, ShAmt0));
13423 }
13424 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
13425 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
13426 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000013427 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000013428 return DAG.getNode(Opc, DL, VT,
13429 N0.getOperand(0), N1.getOperand(0),
13430 DAG.getNode(ISD::TRUNCATE, DL,
13431 MVT::i8, ShAmt0));
13432 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013433
Evan Cheng760d1942010-01-04 21:22:48 +000013434 return SDValue();
13435}
13436
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013437/// PerformLOADCombine - Do target-specific dag combines on LOAD nodes.
13438static SDValue PerformLOADCombine(SDNode *N, SelectionDAG &DAG,
13439 const X86Subtarget *Subtarget) {
13440 LoadSDNode *Ld = cast<LoadSDNode>(N);
13441 EVT RegVT = Ld->getValueType(0);
13442 EVT MemVT = Ld->getMemoryVT();
13443 DebugLoc dl = Ld->getDebugLoc();
13444 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13445
13446 ISD::LoadExtType Ext = Ld->getExtensionType();
13447
Nadav Rotemca6f2962011-09-18 19:00:23 +000013448 // If this is a vector EXT Load then attempt to optimize it using a
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013449 // shuffle. We need SSE4 for the shuffles.
13450 // TODO: It is possible to support ZExt by zeroing the undef values
13451 // during the shuffle phase or after the shuffle.
13452 if (RegVT.isVector() && Ext == ISD::EXTLOAD && Subtarget->hasSSE41()) {
13453 assert(MemVT != RegVT && "Cannot extend to the same type");
13454 assert(MemVT.isVector() && "Must load a vector from memory");
13455
13456 unsigned NumElems = RegVT.getVectorNumElements();
13457 unsigned RegSz = RegVT.getSizeInBits();
13458 unsigned MemSz = MemVT.getSizeInBits();
13459 assert(RegSz > MemSz && "Register size must be greater than the mem size");
Nadav Rotemca6f2962011-09-18 19:00:23 +000013460 // All sizes must be a power of two
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013461 if (!isPowerOf2_32(RegSz * MemSz * NumElems)) return SDValue();
13462
13463 // Attempt to load the original value using a single load op.
13464 // Find a scalar type which is equal to the loaded word size.
13465 MVT SclrLoadTy = MVT::i8;
13466 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
13467 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
13468 MVT Tp = (MVT::SimpleValueType)tp;
13469 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() == MemSz) {
13470 SclrLoadTy = Tp;
13471 break;
13472 }
13473 }
13474
13475 // Proceed if a load word is found.
13476 if (SclrLoadTy.getSizeInBits() != MemSz) return SDValue();
13477
13478 EVT LoadUnitVecVT = EVT::getVectorVT(*DAG.getContext(), SclrLoadTy,
13479 RegSz/SclrLoadTy.getSizeInBits());
13480
13481 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
13482 RegSz/MemVT.getScalarType().getSizeInBits());
13483 // Can't shuffle using an illegal type.
13484 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
13485
13486 // Perform a single load.
13487 SDValue ScalarLoad = DAG.getLoad(SclrLoadTy, dl, Ld->getChain(),
13488 Ld->getBasePtr(),
13489 Ld->getPointerInfo(), Ld->isVolatile(),
13490 Ld->isNonTemporal(), Ld->getAlignment());
13491
13492 // Insert the word loaded into a vector.
13493 SDValue ScalarInVector = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
13494 LoadUnitVecVT, ScalarLoad);
13495
13496 // Bitcast the loaded value to a vector of the original element type, in
13497 // the size of the target vector type.
13498 SDValue SlicedVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, ScalarInVector);
13499 unsigned SizeRatio = RegSz/MemSz;
13500
13501 // Redistribute the loaded elements into the different locations.
13502 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
13503 for (unsigned i = 0; i < NumElems; i++) ShuffleVec[i*SizeRatio] = i;
13504
13505 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
13506 DAG.getUNDEF(SlicedVec.getValueType()),
13507 ShuffleVec.data());
13508
13509 // Bitcast to the requested type.
13510 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
13511 // Replace the original load with the new sequence
13512 // and return the new chain.
13513 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Shuff);
13514 return SDValue(ScalarLoad.getNode(), 1);
13515 }
13516
13517 return SDValue();
13518}
13519
Chris Lattner149a4e52008-02-22 02:09:43 +000013520/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000013521static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000013522 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000013523 StoreSDNode *St = cast<StoreSDNode>(N);
13524 EVT VT = St->getValue().getValueType();
13525 EVT StVT = St->getMemoryVT();
13526 DebugLoc dl = St->getDebugLoc();
Nadav Rotem5e742a32011-08-11 16:41:21 +000013527 SDValue StoredVal = St->getOperand(1);
13528 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13529
13530 // If we are saving a concatination of two XMM registers, perform two stores.
Nadav Rotem6236f7f2011-08-11 17:05:47 +000013531 // This is better in Sandy Bridge cause one 256-bit mem op is done via two
13532 // 128-bit ones. If in the future the cost becomes only one memory access the
13533 // first version would be better.
Nadav Rotem5e742a32011-08-11 16:41:21 +000013534 if (VT.getSizeInBits() == 256 &&
13535 StoredVal.getNode()->getOpcode() == ISD::CONCAT_VECTORS &&
13536 StoredVal.getNumOperands() == 2) {
13537
13538 SDValue Value0 = StoredVal.getOperand(0);
13539 SDValue Value1 = StoredVal.getOperand(1);
13540
13541 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
13542 SDValue Ptr0 = St->getBasePtr();
13543 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
13544
13545 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
13546 St->getPointerInfo(), St->isVolatile(),
13547 St->isNonTemporal(), St->getAlignment());
13548 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
13549 St->getPointerInfo(), St->isVolatile(),
13550 St->isNonTemporal(), St->getAlignment());
13551 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
13552 }
Nadav Rotem614061b2011-08-10 19:30:14 +000013553
13554 // Optimize trunc store (of multiple scalars) to shuffle and store.
13555 // First, pack all of the elements in one place. Next, store to memory
13556 // in fewer chunks.
13557 if (St->isTruncatingStore() && VT.isVector()) {
13558 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13559 unsigned NumElems = VT.getVectorNumElements();
13560 assert(StVT != VT && "Cannot truncate to the same type");
13561 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
13562 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
13563
13564 // From, To sizes and ElemCount must be pow of two
13565 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013566
Nadav Rotem614061b2011-08-10 19:30:14 +000013567 unsigned SizeRatio = FromSz / ToSz;
13568
13569 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
13570
13571 // Create a type on which we perform the shuffle
13572 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
13573 StVT.getScalarType(), NumElems*SizeRatio);
13574
13575 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
13576
13577 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
13578 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
13579 for (unsigned i = 0; i < NumElems; i++ ) ShuffleVec[i] = i * SizeRatio;
13580
13581 // Can't shuffle using an illegal type
13582 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
13583
13584 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
13585 DAG.getUNDEF(WideVec.getValueType()),
13586 ShuffleVec.data());
13587 // At this point all of the data is stored at the bottom of the
13588 // register. We now need to save it to mem.
13589
13590 // Find the largest store unit
13591 MVT StoreType = MVT::i8;
13592 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
13593 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
13594 MVT Tp = (MVT::SimpleValueType)tp;
13595 if (TLI.isTypeLegal(Tp) && StoreType.getSizeInBits() < NumElems * ToSz)
13596 StoreType = Tp;
13597 }
13598
13599 // Bitcast the original vector into a vector of store-size units
13600 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
13601 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
13602 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
13603 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
13604 SmallVector<SDValue, 8> Chains;
13605 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
13606 TLI.getPointerTy());
13607 SDValue Ptr = St->getBasePtr();
13608
13609 // Perform one or more big stores into memory.
13610 for (unsigned i = 0; i < (ToSz*NumElems)/StoreType.getSizeInBits() ; i++) {
13611 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
13612 StoreType, ShuffWide,
13613 DAG.getIntPtrConstant(i));
13614 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
13615 St->getPointerInfo(), St->isVolatile(),
13616 St->isNonTemporal(), St->getAlignment());
13617 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
13618 Chains.push_back(Ch);
13619 }
13620
13621 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
13622 Chains.size());
13623 }
13624
13625
Chris Lattner149a4e52008-02-22 02:09:43 +000013626 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
13627 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000013628 // A preferable solution to the general problem is to figure out the right
13629 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000013630
13631 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000013632 if (VT.getSizeInBits() != 64)
13633 return SDValue();
13634
Devang Patel578efa92009-06-05 21:57:13 +000013635 const Function *F = DAG.getMachineFunction().getFunction();
13636 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +000013637 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013638 && Subtarget->hasXMMInt();
Evan Cheng536e6672009-03-12 05:59:15 +000013639 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000013640 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000013641 isa<LoadSDNode>(St->getValue()) &&
13642 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
13643 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000013644 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000013645 LoadSDNode *Ld = 0;
13646 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000013647 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000013648 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000013649 // Must be a store of a load. We currently handle two cases: the load
13650 // is a direct child, and it's under an intervening TokenFactor. It is
13651 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000013652 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000013653 Ld = cast<LoadSDNode>(St->getChain());
13654 else if (St->getValue().hasOneUse() &&
13655 ChainVal->getOpcode() == ISD::TokenFactor) {
13656 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000013657 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000013658 TokenFactorIndex = i;
13659 Ld = cast<LoadSDNode>(St->getValue());
13660 } else
13661 Ops.push_back(ChainVal->getOperand(i));
13662 }
13663 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000013664
Evan Cheng536e6672009-03-12 05:59:15 +000013665 if (!Ld || !ISD::isNormalLoad(Ld))
13666 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000013667
Evan Cheng536e6672009-03-12 05:59:15 +000013668 // If this is not the MMX case, i.e. we are just turning i64 load/store
13669 // into f64 load/store, avoid the transformation if there are multiple
13670 // uses of the loaded value.
13671 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
13672 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000013673
Evan Cheng536e6672009-03-12 05:59:15 +000013674 DebugLoc LdDL = Ld->getDebugLoc();
13675 DebugLoc StDL = N->getDebugLoc();
13676 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
13677 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
13678 // pair instead.
13679 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000013680 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000013681 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
13682 Ld->getPointerInfo(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000013683 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000013684 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000013685 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000013686 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000013687 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000013688 Ops.size());
13689 }
Evan Cheng536e6672009-03-12 05:59:15 +000013690 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000013691 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000013692 St->isVolatile(), St->isNonTemporal(),
13693 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000013694 }
Evan Cheng536e6672009-03-12 05:59:15 +000013695
13696 // Otherwise, lower to two pairs of 32-bit loads / stores.
13697 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000013698 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
13699 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000013700
Owen Anderson825b72b2009-08-11 20:47:22 +000013701 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000013702 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000013703 Ld->isVolatile(), Ld->isNonTemporal(),
13704 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000013705 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000013706 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000013707 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000013708 MinAlign(Ld->getAlignment(), 4));
13709
13710 SDValue NewChain = LoLd.getValue(1);
13711 if (TokenFactorIndex != -1) {
13712 Ops.push_back(LoLd);
13713 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000013714 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000013715 Ops.size());
13716 }
13717
13718 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000013719 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
13720 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000013721
13722 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000013723 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000013724 St->isVolatile(), St->isNonTemporal(),
13725 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000013726 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000013727 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000013728 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000013729 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000013730 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000013731 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000013732 }
Dan Gohman475871a2008-07-27 21:46:04 +000013733 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000013734}
13735
Chris Lattner6cf73262008-01-25 06:14:17 +000013736/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
13737/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000013738static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000013739 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
13740 // F[X]OR(0.0, x) -> x
13741 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000013742 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
13743 if (C->getValueAPF().isPosZero())
13744 return N->getOperand(1);
13745 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
13746 if (C->getValueAPF().isPosZero())
13747 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000013748 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000013749}
13750
13751/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000013752static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000013753 // FAND(0.0, x) -> 0.0
13754 // FAND(x, 0.0) -> 0.0
13755 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
13756 if (C->getValueAPF().isPosZero())
13757 return N->getOperand(0);
13758 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
13759 if (C->getValueAPF().isPosZero())
13760 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000013761 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000013762}
13763
Dan Gohmane5af2d32009-01-29 01:59:02 +000013764static SDValue PerformBTCombine(SDNode *N,
13765 SelectionDAG &DAG,
13766 TargetLowering::DAGCombinerInfo &DCI) {
13767 // BT ignores high bits in the bit index operand.
13768 SDValue Op1 = N->getOperand(1);
13769 if (Op1.hasOneUse()) {
13770 unsigned BitWidth = Op1.getValueSizeInBits();
13771 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
13772 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000013773 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
13774 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000013775 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000013776 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
13777 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
13778 DCI.CommitTargetLoweringOpt(TLO);
13779 }
13780 return SDValue();
13781}
Chris Lattner83e6c992006-10-04 06:57:07 +000013782
Eli Friedman7a5e5552009-06-07 06:52:44 +000013783static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
13784 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000013785 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000013786 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000013787 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000013788 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000013789 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000013790 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000013791 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000013792 }
13793 return SDValue();
13794}
13795
Evan Cheng2e489c42009-12-16 00:53:11 +000013796static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
13797 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
13798 // (and (i32 x86isd::setcc_carry), 1)
13799 // This eliminates the zext. This transformation is necessary because
13800 // ISD::SETCC is always legalized to i8.
13801 DebugLoc dl = N->getDebugLoc();
13802 SDValue N0 = N->getOperand(0);
13803 EVT VT = N->getValueType(0);
13804 if (N0.getOpcode() == ISD::AND &&
13805 N0.hasOneUse() &&
13806 N0.getOperand(0).hasOneUse()) {
13807 SDValue N00 = N0.getOperand(0);
13808 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
13809 return SDValue();
13810 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
13811 if (!C || C->getZExtValue() != 1)
13812 return SDValue();
13813 return DAG.getNode(ISD::AND, dl, VT,
13814 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
13815 N00.getOperand(0), N00.getOperand(1)),
13816 DAG.getConstant(1, VT));
13817 }
13818
13819 return SDValue();
13820}
13821
Chris Lattnerc19d1c32010-12-19 22:08:31 +000013822// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
13823static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
13824 unsigned X86CC = N->getConstantOperandVal(0);
13825 SDValue EFLAG = N->getOperand(1);
13826 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013827
Chris Lattnerc19d1c32010-12-19 22:08:31 +000013828 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
13829 // a zext and produces an all-ones bit which is more useful than 0/1 in some
13830 // cases.
13831 if (X86CC == X86::COND_B)
13832 return DAG.getNode(ISD::AND, DL, MVT::i8,
13833 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
13834 DAG.getConstant(X86CC, MVT::i8), EFLAG),
13835 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013836
Chris Lattnerc19d1c32010-12-19 22:08:31 +000013837 return SDValue();
13838}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013839
Benjamin Kramer1396c402011-06-18 11:09:41 +000013840static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
13841 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000013842 SDValue Op0 = N->getOperand(0);
13843 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
13844 // a 32-bit target where SSE doesn't support i64->FP operations.
13845 if (Op0.getOpcode() == ISD::LOAD) {
13846 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
13847 EVT VT = Ld->getValueType(0);
13848 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
13849 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
13850 !XTLI->getSubtarget()->is64Bit() &&
13851 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000013852 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
13853 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000013854 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
13855 return FILDChain;
13856 }
13857 }
13858 return SDValue();
13859}
13860
Chris Lattner23a01992010-12-20 01:37:09 +000013861// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
13862static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
13863 X86TargetLowering::DAGCombinerInfo &DCI) {
13864 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
13865 // the result is either zero or one (depending on the input carry bit).
13866 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
13867 if (X86::isZeroNode(N->getOperand(0)) &&
13868 X86::isZeroNode(N->getOperand(1)) &&
13869 // We don't have a good way to replace an EFLAGS use, so only do this when
13870 // dead right now.
13871 SDValue(N, 1).use_empty()) {
13872 DebugLoc DL = N->getDebugLoc();
13873 EVT VT = N->getValueType(0);
13874 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
13875 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
13876 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
13877 DAG.getConstant(X86::COND_B,MVT::i8),
13878 N->getOperand(2)),
13879 DAG.getConstant(1, VT));
13880 return DCI.CombineTo(N, Res1, CarryOut);
13881 }
13882
13883 return SDValue();
13884}
13885
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000013886// fold (add Y, (sete X, 0)) -> adc 0, Y
13887// (add Y, (setne X, 0)) -> sbb -1, Y
13888// (sub (sete X, 0), Y) -> sbb 0, Y
13889// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000013890static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000013891 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013892
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000013893 // Look through ZExts.
13894 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
13895 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
13896 return SDValue();
13897
13898 SDValue SetCC = Ext.getOperand(0);
13899 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
13900 return SDValue();
13901
13902 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
13903 if (CC != X86::COND_E && CC != X86::COND_NE)
13904 return SDValue();
13905
13906 SDValue Cmp = SetCC.getOperand(1);
13907 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000013908 !X86::isZeroNode(Cmp.getOperand(1)) ||
13909 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000013910 return SDValue();
13911
13912 SDValue CmpOp0 = Cmp.getOperand(0);
13913 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
13914 DAG.getConstant(1, CmpOp0.getValueType()));
13915
13916 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
13917 if (CC == X86::COND_NE)
13918 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
13919 DL, OtherVal.getValueType(), OtherVal,
13920 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
13921 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
13922 DL, OtherVal.getValueType(), OtherVal,
13923 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
13924}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000013925
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000013926static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG) {
13927 SDValue Op0 = N->getOperand(0);
13928 SDValue Op1 = N->getOperand(1);
13929
13930 // X86 can't encode an immediate LHS of a sub. See if we can push the
13931 // negation into a preceding instruction.
13932 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000013933 // If the RHS of the sub is a XOR with one use and a constant, invert the
13934 // immediate. Then add one to the LHS of the sub so we can turn
13935 // X-Y -> X+~Y+1, saving one register.
13936 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
13937 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000013938 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000013939 EVT VT = Op0.getValueType();
13940 SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
13941 Op1.getOperand(0),
13942 DAG.getConstant(~XorC, VT));
13943 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000013944 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000013945 }
13946 }
13947
13948 return OptimizeConditionalInDecrement(N, DAG);
13949}
13950
Dan Gohman475871a2008-07-27 21:46:04 +000013951SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000013952 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000013953 SelectionDAG &DAG = DCI.DAG;
13954 switch (N->getOpcode()) {
13955 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013956 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000013957 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Duncan Sands6bcd2192011-09-17 16:49:39 +000013958 case ISD::VSELECT:
Chris Lattneraf723b92008-01-25 05:46:26 +000013959 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000013960 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000013961 case ISD::ADD: return OptimizeConditionalInDecrement(N, DAG);
13962 case ISD::SUB: return PerformSubCombine(N, DAG);
Chris Lattner23a01992010-12-20 01:37:09 +000013963 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000013964 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000013965 case ISD::SHL:
13966 case ISD::SRA:
13967 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000013968 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000013969 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013970 case ISD::LOAD: return PerformLOADCombine(N, DAG, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000013971 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000013972 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Chris Lattner6cf73262008-01-25 06:14:17 +000013973 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000013974 case X86ISD::FOR: return PerformFORCombine(N, DAG);
13975 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000013976 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000013977 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000013978 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000013979 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000013980 case X86ISD::SHUFPS: // Handle all target specific shuffles
13981 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000013982 case X86ISD::PALIGN:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000013983 case X86ISD::PUNPCKHBW:
13984 case X86ISD::PUNPCKHWD:
13985 case X86ISD::PUNPCKHDQ:
13986 case X86ISD::PUNPCKHQDQ:
13987 case X86ISD::UNPCKHPS:
13988 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +000013989 case X86ISD::VUNPCKHPSY:
13990 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000013991 case X86ISD::PUNPCKLBW:
13992 case X86ISD::PUNPCKLWD:
13993 case X86ISD::PUNPCKLDQ:
13994 case X86ISD::PUNPCKLQDQ:
13995 case X86ISD::UNPCKLPS:
13996 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +000013997 case X86ISD::VUNPCKLPSY:
13998 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000013999 case X86ISD::MOVHLPS:
14000 case X86ISD::MOVLHPS:
14001 case X86ISD::PSHUFD:
14002 case X86ISD::PSHUFHW:
14003 case X86ISD::PSHUFLW:
14004 case X86ISD::MOVSS:
14005 case X86ISD::MOVSD:
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +000014006 case X86ISD::VPERMILPS:
14007 case X86ISD::VPERMILPSY:
14008 case X86ISD::VPERMILPD:
14009 case X86ISD::VPERMILPDY:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +000014010 case X86ISD::VPERM2F128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014011 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000014012 }
14013
Dan Gohman475871a2008-07-27 21:46:04 +000014014 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000014015}
14016
Evan Chenge5b51ac2010-04-17 06:13:15 +000014017/// isTypeDesirableForOp - Return true if the target has native support for
14018/// the specified value type and it is 'desirable' to use the type for the
14019/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
14020/// instruction encodings are longer and some i16 instructions are slow.
14021bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
14022 if (!isTypeLegal(VT))
14023 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014024 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000014025 return true;
14026
14027 switch (Opc) {
14028 default:
14029 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000014030 case ISD::LOAD:
14031 case ISD::SIGN_EXTEND:
14032 case ISD::ZERO_EXTEND:
14033 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000014034 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000014035 case ISD::SRL:
14036 case ISD::SUB:
14037 case ISD::ADD:
14038 case ISD::MUL:
14039 case ISD::AND:
14040 case ISD::OR:
14041 case ISD::XOR:
14042 return false;
14043 }
14044}
14045
14046/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000014047/// beneficial for dag combiner to promote the specified node. If true, it
14048/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000014049bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000014050 EVT VT = Op.getValueType();
14051 if (VT != MVT::i16)
14052 return false;
14053
Evan Cheng4c26e932010-04-19 19:29:22 +000014054 bool Promote = false;
14055 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014056 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000014057 default: break;
14058 case ISD::LOAD: {
14059 LoadSDNode *LD = cast<LoadSDNode>(Op);
14060 // If the non-extending load has a single use and it's not live out, then it
14061 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014062 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
14063 Op.hasOneUse()*/) {
14064 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
14065 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
14066 // The only case where we'd want to promote LOAD (rather then it being
14067 // promoted as an operand is when it's only use is liveout.
14068 if (UI->getOpcode() != ISD::CopyToReg)
14069 return false;
14070 }
14071 }
Evan Cheng4c26e932010-04-19 19:29:22 +000014072 Promote = true;
14073 break;
14074 }
14075 case ISD::SIGN_EXTEND:
14076 case ISD::ZERO_EXTEND:
14077 case ISD::ANY_EXTEND:
14078 Promote = true;
14079 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014080 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014081 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000014082 SDValue N0 = Op.getOperand(0);
14083 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000014084 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000014085 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000014086 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014087 break;
14088 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000014089 case ISD::ADD:
14090 case ISD::MUL:
14091 case ISD::AND:
14092 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000014093 case ISD::XOR:
14094 Commute = true;
14095 // fallthrough
14096 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000014097 SDValue N0 = Op.getOperand(0);
14098 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000014099 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014100 return false;
14101 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000014102 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014103 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000014104 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014105 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000014106 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014107 }
14108 }
14109
14110 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000014111 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014112}
14113
Evan Cheng60c07e12006-07-05 22:17:51 +000014114//===----------------------------------------------------------------------===//
14115// X86 Inline Assembly Support
14116//===----------------------------------------------------------------------===//
14117
Chris Lattnerb8105652009-07-20 17:51:36 +000014118bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
14119 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000014120
14121 std::string AsmStr = IA->getAsmString();
14122
14123 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000014124 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000014125 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000014126
14127 switch (AsmPieces.size()) {
14128 default: return false;
14129 case 1:
14130 AsmStr = AsmPieces[0];
14131 AsmPieces.clear();
14132 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
14133
Chris Lattner7a2bdde2011-04-15 05:18:47 +000014134 // FIXME: this should verify that we are targeting a 486 or better. If not,
Evan Cheng55d42002011-01-08 01:24:27 +000014135 // we will turn this bswap into something that will be lowered to logical ops
14136 // instead of emitting the bswap asm. For now, we don't support 486 or lower
14137 // so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000014138 // bswap $0
14139 if (AsmPieces.size() == 2 &&
14140 (AsmPieces[0] == "bswap" ||
14141 AsmPieces[0] == "bswapq" ||
14142 AsmPieces[0] == "bswapl") &&
14143 (AsmPieces[1] == "$0" ||
14144 AsmPieces[1] == "${0:q}")) {
14145 // No need to check constraints, nothing other than the equivalent of
14146 // "=r,0" would be valid here.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014147 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014148 if (!Ty || Ty->getBitWidth() % 16 != 0)
14149 return false;
14150 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000014151 }
14152 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000014153 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000014154 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000014155 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000014156 AsmPieces[1] == "$$8," &&
14157 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000014158 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
14159 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014160 const std::string &ConstraintsStr = IA->getConstraintString();
14161 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000014162 std::sort(AsmPieces.begin(), AsmPieces.end());
14163 if (AsmPieces.size() == 4 &&
14164 AsmPieces[0] == "~{cc}" &&
14165 AsmPieces[1] == "~{dirflag}" &&
14166 AsmPieces[2] == "~{flags}" &&
14167 AsmPieces[3] == "~{fpsr}") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014168 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014169 if (!Ty || Ty->getBitWidth() % 16 != 0)
14170 return false;
14171 return IntrinsicLowering::LowerToByteSwap(CI);
Dan Gohman0ef701e2010-03-04 19:58:08 +000014172 }
Chris Lattnerb8105652009-07-20 17:51:36 +000014173 }
14174 break;
14175 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000014176 if (CI->getType()->isIntegerTy(32) &&
14177 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
14178 SmallVector<StringRef, 4> Words;
14179 SplitString(AsmPieces[0], Words, " \t,");
14180 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
14181 Words[2] == "${0:w}") {
14182 Words.clear();
14183 SplitString(AsmPieces[1], Words, " \t,");
14184 if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
14185 Words[2] == "$0") {
14186 Words.clear();
14187 SplitString(AsmPieces[2], Words, " \t,");
14188 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
14189 Words[2] == "${0:w}") {
14190 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014191 const std::string &ConstraintsStr = IA->getConstraintString();
14192 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Peter Collingbourne948cf022010-11-13 19:54:30 +000014193 std::sort(AsmPieces.begin(), AsmPieces.end());
14194 if (AsmPieces.size() == 4 &&
14195 AsmPieces[0] == "~{cc}" &&
14196 AsmPieces[1] == "~{dirflag}" &&
14197 AsmPieces[2] == "~{flags}" &&
14198 AsmPieces[3] == "~{fpsr}") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014199 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014200 if (!Ty || Ty->getBitWidth() % 16 != 0)
14201 return false;
14202 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000014203 }
14204 }
14205 }
14206 }
14207 }
Evan Cheng55d42002011-01-08 01:24:27 +000014208
14209 if (CI->getType()->isIntegerTy(64)) {
14210 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
14211 if (Constraints.size() >= 2 &&
14212 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
14213 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
14214 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
14215 SmallVector<StringRef, 4> Words;
14216 SplitString(AsmPieces[0], Words, " \t");
14217 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
Chris Lattnerb8105652009-07-20 17:51:36 +000014218 Words.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014219 SplitString(AsmPieces[1], Words, " \t");
14220 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
14221 Words.clear();
14222 SplitString(AsmPieces[2], Words, " \t,");
14223 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
14224 Words[2] == "%edx") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014225 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014226 if (!Ty || Ty->getBitWidth() % 16 != 0)
14227 return false;
14228 return IntrinsicLowering::LowerToByteSwap(CI);
14229 }
Chris Lattnerb8105652009-07-20 17:51:36 +000014230 }
14231 }
14232 }
14233 }
14234 break;
14235 }
14236 return false;
14237}
14238
14239
14240
Chris Lattnerf4dff842006-07-11 02:54:03 +000014241/// getConstraintType - Given a constraint letter, return the type of
14242/// constraint it is for this target.
14243X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000014244X86TargetLowering::getConstraintType(const std::string &Constraint) const {
14245 if (Constraint.size() == 1) {
14246 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000014247 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000014248 case 'q':
14249 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000014250 case 'f':
14251 case 't':
14252 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000014253 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000014254 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000014255 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000014256 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000014257 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000014258 case 'a':
14259 case 'b':
14260 case 'c':
14261 case 'd':
14262 case 'S':
14263 case 'D':
14264 case 'A':
14265 return C_Register;
14266 case 'I':
14267 case 'J':
14268 case 'K':
14269 case 'L':
14270 case 'M':
14271 case 'N':
14272 case 'G':
14273 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000014274 case 'e':
14275 case 'Z':
14276 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000014277 default:
14278 break;
14279 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000014280 }
Chris Lattner4234f572007-03-25 02:14:49 +000014281 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000014282}
14283
John Thompson44ab89e2010-10-29 17:29:13 +000014284/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000014285/// This object must already have been set up with the operand type
14286/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000014287TargetLowering::ConstraintWeight
14288 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000014289 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000014290 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014291 Value *CallOperandVal = info.CallOperandVal;
14292 // If we don't have a value, we can't do a match,
14293 // but allow it at the lowest weight.
14294 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000014295 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014296 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000014297 // Look at the constraint type.
14298 switch (*constraint) {
14299 default:
John Thompson44ab89e2010-10-29 17:29:13 +000014300 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
14301 case 'R':
14302 case 'q':
14303 case 'Q':
14304 case 'a':
14305 case 'b':
14306 case 'c':
14307 case 'd':
14308 case 'S':
14309 case 'D':
14310 case 'A':
14311 if (CallOperandVal->getType()->isIntegerTy())
14312 weight = CW_SpecificReg;
14313 break;
14314 case 'f':
14315 case 't':
14316 case 'u':
14317 if (type->isFloatingPointTy())
14318 weight = CW_SpecificReg;
14319 break;
14320 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000014321 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000014322 weight = CW_SpecificReg;
14323 break;
14324 case 'x':
14325 case 'Y':
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014326 if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
John Thompson44ab89e2010-10-29 17:29:13 +000014327 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014328 break;
14329 case 'I':
14330 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
14331 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000014332 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014333 }
14334 break;
John Thompson44ab89e2010-10-29 17:29:13 +000014335 case 'J':
14336 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14337 if (C->getZExtValue() <= 63)
14338 weight = CW_Constant;
14339 }
14340 break;
14341 case 'K':
14342 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14343 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
14344 weight = CW_Constant;
14345 }
14346 break;
14347 case 'L':
14348 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14349 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
14350 weight = CW_Constant;
14351 }
14352 break;
14353 case 'M':
14354 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14355 if (C->getZExtValue() <= 3)
14356 weight = CW_Constant;
14357 }
14358 break;
14359 case 'N':
14360 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14361 if (C->getZExtValue() <= 0xff)
14362 weight = CW_Constant;
14363 }
14364 break;
14365 case 'G':
14366 case 'C':
14367 if (dyn_cast<ConstantFP>(CallOperandVal)) {
14368 weight = CW_Constant;
14369 }
14370 break;
14371 case 'e':
14372 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14373 if ((C->getSExtValue() >= -0x80000000LL) &&
14374 (C->getSExtValue() <= 0x7fffffffLL))
14375 weight = CW_Constant;
14376 }
14377 break;
14378 case 'Z':
14379 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14380 if (C->getZExtValue() <= 0xffffffff)
14381 weight = CW_Constant;
14382 }
14383 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014384 }
14385 return weight;
14386}
14387
Dale Johannesenba2a0b92008-01-29 02:21:21 +000014388/// LowerXConstraint - try to replace an X constraint, which matches anything,
14389/// with another that has more specific requirements based on the type of the
14390/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000014391const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000014392LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000014393 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
14394 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000014395 if (ConstraintVT.isFloatingPoint()) {
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014396 if (Subtarget->hasXMMInt())
Chris Lattner5e764232008-04-26 23:02:14 +000014397 return "Y";
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014398 if (Subtarget->hasXMM())
Chris Lattner5e764232008-04-26 23:02:14 +000014399 return "x";
14400 }
Scott Michelfdc40a02009-02-17 22:15:04 +000014401
Chris Lattner5e764232008-04-26 23:02:14 +000014402 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000014403}
14404
Chris Lattner48884cd2007-08-25 00:47:38 +000014405/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
14406/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000014407void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000014408 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000014409 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000014410 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000014411 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000014412
Eric Christopher100c8332011-06-02 23:16:42 +000014413 // Only support length 1 constraints for now.
14414 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000014415
Eric Christopher100c8332011-06-02 23:16:42 +000014416 char ConstraintLetter = Constraint[0];
14417 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000014418 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000014419 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000014420 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000014421 if (C->getZExtValue() <= 31) {
14422 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000014423 break;
14424 }
Devang Patel84f7fd22007-03-17 00:13:28 +000014425 }
Chris Lattner48884cd2007-08-25 00:47:38 +000014426 return;
Evan Cheng364091e2008-09-22 23:57:37 +000014427 case 'J':
14428 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000014429 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000014430 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
14431 break;
14432 }
14433 }
14434 return;
14435 case 'K':
14436 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000014437 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000014438 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
14439 break;
14440 }
14441 }
14442 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000014443 case 'N':
14444 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000014445 if (C->getZExtValue() <= 255) {
14446 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000014447 break;
14448 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000014449 }
Chris Lattner48884cd2007-08-25 00:47:38 +000014450 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000014451 case 'e': {
14452 // 32-bit signed value
14453 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000014454 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
14455 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000014456 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000014457 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000014458 break;
14459 }
14460 // FIXME gcc accepts some relocatable values here too, but only in certain
14461 // memory models; it's complicated.
14462 }
14463 return;
14464 }
14465 case 'Z': {
14466 // 32-bit unsigned value
14467 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000014468 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
14469 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000014470 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
14471 break;
14472 }
14473 }
14474 // FIXME gcc accepts some relocatable values here too, but only in certain
14475 // memory models; it's complicated.
14476 return;
14477 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000014478 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000014479 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000014480 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000014481 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000014482 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000014483 break;
14484 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014485
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000014486 // In any sort of PIC mode addresses need to be computed at runtime by
14487 // adding in a register or some sort of table lookup. These can't
14488 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000014489 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000014490 return;
14491
Chris Lattnerdc43a882007-05-03 16:52:29 +000014492 // If we are in non-pic codegen mode, we allow the address of a global (with
14493 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000014494 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000014495 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000014496
Chris Lattner49921962009-05-08 18:23:14 +000014497 // Match either (GA), (GA+C), (GA+C1+C2), etc.
14498 while (1) {
14499 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
14500 Offset += GA->getOffset();
14501 break;
14502 } else if (Op.getOpcode() == ISD::ADD) {
14503 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
14504 Offset += C->getZExtValue();
14505 Op = Op.getOperand(0);
14506 continue;
14507 }
14508 } else if (Op.getOpcode() == ISD::SUB) {
14509 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
14510 Offset += -C->getZExtValue();
14511 Op = Op.getOperand(0);
14512 continue;
14513 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000014514 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000014515
Chris Lattner49921962009-05-08 18:23:14 +000014516 // Otherwise, this isn't something we can handle, reject it.
14517 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000014518 }
Eric Christopherfd179292009-08-27 18:07:15 +000014519
Dan Gohman46510a72010-04-15 01:51:59 +000014520 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000014521 // If we require an extra load to get this address, as in PIC mode, we
14522 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000014523 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
14524 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000014525 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000014526
Devang Patel0d881da2010-07-06 22:08:15 +000014527 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
14528 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000014529 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000014530 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000014531 }
Scott Michelfdc40a02009-02-17 22:15:04 +000014532
Gabor Greifba36cb52008-08-28 21:40:38 +000014533 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000014534 Ops.push_back(Result);
14535 return;
14536 }
Dale Johannesen1784d162010-06-25 21:55:36 +000014537 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000014538}
14539
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014540std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000014541X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000014542 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000014543 // First, see if this is a constraint that directly corresponds to an LLVM
14544 // register class.
14545 if (Constraint.size() == 1) {
14546 // GCC Constraint Letters
14547 switch (Constraint[0]) {
14548 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000014549 // TODO: Slight differences here in allocation order and leaving
14550 // RIP in the class. Do they matter any more here than they do
14551 // in the normal allocation?
14552 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
14553 if (Subtarget->is64Bit()) {
Nick Lewycky9bf45d02011-07-08 00:19:27 +000014554 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000014555 return std::make_pair(0U, X86::GR32RegisterClass);
14556 else if (VT == MVT::i16)
14557 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000014558 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000014559 return std::make_pair(0U, X86::GR8RegisterClass);
Nick Lewycky9bf45d02011-07-08 00:19:27 +000014560 else if (VT == MVT::i64 || VT == MVT::f64)
Eric Christopherd176af82011-06-29 17:23:50 +000014561 return std::make_pair(0U, X86::GR64RegisterClass);
14562 break;
14563 }
14564 // 32-bit fallthrough
14565 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000014566 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000014567 return std::make_pair(0U, X86::GR32_ABCDRegisterClass);
14568 else if (VT == MVT::i16)
14569 return std::make_pair(0U, X86::GR16_ABCDRegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000014570 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000014571 return std::make_pair(0U, X86::GR8_ABCD_LRegisterClass);
14572 else if (VT == MVT::i64)
14573 return std::make_pair(0U, X86::GR64_ABCDRegisterClass);
14574 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000014575 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000014576 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000014577 if (VT == MVT::i8 || VT == MVT::i1)
Chris Lattner0f65cad2007-04-09 05:49:22 +000014578 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000014579 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000014580 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000014581 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000014582 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000014583 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000014584 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000014585 if (VT == MVT::i8 || VT == MVT::i1)
Dale Johannesen5f3663e2009-10-07 22:47:20 +000014586 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
14587 if (VT == MVT::i16)
14588 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
14589 if (VT == MVT::i32 || !Subtarget->is64Bit())
14590 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
14591 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000014592 case 'f': // FP Stack registers.
14593 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
14594 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000014595 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000014596 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000014597 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000014598 return std::make_pair(0U, X86::RFP64RegisterClass);
14599 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000014600 case 'y': // MMX_REGS if MMX allowed.
14601 if (!Subtarget->hasMMX()) break;
14602 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000014603 case 'Y': // SSE_REGS if SSE2 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014604 if (!Subtarget->hasXMMInt()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000014605 // FALL THROUGH.
14606 case 'x': // SSE_REGS if SSE1 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014607 if (!Subtarget->hasXMM()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000014608
Owen Anderson825b72b2009-08-11 20:47:22 +000014609 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000014610 default: break;
14611 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000014612 case MVT::f32:
14613 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000014614 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000014615 case MVT::f64:
14616 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000014617 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000014618 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000014619 case MVT::v16i8:
14620 case MVT::v8i16:
14621 case MVT::v4i32:
14622 case MVT::v2i64:
14623 case MVT::v4f32:
14624 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000014625 return std::make_pair(0U, X86::VR128RegisterClass);
14626 }
Chris Lattnerad043e82007-04-09 05:11:28 +000014627 break;
14628 }
14629 }
Scott Michelfdc40a02009-02-17 22:15:04 +000014630
Chris Lattnerf76d1802006-07-31 23:26:50 +000014631 // Use the default implementation in TargetLowering to convert the register
14632 // constraint into a member of a register class.
14633 std::pair<unsigned, const TargetRegisterClass*> Res;
14634 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000014635
14636 // Not found as a standard register?
14637 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000014638 // Map st(0) -> st(7) -> ST0
14639 if (Constraint.size() == 7 && Constraint[0] == '{' &&
14640 tolower(Constraint[1]) == 's' &&
14641 tolower(Constraint[2]) == 't' &&
14642 Constraint[3] == '(' &&
14643 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
14644 Constraint[5] == ')' &&
14645 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000014646
Chris Lattner56d77c72009-09-13 22:41:48 +000014647 Res.first = X86::ST0+Constraint[4]-'0';
14648 Res.second = X86::RFP80RegisterClass;
14649 return Res;
14650 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000014651
Chris Lattner56d77c72009-09-13 22:41:48 +000014652 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000014653 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000014654 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000014655 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000014656 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000014657 }
Chris Lattner56d77c72009-09-13 22:41:48 +000014658
14659 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000014660 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000014661 Res.first = X86::EFLAGS;
14662 Res.second = X86::CCRRegisterClass;
14663 return Res;
14664 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000014665
Dale Johannesen330169f2008-11-13 21:52:36 +000014666 // 'A' means EAX + EDX.
14667 if (Constraint == "A") {
14668 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000014669 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000014670 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000014671 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000014672 return Res;
14673 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014674
Chris Lattnerf76d1802006-07-31 23:26:50 +000014675 // Otherwise, check to see if this is a register class of the wrong value
14676 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
14677 // turn into {ax},{dx}.
14678 if (Res.second->hasType(VT))
14679 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014680
Chris Lattnerf76d1802006-07-31 23:26:50 +000014681 // All of the single-register GCC register classes map their values onto
14682 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
14683 // really want an 8-bit or 32-bit register, map to the appropriate register
14684 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000014685 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000014686 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000014687 unsigned DestReg = 0;
14688 switch (Res.first) {
14689 default: break;
14690 case X86::AX: DestReg = X86::AL; break;
14691 case X86::DX: DestReg = X86::DL; break;
14692 case X86::CX: DestReg = X86::CL; break;
14693 case X86::BX: DestReg = X86::BL; break;
14694 }
14695 if (DestReg) {
14696 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000014697 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000014698 }
Owen Anderson825b72b2009-08-11 20:47:22 +000014699 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000014700 unsigned DestReg = 0;
14701 switch (Res.first) {
14702 default: break;
14703 case X86::AX: DestReg = X86::EAX; break;
14704 case X86::DX: DestReg = X86::EDX; break;
14705 case X86::CX: DestReg = X86::ECX; break;
14706 case X86::BX: DestReg = X86::EBX; break;
14707 case X86::SI: DestReg = X86::ESI; break;
14708 case X86::DI: DestReg = X86::EDI; break;
14709 case X86::BP: DestReg = X86::EBP; break;
14710 case X86::SP: DestReg = X86::ESP; break;
14711 }
14712 if (DestReg) {
14713 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000014714 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000014715 }
Owen Anderson825b72b2009-08-11 20:47:22 +000014716 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000014717 unsigned DestReg = 0;
14718 switch (Res.first) {
14719 default: break;
14720 case X86::AX: DestReg = X86::RAX; break;
14721 case X86::DX: DestReg = X86::RDX; break;
14722 case X86::CX: DestReg = X86::RCX; break;
14723 case X86::BX: DestReg = X86::RBX; break;
14724 case X86::SI: DestReg = X86::RSI; break;
14725 case X86::DI: DestReg = X86::RDI; break;
14726 case X86::BP: DestReg = X86::RBP; break;
14727 case X86::SP: DestReg = X86::RSP; break;
14728 }
14729 if (DestReg) {
14730 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000014731 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000014732 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000014733 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000014734 } else if (Res.second == X86::FR32RegisterClass ||
14735 Res.second == X86::FR64RegisterClass ||
14736 Res.second == X86::VR128RegisterClass) {
14737 // Handle references to XMM physical registers that got mapped into the
14738 // wrong class. This can happen with constraints like {xmm0} where the
14739 // target independent register mapper will just pick the first match it can
14740 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000014741 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000014742 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000014743 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000014744 Res.second = X86::FR64RegisterClass;
14745 else if (X86::VR128RegisterClass->hasType(VT))
14746 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000014747 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014748
Chris Lattnerf76d1802006-07-31 23:26:50 +000014749 return Res;
14750}