blob: 171349c0663529fd776fa0eb63accecd0c618639 [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 Lopesaed890b2011-08-01 21:54:05 +0000172 X86ScalarSSEf64 = Subtarget->hasXMMInt() || Subtarget->hasAVX();
173 X86ScalarSSEf32 = Subtarget->hasXMM() || Subtarget->hasAVX();
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) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000318 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000319 // Expand FP_TO_UINT into a select.
320 // FIXME: We would like to use a Custom expander here eventually to do
321 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000322 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000323 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000324 // With SSE3 we can use fisttpll to convert to a signed i64; without
325 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000326 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000327 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000328
Chris Lattner399610a2006-12-05 18:22:22 +0000329 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000330 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000331 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
332 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000333 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000334 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000335 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000336 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000337 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000338 }
Chris Lattner21f66852005-12-23 05:15:23 +0000339
Dan Gohmanb00ee212008-02-18 19:34:53 +0000340 // Scalar integer divide and remainder are lowered to use operations that
341 // produce two results, to match the available instructions. This exposes
342 // the two-result form to trivial CSE, which is able to combine x/y and x%y
343 // into a single instruction.
344 //
345 // Scalar integer multiply-high is also lowered to use two-result
346 // operations, to match the available instructions. However, plain multiply
347 // (low) operations are left as Legal, as there are single-result
348 // instructions for this in x86. Using the two-result multiply instructions
349 // when both high and low results are needed must be arranged by dagcombine.
Chris Lattnere019ec12010-12-19 20:07:10 +0000350 for (unsigned i = 0, e = 4; i != e; ++i) {
351 MVT VT = IntVTs[i];
352 setOperationAction(ISD::MULHS, VT, Expand);
353 setOperationAction(ISD::MULHU, VT, Expand);
354 setOperationAction(ISD::SDIV, VT, Expand);
355 setOperationAction(ISD::UDIV, VT, Expand);
356 setOperationAction(ISD::SREM, VT, Expand);
357 setOperationAction(ISD::UREM, VT, Expand);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000358
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000359 // Add/Sub overflow ops with MVT::Glues are lowered to EFLAGS dependences.
Chris Lattnerd8ff7ec2010-12-20 01:03:27 +0000360 setOperationAction(ISD::ADDC, VT, Custom);
361 setOperationAction(ISD::ADDE, VT, Custom);
362 setOperationAction(ISD::SUBC, VT, Custom);
363 setOperationAction(ISD::SUBE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000364 }
Dan Gohmana37c9f72007-09-25 18:23:27 +0000365
Owen Anderson825b72b2009-08-11 20:47:22 +0000366 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
367 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
368 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
369 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000370 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000371 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
372 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
373 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
374 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
375 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
376 setOperationAction(ISD::FREM , MVT::f32 , Expand);
377 setOperationAction(ISD::FREM , MVT::f64 , Expand);
378 setOperationAction(ISD::FREM , MVT::f80 , Expand);
379 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000380
Owen Anderson825b72b2009-08-11 20:47:22 +0000381 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
382 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000383 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
384 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000385 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
386 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000387 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000388 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
389 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000390 }
391
Benjamin Kramer1292c222010-12-04 20:32:23 +0000392 if (Subtarget->hasPOPCNT()) {
393 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
394 } else {
395 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
396 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
397 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
398 if (Subtarget->is64Bit())
399 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
400 }
401
Owen Anderson825b72b2009-08-11 20:47:22 +0000402 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
403 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000404
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000405 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000406 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000407 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000408 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000409 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000410 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
411 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
412 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
413 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
414 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000415 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000416 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
417 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
418 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
419 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000420 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000421 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
Andrew Trickf6c39412011-03-23 23:11:02 +0000422 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000423 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000424 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000425
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000426 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000427 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
428 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
429 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
430 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000431 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000432 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
433 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000434 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000435 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000436 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
437 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
438 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
439 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000440 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000441 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000442 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000443 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
444 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
445 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000446 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000447 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
448 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
449 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000450 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000451
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000452 if (Subtarget->hasXMM())
Owen Anderson825b72b2009-08-11 20:47:22 +0000453 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000454
Eric Christopher9a9d2752010-07-22 02:48:34 +0000455 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000456 setOperationAction(ISD::ATOMIC_FENCE , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000457
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000458 // On X86 and X86-64, atomic operations are lowered to locked instructions.
459 // Locked instructions, in turn, have implicit fence semantics (all memory
460 // operations are flushed before issuing the locked instruction, and they
461 // are not buffered), so we can fold away the common pattern of
462 // fence-atomic-fence.
463 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000464
Mon P Wang63307c32008-05-05 19:05:59 +0000465 // Expand certain atomics
Chris Lattnere019ec12010-12-19 20:07:10 +0000466 for (unsigned i = 0, e = 4; i != e; ++i) {
467 MVT VT = IntVTs[i];
468 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
469 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
Eli Friedman327236c2011-08-24 20:50:09 +0000470 setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000471 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000472
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000473 if (!Subtarget->is64Bit()) {
Eli Friedmanf8f90f02011-08-24 22:33:28 +0000474 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000475 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
476 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
477 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
478 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
479 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
480 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
481 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000482 }
483
Eli Friedman43f51ae2011-08-26 21:21:21 +0000484 if (Subtarget->hasCmpxchg16b()) {
485 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i128, Custom);
486 }
487
Evan Cheng3c992d22006-03-07 02:02:57 +0000488 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000489 if (!Subtarget->isTargetDarwin() &&
490 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000491 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000492 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000493 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000494
Owen Anderson825b72b2009-08-11 20:47:22 +0000495 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
496 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
497 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
498 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000499 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000500 setExceptionPointerRegister(X86::RAX);
501 setExceptionSelectorRegister(X86::RDX);
502 } else {
503 setExceptionPointerRegister(X86::EAX);
504 setExceptionSelectorRegister(X86::EDX);
505 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000506 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
507 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000508
Duncan Sands4a544a72011-09-06 13:37:06 +0000509 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
510 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000511
Owen Anderson825b72b2009-08-11 20:47:22 +0000512 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000513
Nate Begemanacc398c2006-01-25 18:21:52 +0000514 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000515 setOperationAction(ISD::VASTART , MVT::Other, Custom);
516 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000517 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000518 setOperationAction(ISD::VAARG , MVT::Other, Custom);
519 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000520 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000521 setOperationAction(ISD::VAARG , MVT::Other, Expand);
522 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000523 }
Evan Chengae642192007-03-02 23:16:35 +0000524
Owen Anderson825b72b2009-08-11 20:47:22 +0000525 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
526 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eric Christopherc967ad82011-08-31 04:17:21 +0000527
528 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
529 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
530 MVT::i64 : MVT::i32, Custom);
531 else if (EnableSegmentedStacks)
532 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
533 MVT::i64 : MVT::i32, Custom);
534 else
535 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
536 MVT::i64 : MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000537
Evan Chengc7ce29b2009-02-13 22:36:38 +0000538 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000539 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000540 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000541 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
542 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000543
Evan Cheng223547a2006-01-31 22:28:30 +0000544 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000545 setOperationAction(ISD::FABS , MVT::f64, Custom);
546 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000547
548 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000549 setOperationAction(ISD::FNEG , MVT::f64, Custom);
550 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000551
Evan Cheng68c47cb2007-01-05 07:55:56 +0000552 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000553 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
554 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000555
Stuart Hastings4fd0dee2011-06-01 04:39:42 +0000556 // Lower this to FGETSIGNx86 plus an AND.
557 setOperationAction(ISD::FGETSIGN, MVT::i64, Custom);
558 setOperationAction(ISD::FGETSIGN, MVT::i32, Custom);
559
Evan Chengd25e9e82006-02-02 00:28:23 +0000560 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000561 setOperationAction(ISD::FSIN , MVT::f64, Expand);
562 setOperationAction(ISD::FCOS , MVT::f64, Expand);
563 setOperationAction(ISD::FSIN , MVT::f32, Expand);
564 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000565
Chris Lattnera54aa942006-01-29 06:26:08 +0000566 // Expand FP immediates into loads from the stack, except for the special
567 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000568 addLegalFPImmediate(APFloat(+0.0)); // xorpd
569 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000570 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000571 // Use SSE for f32, x87 for f64.
572 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000573 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
574 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000575
576 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000577 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000578
579 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000580 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000581
Owen Anderson825b72b2009-08-11 20:47:22 +0000582 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000583
584 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000585 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
586 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000587
588 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000589 setOperationAction(ISD::FSIN , MVT::f32, Expand);
590 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000591
Nate Begemane1795842008-02-14 08:57:00 +0000592 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000593 addLegalFPImmediate(APFloat(+0.0f)); // xorps
594 addLegalFPImmediate(APFloat(+0.0)); // FLD0
595 addLegalFPImmediate(APFloat(+1.0)); // FLD1
596 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
597 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
598
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000599 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000600 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
601 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000602 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000603 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000604 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000605 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000606 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
607 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000608
Owen Anderson825b72b2009-08-11 20:47:22 +0000609 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
610 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
611 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
612 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000613
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000614 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000615 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
616 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000617 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000618 addLegalFPImmediate(APFloat(+0.0)); // FLD0
619 addLegalFPImmediate(APFloat(+1.0)); // FLD1
620 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
621 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000622 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
623 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
624 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
625 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000626 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000627
Cameron Zwarich33390842011-07-08 21:39:21 +0000628 // We don't support FMA.
629 setOperationAction(ISD::FMA, MVT::f64, Expand);
630 setOperationAction(ISD::FMA, MVT::f32, Expand);
631
Dale Johannesen59a58732007-08-05 18:49:15 +0000632 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000633 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000634 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
635 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
636 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000637 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000638 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000639 addLegalFPImmediate(TmpFlt); // FLD0
640 TmpFlt.changeSign();
641 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000642
643 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000644 APFloat TmpFlt2(+1.0);
645 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
646 &ignored);
647 addLegalFPImmediate(TmpFlt2); // FLD1
648 TmpFlt2.changeSign();
649 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
650 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000651
Evan Chengc7ce29b2009-02-13 22:36:38 +0000652 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000653 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
654 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000655 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000656
657 setOperationAction(ISD::FMA, MVT::f80, Expand);
Dale Johannesen2f429012007-09-26 21:10:55 +0000658 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000659
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000660 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000661 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
662 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
663 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000664
Owen Anderson825b72b2009-08-11 20:47:22 +0000665 setOperationAction(ISD::FLOG, MVT::f80, Expand);
666 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
667 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
668 setOperationAction(ISD::FEXP, MVT::f80, Expand);
669 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000670
Mon P Wangf007a8b2008-11-06 05:31:54 +0000671 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000672 // (for widening) or expand (for scalarization). Then we will selectively
673 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000674 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
675 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
676 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
677 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
678 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
679 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
680 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
681 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
682 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
683 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
684 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
685 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
686 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
687 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
688 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
689 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
690 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000691 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
David Greenecfe33c42011-01-26 19:13:22 +0000692 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
693 setOperationAction(ISD::INSERT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000694 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
695 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
696 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
697 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
698 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
699 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
700 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
701 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
702 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
703 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
704 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
705 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
706 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
707 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
708 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
709 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
710 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
711 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
712 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
713 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
714 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
Duncan Sands28b77e92011-09-06 19:07:46 +0000715 setOperationAction(ISD::SETCC, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000716 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
717 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
718 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
719 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
720 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
721 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
722 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
723 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
724 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000725 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000726 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
727 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
728 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
729 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
730 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
731 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
732 setTruncStoreAction((MVT::SimpleValueType)VT,
733 (MVT::SimpleValueType)InnerVT, Expand);
734 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
735 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
736 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000737 }
738
Evan Chengc7ce29b2009-02-13 22:36:38 +0000739 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
740 // with -msoft-float, disable use of MMX as well.
Chris Lattner2a786eb2010-12-19 20:19:20 +0000741 if (!UseSoftFloat && Subtarget->hasMMX()) {
Dale Johannesene93d99c2010-10-20 21:32:10 +0000742 addRegisterClass(MVT::x86mmx, X86::VR64RegisterClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000743 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000744 }
745
Dale Johannesen0488fb62010-09-30 23:57:10 +0000746 // MMX-sized vectors (other than x86mmx) are expected to be expanded
747 // into smaller operations.
748 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
749 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
750 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
751 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
752 setOperationAction(ISD::AND, MVT::v8i8, Expand);
753 setOperationAction(ISD::AND, MVT::v4i16, Expand);
754 setOperationAction(ISD::AND, MVT::v2i32, Expand);
755 setOperationAction(ISD::AND, MVT::v1i64, Expand);
756 setOperationAction(ISD::OR, MVT::v8i8, Expand);
757 setOperationAction(ISD::OR, MVT::v4i16, Expand);
758 setOperationAction(ISD::OR, MVT::v2i32, Expand);
759 setOperationAction(ISD::OR, MVT::v1i64, Expand);
760 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
761 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
762 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
763 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
764 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
765 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
766 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
767 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
768 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
769 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
770 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
771 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
772 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000773 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
774 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
775 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
776 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000777
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000778 if (!UseSoftFloat && Subtarget->hasXMM()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000779 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000780
Owen Anderson825b72b2009-08-11 20:47:22 +0000781 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
782 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
783 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
784 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
785 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
786 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
787 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
788 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
789 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
790 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
791 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000792 setOperationAction(ISD::SETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000793 }
794
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000795 if (!UseSoftFloat && Subtarget->hasXMMInt()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000796 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000797
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000798 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
799 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000800 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
801 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
802 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
803 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000804
Owen Anderson825b72b2009-08-11 20:47:22 +0000805 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
806 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
807 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
808 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
809 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
810 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
811 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
812 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
813 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
814 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
815 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
816 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
817 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
818 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
819 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
820 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000821
Duncan Sands28b77e92011-09-06 19:07:46 +0000822 setOperationAction(ISD::SETCC, MVT::v2f64, Custom);
823 setOperationAction(ISD::SETCC, MVT::v16i8, Custom);
824 setOperationAction(ISD::SETCC, MVT::v8i16, Custom);
825 setOperationAction(ISD::SETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000826
Owen Anderson825b72b2009-08-11 20:47:22 +0000827 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
828 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
829 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
830 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
831 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000832
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000833 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
834 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
835 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
836 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
837 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
838
Evan Cheng2c3ae372006-04-12 21:21:57 +0000839 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000840 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
841 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000842 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000843 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000844 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000845 // Do not attempt to custom lower non-128-bit vectors
846 if (!VT.is128BitVector())
847 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000848 setOperationAction(ISD::BUILD_VECTOR,
849 VT.getSimpleVT().SimpleTy, Custom);
850 setOperationAction(ISD::VECTOR_SHUFFLE,
851 VT.getSimpleVT().SimpleTy, Custom);
852 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
853 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000854 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000855
Owen Anderson825b72b2009-08-11 20:47:22 +0000856 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
857 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
858 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
859 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
860 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
861 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000862
Nate Begemancdd1eec2008-02-12 22:51:28 +0000863 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000864 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
865 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000866 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000867
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000868 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000869 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
870 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000871 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000872
873 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000874 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000875 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000876
Owen Andersond6662ad2009-08-10 20:46:15 +0000877 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000878 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000879 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000880 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000881 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000882 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000883 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000884 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000885 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000886 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000887 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000888
Owen Anderson825b72b2009-08-11 20:47:22 +0000889 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000890
Evan Cheng2c3ae372006-04-12 21:21:57 +0000891 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000892 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
893 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
894 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
895 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000896
Owen Anderson825b72b2009-08-11 20:47:22 +0000897 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
898 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000899 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000900
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000901 if (Subtarget->hasSSE41() || Subtarget->hasAVX()) {
Dale Johannesen54feef22010-05-27 20:12:41 +0000902 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
903 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
904 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
905 setOperationAction(ISD::FRINT, MVT::f32, Legal);
906 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
907 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
908 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
909 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
910 setOperationAction(ISD::FRINT, MVT::f64, Legal);
911 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
912
Nate Begeman14d12ca2008-02-11 04:19:36 +0000913 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000914 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000915
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000916 // Can turn SHL into an integer multiply.
917 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
Nate Begeman51409212010-07-28 00:21:48 +0000918 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000919
Nadav Rotemffe3e7d2011-09-08 08:11:19 +0000920 setOperationAction(ISD::VSELECT, MVT::v2f64, Custom);
921 setOperationAction(ISD::VSELECT, MVT::v2i64, Custom);
922 setOperationAction(ISD::VSELECT, MVT::v16i8, Custom);
Nadav Rotemffe3e7d2011-09-08 08:11:19 +0000923 setOperationAction(ISD::VSELECT, MVT::v4i32, Custom);
924 setOperationAction(ISD::VSELECT, MVT::v4f32, Custom);
925
Nate Begeman14d12ca2008-02-11 04:19:36 +0000926 // i8 and i16 vectors are custom , because the source register and source
927 // source memory operand types are not the same width. f32 vectors are
928 // custom since the immediate controlling the insert encodes additional
929 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000930 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
931 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
932 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
933 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000934
Owen Anderson825b72b2009-08-11 20:47:22 +0000935 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
936 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
937 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
938 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000939
940 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000941 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
942 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000943 }
944 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000945
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000946 if (Subtarget->hasSSE2() || Subtarget->hasAVX()) {
Nadav Rotem43012222011-05-11 08:12:09 +0000947 setOperationAction(ISD::SRL, MVT::v2i64, Custom);
948 setOperationAction(ISD::SRL, MVT::v4i32, Custom);
949 setOperationAction(ISD::SRL, MVT::v16i8, Custom);
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000950 setOperationAction(ISD::SRL, MVT::v8i16, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +0000951
952 setOperationAction(ISD::SHL, MVT::v2i64, Custom);
953 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
954 setOperationAction(ISD::SHL, MVT::v8i16, Custom);
955
956 setOperationAction(ISD::SRA, MVT::v4i32, Custom);
957 setOperationAction(ISD::SRA, MVT::v8i16, Custom);
958 }
959
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000960 if (Subtarget->hasSSE42() || Subtarget->hasAVX())
Duncan Sands28b77e92011-09-06 19:07:46 +0000961 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000962
David Greene9b9838d2009-06-29 16:47:10 +0000963 if (!UseSoftFloat && Subtarget->hasAVX()) {
Bruno Cardoso Lopesdbd4fe22011-07-21 02:24:08 +0000964 addRegisterClass(MVT::v32i8, X86::VR256RegisterClass);
965 addRegisterClass(MVT::v16i16, X86::VR256RegisterClass);
966 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
967 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
968 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
969 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000970
Owen Anderson825b72b2009-08-11 20:47:22 +0000971 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +0000972 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
973 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +0000974
Owen Anderson825b72b2009-08-11 20:47:22 +0000975 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
976 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
977 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
978 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
979 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
980 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000981
Owen Anderson825b72b2009-08-11 20:47:22 +0000982 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
983 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
984 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
985 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
986 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
987 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000988
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +0000989 setOperationAction(ISD::FP_TO_SINT, MVT::v8i32, Legal);
990 setOperationAction(ISD::SINT_TO_FP, MVT::v8i32, Legal);
Bruno Cardoso Lopes55244ce2011-08-01 21:54:09 +0000991 setOperationAction(ISD::FP_ROUND, MVT::v4f32, Legal);
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +0000992
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +0000993 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4f64, Custom);
994 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i64, Custom);
995 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8f32, Custom);
996 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i32, Custom);
997 setOperationAction(ISD::CONCAT_VECTORS, MVT::v32i8, Custom);
998 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i16, Custom);
999
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001000 setOperationAction(ISD::SRL, MVT::v4i64, Custom);
1001 setOperationAction(ISD::SRL, MVT::v8i32, Custom);
1002 setOperationAction(ISD::SRL, MVT::v16i16, Custom);
1003 setOperationAction(ISD::SRL, MVT::v32i8, Custom);
1004
1005 setOperationAction(ISD::SHL, MVT::v4i64, Custom);
1006 setOperationAction(ISD::SHL, MVT::v8i32, Custom);
1007 setOperationAction(ISD::SHL, MVT::v16i16, Custom);
1008 setOperationAction(ISD::SHL, MVT::v32i8, Custom);
1009
1010 setOperationAction(ISD::SRA, MVT::v8i32, Custom);
1011 setOperationAction(ISD::SRA, MVT::v16i16, Custom);
1012
Duncan Sands28b77e92011-09-06 19:07:46 +00001013 setOperationAction(ISD::SETCC, MVT::v32i8, Custom);
1014 setOperationAction(ISD::SETCC, MVT::v16i16, Custom);
1015 setOperationAction(ISD::SETCC, MVT::v8i32, Custom);
1016 setOperationAction(ISD::SETCC, MVT::v4i64, Custom);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00001017
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +00001018 setOperationAction(ISD::SELECT, MVT::v4f64, Custom);
1019 setOperationAction(ISD::SELECT, MVT::v4i64, Custom);
1020 setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
1021
Craig Topper13894fa2011-08-24 06:14:18 +00001022 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
1023 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
1024 setOperationAction(ISD::ADD, MVT::v16i16, Custom);
1025 setOperationAction(ISD::ADD, MVT::v32i8, Custom);
1026
1027 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
1028 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
1029 setOperationAction(ISD::SUB, MVT::v16i16, Custom);
1030 setOperationAction(ISD::SUB, MVT::v32i8, Custom);
1031
1032 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1033 setOperationAction(ISD::MUL, MVT::v8i32, Custom);
1034 setOperationAction(ISD::MUL, MVT::v16i16, Custom);
1035 // Don't lower v32i8 because there is no 128-bit byte mul
1036
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001037 // Custom lower several nodes for 256-bit types.
David Greene54d8eba2011-01-27 22:38:56 +00001038 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001039 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
1040 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
1041 EVT VT = SVT;
1042
1043 // Extract subvector is special because the value type
1044 // (result) is 128-bit but the source is 256-bit wide.
1045 if (VT.is128BitVector())
1046 setOperationAction(ISD::EXTRACT_SUBVECTOR, SVT, Custom);
1047
1048 // Do not attempt to custom lower other non-256-bit vectors
1049 if (!VT.is256BitVector())
David Greene9b9838d2009-06-29 16:47:10 +00001050 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001051
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001052 setOperationAction(ISD::BUILD_VECTOR, SVT, Custom);
1053 setOperationAction(ISD::VECTOR_SHUFFLE, SVT, Custom);
1054 setOperationAction(ISD::INSERT_VECTOR_ELT, SVT, Custom);
1055 setOperationAction(ISD::EXTRACT_VECTOR_ELT, SVT, Custom);
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00001056 setOperationAction(ISD::SCALAR_TO_VECTOR, SVT, Custom);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001057 setOperationAction(ISD::INSERT_SUBVECTOR, SVT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001058 }
1059
David Greene54d8eba2011-01-27 22:38:56 +00001060 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001061 for (unsigned i = (unsigned)MVT::v32i8; i != (unsigned)MVT::v4i64; ++i) {
1062 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
1063 EVT VT = SVT;
David Greene54d8eba2011-01-27 22:38:56 +00001064
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001065 // Do not attempt to promote non-256-bit vectors
1066 if (!VT.is256BitVector())
David Greene54d8eba2011-01-27 22:38:56 +00001067 continue;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001068
1069 setOperationAction(ISD::AND, SVT, Promote);
1070 AddPromotedToType (ISD::AND, SVT, MVT::v4i64);
1071 setOperationAction(ISD::OR, SVT, Promote);
1072 AddPromotedToType (ISD::OR, SVT, MVT::v4i64);
1073 setOperationAction(ISD::XOR, SVT, Promote);
1074 AddPromotedToType (ISD::XOR, SVT, MVT::v4i64);
1075 setOperationAction(ISD::LOAD, SVT, Promote);
1076 AddPromotedToType (ISD::LOAD, SVT, MVT::v4i64);
1077 setOperationAction(ISD::SELECT, SVT, Promote);
1078 AddPromotedToType (ISD::SELECT, SVT, MVT::v4i64);
David Greene54d8eba2011-01-27 22:38:56 +00001079 }
David Greene9b9838d2009-06-29 16:47:10 +00001080 }
1081
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001082 // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
1083 // of this type with custom code.
1084 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1085 VT != (unsigned)MVT::LAST_VECTOR_VALUETYPE; VT++) {
1086 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT, Custom);
1087 }
1088
Evan Cheng6be2c582006-04-05 23:38:46 +00001089 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001090 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001091
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001092
Eli Friedman962f5492010-06-02 19:35:46 +00001093 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1094 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001095 //
Eli Friedman962f5492010-06-02 19:35:46 +00001096 // FIXME: We really should do custom legalization for addition and
1097 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1098 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001099 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1100 // Add/Sub/Mul with overflow operations are custom lowered.
1101 MVT VT = IntVTs[i];
1102 setOperationAction(ISD::SADDO, VT, Custom);
1103 setOperationAction(ISD::UADDO, VT, Custom);
1104 setOperationAction(ISD::SSUBO, VT, Custom);
1105 setOperationAction(ISD::USUBO, VT, Custom);
1106 setOperationAction(ISD::SMULO, VT, Custom);
1107 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001108 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001109
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001110 // There are no 8-bit 3-address imul/mul instructions
1111 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1112 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001113
Evan Chengd54f2d52009-03-31 19:38:51 +00001114 if (!Subtarget->is64Bit()) {
1115 // These libcalls are not available in 32-bit.
1116 setLibcallName(RTLIB::SHL_I128, 0);
1117 setLibcallName(RTLIB::SRL_I128, 0);
1118 setLibcallName(RTLIB::SRA_I128, 0);
1119 }
1120
Evan Cheng206ee9d2006-07-07 08:33:52 +00001121 // We have target-specific dag combine patterns for the following nodes:
1122 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001123 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +00001124 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +00001125 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001126 setTargetDAGCombine(ISD::SHL);
1127 setTargetDAGCombine(ISD::SRA);
1128 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001129 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001130 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001131 setTargetDAGCombine(ISD::ADD);
1132 setTargetDAGCombine(ISD::SUB);
Chris Lattner149a4e52008-02-22 02:09:43 +00001133 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001134 setTargetDAGCombine(ISD::ZERO_EXTEND);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001135 setTargetDAGCombine(ISD::SINT_TO_FP);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001136 if (Subtarget->is64Bit())
1137 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001138
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001139 computeRegisterProperties();
1140
Evan Cheng05219282011-01-06 06:52:41 +00001141 // On Darwin, -Os means optimize for size without hurting performance,
1142 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001143 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001144 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001145 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001146 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1147 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1148 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengfb8075d2008-02-28 00:43:03 +00001149 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001150 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001151
1152 setPrefFunctionAlignment(4);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001153}
1154
Scott Michel5b8f82e2008-03-10 15:42:14 +00001155
Duncan Sands28b77e92011-09-06 19:07:46 +00001156EVT X86TargetLowering::getSetCCResultType(EVT VT) const {
1157 if (!VT.isVector()) return MVT::i8;
1158 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +00001159}
1160
1161
Evan Cheng29286502008-01-23 23:17:41 +00001162/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1163/// the desired ByVal argument alignment.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001164static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
Evan Cheng29286502008-01-23 23:17:41 +00001165 if (MaxAlign == 16)
1166 return;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001167 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001168 if (VTy->getBitWidth() == 128)
1169 MaxAlign = 16;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001170 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001171 unsigned EltAlign = 0;
1172 getMaxByValAlign(ATy->getElementType(), EltAlign);
1173 if (EltAlign > MaxAlign)
1174 MaxAlign = EltAlign;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001175 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001176 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1177 unsigned EltAlign = 0;
1178 getMaxByValAlign(STy->getElementType(i), EltAlign);
1179 if (EltAlign > MaxAlign)
1180 MaxAlign = EltAlign;
1181 if (MaxAlign == 16)
1182 break;
1183 }
1184 }
1185 return;
1186}
1187
1188/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1189/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001190/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1191/// are at 4-byte boundaries.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001192unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001193 if (Subtarget->is64Bit()) {
1194 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001195 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001196 if (TyAlign > 8)
1197 return TyAlign;
1198 return 8;
1199 }
1200
Evan Cheng29286502008-01-23 23:17:41 +00001201 unsigned Align = 4;
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001202 if (Subtarget->hasXMM())
Dale Johannesen0c191872008-02-08 19:48:20 +00001203 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001204 return Align;
1205}
Chris Lattner2b02a442007-02-25 08:29:00 +00001206
Evan Chengf0df0312008-05-15 08:39:06 +00001207/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001208/// and store operations as a result of memset, memcpy, and memmove
1209/// lowering. If DstAlign is zero that means it's safe to destination
1210/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1211/// means there isn't a need to check it against alignment requirement,
1212/// probably because the source does not need to be loaded. If
1213/// 'NonScalarIntSafe' is true, that means it's safe to return a
1214/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1215/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1216/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001217/// It returns EVT::Other if the type should be determined using generic
1218/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001219EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001220X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1221 unsigned DstAlign, unsigned SrcAlign,
Evan Chengf28f8bc2010-04-02 19:36:14 +00001222 bool NonScalarIntSafe,
Evan Chengc3b0c342010-04-08 07:37:57 +00001223 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001224 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001225 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1226 // linux. This is because the stack realignment code can't handle certain
1227 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001228 const Function *F = MF.getFunction();
Evan Chenga5e13622011-01-07 19:35:30 +00001229 if (NonScalarIntSafe &&
1230 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001231 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001232 (Subtarget->isUnalignedMemAccessFast() ||
1233 ((DstAlign == 0 || DstAlign >= 16) &&
1234 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001235 Subtarget->getStackAlignment() >= 16) {
1236 if (Subtarget->hasSSE2())
1237 return MVT::v4i32;
Evan Chengf28f8bc2010-04-02 19:36:14 +00001238 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001239 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001240 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001241 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001242 Subtarget->getStackAlignment() >= 8 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001243 Subtarget->hasXMMInt()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001244 // Do not use f64 to lower memcpy if source is string constant. It's
1245 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001246 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001247 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001248 }
Evan Chengf0df0312008-05-15 08:39:06 +00001249 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001250 return MVT::i64;
1251 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001252}
1253
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001254/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1255/// current function. The returned value is a member of the
1256/// MachineJumpTableInfo::JTEntryKind enum.
1257unsigned X86TargetLowering::getJumpTableEncoding() const {
1258 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1259 // symbol.
1260 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1261 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001262 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001263
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001264 // Otherwise, use the normal jump table encoding heuristics.
1265 return TargetLowering::getJumpTableEncoding();
1266}
1267
Chris Lattnerc64daab2010-01-26 05:02:42 +00001268const MCExpr *
1269X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1270 const MachineBasicBlock *MBB,
1271 unsigned uid,MCContext &Ctx) const{
1272 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1273 Subtarget->isPICStyleGOT());
1274 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1275 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001276 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1277 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001278}
1279
Evan Chengcc415862007-11-09 01:32:10 +00001280/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1281/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001282SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001283 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001284 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001285 // This doesn't have DebugLoc associated with it, but is not really the
1286 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001287 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001288 return Table;
1289}
1290
Chris Lattner589c6f62010-01-26 06:28:43 +00001291/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1292/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1293/// MCExpr.
1294const MCExpr *X86TargetLowering::
1295getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1296 MCContext &Ctx) const {
1297 // X86-64 uses RIP relative addressing based on the jump table label.
1298 if (Subtarget->isPICStyleRIPRel())
1299 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1300
1301 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001302 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001303}
1304
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001305// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001306std::pair<const TargetRegisterClass*, uint8_t>
1307X86TargetLowering::findRepresentativeClass(EVT VT) const{
1308 const TargetRegisterClass *RRC = 0;
1309 uint8_t Cost = 1;
1310 switch (VT.getSimpleVT().SimpleTy) {
1311 default:
1312 return TargetLowering::findRepresentativeClass(VT);
1313 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
1314 RRC = (Subtarget->is64Bit()
1315 ? X86::GR64RegisterClass : X86::GR32RegisterClass);
1316 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001317 case MVT::x86mmx:
Evan Chengdee81012010-07-26 21:50:05 +00001318 RRC = X86::VR64RegisterClass;
1319 break;
1320 case MVT::f32: case MVT::f64:
1321 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1322 case MVT::v4f32: case MVT::v2f64:
1323 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1324 case MVT::v4f64:
1325 RRC = X86::VR128RegisterClass;
1326 break;
1327 }
1328 return std::make_pair(RRC, Cost);
1329}
1330
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001331bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1332 unsigned &Offset) const {
1333 if (!Subtarget->isTargetLinux())
1334 return false;
1335
1336 if (Subtarget->is64Bit()) {
1337 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1338 Offset = 0x28;
1339 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1340 AddressSpace = 256;
1341 else
1342 AddressSpace = 257;
1343 } else {
1344 // %gs:0x14 on i386
1345 Offset = 0x14;
1346 AddressSpace = 256;
1347 }
1348 return true;
1349}
1350
1351
Chris Lattner2b02a442007-02-25 08:29:00 +00001352//===----------------------------------------------------------------------===//
1353// Return Value Calling Convention Implementation
1354//===----------------------------------------------------------------------===//
1355
Chris Lattner59ed56b2007-02-28 04:55:35 +00001356#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001357
Michael J. Spencerec38de22010-10-10 22:04:20 +00001358bool
Eric Christopher471e4222011-06-08 23:55:35 +00001359X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
1360 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001361 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001362 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001363 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001364 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001365 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001366 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001367}
1368
Dan Gohman98ca4f22009-08-05 01:29:28 +00001369SDValue
1370X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001371 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001372 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001373 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001374 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001375 MachineFunction &MF = DAG.getMachineFunction();
1376 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001377
Chris Lattner9774c912007-02-27 05:28:59 +00001378 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001379 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001380 RVLocs, *DAG.getContext());
1381 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001382
Evan Chengdcea1632010-02-04 02:40:39 +00001383 // Add the regs to the liveout set for the function.
1384 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1385 for (unsigned i = 0; i != RVLocs.size(); ++i)
1386 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1387 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001388
Dan Gohman475871a2008-07-27 21:46:04 +00001389 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001390
Dan Gohman475871a2008-07-27 21:46:04 +00001391 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001392 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1393 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001394 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1395 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001396
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001397 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001398 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1399 CCValAssign &VA = RVLocs[i];
1400 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001401 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001402 EVT ValVT = ValToCopy.getValueType();
1403
Dale Johannesenc4510512010-09-24 19:05:48 +00001404 // If this is x86-64, and we disabled SSE, we can't return FP values,
1405 // or SSE or MMX vectors.
1406 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1407 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001408 (Subtarget->is64Bit() && !Subtarget->hasXMM())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001409 report_fatal_error("SSE register return with SSE disabled");
1410 }
1411 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1412 // llvm-gcc has never done it right and no one has noticed, so this
1413 // should be OK for now.
1414 if (ValVT == MVT::f64 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001415 (Subtarget->is64Bit() && !Subtarget->hasXMMInt()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001416 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001417
Chris Lattner447ff682008-03-11 03:23:40 +00001418 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1419 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001420 if (VA.getLocReg() == X86::ST0 ||
1421 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001422 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1423 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001424 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001425 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001426 RetOps.push_back(ValToCopy);
1427 // Don't emit a copytoreg.
1428 continue;
1429 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001430
Evan Cheng242b38b2009-02-23 09:03:22 +00001431 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1432 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001433 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001434 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001435 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001436 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001437 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1438 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001439 // If we don't have SSE2 available, convert to v4f32 so the generated
1440 // register is legal.
1441 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001442 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001443 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001444 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001445 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001446
Dale Johannesendd64c412009-02-04 00:33:20 +00001447 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001448 Flag = Chain.getValue(1);
1449 }
Dan Gohman61a92132008-04-21 23:59:07 +00001450
1451 // The x86-64 ABI for returning structs by value requires that we copy
1452 // the sret argument into %rax for the return. We saved the argument into
1453 // a virtual register in the entry block, so now we copy the value out
1454 // and into %rax.
1455 if (Subtarget->is64Bit() &&
1456 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1457 MachineFunction &MF = DAG.getMachineFunction();
1458 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1459 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001460 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001461 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001462 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001463
Dale Johannesendd64c412009-02-04 00:33:20 +00001464 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001465 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001466
1467 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001468 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001469 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001470
Chris Lattner447ff682008-03-11 03:23:40 +00001471 RetOps[0] = Chain; // Update chain.
1472
1473 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001474 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001475 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001476
1477 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001478 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001479}
1480
Evan Cheng3d2125c2010-11-30 23:55:39 +00001481bool X86TargetLowering::isUsedByReturnOnly(SDNode *N) const {
1482 if (N->getNumValues() != 1)
1483 return false;
1484 if (!N->hasNUsesOfValue(1, 0))
1485 return false;
1486
1487 SDNode *Copy = *N->use_begin();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001488 if (Copy->getOpcode() != ISD::CopyToReg &&
1489 Copy->getOpcode() != ISD::FP_EXTEND)
Evan Cheng3d2125c2010-11-30 23:55:39 +00001490 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001491
1492 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001493 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001494 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001495 if (UI->getOpcode() != X86ISD::RET_FLAG)
1496 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001497 HasRet = true;
1498 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001499
Evan Cheng1bf891a2010-12-01 22:59:46 +00001500 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001501}
1502
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001503EVT
1504X86TargetLowering::getTypeForExtArgOrReturn(LLVMContext &Context, EVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001505 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001506 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001507 // TODO: Is this also valid on 32-bit?
1508 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001509 ReturnMVT = MVT::i8;
1510 else
1511 ReturnMVT = MVT::i32;
1512
1513 EVT MinVT = getRegisterType(Context, ReturnMVT);
1514 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001515}
1516
Dan Gohman98ca4f22009-08-05 01:29:28 +00001517/// LowerCallResult - Lower the result values of a call into the
1518/// appropriate copies out of appropriate physical registers.
1519///
1520SDValue
1521X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001522 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001523 const SmallVectorImpl<ISD::InputArg> &Ins,
1524 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001525 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001526
Chris Lattnere32bbf62007-02-28 07:09:55 +00001527 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001528 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001529 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001530 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1531 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001532 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001533
Chris Lattner3085e152007-02-25 08:59:22 +00001534 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001535 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001536 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001537 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001538
Torok Edwin3f142c32009-02-01 18:15:56 +00001539 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001540 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001541 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasXMM())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001542 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001543 }
1544
Evan Cheng79fb3b42009-02-20 20:43:02 +00001545 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001546
1547 // If this is a call to a function that returns an fp value on the floating
1548 // point stack, we must guarantee the the value is popped from the stack, so
1549 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001550 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001551 // instead.
1552 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1553 // If we prefer to use the value in xmm registers, copy it out as f80 and
1554 // use a truncate to move it from fp stack reg to xmm reg.
1555 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001556 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001557 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1558 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001559 Val = Chain.getValue(0);
1560
1561 // Round the f80 to the right size, which also moves it to the appropriate
1562 // xmm register.
1563 if (CopyVT != VA.getValVT())
1564 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1565 // This truncation won't change the value.
1566 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001567 } else {
1568 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1569 CopyVT, InFlag).getValue(1);
1570 Val = Chain.getValue(0);
1571 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001572 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001573 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001574 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001575
Dan Gohman98ca4f22009-08-05 01:29:28 +00001576 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001577}
1578
1579
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001580//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001581// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001582//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001583// StdCall calling convention seems to be standard for many Windows' API
1584// routines and around. It differs from C calling convention just a little:
1585// callee should clean up the stack, not caller. Symbols should be also
1586// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001587// For info on fast calling convention see Fast Calling Convention (tail call)
1588// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001589
Dan Gohman98ca4f22009-08-05 01:29:28 +00001590/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001591/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001592static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1593 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001594 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001595
Dan Gohman98ca4f22009-08-05 01:29:28 +00001596 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001597}
1598
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001599/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001600/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001601static bool
1602ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1603 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001604 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001605
Dan Gohman98ca4f22009-08-05 01:29:28 +00001606 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001607}
1608
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001609/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1610/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001611/// the specific parameter attribute. The copy will be passed as a byval
1612/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001613static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001614CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001615 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1616 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001617 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001618
Dale Johannesendd64c412009-02-04 00:33:20 +00001619 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001620 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001621 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001622}
1623
Chris Lattner29689432010-03-11 00:22:57 +00001624/// IsTailCallConvention - Return true if the calling convention is one that
1625/// supports tail call optimization.
1626static bool IsTailCallConvention(CallingConv::ID CC) {
1627 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1628}
1629
Evan Cheng485fafc2011-03-21 01:19:09 +00001630bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1631 if (!CI->isTailCall())
1632 return false;
1633
1634 CallSite CS(CI);
1635 CallingConv::ID CalleeCC = CS.getCallingConv();
1636 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1637 return false;
1638
1639 return true;
1640}
1641
Evan Cheng0c439eb2010-01-27 00:07:07 +00001642/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1643/// a tailcall target by changing its ABI.
1644static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001645 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001646}
1647
Dan Gohman98ca4f22009-08-05 01:29:28 +00001648SDValue
1649X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001650 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001651 const SmallVectorImpl<ISD::InputArg> &Ins,
1652 DebugLoc dl, SelectionDAG &DAG,
1653 const CCValAssign &VA,
1654 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001655 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001656 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001657 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001658 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001659 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001660 EVT ValVT;
1661
1662 // If value is passed by pointer we have address passed instead of the value
1663 // itself.
1664 if (VA.getLocInfo() == CCValAssign::Indirect)
1665 ValVT = VA.getLocVT();
1666 else
1667 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001668
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001669 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001670 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001671 // In case of tail call optimization mark all arguments mutable. Since they
1672 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001673 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001674 unsigned Bytes = Flags.getByValSize();
1675 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1676 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001677 return DAG.getFrameIndex(FI, getPointerTy());
1678 } else {
1679 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001680 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001681 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1682 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001683 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00001684 false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001685 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001686}
1687
Dan Gohman475871a2008-07-27 21:46:04 +00001688SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001689X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001690 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001691 bool isVarArg,
1692 const SmallVectorImpl<ISD::InputArg> &Ins,
1693 DebugLoc dl,
1694 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001695 SmallVectorImpl<SDValue> &InVals)
1696 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001697 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001698 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001699
Gordon Henriksen86737662008-01-05 16:56:59 +00001700 const Function* Fn = MF.getFunction();
1701 if (Fn->hasExternalLinkage() &&
1702 Subtarget->isTargetCygMing() &&
1703 Fn->getName() == "main")
1704 FuncInfo->setForceFramePointer(true);
1705
Evan Cheng1bc78042006-04-26 01:20:17 +00001706 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001707 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001708 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001709
Chris Lattner29689432010-03-11 00:22:57 +00001710 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1711 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001712
Chris Lattner638402b2007-02-28 07:00:42 +00001713 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001714 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001715 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001716 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001717
1718 // Allocate shadow area for Win64
1719 if (IsWin64) {
1720 CCInfo.AllocateStack(32, 8);
1721 }
1722
Duncan Sands45907662010-10-31 13:21:44 +00001723 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001724
Chris Lattnerf39f7712007-02-28 05:46:49 +00001725 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001726 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001727 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1728 CCValAssign &VA = ArgLocs[i];
1729 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1730 // places.
1731 assert(VA.getValNo() != LastVal &&
1732 "Don't support value assigned to multiple locs yet");
1733 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001734
Chris Lattnerf39f7712007-02-28 05:46:49 +00001735 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001736 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001737 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001738 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001739 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001740 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001741 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001742 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001743 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001744 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001745 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001746 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1747 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001748 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001749 RC = X86::VR128RegisterClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001750 else if (RegVT == MVT::x86mmx)
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001751 RC = X86::VR64RegisterClass;
1752 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001753 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001754
Devang Patel68e6bee2011-02-21 23:21:26 +00001755 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001756 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001757
Chris Lattnerf39f7712007-02-28 05:46:49 +00001758 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1759 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1760 // right size.
1761 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001762 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001763 DAG.getValueType(VA.getValVT()));
1764 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001765 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001766 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001767 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001768 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001769
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001770 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001771 // Handle MMX values passed in XMM regs.
1772 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001773 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1774 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001775 } else
1776 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001777 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001778 } else {
1779 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001780 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001781 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001782
1783 // If value is passed via pointer - do a load.
1784 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001785 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
1786 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001787
Dan Gohman98ca4f22009-08-05 01:29:28 +00001788 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001789 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001790
Dan Gohman61a92132008-04-21 23:59:07 +00001791 // The x86-64 ABI for returning structs by value requires that we copy
1792 // the sret argument into %rax for the return. Save the argument into
1793 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001794 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001795 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1796 unsigned Reg = FuncInfo->getSRetReturnReg();
1797 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001798 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001799 FuncInfo->setSRetReturnReg(Reg);
1800 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001801 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001802 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001803 }
1804
Chris Lattnerf39f7712007-02-28 05:46:49 +00001805 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001806 // Align stack specially for tail calls.
1807 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001808 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001809
Evan Cheng1bc78042006-04-26 01:20:17 +00001810 // If the function takes variable number of arguments, make a frame index for
1811 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001812 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001813 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1814 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001815 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001816 }
1817 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001818 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1819
1820 // FIXME: We should really autogenerate these arrays
1821 static const unsigned GPR64ArgRegsWin64[] = {
1822 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001823 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001824 static const unsigned GPR64ArgRegs64Bit[] = {
1825 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1826 };
1827 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001828 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1829 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1830 };
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001831 const unsigned *GPR64ArgRegs;
1832 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001833
1834 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001835 // The XMM registers which might contain var arg parameters are shadowed
1836 // in their paired GPR. So we only need to save the GPR to their home
1837 // slots.
1838 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001839 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001840 } else {
1841 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1842 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001843
1844 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit, TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001845 }
1846 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1847 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001848
Devang Patel578efa92009-06-05 21:57:13 +00001849 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001850 assert(!(NumXMMRegs && !Subtarget->hasXMM()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001851 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001852 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001853 "SSE register cannot be used when SSE is disabled!");
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001854 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasXMM())
Torok Edwin3f142c32009-02-01 18:15:56 +00001855 // Kernel mode asks for SSE to be disabled, so don't push them
1856 // on the stack.
1857 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001858
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001859 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001860 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001861 // Get to the caller-allocated home save location. Add 8 to account
1862 // for the return address.
1863 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001864 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001865 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001866 // Fixup to set vararg frame on shadow area (4 x i64).
1867 if (NumIntRegs < 4)
1868 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001869 } else {
1870 // For X86-64, if there are vararg parameters that are passed via
1871 // registers, then we must store them to their spots on the stack so they
1872 // may be loaded by deferencing the result of va_next.
1873 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1874 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1875 FuncInfo->setRegSaveFrameIndex(
1876 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00001877 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001878 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001879
Gordon Henriksen86737662008-01-05 16:56:59 +00001880 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001881 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001882 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1883 getPointerTy());
1884 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001885 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001886 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1887 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001888 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001889 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001890 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001891 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001892 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001893 MachinePointerInfo::getFixedStack(
1894 FuncInfo->getRegSaveFrameIndex(), Offset),
1895 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001896 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001897 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001898 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001899
Dan Gohmanface41a2009-08-16 21:24:25 +00001900 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1901 // Now store the XMM (fp + vector) parameter registers.
1902 SmallVector<SDValue, 11> SaveXMMOps;
1903 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001904
Devang Patel68e6bee2011-02-21 23:21:26 +00001905 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001906 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1907 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001908
Dan Gohman1e93df62010-04-17 14:41:14 +00001909 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1910 FuncInfo->getRegSaveFrameIndex()));
1911 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1912 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001913
Dan Gohmanface41a2009-08-16 21:24:25 +00001914 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001915 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001916 X86::VR128RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001917 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1918 SaveXMMOps.push_back(Val);
1919 }
1920 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1921 MVT::Other,
1922 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001923 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001924
1925 if (!MemOps.empty())
1926 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1927 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001928 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001929 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001930
Gordon Henriksen86737662008-01-05 16:56:59 +00001931 // Some CCs need callee pop.
Evan Chengef41ff62011-06-23 17:54:54 +00001932 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg, GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001933 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001934 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001935 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001936 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001937 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001938 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001939 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001940
Gordon Henriksen86737662008-01-05 16:56:59 +00001941 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001942 // RegSaveFrameIndex is X86-64 only.
1943 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001944 if (CallConv == CallingConv::X86_FastCall ||
1945 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001946 // fastcc functions can't have varargs.
1947 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001948 }
Evan Cheng25caf632006-05-23 21:06:34 +00001949
Rafael Espindola76927d752011-08-30 19:39:58 +00001950 FuncInfo->setArgumentStackSize(StackSize);
1951
Dan Gohman98ca4f22009-08-05 01:29:28 +00001952 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001953}
1954
Dan Gohman475871a2008-07-27 21:46:04 +00001955SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001956X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1957 SDValue StackPtr, SDValue Arg,
1958 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001959 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001960 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001961 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001962 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001963 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001964 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00001965 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001966
1967 return DAG.getStore(Chain, dl, Arg, PtrOff,
1968 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00001969 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001970}
1971
Bill Wendling64e87322009-01-16 19:25:27 +00001972/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001973/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001974SDValue
1975X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001976 SDValue &OutRetAddr, SDValue Chain,
1977 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00001978 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001979 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001980 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001981 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001982
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001983 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00001984 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
1985 false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001986 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001987}
1988
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001989/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001990/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001991static SDValue
1992EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001993 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001994 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001995 // Store the return address to the appropriate stack slot.
1996 if (!FPDiff) return Chain;
1997 // Calculate the new stack slot for the return address.
1998 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001999 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00002000 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00002001 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00002002 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002003 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00002004 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00002005 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002006 return Chain;
2007}
2008
Dan Gohman98ca4f22009-08-05 01:29:28 +00002009SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00002010X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002011 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00002012 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002013 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002014 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002015 const SmallVectorImpl<ISD::InputArg> &Ins,
2016 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002017 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002018 MachineFunction &MF = DAG.getMachineFunction();
2019 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002020 bool IsWin64 = Subtarget->isTargetWin64();
Dan Gohman98ca4f22009-08-05 01:29:28 +00002021 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00002022 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002023
Evan Cheng5f941932010-02-05 02:21:12 +00002024 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002025 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00002026 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
2027 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00002028 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00002029
2030 // Sibcalls are automatically detected tailcalls which do not require
2031 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00002032 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002033 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002034
2035 if (isTailCall)
2036 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002037 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002038
Chris Lattner29689432010-03-11 00:22:57 +00002039 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
2040 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002041
Chris Lattner638402b2007-02-28 07:00:42 +00002042 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002043 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002044 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002045 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002046
2047 // Allocate shadow area for Win64
2048 if (IsWin64) {
2049 CCInfo.AllocateStack(32, 8);
2050 }
2051
Duncan Sands45907662010-10-31 13:21:44 +00002052 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002053
Chris Lattner423c5f42007-02-28 05:31:48 +00002054 // Get a count of how many bytes are to be pushed on the stack.
2055 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002056 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002057 // This is a sibcall. The memory operands are available in caller's
2058 // own caller's stack.
2059 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00002060 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002061 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002062
Gordon Henriksen86737662008-01-05 16:56:59 +00002063 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002064 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002065 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00002066 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00002067 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2068 FPDiff = NumBytesCallerPushed - NumBytes;
2069
2070 // Set the delta of movement of the returnaddr stackslot.
2071 // But only set if delta is greater than previous delta.
2072 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2073 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2074 }
2075
Evan Chengf22f9b32010-02-06 03:28:46 +00002076 if (!IsSibcall)
2077 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002078
Dan Gohman475871a2008-07-27 21:46:04 +00002079 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002080 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002081 if (isTailCall && FPDiff)
2082 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2083 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002084
Dan Gohman475871a2008-07-27 21:46:04 +00002085 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2086 SmallVector<SDValue, 8> MemOpChains;
2087 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002088
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002089 // Walk the register/memloc assignments, inserting copies/loads. In the case
2090 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002091 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2092 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002093 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002094 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002095 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002096 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002097
Chris Lattner423c5f42007-02-28 05:31:48 +00002098 // Promote the value if needed.
2099 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002100 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002101 case CCValAssign::Full: break;
2102 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002103 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002104 break;
2105 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002106 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002107 break;
2108 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002109 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
2110 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002111 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002112 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2113 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002114 } else
2115 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2116 break;
2117 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002118 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002119 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002120 case CCValAssign::Indirect: {
2121 // Store the argument.
2122 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002123 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002124 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002125 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002126 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002127 Arg = SpillSlot;
2128 break;
2129 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002130 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002131
Chris Lattner423c5f42007-02-28 05:31:48 +00002132 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002133 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2134 if (isVarArg && IsWin64) {
2135 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2136 // shadow reg if callee is a varargs function.
2137 unsigned ShadowReg = 0;
2138 switch (VA.getLocReg()) {
2139 case X86::XMM0: ShadowReg = X86::RCX; break;
2140 case X86::XMM1: ShadowReg = X86::RDX; break;
2141 case X86::XMM2: ShadowReg = X86::R8; break;
2142 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002143 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002144 if (ShadowReg)
2145 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002146 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002147 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002148 assert(VA.isMemLoc());
2149 if (StackPtr.getNode() == 0)
2150 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2151 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2152 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002153 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002154 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002155
Evan Cheng32fe1032006-05-25 00:59:30 +00002156 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002157 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002158 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002159
Evan Cheng347d5f72006-04-28 21:29:37 +00002160 // Build a sequence of copy-to-reg nodes chained together with token chain
2161 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002162 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002163 // Tail call byval lowering might overwrite argument registers so in case of
2164 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002165 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002166 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002167 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002168 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002169 InFlag = Chain.getValue(1);
2170 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002171
Chris Lattner88e1fd52009-07-09 04:24:46 +00002172 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002173 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2174 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002175 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002176 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2177 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00002178 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002179 InFlag);
2180 InFlag = Chain.getValue(1);
2181 } else {
2182 // If we are tail calling and generating PIC/GOT style code load the
2183 // address of the callee into ECX. The value in ecx is used as target of
2184 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2185 // for tail calls on PIC/GOT architectures. Normally we would just put the
2186 // address of GOT into ebx and then call target@PLT. But for tail calls
2187 // ebx would be restored (since ebx is callee saved) before jumping to the
2188 // target@PLT.
2189
2190 // Note: The actual moving to ECX is done further down.
2191 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2192 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2193 !G->getGlobal()->hasProtectedVisibility())
2194 Callee = LowerGlobalAddress(Callee, DAG);
2195 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002196 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002197 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002198 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002199
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002200 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002201 // From AMD64 ABI document:
2202 // For calls that may call functions that use varargs or stdargs
2203 // (prototype-less calls or calls to functions containing ellipsis (...) in
2204 // the declaration) %al is used as hidden argument to specify the number
2205 // of SSE registers used. The contents of %al do not need to match exactly
2206 // the number of registers, but must be an ubound on the number of SSE
2207 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002208
Gordon Henriksen86737662008-01-05 16:56:59 +00002209 // Count the number of XMM registers allocated.
2210 static const unsigned XMMArgRegs[] = {
2211 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2212 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2213 };
2214 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00002215 assert((Subtarget->hasXMM() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002216 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002217
Dale Johannesendd64c412009-02-04 00:33:20 +00002218 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002219 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002220 InFlag = Chain.getValue(1);
2221 }
2222
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002223
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002224 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002225 if (isTailCall) {
2226 // Force all the incoming stack arguments to be loaded from the stack
2227 // before any new outgoing arguments are stored to the stack, because the
2228 // outgoing stack slots may alias the incoming argument stack slots, and
2229 // the alias isn't otherwise explicit. This is slightly more conservative
2230 // than necessary, because it means that each store effectively depends
2231 // on every argument instead of just those arguments it would clobber.
2232 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2233
Dan Gohman475871a2008-07-27 21:46:04 +00002234 SmallVector<SDValue, 8> MemOpChains2;
2235 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002236 int FI = 0;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002237 // Do not flag preceding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002238 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002239 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002240 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2241 CCValAssign &VA = ArgLocs[i];
2242 if (VA.isRegLoc())
2243 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002244 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002245 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002246 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002247 // Create frame index.
2248 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002249 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002250 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002251 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002252
Duncan Sands276dcbd2008-03-21 09:14:45 +00002253 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002254 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002255 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002256 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002257 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002258 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002259 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002260
Dan Gohman98ca4f22009-08-05 01:29:28 +00002261 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2262 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002263 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002264 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002265 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002266 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002267 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002268 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002269 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002270 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002271 }
2272 }
2273
2274 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002275 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002276 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002277
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002278 // Copy arguments to their registers.
2279 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002280 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002281 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002282 InFlag = Chain.getValue(1);
2283 }
Dan Gohman475871a2008-07-27 21:46:04 +00002284 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002285
Gordon Henriksen86737662008-01-05 16:56:59 +00002286 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002287 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002288 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002289 }
2290
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002291 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2292 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2293 // In the 64-bit large code model, we have to make all calls
2294 // through a register, since the call instruction's 32-bit
2295 // pc-relative offset may not be large enough to hold the whole
2296 // address.
2297 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002298 // If the callee is a GlobalAddress node (quite common, every direct call
2299 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2300 // it.
2301
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002302 // We should use extra load for direct calls to dllimported functions in
2303 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002304 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002305 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002306 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002307 bool ExtraLoad = false;
2308 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002309
Chris Lattner48a7d022009-07-09 05:02:21 +00002310 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2311 // external symbols most go through the PLT in PIC mode. If the symbol
2312 // has hidden or protected visibility, or if it is static or local, then
2313 // we don't need to use the PLT - we can directly call it.
2314 if (Subtarget->isTargetELF() &&
2315 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002316 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002317 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002318 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002319 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002320 (!Subtarget->getTargetTriple().isMacOSX() ||
2321 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002322 // PC-relative references to external symbols should go through $stub,
2323 // unless we're building with the leopard linker or later, which
2324 // automatically synthesizes these stubs.
2325 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002326 } else if (Subtarget->isPICStyleRIPRel() &&
2327 isa<Function>(GV) &&
2328 cast<Function>(GV)->hasFnAttr(Attribute::NonLazyBind)) {
2329 // If the function is marked as non-lazy, generate an indirect call
2330 // which loads from the GOT directly. This avoids runtime overhead
2331 // at the cost of eager binding (and one extra byte of encoding).
2332 OpFlags = X86II::MO_GOTPCREL;
2333 WrapperKind = X86ISD::WrapperRIP;
2334 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002335 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002336
Devang Patel0d881da2010-07-06 22:08:15 +00002337 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002338 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002339
2340 // Add a wrapper if needed.
2341 if (WrapperKind != ISD::DELETED_NODE)
2342 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2343 // Add extra indirection if needed.
2344 if (ExtraLoad)
2345 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2346 MachinePointerInfo::getGOT(),
2347 false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002348 }
Bill Wendling056292f2008-09-16 21:48:12 +00002349 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002350 unsigned char OpFlags = 0;
2351
Evan Cheng1bf891a2010-12-01 22:59:46 +00002352 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2353 // external symbols should go through the PLT.
2354 if (Subtarget->isTargetELF() &&
2355 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2356 OpFlags = X86II::MO_PLT;
2357 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002358 (!Subtarget->getTargetTriple().isMacOSX() ||
2359 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002360 // PC-relative references to external symbols should go through $stub,
2361 // unless we're building with the leopard linker or later, which
2362 // automatically synthesizes these stubs.
2363 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002364 }
Eric Christopherfd179292009-08-27 18:07:15 +00002365
Chris Lattner48a7d022009-07-09 05:02:21 +00002366 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2367 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002368 }
2369
Chris Lattnerd96d0722007-02-25 06:40:16 +00002370 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002371 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002372 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002373
Evan Chengf22f9b32010-02-06 03:28:46 +00002374 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002375 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2376 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002377 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002378 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002379
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002380 Ops.push_back(Chain);
2381 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002382
Dan Gohman98ca4f22009-08-05 01:29:28 +00002383 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002384 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002385
Gordon Henriksen86737662008-01-05 16:56:59 +00002386 // Add argument registers to the end of the list so that they are known live
2387 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002388 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2389 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2390 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002391
Evan Cheng586ccac2008-03-18 23:36:35 +00002392 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002393 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002394 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2395
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002396 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002397 if (Is64Bit && isVarArg && !IsWin64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002398 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002399
Gabor Greifba36cb52008-08-28 21:40:38 +00002400 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002401 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002402
Dan Gohman98ca4f22009-08-05 01:29:28 +00002403 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002404 // We used to do:
2405 //// If this is the first return lowered for this function, add the regs
2406 //// to the liveout set for the function.
2407 // This isn't right, although it's probably harmless on x86; liveouts
2408 // should be computed from returns not tail calls. Consider a void
2409 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002410 return DAG.getNode(X86ISD::TC_RETURN, dl,
2411 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002412 }
2413
Dale Johannesenace16102009-02-03 19:33:06 +00002414 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002415 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002416
Chris Lattner2d297092006-05-23 18:50:38 +00002417 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002418 unsigned NumBytesForCalleeToPush;
Evan Chengef41ff62011-06-23 17:54:54 +00002419 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg, GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002420 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002421 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002422 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002423 // pops the hidden struct pointer, so we have to push it back.
2424 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002425 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002426 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002427 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002428
Gordon Henriksenae636f82008-01-03 16:47:34 +00002429 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002430 if (!IsSibcall) {
2431 Chain = DAG.getCALLSEQ_END(Chain,
2432 DAG.getIntPtrConstant(NumBytes, true),
2433 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2434 true),
2435 InFlag);
2436 InFlag = Chain.getValue(1);
2437 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002438
Chris Lattner3085e152007-02-25 08:59:22 +00002439 // Handle result values, copying them out of physregs into vregs that we
2440 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002441 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2442 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002443}
2444
Evan Cheng25ab6902006-09-08 06:48:29 +00002445
2446//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002447// Fast Calling Convention (tail call) implementation
2448//===----------------------------------------------------------------------===//
2449
2450// Like std call, callee cleans arguments, convention except that ECX is
2451// reserved for storing the tail called function address. Only 2 registers are
2452// free for argument passing (inreg). Tail call optimization is performed
2453// provided:
2454// * tailcallopt is enabled
2455// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002456// On X86_64 architecture with GOT-style position independent code only local
2457// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002458// To keep the stack aligned according to platform abi the function
2459// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2460// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002461// If a tail called function callee has more arguments than the caller the
2462// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002463// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002464// original REtADDR, but before the saved framepointer or the spilled registers
2465// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2466// stack layout:
2467// arg1
2468// arg2
2469// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002470// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002471// move area ]
2472// (possible EBP)
2473// ESI
2474// EDI
2475// local1 ..
2476
2477/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2478/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002479unsigned
2480X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2481 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002482 MachineFunction &MF = DAG.getMachineFunction();
2483 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002484 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002485 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002486 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002487 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002488 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002489 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2490 // Number smaller than 12 so just add the difference.
2491 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2492 } else {
2493 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002494 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002495 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002496 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002497 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002498}
2499
Evan Cheng5f941932010-02-05 02:21:12 +00002500/// MatchingStackOffset - Return true if the given stack call argument is
2501/// already available in the same position (relatively) of the caller's
2502/// incoming argument stack.
2503static
2504bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2505 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2506 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002507 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2508 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002509 if (Arg.getOpcode() == ISD::CopyFromReg) {
2510 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002511 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002512 return false;
2513 MachineInstr *Def = MRI->getVRegDef(VR);
2514 if (!Def)
2515 return false;
2516 if (!Flags.isByVal()) {
2517 if (!TII->isLoadFromStackSlot(Def, FI))
2518 return false;
2519 } else {
2520 unsigned Opcode = Def->getOpcode();
2521 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2522 Def->getOperand(1).isFI()) {
2523 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002524 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002525 } else
2526 return false;
2527 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002528 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2529 if (Flags.isByVal())
2530 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002531 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002532 // define @foo(%struct.X* %A) {
2533 // tail call @bar(%struct.X* byval %A)
2534 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002535 return false;
2536 SDValue Ptr = Ld->getBasePtr();
2537 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2538 if (!FINode)
2539 return false;
2540 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002541 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002542 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002543 FI = FINode->getIndex();
2544 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002545 } else
2546 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002547
Evan Cheng4cae1332010-03-05 08:38:04 +00002548 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002549 if (!MFI->isFixedObjectIndex(FI))
2550 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002551 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002552}
2553
Dan Gohman98ca4f22009-08-05 01:29:28 +00002554/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2555/// for tail call optimization. Targets which want to do tail call
2556/// optimization should implement this function.
2557bool
2558X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002559 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002560 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002561 bool isCalleeStructRet,
2562 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002563 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002564 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002565 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002566 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002567 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002568 CalleeCC != CallingConv::C)
2569 return false;
2570
Evan Cheng7096ae42010-01-29 06:45:59 +00002571 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002572 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002573 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002574 CallingConv::ID CallerCC = CallerF->getCallingConv();
2575 bool CCMatch = CallerCC == CalleeCC;
2576
Dan Gohman1797ed52010-02-08 20:27:50 +00002577 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002578 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002579 return true;
2580 return false;
2581 }
2582
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002583 // Look for obvious safe cases to perform tail call optimization that do not
2584 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002585
Evan Cheng2c12cb42010-03-26 16:26:03 +00002586 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2587 // emit a special epilogue.
2588 if (RegInfo->needsStackRealignment(MF))
2589 return false;
2590
Evan Chenga375d472010-03-15 18:54:48 +00002591 // Also avoid sibcall optimization if either caller or callee uses struct
2592 // return semantics.
2593 if (isCalleeStructRet || isCallerStructRet)
2594 return false;
2595
Chad Rosier2416da32011-06-24 21:15:36 +00002596 // An stdcall caller is expected to clean up its arguments; the callee
2597 // isn't going to do that.
2598 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2599 return false;
2600
Chad Rosier871f6642011-05-18 19:59:50 +00002601 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002602 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002603 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002604
2605 // Optimizing for varargs on Win64 is unlikely to be safe without
2606 // additional testing.
2607 if (Subtarget->isTargetWin64())
2608 return false;
2609
Chad Rosier871f6642011-05-18 19:59:50 +00002610 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002611 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2612 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002613
Chad Rosier871f6642011-05-18 19:59:50 +00002614 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2615 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2616 if (!ArgLocs[i].isRegLoc())
2617 return false;
2618 }
2619
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002620 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2621 // Therefore if it's not used by the call it is not safe to optimize this into
2622 // a sibcall.
2623 bool Unused = false;
2624 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2625 if (!Ins[i].Used) {
2626 Unused = true;
2627 break;
2628 }
2629 }
2630 if (Unused) {
2631 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002632 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
2633 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002634 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002635 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002636 CCValAssign &VA = RVLocs[i];
2637 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2638 return false;
2639 }
2640 }
2641
Evan Cheng13617962010-04-30 01:12:32 +00002642 // If the calling conventions do not match, then we'd better make sure the
2643 // results are returned in the same way as what the caller expects.
2644 if (!CCMatch) {
2645 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002646 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
2647 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002648 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2649
2650 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002651 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
2652 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002653 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2654
2655 if (RVLocs1.size() != RVLocs2.size())
2656 return false;
2657 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2658 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2659 return false;
2660 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2661 return false;
2662 if (RVLocs1[i].isRegLoc()) {
2663 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2664 return false;
2665 } else {
2666 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2667 return false;
2668 }
2669 }
2670 }
2671
Evan Chenga6bff982010-01-30 01:22:00 +00002672 // If the callee takes no arguments then go on to check the results of the
2673 // call.
2674 if (!Outs.empty()) {
2675 // Check if stack adjustment is needed. For now, do not do this if any
2676 // argument is passed on the stack.
2677 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002678 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2679 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002680
2681 // Allocate shadow area for Win64
2682 if (Subtarget->isTargetWin64()) {
2683 CCInfo.AllocateStack(32, 8);
2684 }
2685
Duncan Sands45907662010-10-31 13:21:44 +00002686 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002687 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002688 MachineFunction &MF = DAG.getMachineFunction();
2689 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2690 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002691
2692 // Check if the arguments are already laid out in the right way as
2693 // the caller's fixed stack objects.
2694 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002695 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2696 const X86InstrInfo *TII =
2697 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002698 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2699 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002700 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002701 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002702 if (VA.getLocInfo() == CCValAssign::Indirect)
2703 return false;
2704 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002705 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2706 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002707 return false;
2708 }
2709 }
2710 }
Evan Cheng9c044672010-05-29 01:35:22 +00002711
2712 // If the tailcall address may be in a register, then make sure it's
2713 // possible to register allocate for it. In 32-bit, the call address can
2714 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002715 // callee-saved registers are restored. These happen to be the same
2716 // registers used to pass 'inreg' arguments so watch out for those.
2717 if (!Subtarget->is64Bit() &&
2718 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002719 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002720 unsigned NumInRegs = 0;
2721 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2722 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002723 if (!VA.isRegLoc())
2724 continue;
2725 unsigned Reg = VA.getLocReg();
2726 switch (Reg) {
2727 default: break;
2728 case X86::EAX: case X86::EDX: case X86::ECX:
2729 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002730 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002731 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002732 }
2733 }
2734 }
Evan Chenga6bff982010-01-30 01:22:00 +00002735 }
Evan Chengb1712452010-01-27 06:25:16 +00002736
Evan Cheng86809cc2010-02-03 03:28:02 +00002737 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002738}
2739
Dan Gohman3df24e62008-09-03 23:12:08 +00002740FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002741X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2742 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002743}
2744
2745
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002746//===----------------------------------------------------------------------===//
2747// Other Lowering Hooks
2748//===----------------------------------------------------------------------===//
2749
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002750static bool MayFoldLoad(SDValue Op) {
2751 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2752}
2753
2754static bool MayFoldIntoStore(SDValue Op) {
2755 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2756}
2757
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002758static bool isTargetShuffle(unsigned Opcode) {
2759 switch(Opcode) {
2760 default: return false;
2761 case X86ISD::PSHUFD:
2762 case X86ISD::PSHUFHW:
2763 case X86ISD::PSHUFLW:
2764 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002765 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002766 case X86ISD::SHUFPS:
2767 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002768 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002769 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002770 case X86ISD::MOVLPS:
2771 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002772 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002773 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002774 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002775 case X86ISD::MOVSS:
2776 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002777 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002778 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002779 case X86ISD::VUNPCKLPSY:
2780 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002781 case X86ISD::PUNPCKLWD:
2782 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002783 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002784 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002785 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002786 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00002787 case X86ISD::VUNPCKHPSY:
2788 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002789 case X86ISD::PUNPCKHWD:
2790 case X86ISD::PUNPCKHBW:
2791 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002792 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00002793 case X86ISD::VPERMILPS:
2794 case X86ISD::VPERMILPSY:
2795 case X86ISD::VPERMILPD:
2796 case X86ISD::VPERMILPDY:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00002797 case X86ISD::VPERM2F128:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002798 return true;
2799 }
2800 return false;
2801}
2802
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002803static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002804 SDValue V1, SelectionDAG &DAG) {
2805 switch(Opc) {
2806 default: llvm_unreachable("Unknown x86 shuffle node");
2807 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002808 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002809 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002810 return DAG.getNode(Opc, dl, VT, V1);
2811 }
2812
2813 return SDValue();
2814}
2815
2816static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002817 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002818 switch(Opc) {
2819 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002820 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002821 case X86ISD::PSHUFHW:
2822 case X86ISD::PSHUFLW:
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00002823 case X86ISD::VPERMILPS:
2824 case X86ISD::VPERMILPSY:
2825 case X86ISD::VPERMILPD:
2826 case X86ISD::VPERMILPDY:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002827 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2828 }
2829
2830 return SDValue();
2831}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002832
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002833static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2834 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2835 switch(Opc) {
2836 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002837 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002838 case X86ISD::SHUFPD:
2839 case X86ISD::SHUFPS:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00002840 case X86ISD::VPERM2F128:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002841 return DAG.getNode(Opc, dl, VT, V1, V2,
2842 DAG.getConstant(TargetMask, MVT::i8));
2843 }
2844 return SDValue();
2845}
2846
2847static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2848 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2849 switch(Opc) {
2850 default: llvm_unreachable("Unknown x86 shuffle node");
2851 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002852 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002853 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002854 case X86ISD::MOVLPS:
2855 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002856 case X86ISD::MOVSS:
2857 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002858 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002859 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002860 case X86ISD::VUNPCKLPSY:
2861 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002862 case X86ISD::PUNPCKLWD:
2863 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002864 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002865 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002866 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002867 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00002868 case X86ISD::VUNPCKHPSY:
2869 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002870 case X86ISD::PUNPCKHWD:
2871 case X86ISD::PUNPCKHBW:
2872 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002873 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002874 return DAG.getNode(Opc, dl, VT, V1, V2);
2875 }
2876 return SDValue();
2877}
2878
Dan Gohmand858e902010-04-17 15:26:15 +00002879SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002880 MachineFunction &MF = DAG.getMachineFunction();
2881 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2882 int ReturnAddrIndex = FuncInfo->getRAIndex();
2883
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002884 if (ReturnAddrIndex == 0) {
2885 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002886 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002887 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002888 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002889 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002890 }
2891
Evan Cheng25ab6902006-09-08 06:48:29 +00002892 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002893}
2894
2895
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002896bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2897 bool hasSymbolicDisplacement) {
2898 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002899 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002900 return false;
2901
2902 // If we don't have a symbolic displacement - we don't have any extra
2903 // restrictions.
2904 if (!hasSymbolicDisplacement)
2905 return true;
2906
2907 // FIXME: Some tweaks might be needed for medium code model.
2908 if (M != CodeModel::Small && M != CodeModel::Kernel)
2909 return false;
2910
2911 // For small code model we assume that latest object is 16MB before end of 31
2912 // bits boundary. We may also accept pretty large negative constants knowing
2913 // that all objects are in the positive half of address space.
2914 if (M == CodeModel::Small && Offset < 16*1024*1024)
2915 return true;
2916
2917 // For kernel code model we know that all object resist in the negative half
2918 // of 32bits address space. We may not accept negative offsets, since they may
2919 // be just off and we may accept pretty large positive ones.
2920 if (M == CodeModel::Kernel && Offset > 0)
2921 return true;
2922
2923 return false;
2924}
2925
Evan Chengef41ff62011-06-23 17:54:54 +00002926/// isCalleePop - Determines whether the callee is required to pop its
2927/// own arguments. Callee pop is necessary to support tail calls.
2928bool X86::isCalleePop(CallingConv::ID CallingConv,
2929 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
2930 if (IsVarArg)
2931 return false;
2932
2933 switch (CallingConv) {
2934 default:
2935 return false;
2936 case CallingConv::X86_StdCall:
2937 return !is64Bit;
2938 case CallingConv::X86_FastCall:
2939 return !is64Bit;
2940 case CallingConv::X86_ThisCall:
2941 return !is64Bit;
2942 case CallingConv::Fast:
2943 return TailCallOpt;
2944 case CallingConv::GHC:
2945 return TailCallOpt;
2946 }
2947}
2948
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002949/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2950/// specific condition code, returning the condition code and the LHS/RHS of the
2951/// comparison to make.
2952static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2953 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002954 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002955 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2956 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2957 // X > -1 -> X == 0, jump !sign.
2958 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002959 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002960 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2961 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002962 return X86::COND_S;
Andrew Trickf6c39412011-03-23 23:11:02 +00002963 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002964 // X < 1 -> X <= 0
2965 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002966 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002967 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002968 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002969
Evan Chengd9558e02006-01-06 00:43:03 +00002970 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002971 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002972 case ISD::SETEQ: return X86::COND_E;
2973 case ISD::SETGT: return X86::COND_G;
2974 case ISD::SETGE: return X86::COND_GE;
2975 case ISD::SETLT: return X86::COND_L;
2976 case ISD::SETLE: return X86::COND_LE;
2977 case ISD::SETNE: return X86::COND_NE;
2978 case ISD::SETULT: return X86::COND_B;
2979 case ISD::SETUGT: return X86::COND_A;
2980 case ISD::SETULE: return X86::COND_BE;
2981 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002982 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002983 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002984
Chris Lattner4c78e022008-12-23 23:42:27 +00002985 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002986
Chris Lattner4c78e022008-12-23 23:42:27 +00002987 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00002988 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
2989 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00002990 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2991 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002992 }
2993
Chris Lattner4c78e022008-12-23 23:42:27 +00002994 switch (SetCCOpcode) {
2995 default: break;
2996 case ISD::SETOLT:
2997 case ISD::SETOLE:
2998 case ISD::SETUGT:
2999 case ISD::SETUGE:
3000 std::swap(LHS, RHS);
3001 break;
3002 }
3003
3004 // On a floating point condition, the flags are set as follows:
3005 // ZF PF CF op
3006 // 0 | 0 | 0 | X > Y
3007 // 0 | 0 | 1 | X < Y
3008 // 1 | 0 | 0 | X == Y
3009 // 1 | 1 | 1 | unordered
3010 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003011 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00003012 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003013 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00003014 case ISD::SETOLT: // flipped
3015 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003016 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00003017 case ISD::SETOLE: // flipped
3018 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003019 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003020 case ISD::SETUGT: // flipped
3021 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003022 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00003023 case ISD::SETUGE: // flipped
3024 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003025 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003026 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003027 case ISD::SETNE: return X86::COND_NE;
3028 case ISD::SETUO: return X86::COND_P;
3029 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003030 case ISD::SETOEQ:
3031 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003032 }
Evan Chengd9558e02006-01-06 00:43:03 +00003033}
3034
Evan Cheng4a460802006-01-11 00:33:36 +00003035/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3036/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003037/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003038static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003039 switch (X86CC) {
3040 default:
3041 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003042 case X86::COND_B:
3043 case X86::COND_BE:
3044 case X86::COND_E:
3045 case X86::COND_P:
3046 case X86::COND_A:
3047 case X86::COND_AE:
3048 case X86::COND_NE:
3049 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003050 return true;
3051 }
3052}
3053
Evan Chengeb2f9692009-10-27 19:56:55 +00003054/// isFPImmLegal - Returns true if the target can instruction select the
3055/// specified FP immediate natively. If false, the legalizer will
3056/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003057bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003058 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3059 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3060 return true;
3061 }
3062 return false;
3063}
3064
Nate Begeman9008ca62009-04-27 18:41:29 +00003065/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3066/// the specified range (L, H].
3067static bool isUndefOrInRange(int Val, int Low, int Hi) {
3068 return (Val < 0) || (Val >= Low && Val < Hi);
3069}
3070
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00003071/// isUndefOrInRange - Return true if every element in Mask, begining
3072/// from position Pos and ending in Pos+Size, falls within the specified
3073/// range (L, L+Pos]. or is undef.
3074static bool isUndefOrInRange(const SmallVectorImpl<int> &Mask,
3075 int Pos, int Size, int Low, int Hi) {
3076 for (int i = Pos, e = Pos+Size; i != e; ++i)
3077 if (!isUndefOrInRange(Mask[i], Low, Hi))
3078 return false;
3079 return true;
3080}
3081
Nate Begeman9008ca62009-04-27 18:41:29 +00003082/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3083/// specified value.
3084static bool isUndefOrEqual(int Val, int CmpVal) {
3085 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003086 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003087 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00003088}
3089
Bruno Cardoso Lopes4002d7e2011-08-12 21:54:42 +00003090/// isSequentialOrUndefInRange - Return true if every element in Mask, begining
3091/// from position Pos and ending in Pos+Size, falls within the specified
3092/// sequential range (L, L+Pos]. or is undef.
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003093static bool isSequentialOrUndefInRange(const SmallVectorImpl<int> &Mask,
3094 int Pos, int Size, int Low) {
3095 for (int i = Pos, e = Pos+Size; i != e; ++i, ++Low)
3096 if (!isUndefOrEqual(Mask[i], Low))
3097 return false;
3098 return true;
3099}
3100
Nate Begeman9008ca62009-04-27 18:41:29 +00003101/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3102/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3103/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00003104static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003105 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003106 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003107 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003108 return (Mask[0] < 2 && Mask[1] < 2);
3109 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003110}
3111
Nate Begeman9008ca62009-04-27 18:41:29 +00003112bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003113 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003114 N->getMask(M);
3115 return ::isPSHUFDMask(M, N->getValueType(0));
3116}
Evan Cheng0188ecb2006-03-22 18:59:22 +00003117
Nate Begeman9008ca62009-04-27 18:41:29 +00003118/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3119/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00003120static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003121 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00003122 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003123
Nate Begeman9008ca62009-04-27 18:41:29 +00003124 // Lower quadword copied in order or undef.
3125 for (int i = 0; i != 4; ++i)
3126 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00003127 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003128
Evan Cheng506d3df2006-03-29 23:07:14 +00003129 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003130 for (int i = 4; i != 8; ++i)
3131 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00003132 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003133
Evan Cheng506d3df2006-03-29 23:07:14 +00003134 return true;
3135}
3136
Nate Begeman9008ca62009-04-27 18:41:29 +00003137bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003138 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003139 N->getMask(M);
3140 return ::isPSHUFHWMask(M, N->getValueType(0));
3141}
Evan Cheng506d3df2006-03-29 23:07:14 +00003142
Nate Begeman9008ca62009-04-27 18:41:29 +00003143/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3144/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00003145static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003146 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00003147 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003148
Rafael Espindola15684b22009-04-24 12:40:33 +00003149 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00003150 for (int i = 4; i != 8; ++i)
3151 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00003152 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003153
Rafael Espindola15684b22009-04-24 12:40:33 +00003154 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003155 for (int i = 0; i != 4; ++i)
3156 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00003157 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003158
Rafael Espindola15684b22009-04-24 12:40:33 +00003159 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003160}
3161
Nate Begeman9008ca62009-04-27 18:41:29 +00003162bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003163 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003164 N->getMask(M);
3165 return ::isPSHUFLWMask(M, N->getValueType(0));
3166}
3167
Nate Begemana09008b2009-10-19 02:17:23 +00003168/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3169/// is suitable for input to PALIGNR.
3170static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
3171 bool hasSSSE3) {
3172 int i, e = VT.getVectorNumElements();
Bruno Cardoso Lopes9065d4b2011-07-29 01:30:59 +00003173 if (VT.getSizeInBits() != 128 && VT.getSizeInBits() != 64)
3174 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003175
Nate Begemana09008b2009-10-19 02:17:23 +00003176 // Do not handle v2i64 / v2f64 shuffles with palignr.
3177 if (e < 4 || !hasSSSE3)
3178 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003179
Nate Begemana09008b2009-10-19 02:17:23 +00003180 for (i = 0; i != e; ++i)
3181 if (Mask[i] >= 0)
3182 break;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003183
Nate Begemana09008b2009-10-19 02:17:23 +00003184 // All undef, not a palignr.
3185 if (i == e)
3186 return false;
3187
Eli Friedman63f8dde2011-07-25 21:36:45 +00003188 // Make sure we're shifting in the right direction.
3189 if (Mask[i] <= i)
3190 return false;
Nate Begemana09008b2009-10-19 02:17:23 +00003191
3192 int s = Mask[i] - i;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003193
Nate Begemana09008b2009-10-19 02:17:23 +00003194 // Check the rest of the elements to see if they are consecutive.
3195 for (++i; i != e; ++i) {
3196 int m = Mask[i];
Eli Friedman63f8dde2011-07-25 21:36:45 +00003197 if (m >= 0 && m != s+i)
Nate Begemana09008b2009-10-19 02:17:23 +00003198 return false;
3199 }
3200 return true;
3201}
3202
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003203/// isVSHUFPSYMask - Return true if the specified VECTOR_SHUFFLE operand
3204/// specifies a shuffle of elements that is suitable for input to 256-bit
3205/// VSHUFPSY.
3206static bool isVSHUFPSYMask(const SmallVectorImpl<int> &Mask, EVT VT,
3207 const X86Subtarget *Subtarget) {
3208 int NumElems = VT.getVectorNumElements();
3209
3210 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256)
3211 return false;
3212
3213 if (NumElems != 8)
3214 return false;
3215
3216 // VSHUFPSY divides the resulting vector into 4 chunks.
3217 // The sources are also splitted into 4 chunks, and each destination
3218 // chunk must come from a different source chunk.
3219 //
3220 // SRC1 => X7 X6 X5 X4 X3 X2 X1 X0
3221 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3222 //
3223 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3224 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3225 //
3226 int QuarterSize = NumElems/4;
3227 int HalfSize = QuarterSize*2;
3228 for (int i = 0; i < QuarterSize; ++i)
3229 if (!isUndefOrInRange(Mask[i], 0, HalfSize))
3230 return false;
3231 for (int i = QuarterSize; i < QuarterSize*2; ++i)
3232 if (!isUndefOrInRange(Mask[i], NumElems, NumElems+HalfSize))
3233 return false;
3234
3235 // The mask of the second half must be the same as the first but with
3236 // the appropriate offsets. This works in the same way as VPERMILPS
3237 // works with masks.
3238 for (int i = QuarterSize*2; i < QuarterSize*3; ++i) {
3239 if (!isUndefOrInRange(Mask[i], HalfSize, NumElems))
3240 return false;
3241 int FstHalfIdx = i-HalfSize;
3242 if (Mask[FstHalfIdx] < 0)
3243 continue;
3244 if (!isUndefOrEqual(Mask[i], Mask[FstHalfIdx]+HalfSize))
3245 return false;
3246 }
3247 for (int i = QuarterSize*3; i < NumElems; ++i) {
3248 if (!isUndefOrInRange(Mask[i], NumElems+HalfSize, NumElems*2))
3249 return false;
3250 int FstHalfIdx = i-HalfSize;
3251 if (Mask[FstHalfIdx] < 0)
3252 continue;
3253 if (!isUndefOrEqual(Mask[i], Mask[FstHalfIdx]+HalfSize))
3254 return false;
3255
3256 }
3257
3258 return true;
3259}
3260
3261/// getShuffleVSHUFPSYImmediate - Return the appropriate immediate to shuffle
3262/// the specified VECTOR_MASK mask with VSHUFPSY instruction.
3263static unsigned getShuffleVSHUFPSYImmediate(SDNode *N) {
3264 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3265 EVT VT = SVOp->getValueType(0);
3266 int NumElems = VT.getVectorNumElements();
3267
3268 assert(NumElems == 8 && VT.getSizeInBits() == 256 &&
3269 "Only supports v8i32 and v8f32 types");
3270
3271 int HalfSize = NumElems/2;
3272 unsigned Mask = 0;
3273 for (int i = 0; i != NumElems ; ++i) {
3274 if (SVOp->getMaskElt(i) < 0)
3275 continue;
3276 // The mask of the first half must be equal to the second one.
3277 unsigned Shamt = (i%HalfSize)*2;
3278 unsigned Elt = SVOp->getMaskElt(i) % HalfSize;
3279 Mask |= Elt << Shamt;
3280 }
3281
3282 return Mask;
3283}
3284
3285/// isVSHUFPDYMask - Return true if the specified VECTOR_SHUFFLE operand
3286/// specifies a shuffle of elements that is suitable for input to 256-bit
3287/// VSHUFPDY. This shuffle doesn't have the same restriction as the PS
3288/// version and the mask of the second half isn't binded with the first
3289/// one.
3290static bool isVSHUFPDYMask(const SmallVectorImpl<int> &Mask, EVT VT,
3291 const X86Subtarget *Subtarget) {
3292 int NumElems = VT.getVectorNumElements();
3293
3294 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256)
3295 return false;
3296
3297 if (NumElems != 4)
3298 return false;
3299
3300 // VSHUFPSY divides the resulting vector into 4 chunks.
3301 // The sources are also splitted into 4 chunks, and each destination
3302 // chunk must come from a different source chunk.
3303 //
3304 // SRC1 => X3 X2 X1 X0
3305 // SRC2 => Y3 Y2 Y1 Y0
3306 //
3307 // DST => Y2..Y3, X2..X3, Y1..Y0, X1..X0
3308 //
3309 int QuarterSize = NumElems/4;
3310 int HalfSize = QuarterSize*2;
3311 for (int i = 0; i < QuarterSize; ++i)
3312 if (!isUndefOrInRange(Mask[i], 0, HalfSize))
3313 return false;
3314 for (int i = QuarterSize; i < QuarterSize*2; ++i)
3315 if (!isUndefOrInRange(Mask[i], NumElems, NumElems+HalfSize))
3316 return false;
3317 for (int i = QuarterSize*2; i < QuarterSize*3; ++i)
3318 if (!isUndefOrInRange(Mask[i], HalfSize, NumElems))
3319 return false;
3320 for (int i = QuarterSize*3; i < NumElems; ++i)
3321 if (!isUndefOrInRange(Mask[i], NumElems+HalfSize, NumElems*2))
3322 return false;
3323
3324 return true;
3325}
3326
3327/// getShuffleVSHUFPDYImmediate - Return the appropriate immediate to shuffle
3328/// the specified VECTOR_MASK mask with VSHUFPDY instruction.
3329static unsigned getShuffleVSHUFPDYImmediate(SDNode *N) {
3330 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3331 EVT VT = SVOp->getValueType(0);
3332 int NumElems = VT.getVectorNumElements();
3333
3334 assert(NumElems == 4 && VT.getSizeInBits() == 256 &&
3335 "Only supports v4i64 and v4f64 types");
3336
3337 int HalfSize = NumElems/2;
3338 unsigned Mask = 0;
3339 for (int i = 0; i != NumElems ; ++i) {
3340 if (SVOp->getMaskElt(i) < 0)
3341 continue;
3342 int Elt = SVOp->getMaskElt(i) % HalfSize;
3343 Mask |= Elt << i;
3344 }
3345
3346 return Mask;
3347}
3348
Evan Cheng14aed5e2006-03-24 01:18:28 +00003349/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopesaf002d82011-08-24 23:17:55 +00003350/// specifies a shuffle of elements that is suitable for input to 128-bit
3351/// SHUFPS and SHUFPD.
Owen Andersone50ed302009-08-10 22:56:29 +00003352static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003353 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesaf002d82011-08-24 23:17:55 +00003354
3355 if (VT.getSizeInBits() != 128)
3356 return false;
3357
Nate Begeman9008ca62009-04-27 18:41:29 +00003358 if (NumElems != 2 && NumElems != 4)
3359 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003360
Nate Begeman9008ca62009-04-27 18:41:29 +00003361 int Half = NumElems / 2;
3362 for (int i = 0; i < Half; ++i)
3363 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003364 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003365 for (int i = Half; i < NumElems; ++i)
3366 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003367 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003368
Evan Cheng14aed5e2006-03-24 01:18:28 +00003369 return true;
3370}
3371
Nate Begeman9008ca62009-04-27 18:41:29 +00003372bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
3373 SmallVector<int, 8> M;
3374 N->getMask(M);
3375 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003376}
3377
Evan Cheng213d2cf2007-05-17 18:45:50 +00003378/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00003379/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
3380/// half elements to come from vector 1 (which would equal the dest.) and
3381/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00003382static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003383 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003384
3385 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00003386 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003387
Nate Begeman9008ca62009-04-27 18:41:29 +00003388 int Half = NumElems / 2;
3389 for (int i = 0; i < Half; ++i)
3390 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003391 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003392 for (int i = Half; i < NumElems; ++i)
3393 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003394 return false;
3395 return true;
3396}
3397
Nate Begeman9008ca62009-04-27 18:41:29 +00003398static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
3399 SmallVector<int, 8> M;
3400 N->getMask(M);
3401 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003402}
3403
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003404/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3405/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00003406bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003407 EVT VT = N->getValueType(0);
3408 unsigned NumElems = VT.getVectorNumElements();
3409
3410 if (VT.getSizeInBits() != 128)
3411 return false;
3412
3413 if (NumElems != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003414 return false;
3415
Evan Cheng2064a2b2006-03-28 06:50:32 +00003416 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00003417 return isUndefOrEqual(N->getMaskElt(0), 6) &&
3418 isUndefOrEqual(N->getMaskElt(1), 7) &&
3419 isUndefOrEqual(N->getMaskElt(2), 2) &&
3420 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003421}
3422
Nate Begeman0b10b912009-11-07 23:17:15 +00003423/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3424/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3425/// <2, 3, 2, 3>
3426bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003427 EVT VT = N->getValueType(0);
3428 unsigned NumElems = VT.getVectorNumElements();
3429
3430 if (VT.getSizeInBits() != 128)
3431 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003432
Nate Begeman0b10b912009-11-07 23:17:15 +00003433 if (NumElems != 4)
3434 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003435
Nate Begeman0b10b912009-11-07 23:17:15 +00003436 return isUndefOrEqual(N->getMaskElt(0), 2) &&
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003437 isUndefOrEqual(N->getMaskElt(1), 3) &&
3438 isUndefOrEqual(N->getMaskElt(2), 2) &&
3439 isUndefOrEqual(N->getMaskElt(3), 3);
Nate Begeman0b10b912009-11-07 23:17:15 +00003440}
3441
Evan Cheng5ced1d82006-04-06 23:23:56 +00003442/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3443/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003444bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3445 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003446
Evan Cheng5ced1d82006-04-06 23:23:56 +00003447 if (NumElems != 2 && NumElems != 4)
3448 return false;
3449
Evan Chengc5cdff22006-04-07 21:53:05 +00003450 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003451 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003452 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003453
Evan Chengc5cdff22006-04-07 21:53:05 +00003454 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003455 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003456 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003457
3458 return true;
3459}
3460
Nate Begeman0b10b912009-11-07 23:17:15 +00003461/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3462/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3463bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003464 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003465
David Greenea20244d2011-03-02 17:23:43 +00003466 if ((NumElems != 2 && NumElems != 4)
3467 || N->getValueType(0).getSizeInBits() > 128)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003468 return false;
3469
Evan Chengc5cdff22006-04-07 21:53:05 +00003470 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003471 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003472 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003473
Nate Begeman9008ca62009-04-27 18:41:29 +00003474 for (unsigned i = 0; i < NumElems/2; ++i)
3475 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003476 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003477
3478 return true;
3479}
3480
Evan Cheng0038e592006-03-28 00:39:58 +00003481/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3482/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003483static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003484 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003485 int NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003486
3487 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3488 "Unsupported vector type for unpckh");
3489
3490 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8)
Evan Cheng0038e592006-03-28 00:39:58 +00003491 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003492
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003493 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3494 // independently on 128-bit lanes.
3495 unsigned NumLanes = VT.getSizeInBits()/128;
3496 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003497
3498 unsigned Start = 0;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003499 unsigned End = NumLaneElts;
3500 for (unsigned s = 0; s < NumLanes; ++s) {
3501 for (unsigned i = Start, j = s * NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003502 i != End;
3503 i += 2, ++j) {
3504 int BitI = Mask[i];
3505 int BitI1 = Mask[i+1];
3506 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003507 return false;
David Greenea20244d2011-03-02 17:23:43 +00003508 if (V2IsSplat) {
3509 if (!isUndefOrEqual(BitI1, NumElts))
3510 return false;
3511 } else {
3512 if (!isUndefOrEqual(BitI1, j + NumElts))
3513 return false;
3514 }
Evan Cheng39623da2006-04-20 08:58:49 +00003515 }
David Greenea20244d2011-03-02 17:23:43 +00003516 // Process the next 128 bits.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003517 Start += NumLaneElts;
3518 End += NumLaneElts;
Evan Cheng0038e592006-03-28 00:39:58 +00003519 }
David Greenea20244d2011-03-02 17:23:43 +00003520
Evan Cheng0038e592006-03-28 00:39:58 +00003521 return true;
3522}
3523
Nate Begeman9008ca62009-04-27 18:41:29 +00003524bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3525 SmallVector<int, 8> M;
3526 N->getMask(M);
3527 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003528}
3529
Evan Cheng4fcb9222006-03-28 02:43:26 +00003530/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3531/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003532static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003533 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003534 int NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003535
3536 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3537 "Unsupported vector type for unpckh");
3538
3539 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8)
Evan Cheng4fcb9222006-03-28 02:43:26 +00003540 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003541
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003542 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3543 // independently on 128-bit lanes.
3544 unsigned NumLanes = VT.getSizeInBits()/128;
3545 unsigned NumLaneElts = NumElts/NumLanes;
3546
3547 unsigned Start = 0;
3548 unsigned End = NumLaneElts;
3549 for (unsigned l = 0; l != NumLanes; ++l) {
3550 for (unsigned i = Start, j = (l*NumLaneElts)+NumLaneElts/2;
3551 i != End; i += 2, ++j) {
3552 int BitI = Mask[i];
3553 int BitI1 = Mask[i+1];
3554 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003555 return false;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003556 if (V2IsSplat) {
3557 if (isUndefOrEqual(BitI1, NumElts))
3558 return false;
3559 } else {
3560 if (!isUndefOrEqual(BitI1, j+NumElts))
3561 return false;
3562 }
Evan Cheng39623da2006-04-20 08:58:49 +00003563 }
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003564 // Process the next 128 bits.
3565 Start += NumLaneElts;
3566 End += NumLaneElts;
Evan Cheng4fcb9222006-03-28 02:43:26 +00003567 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003568 return true;
3569}
3570
Nate Begeman9008ca62009-04-27 18:41:29 +00003571bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3572 SmallVector<int, 8> M;
3573 N->getMask(M);
3574 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003575}
3576
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003577/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3578/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3579/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003580static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003581 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003582 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003583 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003584
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003585 // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern
3586 // FIXME: Need a better way to get rid of this, there's no latency difference
3587 // between UNPCKLPD and MOVDDUP, the later should always be checked first and
3588 // the former later. We should also remove the "_undef" special mask.
3589 if (NumElems == 4 && VT.getSizeInBits() == 256)
3590 return false;
3591
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003592 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3593 // independently on 128-bit lanes.
3594 unsigned NumLanes = VT.getSizeInBits() / 128;
3595 unsigned NumLaneElts = NumElems / NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003596
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003597 for (unsigned s = 0; s < NumLanes; ++s) {
3598 for (unsigned i = s * NumLaneElts, j = s * NumLaneElts;
3599 i != NumLaneElts * (s + 1);
David Greenea20244d2011-03-02 17:23:43 +00003600 i += 2, ++j) {
3601 int BitI = Mask[i];
3602 int BitI1 = Mask[i+1];
3603
3604 if (!isUndefOrEqual(BitI, j))
3605 return false;
3606 if (!isUndefOrEqual(BitI1, j))
3607 return false;
3608 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003609 }
David Greenea20244d2011-03-02 17:23:43 +00003610
Rafael Espindola15684b22009-04-24 12:40:33 +00003611 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003612}
3613
Nate Begeman9008ca62009-04-27 18:41:29 +00003614bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3615 SmallVector<int, 8> M;
3616 N->getMask(M);
3617 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3618}
3619
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003620/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3621/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3622/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003623static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003624 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003625 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3626 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003627
Nate Begeman9008ca62009-04-27 18:41:29 +00003628 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3629 int BitI = Mask[i];
3630 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003631 if (!isUndefOrEqual(BitI, j))
3632 return false;
3633 if (!isUndefOrEqual(BitI1, j))
3634 return false;
3635 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003636 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003637}
3638
Nate Begeman9008ca62009-04-27 18:41:29 +00003639bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3640 SmallVector<int, 8> M;
3641 N->getMask(M);
3642 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3643}
3644
Evan Cheng017dcc62006-04-21 01:05:10 +00003645/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3646/// specifies a shuffle of elements that is suitable for input to MOVSS,
3647/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003648static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003649 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003650 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003651
3652 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003653
Nate Begeman9008ca62009-04-27 18:41:29 +00003654 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003655 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003656
Nate Begeman9008ca62009-04-27 18:41:29 +00003657 for (int i = 1; i < NumElts; ++i)
3658 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003659 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003660
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003661 return true;
3662}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003663
Nate Begeman9008ca62009-04-27 18:41:29 +00003664bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3665 SmallVector<int, 8> M;
3666 N->getMask(M);
3667 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003668}
3669
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003670/// isVPERM2F128Mask - Match 256-bit shuffles where the elements are considered
3671/// as permutations between 128-bit chunks or halves. As an example: this
3672/// shuffle bellow:
3673/// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
3674/// The first half comes from the second half of V1 and the second half from the
3675/// the second half of V2.
3676static bool isVPERM2F128Mask(const SmallVectorImpl<int> &Mask, EVT VT,
3677 const X86Subtarget *Subtarget) {
3678 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256)
3679 return false;
3680
3681 // The shuffle result is divided into half A and half B. In total the two
3682 // sources have 4 halves, namely: C, D, E, F. The final values of A and
3683 // B must come from C, D, E or F.
3684 int HalfSize = VT.getVectorNumElements()/2;
3685 bool MatchA = false, MatchB = false;
3686
3687 // Check if A comes from one of C, D, E, F.
3688 for (int Half = 0; Half < 4; ++Half) {
3689 if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
3690 MatchA = true;
3691 break;
3692 }
3693 }
3694
3695 // Check if B comes from one of C, D, E, F.
3696 for (int Half = 0; Half < 4; ++Half) {
3697 if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) {
3698 MatchB = true;
3699 break;
3700 }
3701 }
3702
3703 return MatchA && MatchB;
3704}
3705
3706/// getShuffleVPERM2F128Immediate - Return the appropriate immediate to shuffle
3707/// the specified VECTOR_MASK mask with VPERM2F128 instructions.
3708static unsigned getShuffleVPERM2F128Immediate(SDNode *N) {
3709 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3710 EVT VT = SVOp->getValueType(0);
3711
3712 int HalfSize = VT.getVectorNumElements()/2;
3713
3714 int FstHalf = 0, SndHalf = 0;
3715 for (int i = 0; i < HalfSize; ++i) {
3716 if (SVOp->getMaskElt(i) > 0) {
3717 FstHalf = SVOp->getMaskElt(i)/HalfSize;
3718 break;
3719 }
3720 }
3721 for (int i = HalfSize; i < HalfSize*2; ++i) {
3722 if (SVOp->getMaskElt(i) > 0) {
3723 SndHalf = SVOp->getMaskElt(i)/HalfSize;
3724 break;
3725 }
3726 }
3727
3728 return (FstHalf | (SndHalf << 4));
3729}
3730
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003731/// isVPERMILPDMask - Return true if the specified VECTOR_SHUFFLE operand
3732/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
3733/// Note that VPERMIL mask matching is different depending whether theunderlying
3734/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3735/// to the same elements of the low, but to the higher half of the source.
3736/// In VPERMILPD the two lanes could be shuffled independently of each other
3737/// with the same restriction that lanes can't be crossed.
3738static bool isVPERMILPDMask(const SmallVectorImpl<int> &Mask, EVT VT,
3739 const X86Subtarget *Subtarget) {
3740 int NumElts = VT.getVectorNumElements();
3741 int NumLanes = VT.getSizeInBits()/128;
3742
3743 if (!Subtarget->hasAVX())
3744 return false;
3745
3746 // Match any permutation of 128-bit vector with 64-bit types
3747 if (NumLanes == 1 && NumElts != 2)
3748 return false;
3749
3750 // Only match 256-bit with 32 types
3751 if (VT.getSizeInBits() == 256 && NumElts != 4)
3752 return false;
3753
3754 // The mask on the high lane is independent of the low. Both can match
3755 // any element in inside its own lane, but can't cross.
3756 int LaneSize = NumElts/NumLanes;
3757 for (int l = 0; l < NumLanes; ++l)
3758 for (int i = l*LaneSize; i < LaneSize*(l+1); ++i) {
3759 int LaneStart = l*LaneSize;
3760 if (!isUndefOrInRange(Mask[i], LaneStart, LaneStart+LaneSize))
3761 return false;
3762 }
3763
3764 return true;
3765}
3766
3767/// isVPERMILPSMask - Return true if the specified VECTOR_SHUFFLE operand
3768/// specifies a shuffle of elements that is suitable for input to VPERMILPS*.
3769/// Note that VPERMIL mask matching is different depending whether theunderlying
3770/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3771/// to the same elements of the low, but to the higher half of the source.
3772/// In VPERMILPD the two lanes could be shuffled independently of each other
3773/// with the same restriction that lanes can't be crossed.
3774static bool isVPERMILPSMask(const SmallVectorImpl<int> &Mask, EVT VT,
3775 const X86Subtarget *Subtarget) {
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003776 unsigned NumElts = VT.getVectorNumElements();
3777 unsigned NumLanes = VT.getSizeInBits()/128;
3778
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003779 if (!Subtarget->hasAVX())
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003780 return false;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003781
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003782 // Match any permutation of 128-bit vector with 32-bit types
3783 if (NumLanes == 1 && NumElts != 4)
3784 return false;
3785
3786 // Only match 256-bit with 32 types
3787 if (VT.getSizeInBits() == 256 && NumElts != 8)
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003788 return false;
3789
3790 // The mask on the high lane should be the same as the low. Actually,
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003791 // they can differ if any of the corresponding index in a lane is undef
3792 // and the other stays in range.
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003793 int LaneSize = NumElts/NumLanes;
3794 for (int i = 0; i < LaneSize; ++i) {
3795 int HighElt = i+LaneSize;
Bruno Cardoso Lopes155a92a2011-08-10 01:54:17 +00003796 bool HighValid = isUndefOrInRange(Mask[HighElt], LaneSize, NumElts);
3797 bool LowValid = isUndefOrInRange(Mask[i], 0, LaneSize);
3798
3799 if (!HighValid || !LowValid)
3800 return false;
3801 if (Mask[i] < 0 || Mask[HighElt] < 0)
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003802 continue;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003803 if (Mask[HighElt]-Mask[i] != LaneSize)
3804 return false;
3805 }
3806
3807 return true;
3808}
3809
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003810/// getShuffleVPERMILPSImmediate - Return the appropriate immediate to shuffle
3811/// the specified VECTOR_MASK mask with VPERMILPS* instructions.
3812static unsigned getShuffleVPERMILPSImmediate(SDNode *N) {
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003813 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3814 EVT VT = SVOp->getValueType(0);
3815
3816 int NumElts = VT.getVectorNumElements();
3817 int NumLanes = VT.getSizeInBits()/128;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003818 int LaneSize = NumElts/NumLanes;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003819
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003820 // Although the mask is equal for both lanes do it twice to get the cases
3821 // where a mask will match because the same mask element is undef on the
3822 // first half but valid on the second. This would get pathological cases
3823 // such as: shuffle <u, 0, 1, 2, 4, 4, 5, 6>, which is completely valid.
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003824 unsigned Mask = 0;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003825 for (int l = 0; l < NumLanes; ++l) {
3826 for (int i = 0; i < LaneSize; ++i) {
3827 int MaskElt = SVOp->getMaskElt(i+(l*LaneSize));
3828 if (MaskElt < 0)
3829 continue;
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003830 if (MaskElt >= LaneSize)
3831 MaskElt -= LaneSize;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003832 Mask |= MaskElt << (i*2);
3833 }
Bruno Cardoso Lopes377baa52011-07-29 01:31:04 +00003834 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003835
3836 return Mask;
3837}
3838
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003839/// getShuffleVPERMILPDImmediate - Return the appropriate immediate to shuffle
3840/// the specified VECTOR_MASK mask with VPERMILPD* instructions.
3841static unsigned getShuffleVPERMILPDImmediate(SDNode *N) {
3842 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3843 EVT VT = SVOp->getValueType(0);
3844
3845 int NumElts = VT.getVectorNumElements();
3846 int NumLanes = VT.getSizeInBits()/128;
3847
3848 unsigned Mask = 0;
3849 int LaneSize = NumElts/NumLanes;
3850 for (int l = 0; l < NumLanes; ++l)
Bruno Cardoso Lopes377baa52011-07-29 01:31:04 +00003851 for (int i = l*LaneSize; i < LaneSize*(l+1); ++i) {
3852 int MaskElt = SVOp->getMaskElt(i);
3853 if (MaskElt < 0)
3854 continue;
3855 Mask |= (MaskElt-l*LaneSize) << i;
3856 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003857
3858 return Mask;
3859}
3860
Evan Cheng017dcc62006-04-21 01:05:10 +00003861/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3862/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003863/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003864static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003865 bool V2IsSplat = false, bool V2IsUndef = false) {
3866 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003867 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003868 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003869
Nate Begeman9008ca62009-04-27 18:41:29 +00003870 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003871 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003872
Nate Begeman9008ca62009-04-27 18:41:29 +00003873 for (int i = 1; i < NumOps; ++i)
3874 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3875 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3876 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003877 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003878
Evan Cheng39623da2006-04-20 08:58:49 +00003879 return true;
3880}
3881
Nate Begeman9008ca62009-04-27 18:41:29 +00003882static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003883 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003884 SmallVector<int, 8> M;
3885 N->getMask(M);
3886 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003887}
3888
Evan Chengd9539472006-04-14 21:59:03 +00003889/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3890/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003891/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
3892bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N,
3893 const X86Subtarget *Subtarget) {
3894 if (!Subtarget->hasSSE3() && !Subtarget->hasAVX())
Evan Chengd9539472006-04-14 21:59:03 +00003895 return false;
3896
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003897 // The second vector must be undef
3898 if (N->getOperand(1).getOpcode() != ISD::UNDEF)
3899 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003900
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003901 EVT VT = N->getValueType(0);
3902 unsigned NumElems = VT.getVectorNumElements();
3903
3904 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3905 (VT.getSizeInBits() == 256 && NumElems != 8))
3906 return false;
3907
3908 // "i+1" is the value the indexed mask element must have
3909 for (unsigned i = 0; i < NumElems; i += 2)
3910 if (!isUndefOrEqual(N->getMaskElt(i), i+1) ||
3911 !isUndefOrEqual(N->getMaskElt(i+1), i+1))
Nate Begeman9008ca62009-04-27 18:41:29 +00003912 return false;
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003913
3914 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003915}
3916
3917/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3918/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003919/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
3920bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N,
3921 const X86Subtarget *Subtarget) {
3922 if (!Subtarget->hasSSE3() && !Subtarget->hasAVX())
Evan Chengd9539472006-04-14 21:59:03 +00003923 return false;
3924
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003925 // The second vector must be undef
3926 if (N->getOperand(1).getOpcode() != ISD::UNDEF)
3927 return false;
3928
3929 EVT VT = N->getValueType(0);
3930 unsigned NumElems = VT.getVectorNumElements();
3931
3932 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3933 (VT.getSizeInBits() == 256 && NumElems != 8))
3934 return false;
3935
3936 // "i" is the value the indexed mask element must have
3937 for (unsigned i = 0; i < NumElems; i += 2)
3938 if (!isUndefOrEqual(N->getMaskElt(i), i) ||
3939 !isUndefOrEqual(N->getMaskElt(i+1), i))
Nate Begeman9008ca62009-04-27 18:41:29 +00003940 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003941
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003942 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003943}
3944
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003945/// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand
3946/// specifies a shuffle of elements that is suitable for input to 256-bit
3947/// version of MOVDDUP.
3948static bool isMOVDDUPYMask(ShuffleVectorSDNode *N,
3949 const X86Subtarget *Subtarget) {
3950 EVT VT = N->getValueType(0);
3951 int NumElts = VT.getVectorNumElements();
3952 bool V2IsUndef = N->getOperand(1).getOpcode() == ISD::UNDEF;
3953
3954 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256 ||
3955 !V2IsUndef || NumElts != 4)
3956 return false;
3957
3958 for (int i = 0; i != NumElts/2; ++i)
3959 if (!isUndefOrEqual(N->getMaskElt(i), 0))
3960 return false;
3961 for (int i = NumElts/2; i != NumElts; ++i)
3962 if (!isUndefOrEqual(N->getMaskElt(i), NumElts/2))
3963 return false;
3964 return true;
3965}
3966
Evan Cheng0b457f02008-09-25 20:50:48 +00003967/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003968/// specifies a shuffle of elements that is suitable for input to 128-bit
3969/// version of MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003970bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003971 EVT VT = N->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00003972
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003973 if (VT.getSizeInBits() != 128)
3974 return false;
3975
3976 int e = VT.getVectorNumElements() / 2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003977 for (int i = 0; i < e; ++i)
3978 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003979 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003980 for (int i = 0; i < e; ++i)
3981 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003982 return false;
3983 return true;
3984}
3985
David Greenec38a03e2011-02-03 15:50:00 +00003986/// isVEXTRACTF128Index - Return true if the specified
3987/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3988/// suitable for input to VEXTRACTF128.
3989bool X86::isVEXTRACTF128Index(SDNode *N) {
3990 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3991 return false;
3992
3993 // The index should be aligned on a 128-bit boundary.
3994 uint64_t Index =
3995 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3996
3997 unsigned VL = N->getValueType(0).getVectorNumElements();
3998 unsigned VBits = N->getValueType(0).getSizeInBits();
3999 unsigned ElSize = VBits / VL;
4000 bool Result = (Index * ElSize) % 128 == 0;
4001
4002 return Result;
4003}
4004
David Greeneccacdc12011-02-04 16:08:29 +00004005/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
4006/// operand specifies a subvector insert that is suitable for input to
4007/// VINSERTF128.
4008bool X86::isVINSERTF128Index(SDNode *N) {
4009 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4010 return false;
4011
4012 // The index should be aligned on a 128-bit boundary.
4013 uint64_t Index =
4014 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
4015
4016 unsigned VL = N->getValueType(0).getVectorNumElements();
4017 unsigned VBits = N->getValueType(0).getSizeInBits();
4018 unsigned ElSize = VBits / VL;
4019 bool Result = (Index * ElSize) % 128 == 0;
4020
4021 return Result;
4022}
4023
Evan Cheng63d33002006-03-22 08:01:21 +00004024/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004025/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00004026unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004027 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
4028 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
4029
Evan Chengb9df0ca2006-03-22 02:53:00 +00004030 unsigned Shift = (NumOperands == 4) ? 2 : 1;
4031 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00004032 for (int i = 0; i < NumOperands; ++i) {
4033 int Val = SVOp->getMaskElt(NumOperands-i-1);
4034 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00004035 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00004036 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00004037 if (i != NumOperands - 1)
4038 Mask <<= Shift;
4039 }
Evan Cheng63d33002006-03-22 08:01:21 +00004040 return Mask;
4041}
4042
Evan Cheng506d3df2006-03-29 23:07:14 +00004043/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004044/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00004045unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004046 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00004047 unsigned Mask = 0;
4048 // 8 nodes, but we only care about the last 4.
4049 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004050 int Val = SVOp->getMaskElt(i);
4051 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00004052 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00004053 if (i != 4)
4054 Mask <<= 2;
4055 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004056 return Mask;
4057}
4058
4059/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004060/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00004061unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004062 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00004063 unsigned Mask = 0;
4064 // 8 nodes, but we only care about the first 4.
4065 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004066 int Val = SVOp->getMaskElt(i);
4067 if (Val >= 0)
4068 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00004069 if (i != 0)
4070 Mask <<= 2;
4071 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004072 return Mask;
4073}
4074
Nate Begemana09008b2009-10-19 02:17:23 +00004075/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
4076/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
4077unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
4078 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
4079 EVT VVT = N->getValueType(0);
4080 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
4081 int Val = 0;
4082
4083 unsigned i, e;
4084 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
4085 Val = SVOp->getMaskElt(i);
4086 if (Val >= 0)
4087 break;
4088 }
Eli Friedman63f8dde2011-07-25 21:36:45 +00004089 assert(Val - i > 0 && "PALIGNR imm should be positive");
Nate Begemana09008b2009-10-19 02:17:23 +00004090 return (Val - i) * EltSize;
4091}
4092
David Greenec38a03e2011-02-03 15:50:00 +00004093/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
4094/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
4095/// instructions.
4096unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
4097 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4098 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
4099
4100 uint64_t Index =
4101 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4102
4103 EVT VecVT = N->getOperand(0).getValueType();
4104 EVT ElVT = VecVT.getVectorElementType();
4105
4106 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004107 return Index / NumElemsPerChunk;
4108}
4109
David Greeneccacdc12011-02-04 16:08:29 +00004110/// getInsertVINSERTF128Immediate - Return the appropriate immediate
4111/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
4112/// instructions.
4113unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
4114 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4115 llvm_unreachable("Illegal insert subvector for VINSERTF128");
4116
4117 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00004118 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00004119
4120 EVT VecVT = N->getValueType(0);
4121 EVT ElVT = VecVT.getVectorElementType();
4122
4123 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004124 return Index / NumElemsPerChunk;
4125}
4126
Evan Cheng37b73872009-07-30 08:33:02 +00004127/// isZeroNode - Returns true if Elt is a constant zero or a floating point
4128/// constant +0.0.
4129bool X86::isZeroNode(SDValue Elt) {
4130 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00004131 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00004132 (isa<ConstantFPSDNode>(Elt) &&
4133 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
4134}
4135
Nate Begeman9008ca62009-04-27 18:41:29 +00004136/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
4137/// their permute mask.
4138static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
4139 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004140 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004141 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004142 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00004143
Nate Begeman5a5ca152009-04-29 05:20:52 +00004144 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004145 int idx = SVOp->getMaskElt(i);
4146 if (idx < 0)
4147 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004148 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00004149 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004150 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004151 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004152 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004153 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
4154 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004155}
4156
Evan Cheng779ccea2007-12-07 21:30:01 +00004157/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
4158/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00004159static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00004160 unsigned NumElems = VT.getVectorNumElements();
4161 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004162 int idx = Mask[i];
4163 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004164 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004165 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00004166 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004167 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004168 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004169 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004170}
4171
Evan Cheng533a0aa2006-04-19 20:35:22 +00004172/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
4173/// match movhlps. The lower half elements should come from upper half of
4174/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004175/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00004176static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004177 EVT VT = Op->getValueType(0);
4178 if (VT.getSizeInBits() != 128)
4179 return false;
4180 if (VT.getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00004181 return false;
4182 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004183 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004184 return false;
4185 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004186 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004187 return false;
4188 return true;
4189}
4190
Evan Cheng5ced1d82006-04-06 23:23:56 +00004191/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00004192/// is promoted to a vector. It also returns the LoadSDNode by reference if
4193/// required.
4194static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00004195 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
4196 return false;
4197 N = N->getOperand(0).getNode();
4198 if (!ISD::isNON_EXTLoad(N))
4199 return false;
4200 if (LD)
4201 *LD = cast<LoadSDNode>(N);
4202 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004203}
4204
Evan Cheng533a0aa2006-04-19 20:35:22 +00004205/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4206/// match movlp{s|d}. The lower half elements should come from lower half of
4207/// V1 (and in order), and the upper half elements should come from the upper
4208/// half of V2 (and in order). And since V1 will become the source of the
4209/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004210static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
4211 ShuffleVectorSDNode *Op) {
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004212 EVT VT = Op->getValueType(0);
4213 if (VT.getSizeInBits() != 128)
4214 return false;
4215
Evan Cheng466685d2006-10-09 20:57:25 +00004216 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004217 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00004218 // Is V2 is a vector load, don't do this transformation. We will try to use
4219 // load folding shufps op.
4220 if (ISD::isNON_EXTLoad(V2))
4221 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004222
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004223 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004224
Evan Cheng533a0aa2006-04-19 20:35:22 +00004225 if (NumElems != 2 && NumElems != 4)
4226 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004227 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004228 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004229 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004230 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004231 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004232 return false;
4233 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004234}
4235
Evan Cheng39623da2006-04-20 08:58:49 +00004236/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4237/// all the same.
4238static bool isSplatVector(SDNode *N) {
4239 if (N->getOpcode() != ISD::BUILD_VECTOR)
4240 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004241
Dan Gohman475871a2008-07-27 21:46:04 +00004242 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00004243 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4244 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00004245 return false;
4246 return true;
4247}
4248
Evan Cheng213d2cf2007-05-17 18:45:50 +00004249/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00004250/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00004251/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00004252static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00004253 SDValue V1 = N->getOperand(0);
4254 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004255 unsigned NumElems = N->getValueType(0).getVectorNumElements();
4256 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004257 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004258 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004259 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00004260 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4261 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004262 if (Opc != ISD::BUILD_VECTOR ||
4263 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00004264 return false;
4265 } else if (Idx >= 0) {
4266 unsigned Opc = V1.getOpcode();
4267 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4268 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004269 if (Opc != ISD::BUILD_VECTOR ||
4270 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00004271 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00004272 }
4273 }
4274 return true;
4275}
4276
4277/// getZeroVector - Returns a vector of specified type with all zero elements.
4278///
Owen Andersone50ed302009-08-10 22:56:29 +00004279static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00004280 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004281 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004282
Dale Johannesen0488fb62010-09-30 23:57:10 +00004283 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004284 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00004285 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00004286 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004287 if (HasSSE2) { // SSE2
4288 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4289 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4290 } else { // SSE1
4291 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4292 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4293 }
4294 } else if (VT.getSizeInBits() == 256) { // AVX
4295 // 256-bit logic and arithmetic instructions in AVX are
4296 // all floating-point, no support for integer ops. Default
4297 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00004298 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004299 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4300 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00004301 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004302 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004303}
4304
Chris Lattner8a594482007-11-25 00:24:49 +00004305/// getOnesVector - Returns a vector of specified type with all bits set.
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004306/// Always build ones vectors as <4 x i32>. For 256-bit types, use two
4307/// <4 x i32> inserted in a <8 x i32> appropriately. Then bitcast to their
4308/// original type, ensuring they get CSE'd.
Owen Andersone50ed302009-08-10 22:56:29 +00004309static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004310 assert(VT.isVector() && "Expected a vector type");
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004311 assert((VT.is128BitVector() || VT.is256BitVector())
4312 && "Expected a 128-bit or 256-bit vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004313
Owen Anderson825b72b2009-08-11 20:47:22 +00004314 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004315 SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
4316 Cst, Cst, Cst, Cst);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004317
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004318 if (VT.is256BitVector()) {
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004319 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, MVT::v8i32),
4320 Vec, DAG.getConstant(0, MVT::i32), DAG, dl);
4321 Vec = Insert128BitVector(InsV, Vec,
4322 DAG.getConstant(4 /* NumElems/2 */, MVT::i32), DAG, dl);
4323 }
4324
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004325 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00004326}
4327
Evan Cheng39623da2006-04-20 08:58:49 +00004328/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4329/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00004330static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004331 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004332 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004333
Evan Cheng39623da2006-04-20 08:58:49 +00004334 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00004335 SmallVector<int, 8> MaskVec;
4336 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00004337
Nate Begeman5a5ca152009-04-29 05:20:52 +00004338 for (unsigned i = 0; i != NumElems; ++i) {
4339 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004340 MaskVec[i] = NumElems;
4341 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00004342 }
Evan Cheng39623da2006-04-20 08:58:49 +00004343 }
Evan Cheng39623da2006-04-20 08:58:49 +00004344 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00004345 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
4346 SVOp->getOperand(1), &MaskVec[0]);
4347 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00004348}
4349
Evan Cheng017dcc62006-04-21 01:05:10 +00004350/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4351/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00004352static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004353 SDValue V2) {
4354 unsigned NumElems = VT.getVectorNumElements();
4355 SmallVector<int, 8> Mask;
4356 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00004357 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004358 Mask.push_back(i);
4359 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00004360}
4361
Nate Begeman9008ca62009-04-27 18:41:29 +00004362/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004363static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004364 SDValue V2) {
4365 unsigned NumElems = VT.getVectorNumElements();
4366 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00004367 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004368 Mask.push_back(i);
4369 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00004370 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004371 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00004372}
4373
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004374/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004375static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004376 SDValue V2) {
4377 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00004378 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00004379 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00004380 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004381 Mask.push_back(i + Half);
4382 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00004383 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004384 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004385}
4386
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004387// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004388// a generic shuffle instruction because the target has no such instructions.
4389// Generate shuffles which repeat i16 and i8 several times until they can be
4390// represented by v4f32 and then be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004391static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004392 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004393 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004394 DebugLoc dl = V.getDebugLoc();
Rafael Espindola15684b22009-04-24 12:40:33 +00004395
Nate Begeman9008ca62009-04-27 18:41:29 +00004396 while (NumElems > 4) {
4397 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004398 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004399 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004400 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004401 EltNo -= NumElems/2;
4402 }
4403 NumElems >>= 1;
4404 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004405 return V;
4406}
Eric Christopherfd179292009-08-27 18:07:15 +00004407
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004408/// getLegalSplat - Generate a legal splat with supported x86 shuffles
4409static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
4410 EVT VT = V.getValueType();
4411 DebugLoc dl = V.getDebugLoc();
4412 assert((VT.getSizeInBits() == 128 || VT.getSizeInBits() == 256)
4413 && "Vector size not supported");
4414
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004415 if (VT.getSizeInBits() == 128) {
4416 V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004417 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004418 V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
4419 &SplatMask[0]);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004420 } else {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004421 // To use VPERMILPS to splat scalars, the second half of indicies must
4422 // refer to the higher part, which is a duplication of the lower one,
4423 // because VPERMILPS can only handle in-lane permutations.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004424 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
4425 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004426
4427 V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
4428 V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
4429 &SplatMask[0]);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004430 }
4431
4432 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4433}
4434
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004435/// PromoteSplat - Splat is promoted to target supported vector shuffles.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004436static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4437 EVT SrcVT = SV->getValueType(0);
4438 SDValue V1 = SV->getOperand(0);
4439 DebugLoc dl = SV->getDebugLoc();
4440
4441 int EltNo = SV->getSplatIndex();
4442 int NumElems = SrcVT.getVectorNumElements();
4443 unsigned Size = SrcVT.getSizeInBits();
4444
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004445 assert(((Size == 128 && NumElems > 4) || Size == 256) &&
4446 "Unknown how to promote splat for type");
4447
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004448 // Extract the 128-bit part containing the splat element and update
4449 // the splat element index when it refers to the higher register.
4450 if (Size == 256) {
4451 unsigned Idx = (EltNo > NumElems/2) ? NumElems/2 : 0;
4452 V1 = Extract128BitVector(V1, DAG.getConstant(Idx, MVT::i32), DAG, dl);
4453 if (Idx > 0)
4454 EltNo -= NumElems/2;
4455 }
4456
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004457 // All i16 and i8 vector types can't be used directly by a generic shuffle
4458 // instruction because the target has no such instruction. Generate shuffles
4459 // which repeat i16 and i8 several times until they fit in i32, and then can
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004460 // be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004461 EVT EltVT = SrcVT.getVectorElementType();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004462 if (EltVT == MVT::i8 || EltVT == MVT::i16)
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004463 V1 = PromoteSplati8i16(V1, DAG, EltNo);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004464
4465 // Recreate the 256-bit vector and place the same 128-bit vector
4466 // into the low and high part. This is necessary because we want
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004467 // to use VPERM* to shuffle the vectors
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004468 if (Size == 256) {
4469 SDValue InsV = Insert128BitVector(DAG.getUNDEF(SrcVT), V1,
4470 DAG.getConstant(0, MVT::i32), DAG, dl);
4471 V1 = Insert128BitVector(InsV, V1,
4472 DAG.getConstant(NumElems/2, MVT::i32), DAG, dl);
4473 }
4474
4475 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004476}
4477
Evan Chengba05f722006-04-21 23:03:30 +00004478/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004479/// vector of zero or undef vector. This produces a shuffle where the low
4480/// element of V2 is swizzled into the zero/undef vector, landing at element
4481/// 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 +00004482static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00004483 bool isZero, bool HasSSE2,
4484 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004485 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00004486 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00004487 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
4488 unsigned NumElems = VT.getVectorNumElements();
4489 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004490 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004491 // If this is the insertion idx, put the low elt of V2 here.
4492 MaskVec.push_back(i == Idx ? NumElems : i);
4493 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004494}
4495
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004496/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4497/// element of the result of the vector shuffle.
Benjamin Kramer050db522011-03-26 12:38:19 +00004498static SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
4499 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004500 if (Depth == 6)
4501 return SDValue(); // Limit search depth.
4502
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004503 SDValue V = SDValue(N, 0);
4504 EVT VT = V.getValueType();
4505 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004506
4507 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4508 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
4509 Index = SV->getMaskElt(Index);
4510
4511 if (Index < 0)
4512 return DAG.getUNDEF(VT.getVectorElementType());
4513
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004514 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004515 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004516 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004517 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004518
4519 // Recurse into target specific vector shuffles to find scalars.
4520 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004521 int NumElems = VT.getVectorNumElements();
4522 SmallVector<unsigned, 16> ShuffleMask;
4523 SDValue ImmN;
4524
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004525 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004526 case X86ISD::SHUFPS:
4527 case X86ISD::SHUFPD:
4528 ImmN = N->getOperand(N->getNumOperands()-1);
4529 DecodeSHUFPSMask(NumElems,
4530 cast<ConstantSDNode>(ImmN)->getZExtValue(),
4531 ShuffleMask);
4532 break;
4533 case X86ISD::PUNPCKHBW:
4534 case X86ISD::PUNPCKHWD:
4535 case X86ISD::PUNPCKHDQ:
4536 case X86ISD::PUNPCKHQDQ:
4537 DecodePUNPCKHMask(NumElems, ShuffleMask);
4538 break;
4539 case X86ISD::UNPCKHPS:
4540 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00004541 case X86ISD::VUNPCKHPSY:
4542 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004543 DecodeUNPCKHPMask(NumElems, ShuffleMask);
4544 break;
4545 case X86ISD::PUNPCKLBW:
4546 case X86ISD::PUNPCKLWD:
4547 case X86ISD::PUNPCKLDQ:
4548 case X86ISD::PUNPCKLQDQ:
David Greenec4db4e52011-02-28 19:06:56 +00004549 DecodePUNPCKLMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004550 break;
4551 case X86ISD::UNPCKLPS:
4552 case X86ISD::UNPCKLPD:
David Greenec4db4e52011-02-28 19:06:56 +00004553 case X86ISD::VUNPCKLPSY:
4554 case X86ISD::VUNPCKLPDY:
4555 DecodeUNPCKLPMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004556 break;
4557 case X86ISD::MOVHLPS:
4558 DecodeMOVHLPSMask(NumElems, ShuffleMask);
4559 break;
4560 case X86ISD::MOVLHPS:
4561 DecodeMOVLHPSMask(NumElems, ShuffleMask);
4562 break;
4563 case X86ISD::PSHUFD:
4564 ImmN = N->getOperand(N->getNumOperands()-1);
4565 DecodePSHUFMask(NumElems,
4566 cast<ConstantSDNode>(ImmN)->getZExtValue(),
4567 ShuffleMask);
4568 break;
4569 case X86ISD::PSHUFHW:
4570 ImmN = N->getOperand(N->getNumOperands()-1);
4571 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4572 ShuffleMask);
4573 break;
4574 case X86ISD::PSHUFLW:
4575 ImmN = N->getOperand(N->getNumOperands()-1);
4576 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4577 ShuffleMask);
4578 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004579 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004580 case X86ISD::MOVSD: {
4581 // The index 0 always comes from the first element of the second source,
4582 // this is why MOVSS and MOVSD are used in the first place. The other
4583 // elements come from the other positions of the first source vector.
4584 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004585 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
4586 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004587 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00004588 case X86ISD::VPERMILPS:
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00004589 ImmN = N->getOperand(N->getNumOperands()-1);
Bruno Cardoso Lopes2eb4c2b2011-07-29 01:31:11 +00004590 DecodeVPERMILPSMask(4, cast<ConstantSDNode>(ImmN)->getZExtValue(),
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00004591 ShuffleMask);
Bruno Cardoso Lopes2eb4c2b2011-07-29 01:31:11 +00004592 break;
4593 case X86ISD::VPERMILPSY:
4594 ImmN = N->getOperand(N->getNumOperands()-1);
4595 DecodeVPERMILPSMask(8, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4596 ShuffleMask);
4597 break;
4598 case X86ISD::VPERMILPD:
4599 ImmN = N->getOperand(N->getNumOperands()-1);
4600 DecodeVPERMILPDMask(2, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4601 ShuffleMask);
4602 break;
4603 case X86ISD::VPERMILPDY:
4604 ImmN = N->getOperand(N->getNumOperands()-1);
4605 DecodeVPERMILPDMask(4, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4606 ShuffleMask);
4607 break;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00004608 case X86ISD::VPERM2F128:
4609 ImmN = N->getOperand(N->getNumOperands()-1);
4610 DecodeVPERM2F128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4611 ShuffleMask);
4612 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004613 default:
4614 assert("not implemented for target shuffle node");
4615 return SDValue();
4616 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004617
4618 Index = ShuffleMask[Index];
4619 if (Index < 0)
4620 return DAG.getUNDEF(VT.getVectorElementType());
4621
4622 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
4623 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
4624 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004625 }
4626
4627 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004628 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004629 V = V.getOperand(0);
4630 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004631 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004632
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004633 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004634 return SDValue();
4635 }
4636
4637 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4638 return (Index == 0) ? V.getOperand(0)
4639 : DAG.getUNDEF(VT.getVectorElementType());
4640
4641 if (V.getOpcode() == ISD::BUILD_VECTOR)
4642 return V.getOperand(Index);
4643
4644 return SDValue();
4645}
4646
4647/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4648/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004649/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004650static
4651unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
4652 bool ZerosFromLeft, SelectionDAG &DAG) {
4653 int i = 0;
4654
4655 while (i < NumElems) {
4656 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004657 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004658 if (!(Elt.getNode() &&
4659 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4660 break;
4661 ++i;
4662 }
4663
4664 return i;
4665}
4666
4667/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
4668/// MaskE correspond consecutively to elements from one of the vector operands,
4669/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4670static
4671bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
4672 int OpIdx, int NumElems, unsigned &OpNum) {
4673 bool SeenV1 = false;
4674 bool SeenV2 = false;
4675
4676 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
4677 int Idx = SVOp->getMaskElt(i);
4678 // Ignore undef indicies
4679 if (Idx < 0)
4680 continue;
4681
4682 if (Idx < NumElems)
4683 SeenV1 = true;
4684 else
4685 SeenV2 = true;
4686
4687 // Only accept consecutive elements from the same vector
4688 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4689 return false;
4690 }
4691
4692 OpNum = SeenV1 ? 0 : 1;
4693 return true;
4694}
4695
4696/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4697/// logical left shift of a vector.
4698static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4699 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4700 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4701 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4702 false /* check zeros from right */, DAG);
4703 unsigned OpSrc;
4704
4705 if (!NumZeros)
4706 return false;
4707
4708 // Considering the elements in the mask that are not consecutive zeros,
4709 // check if they consecutively come from only one of the source vectors.
4710 //
4711 // V1 = {X, A, B, C} 0
4712 // \ \ \ /
4713 // vector_shuffle V1, V2 <1, 2, 3, X>
4714 //
4715 if (!isShuffleMaskConsecutive(SVOp,
4716 0, // Mask Start Index
4717 NumElems-NumZeros-1, // Mask End Index
4718 NumZeros, // Where to start looking in the src vector
4719 NumElems, // Number of elements in vector
4720 OpSrc)) // Which source operand ?
4721 return false;
4722
4723 isLeft = false;
4724 ShAmt = NumZeros;
4725 ShVal = SVOp->getOperand(OpSrc);
4726 return true;
4727}
4728
4729/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4730/// logical left shift of a vector.
4731static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4732 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4733 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4734 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4735 true /* check zeros from left */, DAG);
4736 unsigned OpSrc;
4737
4738 if (!NumZeros)
4739 return false;
4740
4741 // Considering the elements in the mask that are not consecutive zeros,
4742 // check if they consecutively come from only one of the source vectors.
4743 //
4744 // 0 { A, B, X, X } = V2
4745 // / \ / /
4746 // vector_shuffle V1, V2 <X, X, 4, 5>
4747 //
4748 if (!isShuffleMaskConsecutive(SVOp,
4749 NumZeros, // Mask Start Index
4750 NumElems-1, // Mask End Index
4751 0, // Where to start looking in the src vector
4752 NumElems, // Number of elements in vector
4753 OpSrc)) // Which source operand ?
4754 return false;
4755
4756 isLeft = true;
4757 ShAmt = NumZeros;
4758 ShVal = SVOp->getOperand(OpSrc);
4759 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004760}
4761
4762/// isVectorShift - Returns true if the shuffle can be implemented as a
4763/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004764static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004765 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004766 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4767 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4768 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004769
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004770 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004771}
4772
Evan Chengc78d3b42006-04-24 18:01:45 +00004773/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4774///
Dan Gohman475871a2008-07-27 21:46:04 +00004775static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004776 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004777 SelectionDAG &DAG,
4778 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004779 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004780 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004781
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004782 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004783 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004784 bool First = true;
4785 for (unsigned i = 0; i < 16; ++i) {
4786 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4787 if (ThisIsNonZero && First) {
4788 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004789 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004790 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004791 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004792 First = false;
4793 }
4794
4795 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004796 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004797 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4798 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004799 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004800 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004801 }
4802 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004803 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4804 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4805 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004806 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004807 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004808 } else
4809 ThisElt = LastElt;
4810
Gabor Greifba36cb52008-08-28 21:40:38 +00004811 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004812 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004813 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004814 }
4815 }
4816
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004817 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004818}
4819
Bill Wendlinga348c562007-03-22 18:42:45 +00004820/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004821///
Dan Gohman475871a2008-07-27 21:46:04 +00004822static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004823 unsigned NumNonZero, unsigned NumZero,
4824 SelectionDAG &DAG,
4825 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004826 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004827 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004828
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004829 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004830 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004831 bool First = true;
4832 for (unsigned i = 0; i < 8; ++i) {
4833 bool isNonZero = (NonZeros & (1 << i)) != 0;
4834 if (isNonZero) {
4835 if (First) {
4836 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004837 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004838 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004839 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004840 First = false;
4841 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004842 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004843 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004844 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004845 }
4846 }
4847
4848 return V;
4849}
4850
Evan Chengf26ffe92008-05-29 08:22:04 +00004851/// getVShift - Return a vector logical shift node.
4852///
Owen Andersone50ed302009-08-10 22:56:29 +00004853static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004854 unsigned NumBits, SelectionDAG &DAG,
4855 const TargetLowering &TLI, DebugLoc dl) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004856 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00004857 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004858 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4859 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004860 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004861 DAG.getConstant(NumBits,
4862 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004863}
4864
Dan Gohman475871a2008-07-27 21:46:04 +00004865SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004866X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004867 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004868
Evan Chengc3630942009-12-09 21:00:30 +00004869 // Check if the scalar load can be widened into a vector load. And if
4870 // the address is "base + cst" see if the cst can be "absorbed" into
4871 // the shuffle mask.
4872 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4873 SDValue Ptr = LD->getBasePtr();
4874 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4875 return SDValue();
4876 EVT PVT = LD->getValueType(0);
4877 if (PVT != MVT::i32 && PVT != MVT::f32)
4878 return SDValue();
4879
4880 int FI = -1;
4881 int64_t Offset = 0;
4882 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4883 FI = FINode->getIndex();
4884 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004885 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004886 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4887 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4888 Offset = Ptr.getConstantOperandVal(1);
4889 Ptr = Ptr.getOperand(0);
4890 } else {
4891 return SDValue();
4892 }
4893
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004894 // FIXME: 256-bit vector instructions don't require a strict alignment,
4895 // improve this code to support it better.
4896 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00004897 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004898 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00004899 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004900 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00004901 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004902 // Can't change the alignment. FIXME: It's possible to compute
4903 // the exact stack offset and reference FI + adjust offset instead.
4904 // If someone *really* cares about this. That's the way to implement it.
4905 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004906 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004907 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00004908 }
4909 }
4910
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004911 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00004912 // Ptr + (Offset & ~15).
4913 if (Offset < 0)
4914 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004915 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00004916 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004917 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00004918 if (StartOffset)
4919 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4920 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4921
4922 int EltNo = (Offset - StartOffset) >> 2;
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004923 int NumElems = VT.getVectorNumElements();
4924
4925 EVT CanonVT = VT.getSizeInBits() == 128 ? MVT::v4i32 : MVT::v8i32;
4926 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
4927 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00004928 LD->getPointerInfo().getWithOffset(StartOffset),
David Greene67c9d422010-02-15 16:53:33 +00004929 false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004930
4931 // Canonicalize it to a v4i32 or v8i32 shuffle.
4932 SmallVector<int, 8> Mask;
4933 for (int i = 0; i < NumElems; ++i)
4934 Mask.push_back(EltNo);
4935
4936 V1 = DAG.getNode(ISD::BITCAST, dl, CanonVT, V1);
4937 return DAG.getNode(ISD::BITCAST, dl, NVT,
4938 DAG.getVectorShuffle(CanonVT, dl, V1,
4939 DAG.getUNDEF(CanonVT),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00004940 }
4941
4942 return SDValue();
4943}
4944
Michael J. Spencerec38de22010-10-10 22:04:20 +00004945/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4946/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004947/// load which has the same value as a build_vector whose operands are 'elts'.
4948///
4949/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004950///
Nate Begeman1449f292010-03-24 22:19:06 +00004951/// FIXME: we'd also like to handle the case where the last elements are zero
4952/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4953/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004954static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004955 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004956 EVT EltVT = VT.getVectorElementType();
4957 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004958
Nate Begemanfdea31a2010-03-24 20:49:50 +00004959 LoadSDNode *LDBase = NULL;
4960 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004961
Nate Begeman1449f292010-03-24 22:19:06 +00004962 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004963 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004964 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004965 for (unsigned i = 0; i < NumElems; ++i) {
4966 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004967
Nate Begemanfdea31a2010-03-24 20:49:50 +00004968 if (!Elt.getNode() ||
4969 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4970 return SDValue();
4971 if (!LDBase) {
4972 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4973 return SDValue();
4974 LDBase = cast<LoadSDNode>(Elt.getNode());
4975 LastLoadedElt = i;
4976 continue;
4977 }
4978 if (Elt.getOpcode() == ISD::UNDEF)
4979 continue;
4980
4981 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4982 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4983 return SDValue();
4984 LastLoadedElt = i;
4985 }
Nate Begeman1449f292010-03-24 22:19:06 +00004986
4987 // If we have found an entire vector of loads and undefs, then return a large
4988 // load of the entire vector width starting at the base pointer. If we found
4989 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004990 if (LastLoadedElt == NumElems - 1) {
4991 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00004992 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004993 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004994 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00004995 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004996 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004997 LDBase->isVolatile(), LDBase->isNonTemporal(),
4998 LDBase->getAlignment());
Eli Friedman61cc47e2011-07-26 21:02:58 +00004999 } else if (NumElems == 4 && LastLoadedElt == 1 &&
5000 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005001 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
5002 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Chris Lattner88641552010-09-22 00:34:38 +00005003 SDValue ResNode = DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys,
5004 Ops, 2, MVT::i32,
5005 LDBase->getMemOperand());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005006 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00005007 }
5008 return SDValue();
5009}
5010
Evan Chengc3630942009-12-09 21:00:30 +00005011SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005012X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005013 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00005014
David Greenef125a292011-02-08 19:04:41 +00005015 EVT VT = Op.getValueType();
5016 EVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00005017 unsigned NumElems = Op.getNumOperands();
5018
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005019 // Vectors containing all zeros can be matched by pxor and xorps later
5020 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5021 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
5022 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00005023 if (Op.getValueType() == MVT::v4i32 ||
5024 Op.getValueType() == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00005025 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005026
Dale Johannesenace16102009-02-03 19:33:06 +00005027 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00005028 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005029
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005030 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
5031 // vectors or broken into v4i32 operations on 256-bit vectors.
5032 if (ISD::isBuildVectorAllOnes(Op.getNode())) {
5033 if (Op.getValueType() == MVT::v4i32)
5034 return Op;
5035
5036 return getOnesVector(Op.getValueType(), DAG, dl);
5037 }
5038
Owen Andersone50ed302009-08-10 22:56:29 +00005039 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005040
Evan Cheng0db9fe62006-04-25 20:13:52 +00005041 unsigned NumZero = 0;
5042 unsigned NumNonZero = 0;
5043 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005044 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005045 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005046 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005047 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00005048 if (Elt.getOpcode() == ISD::UNDEF)
5049 continue;
5050 Values.insert(Elt);
5051 if (Elt.getOpcode() != ISD::Constant &&
5052 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005053 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00005054 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00005055 NumZero++;
5056 else {
5057 NonZeros |= (1 << i);
5058 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005059 }
5060 }
5061
Chris Lattner97a2a562010-08-26 05:24:29 +00005062 // All undef vector. Return an UNDEF. All zero vectors were handled above.
5063 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00005064 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005065
Chris Lattner67f453a2008-03-09 05:42:06 +00005066 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00005067 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005068 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00005069 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005070
Chris Lattner62098042008-03-09 01:05:04 +00005071 // If this is an insertion of an i64 value on x86-32, and if the top bits of
5072 // the value are obviously zero, truncate the value to i32 and do the
5073 // insertion that way. Only do this if the value is non-constant or if the
5074 // value is a constant being inserted into element 0. It is cheaper to do
5075 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00005076 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00005077 (!IsAllConstants || Idx == 0)) {
5078 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00005079 // Handle SSE only.
5080 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
5081 EVT VecVT = MVT::v4i32;
5082 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00005083
Chris Lattner62098042008-03-09 01:05:04 +00005084 // Truncate the value (which may itself be a constant) to i32, and
5085 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00005086 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00005087 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00005088 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
5089 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005090
Chris Lattner62098042008-03-09 01:05:04 +00005091 // Now we have our 32-bit value zero extended in the low element of
5092 // a vector. If Idx != 0, swizzle it into place.
5093 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005094 SmallVector<int, 4> Mask;
5095 Mask.push_back(Idx);
5096 for (unsigned i = 1; i != VecElts; ++i)
5097 Mask.push_back(i);
5098 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00005099 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00005100 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00005101 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005102 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00005103 }
5104 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005105
Chris Lattner19f79692008-03-08 22:59:52 +00005106 // If we have a constant or non-constant insertion into the low element of
5107 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5108 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00005109 // depending on what the source datatype is.
5110 if (Idx == 0) {
5111 if (NumZero == 0) {
5112 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00005113 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
5114 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00005115 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5116 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
5117 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
5118 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00005119 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
5120 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005121 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
5122 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00005123 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
5124 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
5125 Subtarget->hasSSE2(), DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005126 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00005127 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005128 }
Evan Chengf26ffe92008-05-29 08:22:04 +00005129
5130 // Is it a vector logical left shift?
5131 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00005132 X86::isZeroNode(Op.getOperand(0)) &&
5133 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005134 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00005135 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00005136 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005137 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00005138 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005139 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005140
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005141 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00005142 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005143
Chris Lattner19f79692008-03-08 22:59:52 +00005144 // Otherwise, if this is a vector with i32 or f32 elements, and the element
5145 // is a non-constant being inserted into an element other than the low one,
5146 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
5147 // movd/movss) to move this into the low element, then shuffle it into
5148 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005149 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00005150 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00005151
Evan Cheng0db9fe62006-04-25 20:13:52 +00005152 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00005153 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
5154 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00005155 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005156 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00005157 MaskVec.push_back(i == Idx ? 0 : 1);
5158 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005159 }
5160 }
5161
Chris Lattner67f453a2008-03-09 05:42:06 +00005162 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00005163 if (Values.size() == 1) {
5164 if (EVTBits == 32) {
5165 // Instead of a shuffle like this:
5166 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5167 // Check if it's possible to issue this instead.
5168 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
5169 unsigned Idx = CountTrailingZeros_32(NonZeros);
5170 SDValue Item = Op.getOperand(Idx);
5171 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5172 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5173 }
Dan Gohman475871a2008-07-27 21:46:04 +00005174 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005175 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005176
Dan Gohmana3941172007-07-24 22:55:08 +00005177 // A vector full of immediates; various special cases are already
5178 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005179 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00005180 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00005181
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005182 // For AVX-length vectors, build the individual 128-bit pieces and use
5183 // shuffles to put them in place.
5184 if (VT.getSizeInBits() == 256 && !ISD::isBuildVectorAllZeros(Op.getNode())) {
5185 SmallVector<SDValue, 32> V;
5186 for (unsigned i = 0; i < NumElems; ++i)
5187 V.push_back(Op.getOperand(i));
5188
5189 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
5190
5191 // Build both the lower and upper subvector.
5192 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
5193 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
5194 NumElems/2);
5195
5196 // Recreate the wider vector with the lower and upper part.
Bruno Cardoso Lopes15d03fb2011-07-28 01:26:53 +00005197 SDValue Vec = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), Lower,
5198 DAG.getConstant(0, MVT::i32), DAG, dl);
5199 return Insert128BitVector(Vec, Upper, DAG.getConstant(NumElems/2, MVT::i32),
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005200 DAG, dl);
5201 }
5202
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005203 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005204 if (EVTBits == 64) {
5205 if (NumNonZero == 1) {
5206 // One half is zero or undef.
5207 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005208 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005209 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00005210 return getShuffleVectorZeroOrUndef(V2, Idx, true,
5211 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005212 }
Dan Gohman475871a2008-07-27 21:46:04 +00005213 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005214 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005215
5216 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005217 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005218 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00005219 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005220 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005221 }
5222
Bill Wendling826f36f2007-03-28 00:57:11 +00005223 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005224 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00005225 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005226 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005227 }
5228
5229 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00005230 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00005231 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005232 if (NumElems == 4 && NumZero > 0) {
5233 for (unsigned i = 0; i < 4; ++i) {
5234 bool isZero = !(NonZeros & (1 << i));
5235 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00005236 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005237 else
Dale Johannesenace16102009-02-03 19:33:06 +00005238 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005239 }
5240
5241 for (unsigned i = 0; i < 2; ++i) {
5242 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5243 default: break;
5244 case 0:
5245 V[i] = V[i*2]; // Must be a zero vector.
5246 break;
5247 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005248 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005249 break;
5250 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005251 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005252 break;
5253 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005254 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005255 break;
5256 }
5257 }
5258
Nate Begeman9008ca62009-04-27 18:41:29 +00005259 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005260 bool Reverse = (NonZeros & 0x3) == 2;
5261 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005262 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005263 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5264 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005265 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
5266 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005267 }
5268
Nate Begemanfdea31a2010-03-24 20:49:50 +00005269 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
5270 // Check for a build vector of consecutive loads.
5271 for (unsigned i = 0; i < NumElems; ++i)
5272 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005273
Nate Begemanfdea31a2010-03-24 20:49:50 +00005274 // Check for elements which are consecutive loads.
5275 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5276 if (LD.getNode())
5277 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005278
5279 // For SSE 4.1, use insertps to put the high elements into the low element.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005280 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005281 SDValue Result;
5282 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5283 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5284 else
5285 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005286
Chris Lattner24faf612010-08-28 17:59:08 +00005287 for (unsigned i = 1; i < NumElems; ++i) {
5288 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5289 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005290 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005291 }
5292 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005293 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005294
Chris Lattner6e80e442010-08-28 17:15:43 +00005295 // Otherwise, expand into a number of unpckl*, start by extending each of
5296 // our (non-undef) elements to the full vector width with the element in the
5297 // bottom slot of the vector (which generates no code for SSE).
5298 for (unsigned i = 0; i < NumElems; ++i) {
5299 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5300 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5301 else
5302 V[i] = DAG.getUNDEF(VT);
5303 }
5304
5305 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005306 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5307 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5308 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005309 unsigned EltStride = NumElems >> 1;
5310 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005311 for (unsigned i = 0; i < EltStride; ++i) {
5312 // If V[i+EltStride] is undef and this is the first round of mixing,
5313 // then it is safe to just drop this shuffle: V[i] is already in the
5314 // right place, the one element (since it's the first round) being
5315 // inserted as undef can be dropped. This isn't safe for successive
5316 // rounds because they will permute elements within both vectors.
5317 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5318 EltStride == NumElems/2)
5319 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005320
Chris Lattner6e80e442010-08-28 17:15:43 +00005321 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005322 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005323 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005324 }
5325 return V[0];
5326 }
Dan Gohman475871a2008-07-27 21:46:04 +00005327 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005328}
5329
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005330// LowerMMXCONCAT_VECTORS - We support concatenate two MMX registers and place
5331// them in a MMX register. This is better than doing a stack convert.
5332static SDValue LowerMMXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005333 DebugLoc dl = Op.getDebugLoc();
5334 EVT ResVT = Op.getValueType();
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005335
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005336 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
5337 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
5338 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005339 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005340 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
5341 InVec = Op.getOperand(1);
5342 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5343 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005344 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005345 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
5346 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
5347 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005348 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005349 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
5350 Mask[0] = 0; Mask[1] = 2;
5351 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
5352 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005353 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005354}
5355
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005356// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5357// to create 256-bit vectors from two other 128-bit ones.
5358static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5359 DebugLoc dl = Op.getDebugLoc();
5360 EVT ResVT = Op.getValueType();
5361
5362 assert(ResVT.getSizeInBits() == 256 && "Value type must be 256-bit wide");
5363
5364 SDValue V1 = Op.getOperand(0);
5365 SDValue V2 = Op.getOperand(1);
5366 unsigned NumElems = ResVT.getVectorNumElements();
5367
5368 SDValue V = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, ResVT), V1,
5369 DAG.getConstant(0, MVT::i32), DAG, dl);
5370 return Insert128BitVector(V, V2, DAG.getConstant(NumElems/2, MVT::i32),
5371 DAG, dl);
5372}
5373
5374SDValue
5375X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005376 EVT ResVT = Op.getValueType();
5377
5378 assert(Op.getNumOperands() == 2);
5379 assert((ResVT.getSizeInBits() == 128 || ResVT.getSizeInBits() == 256) &&
5380 "Unsupported CONCAT_VECTORS for value type");
5381
5382 // We support concatenate two MMX registers and place them in a MMX register.
5383 // This is better than doing a stack convert.
5384 if (ResVT.is128BitVector())
5385 return LowerMMXCONCAT_VECTORS(Op, DAG);
5386
5387 // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
5388 // from two other 128-bit ones.
5389 return LowerAVXCONCAT_VECTORS(Op, DAG);
5390}
5391
Nate Begemanb9a47b82009-02-23 08:49:38 +00005392// v8i16 shuffles - Prefer shuffles in the following order:
5393// 1. [all] pshuflw, pshufhw, optional move
5394// 2. [ssse3] 1 x pshufb
5395// 3. [ssse3] 2 x pshufb + 1 x por
5396// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005397SDValue
5398X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
5399 SelectionDAG &DAG) const {
5400 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00005401 SDValue V1 = SVOp->getOperand(0);
5402 SDValue V2 = SVOp->getOperand(1);
5403 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005404 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00005405
Nate Begemanb9a47b82009-02-23 08:49:38 +00005406 // Determine if more than 1 of the words in each of the low and high quadwords
5407 // of the result come from the same quadword of one of the two inputs. Undef
5408 // mask values count as coming from any quadword, for better codegen.
5409 SmallVector<unsigned, 4> LoQuad(4);
5410 SmallVector<unsigned, 4> HiQuad(4);
5411 BitVector InputQuads(4);
5412 for (unsigned i = 0; i < 8; ++i) {
5413 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00005414 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005415 MaskVals.push_back(EltIdx);
5416 if (EltIdx < 0) {
5417 ++Quad[0];
5418 ++Quad[1];
5419 ++Quad[2];
5420 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00005421 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005422 }
5423 ++Quad[EltIdx / 4];
5424 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00005425 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005426
Nate Begemanb9a47b82009-02-23 08:49:38 +00005427 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005428 unsigned MaxQuad = 1;
5429 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005430 if (LoQuad[i] > MaxQuad) {
5431 BestLoQuad = i;
5432 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005433 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005434 }
5435
Nate Begemanb9a47b82009-02-23 08:49:38 +00005436 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005437 MaxQuad = 1;
5438 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005439 if (HiQuad[i] > MaxQuad) {
5440 BestHiQuad = i;
5441 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005442 }
5443 }
5444
Nate Begemanb9a47b82009-02-23 08:49:38 +00005445 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00005446 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00005447 // single pshufb instruction is necessary. If There are more than 2 input
5448 // quads, disable the next transformation since it does not help SSSE3.
5449 bool V1Used = InputQuads[0] || InputQuads[1];
5450 bool V2Used = InputQuads[2] || InputQuads[3];
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005451 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005452 if (InputQuads.count() == 2 && V1Used && V2Used) {
5453 BestLoQuad = InputQuads.find_first();
5454 BestHiQuad = InputQuads.find_next(BestLoQuad);
5455 }
5456 if (InputQuads.count() > 2) {
5457 BestLoQuad = -1;
5458 BestHiQuad = -1;
5459 }
5460 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005461
Nate Begemanb9a47b82009-02-23 08:49:38 +00005462 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5463 // the shuffle mask. If a quad is scored as -1, that means that it contains
5464 // words from all 4 input quadwords.
5465 SDValue NewV;
5466 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005467 SmallVector<int, 8> MaskV;
5468 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
5469 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00005470 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005471 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
5472 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
5473 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005474
Nate Begemanb9a47b82009-02-23 08:49:38 +00005475 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5476 // source words for the shuffle, to aid later transformations.
5477 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00005478 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00005479 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005480 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00005481 if (idx != (int)i)
5482 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005483 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00005484 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005485 AllWordsInNewV = false;
5486 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00005487 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005488
Nate Begemanb9a47b82009-02-23 08:49:38 +00005489 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5490 if (AllWordsInNewV) {
5491 for (int i = 0; i != 8; ++i) {
5492 int idx = MaskVals[i];
5493 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005494 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005495 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005496 if ((idx != i) && idx < 4)
5497 pshufhw = false;
5498 if ((idx != i) && idx > 3)
5499 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00005500 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005501 V1 = NewV;
5502 V2Used = false;
5503 BestLoQuad = 0;
5504 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005505 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005506
Nate Begemanb9a47b82009-02-23 08:49:38 +00005507 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5508 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005509 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005510 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5511 unsigned TargetMask = 0;
5512 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005513 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005514 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
5515 X86::getShufflePSHUFLWImmediate(NewV.getNode());
5516 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005517 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005518 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005519 }
Eric Christopherfd179292009-08-27 18:07:15 +00005520
Nate Begemanb9a47b82009-02-23 08:49:38 +00005521 // If we have SSSE3, and all words of the result are from 1 input vector,
5522 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5523 // is present, fall back to case 4.
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005524 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005525 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005526
Nate Begemanb9a47b82009-02-23 08:49:38 +00005527 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005528 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005529 // mask, and elements that come from V1 in the V2 mask, so that the two
5530 // results can be OR'd together.
5531 bool TwoInputs = V1Used && V2Used;
5532 for (unsigned i = 0; i != 8; ++i) {
5533 int EltIdx = MaskVals[i] * 2;
5534 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005535 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5536 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005537 continue;
5538 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005539 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
5540 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005541 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005542 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005543 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005544 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005545 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005546 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005547 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005548
Nate Begemanb9a47b82009-02-23 08:49:38 +00005549 // Calculate the shuffle mask for the second input, shuffle it, and
5550 // OR it with the first shuffled input.
5551 pshufbMask.clear();
5552 for (unsigned i = 0; i != 8; ++i) {
5553 int EltIdx = MaskVals[i] * 2;
5554 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005555 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5556 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005557 continue;
5558 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005559 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
5560 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005561 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005562 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005563 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005564 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005565 MVT::v16i8, &pshufbMask[0], 16));
5566 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005567 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005568 }
5569
5570 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5571 // and update MaskVals with new element order.
5572 BitVector InOrder(8);
5573 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005574 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005575 for (int i = 0; i != 4; ++i) {
5576 int idx = MaskVals[i];
5577 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005578 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005579 InOrder.set(i);
5580 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005581 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005582 InOrder.set(i);
5583 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005584 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005585 }
5586 }
5587 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005588 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00005589 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005590 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005591
5592 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
5593 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
5594 NewV.getOperand(0),
5595 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
5596 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005597 }
Eric Christopherfd179292009-08-27 18:07:15 +00005598
Nate Begemanb9a47b82009-02-23 08:49:38 +00005599 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5600 // and update MaskVals with the new element order.
5601 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005602 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005603 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005604 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005605 for (unsigned i = 4; i != 8; ++i) {
5606 int idx = MaskVals[i];
5607 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005608 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005609 InOrder.set(i);
5610 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005611 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005612 InOrder.set(i);
5613 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005614 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005615 }
5616 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005617 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005618 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005619
5620 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
5621 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
5622 NewV.getOperand(0),
5623 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
5624 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005625 }
Eric Christopherfd179292009-08-27 18:07:15 +00005626
Nate Begemanb9a47b82009-02-23 08:49:38 +00005627 // In case BestHi & BestLo were both -1, which means each quadword has a word
5628 // from each of the four input quadwords, calculate the InOrder bitvector now
5629 // before falling through to the insert/extract cleanup.
5630 if (BestLoQuad == -1 && BestHiQuad == -1) {
5631 NewV = V1;
5632 for (int i = 0; i != 8; ++i)
5633 if (MaskVals[i] < 0 || MaskVals[i] == i)
5634 InOrder.set(i);
5635 }
Eric Christopherfd179292009-08-27 18:07:15 +00005636
Nate Begemanb9a47b82009-02-23 08:49:38 +00005637 // The other elements are put in the right place using pextrw and pinsrw.
5638 for (unsigned i = 0; i != 8; ++i) {
5639 if (InOrder[i])
5640 continue;
5641 int EltIdx = MaskVals[i];
5642 if (EltIdx < 0)
5643 continue;
5644 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00005645 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005646 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00005647 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005648 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005649 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005650 DAG.getIntPtrConstant(i));
5651 }
5652 return NewV;
5653}
5654
5655// v16i8 shuffles - Prefer shuffles in the following order:
5656// 1. [ssse3] 1 x pshufb
5657// 2. [ssse3] 2 x pshufb + 1 x por
5658// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5659static
Nate Begeman9008ca62009-04-27 18:41:29 +00005660SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005661 SelectionDAG &DAG,
5662 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005663 SDValue V1 = SVOp->getOperand(0);
5664 SDValue V2 = SVOp->getOperand(1);
5665 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005666 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00005667 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00005668
Nate Begemanb9a47b82009-02-23 08:49:38 +00005669 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005670 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005671 // present, fall back to case 3.
5672 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
5673 bool V1Only = true;
5674 bool V2Only = true;
5675 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005676 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00005677 if (EltIdx < 0)
5678 continue;
5679 if (EltIdx < 16)
5680 V2Only = false;
5681 else
5682 V1Only = false;
5683 }
Eric Christopherfd179292009-08-27 18:07:15 +00005684
Nate Begemanb9a47b82009-02-23 08:49:38 +00005685 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
5686 if (TLI.getSubtarget()->hasSSSE3()) {
5687 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005688
Nate Begemanb9a47b82009-02-23 08:49:38 +00005689 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005690 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005691 //
5692 // Otherwise, we have elements from both input vectors, and must zero out
5693 // elements that come from V2 in the first mask, and V1 in the second mask
5694 // so that we can OR them together.
5695 bool TwoInputs = !(V1Only || V2Only);
5696 for (unsigned i = 0; i != 16; ++i) {
5697 int EltIdx = MaskVals[i];
5698 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005699 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005700 continue;
5701 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005702 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005703 }
5704 // If all the elements are from V2, assign it to V1 and return after
5705 // building the first pshufb.
5706 if (V2Only)
5707 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00005708 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005709 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005710 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005711 if (!TwoInputs)
5712 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005713
Nate Begemanb9a47b82009-02-23 08:49:38 +00005714 // Calculate the shuffle mask for the second input, shuffle it, and
5715 // OR it with the first shuffled input.
5716 pshufbMask.clear();
5717 for (unsigned i = 0; i != 16; ++i) {
5718 int EltIdx = MaskVals[i];
5719 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005720 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005721 continue;
5722 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005723 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005724 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005725 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005726 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005727 MVT::v16i8, &pshufbMask[0], 16));
5728 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005729 }
Eric Christopherfd179292009-08-27 18:07:15 +00005730
Nate Begemanb9a47b82009-02-23 08:49:38 +00005731 // No SSSE3 - Calculate in place words and then fix all out of place words
5732 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5733 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005734 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5735 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005736 SDValue NewV = V2Only ? V2 : V1;
5737 for (int i = 0; i != 8; ++i) {
5738 int Elt0 = MaskVals[i*2];
5739 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005740
Nate Begemanb9a47b82009-02-23 08:49:38 +00005741 // This word of the result is all undef, skip it.
5742 if (Elt0 < 0 && Elt1 < 0)
5743 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005744
Nate Begemanb9a47b82009-02-23 08:49:38 +00005745 // This word of the result is already in the correct place, skip it.
5746 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
5747 continue;
5748 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
5749 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005750
Nate Begemanb9a47b82009-02-23 08:49:38 +00005751 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5752 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5753 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005754
5755 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5756 // using a single extract together, load it and store it.
5757 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005758 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005759 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005760 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005761 DAG.getIntPtrConstant(i));
5762 continue;
5763 }
5764
Nate Begemanb9a47b82009-02-23 08:49:38 +00005765 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00005766 // source byte is not also odd, shift the extracted word left 8 bits
5767 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005768 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005769 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005770 DAG.getIntPtrConstant(Elt1 / 2));
5771 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005772 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00005773 DAG.getConstant(8,
5774 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005775 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005776 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
5777 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005778 }
5779 // If Elt0 is defined, extract it from the appropriate source. If the
5780 // source byte is not also even, shift the extracted word right 8 bits. If
5781 // Elt1 was also defined, OR the extracted values together before
5782 // inserting them in the result.
5783 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005784 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005785 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
5786 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005787 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00005788 DAG.getConstant(8,
5789 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005790 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005791 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
5792 DAG.getConstant(0x00FF, MVT::i16));
5793 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00005794 : InsElt0;
5795 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005796 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005797 DAG.getIntPtrConstant(i));
5798 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005799 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005800}
5801
Evan Cheng7a831ce2007-12-15 03:00:47 +00005802/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005803/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00005804/// done when every pair / quad of shuffle mask elements point to elements in
5805/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005806/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00005807static
Nate Begeman9008ca62009-04-27 18:41:29 +00005808SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005809 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00005810 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00005811 SDValue V1 = SVOp->getOperand(0);
5812 SDValue V2 = SVOp->getOperand(1);
5813 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00005814 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005815 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00005816 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005817 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005818 case MVT::v4f32: NewVT = MVT::v2f64; break;
5819 case MVT::v4i32: NewVT = MVT::v2i64; break;
5820 case MVT::v8i16: NewVT = MVT::v4i32; break;
5821 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00005822 }
5823
Nate Begeman9008ca62009-04-27 18:41:29 +00005824 int Scale = NumElems / NewWidth;
5825 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00005826 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005827 int StartIdx = -1;
5828 for (int j = 0; j < Scale; ++j) {
5829 int EltIdx = SVOp->getMaskElt(i+j);
5830 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005831 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00005832 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00005833 StartIdx = EltIdx - (EltIdx % Scale);
5834 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00005835 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005836 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005837 if (StartIdx == -1)
5838 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00005839 else
Nate Begeman9008ca62009-04-27 18:41:29 +00005840 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005841 }
5842
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005843 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
5844 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00005845 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005846}
5847
Evan Chengd880b972008-05-09 21:53:03 +00005848/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005849///
Owen Andersone50ed302009-08-10 22:56:29 +00005850static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00005851 SDValue SrcOp, SelectionDAG &DAG,
5852 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005853 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005854 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00005855 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00005856 LD = dyn_cast<LoadSDNode>(SrcOp);
5857 if (!LD) {
5858 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
5859 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00005860 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00005861 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00005862 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005863 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00005864 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005865 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00005866 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005867 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005868 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
5869 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
5870 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00005871 SrcOp.getOperand(0)
5872 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005873 }
5874 }
5875 }
5876
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005877 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005878 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005879 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00005880 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005881}
5882
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00005883/// areShuffleHalvesWithinDisjointLanes - Check whether each half of a vector
5884/// shuffle node referes to only one lane in the sources.
5885static bool areShuffleHalvesWithinDisjointLanes(ShuffleVectorSDNode *SVOp) {
5886 EVT VT = SVOp->getValueType(0);
5887 int NumElems = VT.getVectorNumElements();
5888 int HalfSize = NumElems/2;
5889 SmallVector<int, 16> M;
5890 SVOp->getMask(M);
5891 bool MatchA = false, MatchB = false;
5892
5893 for (int l = 0; l < NumElems*2; l += HalfSize) {
5894 if (isUndefOrInRange(M, 0, HalfSize, l, l+HalfSize)) {
5895 MatchA = true;
5896 break;
5897 }
5898 }
5899
5900 for (int l = 0; l < NumElems*2; l += HalfSize) {
5901 if (isUndefOrInRange(M, HalfSize, HalfSize, l, l+HalfSize)) {
5902 MatchB = true;
5903 break;
5904 }
5905 }
5906
5907 return MatchA && MatchB;
5908}
5909
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00005910/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
5911/// which could not be matched by any known target speficic shuffle
5912static SDValue
5913LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00005914 if (areShuffleHalvesWithinDisjointLanes(SVOp)) {
5915 // If each half of a vector shuffle node referes to only one lane in the
5916 // source vectors, extract each used 128-bit lane and shuffle them using
5917 // 128-bit shuffles. Then, concatenate the results. Otherwise leave
5918 // the work to the legalizer.
5919 DebugLoc dl = SVOp->getDebugLoc();
5920 EVT VT = SVOp->getValueType(0);
5921 int NumElems = VT.getVectorNumElements();
5922 int HalfSize = NumElems/2;
5923
5924 // Extract the reference for each half
5925 int FstVecExtractIdx = 0, SndVecExtractIdx = 0;
5926 int FstVecOpNum = 0, SndVecOpNum = 0;
5927 for (int i = 0; i < HalfSize; ++i) {
5928 int Elt = SVOp->getMaskElt(i);
5929 if (SVOp->getMaskElt(i) < 0)
5930 continue;
5931 FstVecOpNum = Elt/NumElems;
5932 FstVecExtractIdx = Elt % NumElems < HalfSize ? 0 : HalfSize;
5933 break;
5934 }
5935 for (int i = HalfSize; i < NumElems; ++i) {
5936 int Elt = SVOp->getMaskElt(i);
5937 if (SVOp->getMaskElt(i) < 0)
5938 continue;
5939 SndVecOpNum = Elt/NumElems;
5940 SndVecExtractIdx = Elt % NumElems < HalfSize ? 0 : HalfSize;
5941 break;
5942 }
5943
5944 // Extract the subvectors
5945 SDValue V1 = Extract128BitVector(SVOp->getOperand(FstVecOpNum),
5946 DAG.getConstant(FstVecExtractIdx, MVT::i32), DAG, dl);
5947 SDValue V2 = Extract128BitVector(SVOp->getOperand(SndVecOpNum),
5948 DAG.getConstant(SndVecExtractIdx, MVT::i32), DAG, dl);
5949
5950 // Generate 128-bit shuffles
5951 SmallVector<int, 16> MaskV1, MaskV2;
5952 for (int i = 0; i < HalfSize; ++i) {
5953 int Elt = SVOp->getMaskElt(i);
5954 MaskV1.push_back(Elt < 0 ? Elt : Elt % HalfSize);
5955 }
5956 for (int i = HalfSize; i < NumElems; ++i) {
5957 int Elt = SVOp->getMaskElt(i);
5958 MaskV2.push_back(Elt < 0 ? Elt : Elt % HalfSize);
5959 }
5960
5961 EVT NVT = V1.getValueType();
5962 V1 = DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &MaskV1[0]);
5963 V2 = DAG.getVectorShuffle(NVT, dl, V2, DAG.getUNDEF(NVT), &MaskV2[0]);
5964
5965 // Concatenate the result back
5966 SDValue V = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), V1,
5967 DAG.getConstant(0, MVT::i32), DAG, dl);
5968 return Insert128BitVector(V, V2, DAG.getConstant(NumElems/2, MVT::i32),
5969 DAG, dl);
5970 }
5971
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00005972 return SDValue();
5973}
5974
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00005975/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
5976/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00005977static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00005978LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005979 SDValue V1 = SVOp->getOperand(0);
5980 SDValue V2 = SVOp->getOperand(1);
5981 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005982 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00005983
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00005984 assert(VT.getSizeInBits() == 128 && "Unsupported vector size");
5985
Evan Chengace3c172008-07-22 21:13:36 +00005986 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00005987 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00005988 SmallVector<int, 8> Mask1(4U, -1);
5989 SmallVector<int, 8> PermMask;
5990 SVOp->getMask(PermMask);
5991
Evan Chengace3c172008-07-22 21:13:36 +00005992 unsigned NumHi = 0;
5993 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00005994 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005995 int Idx = PermMask[i];
5996 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005997 Locs[i] = std::make_pair(-1, -1);
5998 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005999 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
6000 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006001 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00006002 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006003 NumLo++;
6004 } else {
6005 Locs[i] = std::make_pair(1, NumHi);
6006 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00006007 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006008 NumHi++;
6009 }
6010 }
6011 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006012
Evan Chengace3c172008-07-22 21:13:36 +00006013 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006014 // If no more than two elements come from either vector. This can be
6015 // implemented with two shuffles. First shuffle gather the elements.
6016 // The second shuffle, which takes the first shuffle as both of its
6017 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00006018 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006019
Nate Begeman9008ca62009-04-27 18:41:29 +00006020 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00006021
Evan Chengace3c172008-07-22 21:13:36 +00006022 for (unsigned i = 0; i != 4; ++i) {
6023 if (Locs[i].first == -1)
6024 continue;
6025 else {
6026 unsigned Idx = (i < 2) ? 0 : 4;
6027 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006028 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006029 }
6030 }
6031
Nate Begeman9008ca62009-04-27 18:41:29 +00006032 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006033 } else if (NumLo == 3 || NumHi == 3) {
6034 // Otherwise, we must have three elements from one vector, call it X, and
6035 // one element from the other, call it Y. First, use a shufps to build an
6036 // intermediate vector with the one element from Y and the element from X
6037 // that will be in the same half in the final destination (the indexes don't
6038 // matter). Then, use a shufps to build the final vector, taking the half
6039 // containing the element from Y from the intermediate, and the other half
6040 // from X.
6041 if (NumHi == 3) {
6042 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00006043 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006044 std::swap(V1, V2);
6045 }
6046
6047 // Find the element from V2.
6048 unsigned HiIndex;
6049 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006050 int Val = PermMask[HiIndex];
6051 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006052 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006053 if (Val >= 4)
6054 break;
6055 }
6056
Nate Begeman9008ca62009-04-27 18:41:29 +00006057 Mask1[0] = PermMask[HiIndex];
6058 Mask1[1] = -1;
6059 Mask1[2] = PermMask[HiIndex^1];
6060 Mask1[3] = -1;
6061 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006062
6063 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006064 Mask1[0] = PermMask[0];
6065 Mask1[1] = PermMask[1];
6066 Mask1[2] = HiIndex & 1 ? 6 : 4;
6067 Mask1[3] = HiIndex & 1 ? 4 : 6;
6068 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006069 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006070 Mask1[0] = HiIndex & 1 ? 2 : 0;
6071 Mask1[1] = HiIndex & 1 ? 0 : 2;
6072 Mask1[2] = PermMask[2];
6073 Mask1[3] = PermMask[3];
6074 if (Mask1[2] >= 0)
6075 Mask1[2] += 4;
6076 if (Mask1[3] >= 0)
6077 Mask1[3] += 4;
6078 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006079 }
Evan Chengace3c172008-07-22 21:13:36 +00006080 }
6081
6082 // Break it into (shuffle shuffle_hi, shuffle_lo).
6083 Locs.clear();
David Greenec4db4e52011-02-28 19:06:56 +00006084 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00006085 SmallVector<int,8> LoMask(4U, -1);
6086 SmallVector<int,8> HiMask(4U, -1);
6087
6088 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00006089 unsigned MaskIdx = 0;
6090 unsigned LoIdx = 0;
6091 unsigned HiIdx = 2;
6092 for (unsigned i = 0; i != 4; ++i) {
6093 if (i == 2) {
6094 MaskPtr = &HiMask;
6095 MaskIdx = 1;
6096 LoIdx = 0;
6097 HiIdx = 2;
6098 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006099 int Idx = PermMask[i];
6100 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006101 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00006102 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006103 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00006104 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006105 LoIdx++;
6106 } else {
6107 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00006108 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006109 HiIdx++;
6110 }
6111 }
6112
Nate Begeman9008ca62009-04-27 18:41:29 +00006113 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6114 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
6115 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00006116 for (unsigned i = 0; i != 4; ++i) {
6117 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006118 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00006119 } else {
6120 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006121 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00006122 }
6123 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006124 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006125}
6126
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006127static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006128 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006129 V = V.getOperand(0);
6130 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6131 V = V.getOperand(0);
6132 if (MayFoldLoad(V))
6133 return true;
6134 return false;
6135}
6136
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006137// FIXME: the version above should always be used. Since there's
6138// a bug where several vector shuffles can't be folded because the
6139// DAG is not updated during lowering and a node claims to have two
6140// uses while it only has one, use this version, and let isel match
6141// another instruction if the load really happens to have more than
6142// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00006143// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006144static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006145 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006146 V = V.getOperand(0);
6147 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6148 V = V.getOperand(0);
6149 if (ISD::isNormalLoad(V.getNode()))
6150 return true;
6151 return false;
6152}
6153
6154/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
6155/// a vector extract, and if both can be later optimized into a single load.
6156/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
6157/// here because otherwise a target specific shuffle node is going to be
6158/// emitted for this shuffle, and the optimization not done.
6159/// FIXME: This is probably not the best approach, but fix the problem
6160/// until the right path is decided.
6161static
6162bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
6163 const TargetLowering &TLI) {
6164 EVT VT = V.getValueType();
6165 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
6166
6167 // Be sure that the vector shuffle is present in a pattern like this:
6168 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
6169 if (!V.hasOneUse())
6170 return false;
6171
6172 SDNode *N = *V.getNode()->use_begin();
6173 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
6174 return false;
6175
6176 SDValue EltNo = N->getOperand(1);
6177 if (!isa<ConstantSDNode>(EltNo))
6178 return false;
6179
6180 // If the bit convert changed the number of elements, it is unsafe
6181 // to examine the mask.
6182 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006183 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006184 EVT SrcVT = V.getOperand(0).getValueType();
6185 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
6186 return false;
6187 V = V.getOperand(0);
6188 HasShuffleIntoBitcast = true;
6189 }
6190
6191 // Select the input vector, guarding against out of range extract vector.
6192 unsigned NumElems = VT.getVectorNumElements();
6193 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
6194 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
6195 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
6196
6197 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006198 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006199 V = V.getOperand(0);
6200
6201 if (ISD::isNormalLoad(V.getNode())) {
6202 // Is the original load suitable?
6203 LoadSDNode *LN0 = cast<LoadSDNode>(V);
6204
6205 // FIXME: avoid the multi-use bug that is preventing lots of
6206 // of foldings to be detected, this is still wrong of course, but
6207 // give the temporary desired behavior, and if it happens that
6208 // the load has real more uses, during isel it will not fold, and
6209 // will generate poor code.
6210 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
6211 return false;
6212
6213 if (!HasShuffleIntoBitcast)
6214 return true;
6215
6216 // If there's a bitcast before the shuffle, check if the load type and
6217 // alignment is valid.
6218 unsigned Align = LN0->getAlignment();
6219 unsigned NewAlign =
6220 TLI.getTargetData()->getABITypeAlignment(
6221 VT.getTypeForEVT(*DAG.getContext()));
6222
6223 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
6224 return false;
6225 }
6226
6227 return true;
6228}
6229
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006230static
Evan Cheng835580f2010-10-07 20:50:20 +00006231SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
6232 EVT VT = Op.getValueType();
6233
6234 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006235 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6236 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006237 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6238 V1, DAG));
6239}
6240
6241static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006242SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
6243 bool HasSSE2) {
6244 SDValue V1 = Op.getOperand(0);
6245 SDValue V2 = Op.getOperand(1);
6246 EVT VT = Op.getValueType();
6247
6248 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6249
6250 if (HasSSE2 && VT == MVT::v2f64)
6251 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6252
Evan Cheng0899f5c2011-08-31 02:05:24 +00006253 // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
6254 return DAG.getNode(ISD::BITCAST, dl, VT,
6255 getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
6256 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
6257 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006258}
6259
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006260static
6261SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
6262 SDValue V1 = Op.getOperand(0);
6263 SDValue V2 = Op.getOperand(1);
6264 EVT VT = Op.getValueType();
6265
6266 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6267 "unsupported shuffle type");
6268
6269 if (V2.getOpcode() == ISD::UNDEF)
6270 V2 = V1;
6271
6272 // v4i32 or v4f32
6273 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6274}
6275
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006276static inline unsigned getSHUFPOpcode(EVT VT) {
6277 switch(VT.getSimpleVT().SimpleTy) {
6278 case MVT::v8i32: // Use fp unit for int unpack.
6279 case MVT::v8f32:
6280 case MVT::v4i32: // Use fp unit for int unpack.
6281 case MVT::v4f32: return X86ISD::SHUFPS;
6282 case MVT::v4i64: // Use fp unit for int unpack.
6283 case MVT::v4f64:
6284 case MVT::v2i64: // Use fp unit for int unpack.
6285 case MVT::v2f64: return X86ISD::SHUFPD;
6286 default:
6287 llvm_unreachable("Unknown type for shufp*");
6288 }
6289 return 0;
6290}
6291
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006292static
6293SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
6294 SDValue V1 = Op.getOperand(0);
6295 SDValue V2 = Op.getOperand(1);
6296 EVT VT = Op.getValueType();
6297 unsigned NumElems = VT.getVectorNumElements();
6298
6299 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6300 // operand of these instructions is only memory, so check if there's a
6301 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6302 // same masks.
6303 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006304
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006305 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006306 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006307 CanFoldLoad = true;
6308
6309 // When V1 is a load, it can be folded later into a store in isel, example:
6310 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6311 // turns into:
6312 // (MOVLPSmr addr:$src1, VR128:$src2)
6313 // So, recognize this potential and also use MOVLPS or MOVLPD
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006314 if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006315 CanFoldLoad = true;
6316
Eric Christopher893a8822011-02-20 05:04:42 +00006317 // Both of them can't be memory operations though.
6318 if (MayFoldVectorLoad(V1) && MayFoldVectorLoad(V2))
6319 CanFoldLoad = false;
Owen Anderson95771af2011-02-25 21:41:48 +00006320
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006321 if (CanFoldLoad) {
6322 if (HasSSE2 && NumElems == 2)
6323 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6324
6325 if (NumElems == 4)
6326 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
6327 }
6328
6329 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6330 // movl and movlp will both match v2i64, but v2i64 is never matched by
6331 // movl earlier because we make it strict to avoid messing with the movlp load
6332 // folding logic (see the code above getMOVLP call). Match it here then,
6333 // this is horrible, but will stay like this until we move all shuffle
6334 // matching to x86 specific nodes. Note that for the 1st condition all
6335 // types are matched with movsd.
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006336 if (HasSSE2) {
6337 if (NumElems == 2)
6338 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006339 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006340 }
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006341
6342 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6343
6344 // Invert the operand order and use SHUFPS to match it.
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006345 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V2, V1,
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006346 X86::getShuffleSHUFImmediate(SVOp), DAG);
6347}
6348
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006349static inline unsigned getUNPCKLOpcode(EVT VT) {
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006350 switch(VT.getSimpleVT().SimpleTy) {
6351 case MVT::v4i32: return X86ISD::PUNPCKLDQ;
6352 case MVT::v2i64: return X86ISD::PUNPCKLQDQ;
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00006353 case MVT::v4f32: return X86ISD::UNPCKLPS;
6354 case MVT::v2f64: return X86ISD::UNPCKLPD;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006355 case MVT::v8i32: // Use fp unit for int unpack.
David Greenec4db4e52011-02-28 19:06:56 +00006356 case MVT::v8f32: return X86ISD::VUNPCKLPSY;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006357 case MVT::v4i64: // Use fp unit for int unpack.
David Greenec4db4e52011-02-28 19:06:56 +00006358 case MVT::v4f64: return X86ISD::VUNPCKLPDY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006359 case MVT::v16i8: return X86ISD::PUNPCKLBW;
6360 case MVT::v8i16: return X86ISD::PUNPCKLWD;
6361 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00006362 llvm_unreachable("Unknown type for unpckl");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006363 }
6364 return 0;
6365}
6366
6367static inline unsigned getUNPCKHOpcode(EVT VT) {
6368 switch(VT.getSimpleVT().SimpleTy) {
6369 case MVT::v4i32: return X86ISD::PUNPCKHDQ;
6370 case MVT::v2i64: return X86ISD::PUNPCKHQDQ;
6371 case MVT::v4f32: return X86ISD::UNPCKHPS;
6372 case MVT::v2f64: return X86ISD::UNPCKHPD;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006373 case MVT::v8i32: // Use fp unit for int unpack.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00006374 case MVT::v8f32: return X86ISD::VUNPCKHPSY;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006375 case MVT::v4i64: // Use fp unit for int unpack.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00006376 case MVT::v4f64: return X86ISD::VUNPCKHPDY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006377 case MVT::v16i8: return X86ISD::PUNPCKHBW;
6378 case MVT::v8i16: return X86ISD::PUNPCKHWD;
6379 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00006380 llvm_unreachable("Unknown type for unpckh");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006381 }
6382 return 0;
6383}
6384
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006385static inline unsigned getVPERMILOpcode(EVT VT) {
6386 switch(VT.getSimpleVT().SimpleTy) {
6387 case MVT::v4i32:
6388 case MVT::v4f32: return X86ISD::VPERMILPS;
6389 case MVT::v2i64:
6390 case MVT::v2f64: return X86ISD::VPERMILPD;
6391 case MVT::v8i32:
6392 case MVT::v8f32: return X86ISD::VPERMILPSY;
6393 case MVT::v4i64:
6394 case MVT::v4f64: return X86ISD::VPERMILPDY;
6395 default:
6396 llvm_unreachable("Unknown type for vpermil");
6397 }
6398 return 0;
6399}
6400
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006401/// isVectorBroadcast - Check if the node chain is suitable to be xformed to
6402/// a vbroadcast node. The nodes are suitable whenever we can fold a load coming
6403/// from a 32 or 64 bit scalar. Update Op to the desired load to be folded.
6404static bool isVectorBroadcast(SDValue &Op) {
6405 EVT VT = Op.getValueType();
6406 bool Is256 = VT.getSizeInBits() == 256;
6407
6408 assert((VT.getSizeInBits() == 128 || Is256) &&
6409 "Unsupported type for vbroadcast node");
6410
6411 SDValue V = Op;
6412 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
6413 V = V.getOperand(0);
6414
6415 if (Is256 && !(V.hasOneUse() &&
6416 V.getOpcode() == ISD::INSERT_SUBVECTOR &&
6417 V.getOperand(0).getOpcode() == ISD::UNDEF))
6418 return false;
6419
6420 if (Is256)
6421 V = V.getOperand(1);
Bruno Cardoso Lopesa39ccdb2011-09-01 18:15:06 +00006422
6423 if (!V.hasOneUse())
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006424 return false;
6425
6426 // Check the source scalar_to_vector type. 256-bit broadcasts are
6427 // supported for 32/64-bit sizes, while 128-bit ones are only supported
6428 // for 32-bit scalars.
Bruno Cardoso Lopesa39ccdb2011-09-01 18:15:06 +00006429 if (V.getOpcode() != ISD::SCALAR_TO_VECTOR)
6430 return false;
6431
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006432 unsigned ScalarSize = V.getOperand(0).getValueType().getSizeInBits();
6433 if (ScalarSize != 32 && ScalarSize != 64)
6434 return false;
6435 if (!Is256 && ScalarSize == 64)
6436 return false;
6437
6438 V = V.getOperand(0);
6439 if (!MayFoldLoad(V))
6440 return false;
6441
6442 // Return the load node
6443 Op = V;
6444 return true;
6445}
6446
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006447static
6448SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006449 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006450 const X86Subtarget *Subtarget) {
6451 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6452 EVT VT = Op.getValueType();
6453 DebugLoc dl = Op.getDebugLoc();
6454 SDValue V1 = Op.getOperand(0);
6455 SDValue V2 = Op.getOperand(1);
6456
6457 if (isZeroShuffle(SVOp))
6458 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
6459
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006460 // Handle splat operations
6461 if (SVOp->isSplat()) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006462 unsigned NumElem = VT.getVectorNumElements();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006463 int Size = VT.getSizeInBits();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006464 // Special case, this is the only place now where it's allowed to return
6465 // a vector_shuffle operation without using a target specific node, because
6466 // *hopefully* it will be optimized away by the dag combiner. FIXME: should
6467 // this be moved to DAGCombine instead?
6468 if (NumElem <= 4 && CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006469 return Op;
6470
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006471 // Use vbroadcast whenever the splat comes from a foldable load
6472 if (Subtarget->hasAVX() && isVectorBroadcast(V1))
6473 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, V1);
6474
Bruno Cardoso Lopes6a32adc2011-07-25 23:05:25 +00006475 // Handle splats by matching through known shuffle masks
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006476 if ((Size == 128 && NumElem <= 4) ||
6477 (Size == 256 && NumElem < 8))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006478 return SDValue();
6479
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00006480 // All remaning splats are promoted to target supported vector shuffles.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006481 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006482 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006483
6484 // If the shuffle can be profitably rewritten as a narrower shuffle, then
6485 // do it!
6486 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
6487 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6488 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006489 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006490 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
6491 // FIXME: Figure out a cleaner way to do this.
6492 // Try to make use of movq to zero out the top part.
6493 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
6494 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6495 if (NewOp.getNode()) {
6496 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
6497 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
6498 DAG, Subtarget, dl);
6499 }
6500 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
6501 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6502 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
6503 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
6504 DAG, Subtarget, dl);
6505 }
6506 }
6507 return SDValue();
6508}
6509
Dan Gohman475871a2008-07-27 21:46:04 +00006510SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006511X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00006512 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00006513 SDValue V1 = Op.getOperand(0);
6514 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006515 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006516 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006517 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006518 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006519 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
6520 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00006521 bool V1IsSplat = false;
6522 bool V2IsSplat = false;
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006523 bool HasSSE2 = Subtarget->hasSSE2() || Subtarget->hasAVX();
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006524 bool HasSSE3 = Subtarget->hasSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006525 bool HasSSSE3 = Subtarget->hasSSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006526 MachineFunction &MF = DAG.getMachineFunction();
6527 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006528
Dale Johannesen0488fb62010-09-30 23:57:10 +00006529 // Shuffle operations on MMX not supported.
6530 if (isMMX)
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00006531 return Op;
6532
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006533 // Vector shuffle lowering takes 3 steps:
6534 //
6535 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
6536 // narrowing and commutation of operands should be handled.
6537 // 2) Matching of shuffles with known shuffle masks to x86 target specific
6538 // shuffle nodes.
6539 // 3) Rewriting of unmatched masks into new generic shuffle operations,
6540 // so the shuffle can be broken into other shuffles and the legalizer can
6541 // try the lowering again.
6542 //
6543 // The general ideia is that no vector_shuffle operation should be left to
6544 // be matched during isel, all of them must be converted to a target specific
6545 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00006546
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006547 // Normalize the input vectors. Here splats, zeroed vectors, profitable
6548 // narrowing and commutation of operands should be handled. The actual code
6549 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006550 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006551 if (NewOp.getNode())
6552 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00006553
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006554 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
6555 // unpckh_undef). Only use pshufd if speed is more important than size.
6556 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006557 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006558 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
Rafael Espindola23e31012011-07-22 18:56:05 +00006559 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00006560
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006561 if (X86::isMOVDDUPMask(SVOp) && HasSSE3 && V2IsUndef &&
Dale Johannesen0488fb62010-09-30 23:57:10 +00006562 RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00006563 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006564
Dale Johannesen0488fb62010-09-30 23:57:10 +00006565 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006566 return getMOVHighToLow(Op, dl, DAG);
6567
6568 // Use to match splats
6569 if (HasSSE2 && X86::isUNPCKHMask(SVOp) && V2IsUndef &&
6570 (VT == MVT::v2f64 || VT == MVT::v2i64))
6571 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
6572
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006573 if (X86::isPSHUFDMask(SVOp)) {
6574 // The actual implementation will match the mask in the if above and then
6575 // during isel it can match several different instructions, not only pshufd
6576 // as its name says, sad but true, emulate the behavior for now...
6577 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
6578 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
6579
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006580 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
6581
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006582 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006583 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
6584
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006585 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V1,
6586 TargetMask, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006587 }
Eric Christopherfd179292009-08-27 18:07:15 +00006588
Evan Chengf26ffe92008-05-29 08:22:04 +00006589 // Check if this can be converted into a logical shift.
6590 bool isLeft = false;
6591 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00006592 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00006593 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00006594 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00006595 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006596 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00006597 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006598 EVT EltVT = VT.getVectorElementType();
6599 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006600 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006601 }
Eric Christopherfd179292009-08-27 18:07:15 +00006602
Nate Begeman9008ca62009-04-27 18:41:29 +00006603 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006604 if (V1IsUndef)
6605 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00006606 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00006607 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00006608 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006609 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006610 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6611
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006612 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006613 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6614 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00006615 }
Eric Christopherfd179292009-08-27 18:07:15 +00006616
Nate Begeman9008ca62009-04-27 18:41:29 +00006617 // FIXME: fold these into legal mask.
Dale Johannesen0488fb62010-09-30 23:57:10 +00006618 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp))
6619 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006620
Dale Johannesen0488fb62010-09-30 23:57:10 +00006621 if (X86::isMOVHLPSMask(SVOp))
6622 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006623
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00006624 if (X86::isMOVSHDUPMask(SVOp, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006625 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006626
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00006627 if (X86::isMOVSLDUPMask(SVOp, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006628 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00006629
Dale Johannesen0488fb62010-09-30 23:57:10 +00006630 if (X86::isMOVLPMask(SVOp))
6631 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006632
Nate Begeman9008ca62009-04-27 18:41:29 +00006633 if (ShouldXformToMOVHLPS(SVOp) ||
6634 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
6635 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006636
Evan Chengf26ffe92008-05-29 08:22:04 +00006637 if (isShift) {
6638 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006639 EVT EltVT = VT.getVectorElementType();
6640 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006641 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006642 }
Eric Christopherfd179292009-08-27 18:07:15 +00006643
Evan Cheng9eca5e82006-10-25 21:49:50 +00006644 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00006645 // FIXME: This should also accept a bitcast of a splat? Be careful, not
6646 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00006647 V1IsSplat = isSplatVector(V1.getNode());
6648 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006649
Chris Lattner8a594482007-11-25 00:24:49 +00006650 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00006651 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006652 Op = CommuteVectorShuffle(SVOp, DAG);
6653 SVOp = cast<ShuffleVectorSDNode>(Op);
6654 V1 = SVOp->getOperand(0);
6655 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00006656 std::swap(V1IsSplat, V2IsSplat);
6657 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006658 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00006659 }
6660
Nate Begeman9008ca62009-04-27 18:41:29 +00006661 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
6662 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00006663 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00006664 return V1;
6665 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6666 // the instruction selector will not match, so get a canonical MOVL with
6667 // swapped operands to undo the commute.
6668 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006669 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006670
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006671 if (X86::isUNPCKLMask(SVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006672 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006673
6674 if (X86::isUNPCKHMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006675 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006676
Evan Cheng9bbbb982006-10-25 20:48:19 +00006677 if (V2IsSplat) {
6678 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006679 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00006680 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00006681 SDValue NewMask = NormalizeMask(SVOp, DAG);
6682 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
6683 if (NSVOp != SVOp) {
6684 if (X86::isUNPCKLMask(NSVOp, true)) {
6685 return NewMask;
6686 } else if (X86::isUNPCKHMask(NSVOp, true)) {
6687 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006688 }
6689 }
6690 }
6691
Evan Cheng9eca5e82006-10-25 21:49:50 +00006692 if (Commuted) {
6693 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006694 // FIXME: this seems wrong.
6695 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
6696 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006697
6698 if (X86::isUNPCKLMask(NewSVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006699 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V2, V1, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006700
6701 if (X86::isUNPCKHMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006702 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V2, V1, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006703 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006704
Nate Begeman9008ca62009-04-27 18:41:29 +00006705 // Normalize the node to match x86 shuffle ops if needed
Dale Johannesen0488fb62010-09-30 23:57:10 +00006706 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
Nate Begeman9008ca62009-04-27 18:41:29 +00006707 return CommuteVectorShuffle(SVOp, DAG);
6708
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006709 // The checks below are all present in isShuffleMaskLegal, but they are
6710 // inlined here right now to enable us to directly emit target specific
6711 // nodes, and remove one by one until they don't return Op anymore.
6712 SmallVector<int, 16> M;
6713 SVOp->getMask(M);
6714
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006715 if (isPALIGNRMask(M, VT, HasSSSE3))
6716 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
6717 X86::getShufflePALIGNRImmediate(SVOp),
6718 DAG);
6719
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006720 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6721 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00006722 if (VT == MVT::v2f64)
6723 return getTargetShuffleNode(X86ISD::UNPCKLPD, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006724 if (VT == MVT::v2i64)
6725 return getTargetShuffleNode(X86ISD::PUNPCKLQDQ, dl, VT, V1, V1, DAG);
6726 }
6727
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006728 if (isPSHUFHWMask(M, VT))
6729 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
6730 X86::getShufflePSHUFHWImmediate(SVOp),
6731 DAG);
6732
6733 if (isPSHUFLWMask(M, VT))
6734 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
6735 X86::getShufflePSHUFLWImmediate(SVOp),
6736 DAG);
6737
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006738 if (isSHUFPMask(M, VT))
6739 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6740 X86::getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00006741
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006742 if (X86::isUNPCKL_v_undef_Mask(SVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006743 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006744 if (X86::isUNPCKH_v_undef_Mask(SVOp))
Rafael Espindola23e31012011-07-22 18:56:05 +00006745 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006746
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006747 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006748 // Generate target specific nodes for 128 or 256-bit shuffles only
6749 // supported in the AVX instruction set.
6750 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006751
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006752 // Handle VMOVDDUPY permutations
6753 if (isMOVDDUPYMask(SVOp, Subtarget))
6754 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
6755
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006756 // Handle VPERMILPS* permutations
6757 if (isVPERMILPSMask(M, VT, Subtarget))
6758 return getTargetShuffleNode(getVPERMILOpcode(VT), dl, VT, V1,
6759 getShuffleVPERMILPSImmediate(SVOp), DAG);
6760
6761 // Handle VPERMILPD* permutations
6762 if (isVPERMILPDMask(M, VT, Subtarget))
6763 return getTargetShuffleNode(getVPERMILOpcode(VT), dl, VT, V1,
6764 getShuffleVPERMILPDImmediate(SVOp), DAG);
6765
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00006766 // Handle VPERM2F128 permutations
6767 if (isVPERM2F128Mask(M, VT, Subtarget))
6768 return getTargetShuffleNode(X86ISD::VPERM2F128, dl, VT, V1, V2,
6769 getShuffleVPERM2F128Immediate(SVOp), DAG);
6770
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006771 // Handle VSHUFPSY permutations
6772 if (isVSHUFPSYMask(M, VT, Subtarget))
6773 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6774 getShuffleVSHUFPSYImmediate(SVOp), DAG);
6775
6776 // Handle VSHUFPDY permutations
6777 if (isVSHUFPDYMask(M, VT, Subtarget))
6778 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6779 getShuffleVSHUFPDYImmediate(SVOp), DAG);
6780
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006781 //===--------------------------------------------------------------------===//
6782 // Since no target specific shuffle was selected for this generic one,
6783 // lower it into other known shuffles. FIXME: this isn't true yet, but
6784 // this is the plan.
6785 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00006786
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006787 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
6788 if (VT == MVT::v8i16) {
6789 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
6790 if (NewOp.getNode())
6791 return NewOp;
6792 }
6793
6794 if (VT == MVT::v16i8) {
6795 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
6796 if (NewOp.getNode())
6797 return NewOp;
6798 }
6799
6800 // Handle all 128-bit wide vectors with 4 elements, and match them with
6801 // several different shuffle types.
6802 if (NumElems == 4 && VT.getSizeInBits() == 128)
6803 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
6804
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006805 // Handle general 256-bit shuffles
6806 if (VT.is256BitVector())
6807 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
6808
Dan Gohman475871a2008-07-27 21:46:04 +00006809 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006810}
6811
Dan Gohman475871a2008-07-27 21:46:04 +00006812SDValue
6813X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00006814 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006815 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006816 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006817
6818 if (Op.getOperand(0).getValueType().getSizeInBits() != 128)
6819 return SDValue();
6820
Duncan Sands83ec4b62008-06-06 12:08:01 +00006821 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006822 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006823 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006824 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006825 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006826 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006827 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00006828 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
6829 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
6830 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006831 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6832 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006833 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006834 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00006835 Op.getOperand(0)),
6836 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006837 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006838 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006839 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006840 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006841 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00006842 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00006843 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
6844 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006845 // result has a single use which is a store or a bitcast to i32. And in
6846 // the case of a store, it's not worth it if the index is a constant 0,
6847 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00006848 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00006849 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00006850 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006851 if ((User->getOpcode() != ISD::STORE ||
6852 (isa<ConstantSDNode>(Op.getOperand(1)) &&
6853 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006854 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00006855 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00006856 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00006857 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006858 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00006859 Op.getOperand(0)),
6860 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006861 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Owen Anderson825b72b2009-08-11 20:47:22 +00006862 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00006863 // ExtractPS works with constant index.
6864 if (isa<ConstantSDNode>(Op.getOperand(1)))
6865 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006866 }
Dan Gohman475871a2008-07-27 21:46:04 +00006867 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006868}
6869
6870
Dan Gohman475871a2008-07-27 21:46:04 +00006871SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006872X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
6873 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006874 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00006875 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006876
David Greene74a579d2011-02-10 16:57:36 +00006877 SDValue Vec = Op.getOperand(0);
6878 EVT VecVT = Vec.getValueType();
6879
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006880 // If this is a 256-bit vector result, first extract the 128-bit vector and
6881 // then extract the element from the 128-bit vector.
6882 if (VecVT.getSizeInBits() == 256) {
David Greene74a579d2011-02-10 16:57:36 +00006883 DebugLoc dl = Op.getNode()->getDebugLoc();
6884 unsigned NumElems = VecVT.getVectorNumElements();
6885 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00006886 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
6887
6888 // Get the 128-bit vector.
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006889 bool Upper = IdxVal >= NumElems/2;
6890 Vec = Extract128BitVector(Vec,
6891 DAG.getConstant(Upper ? NumElems/2 : 0, MVT::i32), DAG, dl);
David Greene74a579d2011-02-10 16:57:36 +00006892
David Greene74a579d2011-02-10 16:57:36 +00006893 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006894 Upper ? DAG.getConstant(IdxVal-NumElems/2, MVT::i32) : Idx);
David Greene74a579d2011-02-10 16:57:36 +00006895 }
6896
6897 assert(Vec.getValueSizeInBits() <= 128 && "Unexpected vector length");
6898
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006899 if (Subtarget->hasSSE41() || Subtarget->hasAVX()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006900 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006901 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00006902 return Res;
6903 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00006904
Owen Andersone50ed302009-08-10 22:56:29 +00006905 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006906 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006907 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006908 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00006909 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006910 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006911 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006912 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6913 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006914 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006915 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00006916 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006917 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00006918 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00006919 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006920 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00006921 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006922 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006923 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006924 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006925 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006926 if (Idx == 0)
6927 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00006928
Evan Cheng0db9fe62006-04-25 20:13:52 +00006929 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00006930 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006931 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006932 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006933 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006934 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006935 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00006936 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006937 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
6938 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
6939 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006940 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006941 if (Idx == 0)
6942 return Op;
6943
6944 // UNPCKHPD the element to the lowest double word, then movsd.
6945 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
6946 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00006947 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006948 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006949 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006950 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006951 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006952 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006953 }
6954
Dan Gohman475871a2008-07-27 21:46:04 +00006955 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006956}
6957
Dan Gohman475871a2008-07-27 21:46:04 +00006958SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006959X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
6960 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006961 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006962 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006963 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006964
Dan Gohman475871a2008-07-27 21:46:04 +00006965 SDValue N0 = Op.getOperand(0);
6966 SDValue N1 = Op.getOperand(1);
6967 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00006968
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006969 if (VT.getSizeInBits() == 256)
6970 return SDValue();
6971
Dan Gohman8a55ce42009-09-23 21:02:20 +00006972 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00006973 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006974 unsigned Opc;
6975 if (VT == MVT::v8i16)
6976 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006977 else if (VT == MVT::v16i8)
6978 Opc = X86ISD::PINSRB;
6979 else
6980 Opc = X86ISD::PINSRB;
6981
Nate Begeman14d12ca2008-02-11 04:19:36 +00006982 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
6983 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006984 if (N1.getValueType() != MVT::i32)
6985 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6986 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006987 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00006988 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006989 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006990 // Bits [7:6] of the constant are the source select. This will always be
6991 // zero here. The DAG Combiner may combine an extract_elt index into these
6992 // bits. For example (insert (extract, 3), 2) could be matched by putting
6993 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00006994 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00006995 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00006996 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00006997 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006998 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00006999 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00007000 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00007001 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00007002 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00007003 // PINSR* works with constant index.
7004 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007005 }
Dan Gohman475871a2008-07-27 21:46:04 +00007006 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007007}
7008
Dan Gohman475871a2008-07-27 21:46:04 +00007009SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007010X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007011 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007012 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007013
David Greene6b381262011-02-09 15:32:06 +00007014 DebugLoc dl = Op.getDebugLoc();
7015 SDValue N0 = Op.getOperand(0);
7016 SDValue N1 = Op.getOperand(1);
7017 SDValue N2 = Op.getOperand(2);
7018
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007019 // If this is a 256-bit vector result, first extract the 128-bit vector,
7020 // insert the element into the extracted half and then place it back.
7021 if (VT.getSizeInBits() == 256) {
David Greene6b381262011-02-09 15:32:06 +00007022 if (!isa<ConstantSDNode>(N2))
7023 return SDValue();
7024
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007025 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00007026 unsigned NumElems = VT.getVectorNumElements();
7027 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007028 bool Upper = IdxVal >= NumElems/2;
7029 SDValue Ins128Idx = DAG.getConstant(Upper ? NumElems/2 : 0, MVT::i32);
7030 SDValue V = Extract128BitVector(N0, Ins128Idx, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007031
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007032 // Insert the element into the desired half.
7033 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V,
7034 N1, Upper ? DAG.getConstant(IdxVal-NumElems/2, MVT::i32) : N2);
David Greene6b381262011-02-09 15:32:06 +00007035
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007036 // Insert the changed part back to the 256-bit vector
7037 return Insert128BitVector(N0, V, Ins128Idx, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007038 }
7039
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007040 if (Subtarget->hasSSE41() || Subtarget->hasAVX())
Nate Begeman14d12ca2008-02-11 04:19:36 +00007041 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
7042
Dan Gohman8a55ce42009-09-23 21:02:20 +00007043 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00007044 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00007045
Dan Gohman8a55ce42009-09-23 21:02:20 +00007046 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00007047 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
7048 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007049 if (N1.getValueType() != MVT::i32)
7050 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7051 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007052 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00007053 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007054 }
Dan Gohman475871a2008-07-27 21:46:04 +00007055 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007056}
7057
Dan Gohman475871a2008-07-27 21:46:04 +00007058SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007059X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007060 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007061 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00007062 EVT OpVT = Op.getValueType();
7063
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007064 // If this is a 256-bit vector result, first insert into a 128-bit
7065 // vector and then insert into the 256-bit vector.
7066 if (OpVT.getSizeInBits() > 128) {
7067 // Insert into a 128-bit vector.
7068 EVT VT128 = EVT::getVectorVT(*Context,
7069 OpVT.getVectorElementType(),
7070 OpVT.getVectorNumElements() / 2);
7071
7072 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
7073
7074 // Insert the 128-bit vector.
7075 return Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, OpVT), Op,
7076 DAG.getConstant(0, MVT::i32),
7077 DAG, dl);
7078 }
7079
Chris Lattnerf172ecd2010-07-04 23:07:25 +00007080 if (Op.getValueType() == MVT::v1i64 &&
7081 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00007082 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00007083
Owen Anderson825b72b2009-08-11 20:47:22 +00007084 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00007085 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
7086 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007087 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00007088 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007089}
7090
David Greene91585092011-01-26 15:38:49 +00007091// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
7092// a simple subregister reference or explicit instructions to grab
7093// upper bits of a vector.
7094SDValue
7095X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7096 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00007097 DebugLoc dl = Op.getNode()->getDebugLoc();
7098 SDValue Vec = Op.getNode()->getOperand(0);
7099 SDValue Idx = Op.getNode()->getOperand(1);
7100
7101 if (Op.getNode()->getValueType(0).getSizeInBits() == 128
7102 && Vec.getNode()->getValueType(0).getSizeInBits() == 256) {
7103 return Extract128BitVector(Vec, Idx, DAG, dl);
7104 }
David Greene91585092011-01-26 15:38:49 +00007105 }
7106 return SDValue();
7107}
7108
David Greenecfe33c42011-01-26 19:13:22 +00007109// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
7110// simple superregister reference or explicit instructions to insert
7111// the upper bits of a vector.
7112SDValue
7113X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7114 if (Subtarget->hasAVX()) {
7115 DebugLoc dl = Op.getNode()->getDebugLoc();
7116 SDValue Vec = Op.getNode()->getOperand(0);
7117 SDValue SubVec = Op.getNode()->getOperand(1);
7118 SDValue Idx = Op.getNode()->getOperand(2);
7119
7120 if (Op.getNode()->getValueType(0).getSizeInBits() == 256
7121 && SubVec.getNode()->getValueType(0).getSizeInBits() == 128) {
David Greenea5f26012011-02-07 19:36:54 +00007122 return Insert128BitVector(Vec, SubVec, Idx, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00007123 }
7124 }
7125 return SDValue();
7126}
7127
Bill Wendling056292f2008-09-16 21:48:12 +00007128// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7129// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7130// one of the above mentioned nodes. It has to be wrapped because otherwise
7131// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7132// be used to form addressing mode. These wrapped nodes will be selected
7133// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00007134SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007135X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007136 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007137
Chris Lattner41621a22009-06-26 19:22:52 +00007138 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7139 // global base reg.
7140 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007141 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007142 CodeModel::Model M = getTargetMachine().getCodeModel();
7143
Chris Lattner4f066492009-07-11 20:29:19 +00007144 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007145 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007146 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007147 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007148 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007149 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007150 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007151
Evan Cheng1606e8e2009-03-13 07:51:59 +00007152 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00007153 CP->getAlignment(),
7154 CP->getOffset(), OpFlag);
7155 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00007156 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007157 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00007158 if (OpFlag) {
7159 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007160 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007161 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007162 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007163 }
7164
7165 return Result;
7166}
7167
Dan Gohmand858e902010-04-17 15:26:15 +00007168SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007169 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007170
Chris Lattner18c59872009-06-27 04:16:01 +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;
7174 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
Chris Lattner18c59872009-06-27 04:16:01 +00007185 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7186 OpFlag);
7187 DebugLoc DL = JT->getDebugLoc();
7188 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007189
Chris Lattner18c59872009-06-27 04:16:01 +00007190 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00007191 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00007192 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7193 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007194 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007195 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007196
Chris Lattner18c59872009-06-27 04:16:01 +00007197 return Result;
7198}
7199
7200SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007201X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007202 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
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() &&
Eli Friedman586272d2011-08-11 01:48:05 +00007211 (M == CodeModel::Small || M == CodeModel::Kernel)) {
7212 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
7213 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00007214 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00007215 } else if (Subtarget->isPICStyleGOT()) {
7216 OpFlag = X86II::MO_GOT;
7217 } else if (Subtarget->isPICStyleStubPIC()) {
7218 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
7219 } else if (Subtarget->isPICStyleStubNoDynamic()) {
7220 OpFlag = X86II::MO_DARWIN_NONLAZY;
7221 }
Eric Christopherfd179292009-08-27 18:07:15 +00007222
Chris Lattner18c59872009-06-27 04:16:01 +00007223 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00007224
Chris Lattner18c59872009-06-27 04:16:01 +00007225 DebugLoc DL = Op.getDebugLoc();
7226 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007227
7228
Chris Lattner18c59872009-06-27 04:16:01 +00007229 // With PIC, the address is actually $g + Offset.
7230 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007231 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007232 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7233 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007234 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007235 Result);
7236 }
Eric Christopherfd179292009-08-27 18:07:15 +00007237
Eli Friedman586272d2011-08-11 01:48:05 +00007238 // For symbols that require a load from a stub to get the address, emit the
7239 // load.
7240 if (isGlobalStubReference(OpFlag))
7241 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
7242 MachinePointerInfo::getGOT(), false, false, 0);
7243
Chris Lattner18c59872009-06-27 04:16:01 +00007244 return Result;
7245}
7246
Dan Gohman475871a2008-07-27 21:46:04 +00007247SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007248X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007249 // Create the TargetBlockAddressAddress node.
7250 unsigned char OpFlags =
7251 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007252 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007253 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00007254 DebugLoc dl = Op.getDebugLoc();
7255 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
7256 /*isTarget=*/true, OpFlags);
7257
Dan Gohmanf705adb2009-10-30 01:28:02 +00007258 if (Subtarget->isPICStyleRIPRel() &&
7259 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007260 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7261 else
7262 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007263
Dan Gohman29cbade2009-11-20 23:18:13 +00007264 // With PIC, the address is actually $g + Offset.
7265 if (isGlobalRelativeToPICBase(OpFlags)) {
7266 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7267 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7268 Result);
7269 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007270
7271 return Result;
7272}
7273
7274SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00007275X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00007276 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00007277 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007278 // Create the TargetGlobalAddress node, folding in the constant
7279 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007280 unsigned char OpFlags =
7281 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007282 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007283 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007284 if (OpFlags == X86II::MO_NO_FLAG &&
7285 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007286 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007287 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007288 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007289 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007290 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007291 }
Eric Christopherfd179292009-08-27 18:07:15 +00007292
Chris Lattner4f066492009-07-11 20:29:19 +00007293 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007294 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007295 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7296 else
7297 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007298
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007299 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007300 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007301 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7302 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007303 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007304 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007305
Chris Lattner36c25012009-07-10 07:34:39 +00007306 // For globals that require a load from a stub to get the address, emit the
7307 // load.
7308 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007309 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00007310 MachinePointerInfo::getGOT(), false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007311
Dan Gohman6520e202008-10-18 02:06:02 +00007312 // If there was a non-zero offset that we didn't fold, create an explicit
7313 // addition for it.
7314 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007315 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007316 DAG.getConstant(Offset, getPointerTy()));
7317
Evan Cheng0db9fe62006-04-25 20:13:52 +00007318 return Result;
7319}
7320
Evan Chengda43bcf2008-09-24 00:05:32 +00007321SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007322X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007323 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007324 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007325 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007326}
7327
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007328static SDValue
7329GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007330 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00007331 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007332 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007333 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007334 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007335 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007336 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007337 GA->getOffset(),
7338 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007339 if (InFlag) {
7340 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00007341 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007342 } else {
7343 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00007344 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007345 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007346
7347 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007348 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007349
Rafael Espindola15f1b662009-04-24 12:59:40 +00007350 SDValue Flag = Chain.getValue(1);
7351 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007352}
7353
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007354// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007355static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007356LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007357 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007358 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00007359 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
7360 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007361 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007362 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007363 InFlag = Chain.getValue(1);
7364
Chris Lattnerb903bed2009-06-26 21:20:29 +00007365 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007366}
7367
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007368// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007369static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007370LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007371 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007372 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7373 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007374}
7375
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007376// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
7377// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007378static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007379 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00007380 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007381 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00007382
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007383 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
7384 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
7385 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00007386
Michael J. Spencerec38de22010-10-10 22:04:20 +00007387 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007388 DAG.getIntPtrConstant(0),
7389 MachinePointerInfo(Ptr), false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00007390
Chris Lattnerb903bed2009-06-26 21:20:29 +00007391 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007392 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
7393 // initialexec.
7394 unsigned WrapperKind = X86ISD::Wrapper;
7395 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007396 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00007397 } else if (is64Bit) {
7398 assert(model == TLSModel::InitialExec);
7399 OperandFlags = X86II::MO_GOTTPOFF;
7400 WrapperKind = X86ISD::WrapperRIP;
7401 } else {
7402 assert(model == TLSModel::InitialExec);
7403 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00007404 }
Eric Christopherfd179292009-08-27 18:07:15 +00007405
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007406 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
7407 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00007408 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00007409 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007410 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007411 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007412
Rafael Espindola9a580232009-02-27 13:37:18 +00007413 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007414 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00007415 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007416
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007417 // The address of the thread local variable is the add of the thread
7418 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00007419 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007420}
7421
Dan Gohman475871a2008-07-27 21:46:04 +00007422SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007423X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00007424
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007425 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00007426 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00007427
Eric Christopher30ef0e52010-06-03 04:07:48 +00007428 if (Subtarget->isTargetELF()) {
7429 // TODO: implement the "local dynamic" model
7430 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00007431
Eric Christopher30ef0e52010-06-03 04:07:48 +00007432 // If GV is an alias then use the aliasee for determining
7433 // thread-localness.
7434 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
7435 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007436
7437 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00007438 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00007439
Eric Christopher30ef0e52010-06-03 04:07:48 +00007440 switch (model) {
7441 case TLSModel::GeneralDynamic:
7442 case TLSModel::LocalDynamic: // not implemented
7443 if (Subtarget->is64Bit())
7444 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
7445 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00007446
Eric Christopher30ef0e52010-06-03 04:07:48 +00007447 case TLSModel::InitialExec:
7448 case TLSModel::LocalExec:
7449 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
7450 Subtarget->is64Bit());
7451 }
7452 } else if (Subtarget->isTargetDarwin()) {
7453 // Darwin only has one model of TLS. Lower to that.
7454 unsigned char OpFlag = 0;
7455 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7456 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007457
Eric Christopher30ef0e52010-06-03 04:07:48 +00007458 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7459 // global base reg.
7460 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7461 !Subtarget->is64Bit();
7462 if (PIC32)
7463 OpFlag = X86II::MO_TLVP_PIC_BASE;
7464 else
7465 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007466 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007467 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00007468 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007469 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007470 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007471
Eric Christopher30ef0e52010-06-03 04:07:48 +00007472 // With PIC32, the address is actually $g + Offset.
7473 if (PIC32)
7474 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7475 DAG.getNode(X86ISD::GlobalBaseReg,
7476 DebugLoc(), getPointerTy()),
7477 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007478
Eric Christopher30ef0e52010-06-03 04:07:48 +00007479 // Lowering the machine isd will make sure everything is in the right
7480 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007481 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007482 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00007483 SDValue Args[] = { Chain, Offset };
7484 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007485
Eric Christopher30ef0e52010-06-03 04:07:48 +00007486 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7487 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7488 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007489
Eric Christopher30ef0e52010-06-03 04:07:48 +00007490 // And our return value (tls address) is in the standard call return value
7491 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007492 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
7493 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007494 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007495
Eric Christopher30ef0e52010-06-03 04:07:48 +00007496 assert(false &&
7497 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00007498
Torok Edwinc23197a2009-07-14 16:55:14 +00007499 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00007500 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007501}
7502
Evan Cheng0db9fe62006-04-25 20:13:52 +00007503
Nadav Rotem43012222011-05-11 08:12:09 +00007504/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00007505/// take a 2 x i32 value to shift plus a shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +00007506SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00007507 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00007508 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007509 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007510 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007511 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00007512 SDValue ShOpLo = Op.getOperand(0);
7513 SDValue ShOpHi = Op.getOperand(1);
7514 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00007515 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00007516 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00007517 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00007518
Dan Gohman475871a2008-07-27 21:46:04 +00007519 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007520 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007521 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
7522 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007523 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007524 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
7525 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007526 }
Evan Chenge3413162006-01-09 18:33:28 +00007527
Owen Anderson825b72b2009-08-11 20:47:22 +00007528 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
7529 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00007530 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00007531 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00007532
Dan Gohman475871a2008-07-27 21:46:04 +00007533 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00007534 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00007535 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
7536 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00007537
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007538 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007539 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7540 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007541 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007542 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7543 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007544 }
7545
Dan Gohman475871a2008-07-27 21:46:04 +00007546 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00007547 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007548}
Evan Chenga3195e82006-01-12 22:54:21 +00007549
Dan Gohmand858e902010-04-17 15:26:15 +00007550SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
7551 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007552 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00007553
Dale Johannesen0488fb62010-09-30 23:57:10 +00007554 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007555 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007556
Owen Anderson825b72b2009-08-11 20:47:22 +00007557 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00007558 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007559
Eli Friedman36df4992009-05-27 00:47:34 +00007560 // These are really Legal; return the operand so the caller accepts it as
7561 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007562 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00007563 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00007564 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00007565 Subtarget->is64Bit()) {
7566 return Op;
7567 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007568
Bruno Cardoso Lopesa511b8e2011-08-09 17:39:01 +00007569 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007570 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007571 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00007572 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007573 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00007574 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00007575 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007576 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007577 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007578 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
7579}
Evan Cheng0db9fe62006-04-25 20:13:52 +00007580
Owen Andersone50ed302009-08-10 22:56:29 +00007581SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007582 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00007583 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007584 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00007585 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00007586 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00007587 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007588 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007589 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00007590 else
Owen Anderson825b72b2009-08-11 20:47:22 +00007591 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007592
Chris Lattner492a43e2010-09-22 01:28:21 +00007593 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007594
Stuart Hastings84be9582011-06-02 15:57:11 +00007595 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
7596 MachineMemOperand *MMO;
7597 if (FI) {
7598 int SSFI = FI->getIndex();
7599 MMO =
7600 DAG.getMachineFunction()
7601 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7602 MachineMemOperand::MOLoad, ByteSize, ByteSize);
7603 } else {
7604 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
7605 StackSlot = StackSlot.getOperand(1);
7606 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007607 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007608 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
7609 X86ISD::FILD, DL,
7610 Tys, Ops, array_lengthof(Ops),
7611 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007612
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007613 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007614 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00007615 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007616
7617 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
7618 // shouldn't be necessary except that RFP cannot be live across
7619 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007620 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007621 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
7622 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007623 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00007624 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007625 SDValue Ops[] = {
7626 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
7627 };
Chris Lattner492a43e2010-09-22 01:28:21 +00007628 MachineMemOperand *MMO =
7629 DAG.getMachineFunction()
7630 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007631 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007632
Chris Lattner492a43e2010-09-22 01:28:21 +00007633 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
7634 Ops, array_lengthof(Ops),
7635 Op.getValueType(), MMO);
7636 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007637 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007638 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007639 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007640
Evan Cheng0db9fe62006-04-25 20:13:52 +00007641 return Result;
7642}
7643
Bill Wendling8b8a6362009-01-17 03:56:04 +00007644// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007645SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
7646 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00007647 // This algorithm is not obvious. Here it is in C code, more or less:
7648 /*
7649 double uint64_to_double( uint32_t hi, uint32_t lo ) {
7650 static const __m128i exp = { 0x4330000045300000ULL, 0 };
7651 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00007652
Bill Wendling8b8a6362009-01-17 03:56:04 +00007653 // Copy ints to xmm registers.
7654 __m128i xh = _mm_cvtsi32_si128( hi );
7655 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00007656
Bill Wendling8b8a6362009-01-17 03:56:04 +00007657 // Combine into low half of a single xmm register.
7658 __m128i x = _mm_unpacklo_epi32( xh, xl );
7659 __m128d d;
7660 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00007661
Bill Wendling8b8a6362009-01-17 03:56:04 +00007662 // Merge in appropriate exponents to give the integer bits the right
7663 // magnitude.
7664 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00007665
Bill Wendling8b8a6362009-01-17 03:56:04 +00007666 // Subtract away the biases to deal with the IEEE-754 double precision
7667 // implicit 1.
7668 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00007669
Bill Wendling8b8a6362009-01-17 03:56:04 +00007670 // All conversions up to here are exact. The correctly rounded result is
7671 // calculated using the current rounding mode using the following
7672 // horizontal add.
7673 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
7674 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
7675 // store doesn't really need to be here (except
7676 // maybe to zero the other double)
7677 return sd;
7678 }
7679 */
Dale Johannesen040225f2008-10-21 23:07:49 +00007680
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007681 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00007682 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00007683
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007684 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00007685 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00007686 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
7687 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
7688 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
7689 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00007690 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007691 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007692
Bill Wendling8b8a6362009-01-17 03:56:04 +00007693 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00007694 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007695 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00007696 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007697 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00007698 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007699 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007700
Owen Anderson825b72b2009-08-11 20:47:22 +00007701 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7702 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00007703 Op.getOperand(0),
7704 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007705 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7706 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00007707 Op.getOperand(0),
7708 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007709 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
7710 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00007711 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007712 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00007713 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007714 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
Owen Anderson825b72b2009-08-11 20:47:22 +00007715 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00007716 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007717 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00007718 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007719
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007720 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00007721 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00007722 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
7723 DAG.getUNDEF(MVT::v2f64), ShufMask);
7724 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
7725 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007726 DAG.getIntPtrConstant(0));
7727}
7728
Bill Wendling8b8a6362009-01-17 03:56:04 +00007729// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007730SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
7731 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007732 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007733 // FP constant to bias correct the final result.
7734 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00007735 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007736
7737 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00007738 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00007739 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007740
Eli Friedmanf3704762011-08-29 21:15:46 +00007741 // Zero out the upper parts of the register.
7742 Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget->hasSSE2(), DAG);
7743
Owen Anderson825b72b2009-08-11 20:47:22 +00007744 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007745 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007746 DAG.getIntPtrConstant(0));
7747
7748 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007749 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007750 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007751 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007752 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007753 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007754 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007755 MVT::v2f64, Bias)));
7756 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007757 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007758 DAG.getIntPtrConstant(0));
7759
7760 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007761 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007762
7763 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00007764 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00007765
Owen Anderson825b72b2009-08-11 20:47:22 +00007766 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007767 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00007768 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007769 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007770 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00007771 }
7772
7773 // Handle final rounding.
7774 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00007775}
7776
Dan Gohmand858e902010-04-17 15:26:15 +00007777SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
7778 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00007779 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007780 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007781
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007782 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00007783 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
7784 // the optimization here.
7785 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00007786 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00007787
Owen Andersone50ed302009-08-10 22:56:29 +00007788 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007789 EVT DstVT = Op.getValueType();
7790 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007791 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007792 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007793 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00007794
7795 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00007796 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007797 if (SrcVT == MVT::i32) {
7798 SDValue WordOff = DAG.getConstant(4, getPointerTy());
7799 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
7800 getPointerTy(), StackSlot, WordOff);
7801 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007802 StackSlot, MachinePointerInfo(),
7803 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007804 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007805 OffsetSlot, MachinePointerInfo(),
7806 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007807 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
7808 return Fild;
7809 }
7810
7811 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
7812 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007813 StackSlot, MachinePointerInfo(),
7814 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007815 // For i64 source, we need to add the appropriate power of 2 if the input
7816 // was negative. This is the same as the optimization in
7817 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
7818 // we must be careful to do the computation in x87 extended precision, not
7819 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00007820 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
7821 MachineMemOperand *MMO =
7822 DAG.getMachineFunction()
7823 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7824 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007825
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007826 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
7827 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007828 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
7829 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007830
7831 APInt FF(32, 0x5F800000ULL);
7832
7833 // Check whether the sign bit is set.
7834 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
7835 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
7836 ISD::SETLT);
7837
7838 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
7839 SDValue FudgePtr = DAG.getConstantPool(
7840 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
7841 getPointerTy());
7842
7843 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
7844 SDValue Zero = DAG.getIntPtrConstant(0);
7845 SDValue Four = DAG.getIntPtrConstant(4);
7846 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
7847 Zero, Four);
7848 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
7849
7850 // Load the value out, extending it from f32 to f80.
7851 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00007852 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00007853 FudgePtr, MachinePointerInfo::getConstantPool(),
7854 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007855 // Extend everything to 80 bits to force it to be done on x87.
7856 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
7857 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007858}
7859
Dan Gohman475871a2008-07-27 21:46:04 +00007860std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00007861FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00007862 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00007863
Owen Andersone50ed302009-08-10 22:56:29 +00007864 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00007865
7866 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007867 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
7868 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00007869 }
7870
Owen Anderson825b72b2009-08-11 20:47:22 +00007871 assert(DstTy.getSimpleVT() <= MVT::i64 &&
7872 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00007873 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00007874
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007875 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007876 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00007877 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007878 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00007879 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00007880 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00007881 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007882 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007883
Evan Cheng87c89352007-10-15 20:11:21 +00007884 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
7885 // stack slot.
7886 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00007887 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00007888 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007889 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00007890
Michael J. Spencerec38de22010-10-10 22:04:20 +00007891
7892
Evan Cheng0db9fe62006-04-25 20:13:52 +00007893 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00007894 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007895 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007896 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
7897 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
7898 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007899 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007900
Dan Gohman475871a2008-07-27 21:46:04 +00007901 SDValue Chain = DAG.getEntryNode();
7902 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00007903 EVT TheVT = Op.getOperand(0).getValueType();
7904 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007905 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00007906 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007907 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007908 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007909 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00007910 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00007911 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00007912 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00007913
Chris Lattner492a43e2010-09-22 01:28:21 +00007914 MachineMemOperand *MMO =
7915 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7916 MachineMemOperand::MOLoad, MemSize, MemSize);
7917 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
7918 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007919 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00007920 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007921 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
7922 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007923
Chris Lattner07290932010-09-22 01:05:16 +00007924 MachineMemOperand *MMO =
7925 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7926 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007927
Evan Cheng0db9fe62006-04-25 20:13:52 +00007928 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00007929 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00007930 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
7931 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00007932
Chris Lattner27a6c732007-11-24 07:07:01 +00007933 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007934}
7935
Dan Gohmand858e902010-04-17 15:26:15 +00007936SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
7937 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00007938 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007939 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007940
Eli Friedman948e95a2009-05-23 09:59:16 +00007941 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00007942 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00007943 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
7944 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00007945
Chris Lattner27a6c732007-11-24 07:07:01 +00007946 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007947 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007948 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00007949}
7950
Dan Gohmand858e902010-04-17 15:26:15 +00007951SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
7952 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00007953 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
7954 SDValue FIST = Vals.first, StackSlot = Vals.second;
7955 assert(FIST.getNode() && "Unexpected failure");
7956
7957 // Load the result.
7958 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007959 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007960}
7961
Dan Gohmand858e902010-04-17 15:26:15 +00007962SDValue X86TargetLowering::LowerFABS(SDValue Op,
7963 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007964 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007965 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007966 EVT VT = Op.getValueType();
7967 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007968 if (VT.isVector())
7969 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007970 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007971 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007972 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00007973 CV.push_back(C);
7974 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007975 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007976 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00007977 CV.push_back(C);
7978 CV.push_back(C);
7979 CV.push_back(C);
7980 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007981 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007982 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007983 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007984 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007985 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007986 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007987 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007988}
7989
Dan Gohmand858e902010-04-17 15:26:15 +00007990SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007991 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007992 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007993 EVT VT = Op.getValueType();
7994 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00007995 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00007996 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007997 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007998 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007999 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00008000 CV.push_back(C);
8001 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008002 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008003 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00008004 CV.push_back(C);
8005 CV.push_back(C);
8006 CV.push_back(C);
8007 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008008 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008009 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008010 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008011 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008012 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00008013 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00008014 if (VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008015 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008016 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008017 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00008018 Op.getOperand(0)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008019 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00008020 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00008021 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00008022 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008023}
8024
Dan Gohmand858e902010-04-17 15:26:15 +00008025SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008026 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00008027 SDValue Op0 = Op.getOperand(0);
8028 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008029 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008030 EVT VT = Op.getValueType();
8031 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00008032
8033 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008034 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008035 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008036 SrcVT = VT;
8037 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008038 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008039 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008040 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008041 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008042 }
8043
8044 // At this point the operands and the result should have the same
8045 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00008046
Evan Cheng68c47cb2007-01-05 07:55:56 +00008047 // First get the sign bit of second operand.
8048 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008049 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008050 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
8051 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008052 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008053 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
8054 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8055 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8056 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008057 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008058 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008059 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008060 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008061 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00008062 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008063 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008064
8065 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008066 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008067 // Op0 is MVT::f32, Op1 is MVT::f64.
8068 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
8069 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
8070 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008071 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00008072 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00008073 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008074 }
8075
Evan Cheng73d6cf12007-01-05 21:37:56 +00008076 // Clear first operand sign bit.
8077 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00008078 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008079 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
8080 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008081 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008082 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
8083 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8084 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8085 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008086 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008087 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008088 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008089 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008090 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00008091 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008092 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008093
8094 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00008095 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008096}
8097
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008098SDValue X86TargetLowering::LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) const {
8099 SDValue N0 = Op.getOperand(0);
8100 DebugLoc dl = Op.getDebugLoc();
8101 EVT VT = Op.getValueType();
8102
8103 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
8104 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
8105 DAG.getConstant(1, VT));
8106 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
8107}
8108
Dan Gohman076aee32009-03-04 19:44:21 +00008109/// Emit nodes that will be selected as "test Op0,Op0", or something
8110/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008111SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008112 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008113 DebugLoc dl = Op.getDebugLoc();
8114
Dan Gohman31125812009-03-07 01:58:32 +00008115 // CF and OF aren't always set the way we want. Determine which
8116 // of these we need.
8117 bool NeedCF = false;
8118 bool NeedOF = false;
8119 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008120 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00008121 case X86::COND_A: case X86::COND_AE:
8122 case X86::COND_B: case X86::COND_BE:
8123 NeedCF = true;
8124 break;
8125 case X86::COND_G: case X86::COND_GE:
8126 case X86::COND_L: case X86::COND_LE:
8127 case X86::COND_O: case X86::COND_NO:
8128 NeedOF = true;
8129 break;
Dan Gohman31125812009-03-07 01:58:32 +00008130 }
8131
Dan Gohman076aee32009-03-04 19:44:21 +00008132 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00008133 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
8134 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008135 if (Op.getResNo() != 0 || NeedOF || NeedCF)
8136 // Emit a CMP with 0, which is the TEST pattern.
8137 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8138 DAG.getConstant(0, Op.getValueType()));
8139
8140 unsigned Opcode = 0;
8141 unsigned NumOperands = 0;
8142 switch (Op.getNode()->getOpcode()) {
8143 case ISD::ADD:
8144 // Due to an isel shortcoming, be conservative if this add is likely to be
8145 // selected as part of a load-modify-store instruction. When the root node
8146 // in a match is a store, isel doesn't know how to remap non-chain non-flag
8147 // uses of other nodes in the match, such as the ADD in this case. This
8148 // leads to the ADD being left around and reselected, with the result being
8149 // two adds in the output. Alas, even if none our users are stores, that
8150 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
8151 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
8152 // climbing the DAG back to the root, and it doesn't seem to be worth the
8153 // effort.
8154 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Dan Gohman076aee32009-03-04 19:44:21 +00008155 UE = Op.getNode()->use_end(); UI != UE; ++UI)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008156 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
8157 goto default_case;
8158
8159 if (ConstantSDNode *C =
8160 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
8161 // An add of one will be selected as an INC.
8162 if (C->getAPIntValue() == 1) {
8163 Opcode = X86ISD::INC;
8164 NumOperands = 1;
8165 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00008166 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008167
8168 // An add of negative one (subtract of one) will be selected as a DEC.
8169 if (C->getAPIntValue().isAllOnesValue()) {
8170 Opcode = X86ISD::DEC;
8171 NumOperands = 1;
8172 break;
8173 }
Dan Gohman076aee32009-03-04 19:44:21 +00008174 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008175
8176 // Otherwise use a regular EFLAGS-setting add.
8177 Opcode = X86ISD::ADD;
8178 NumOperands = 2;
8179 break;
8180 case ISD::AND: {
8181 // If the primary and result isn't used, don't bother using X86ISD::AND,
8182 // because a TEST instruction will be better.
8183 bool NonFlagUse = false;
8184 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8185 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
8186 SDNode *User = *UI;
8187 unsigned UOpNo = UI.getOperandNo();
8188 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
8189 // Look pass truncate.
8190 UOpNo = User->use_begin().getOperandNo();
8191 User = *User->use_begin();
8192 }
8193
8194 if (User->getOpcode() != ISD::BRCOND &&
8195 User->getOpcode() != ISD::SETCC &&
8196 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
8197 NonFlagUse = true;
8198 break;
8199 }
Dan Gohman076aee32009-03-04 19:44:21 +00008200 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008201
8202 if (!NonFlagUse)
8203 break;
8204 }
8205 // FALL THROUGH
8206 case ISD::SUB:
8207 case ISD::OR:
8208 case ISD::XOR:
8209 // Due to the ISEL shortcoming noted above, be conservative if this op is
8210 // likely to be selected as part of a load-modify-store instruction.
8211 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8212 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8213 if (UI->getOpcode() == ISD::STORE)
8214 goto default_case;
8215
8216 // Otherwise use a regular EFLAGS-setting instruction.
8217 switch (Op.getNode()->getOpcode()) {
8218 default: llvm_unreachable("unexpected operator!");
8219 case ISD::SUB: Opcode = X86ISD::SUB; break;
8220 case ISD::OR: Opcode = X86ISD::OR; break;
8221 case ISD::XOR: Opcode = X86ISD::XOR; break;
8222 case ISD::AND: Opcode = X86ISD::AND; break;
8223 }
8224
8225 NumOperands = 2;
8226 break;
8227 case X86ISD::ADD:
8228 case X86ISD::SUB:
8229 case X86ISD::INC:
8230 case X86ISD::DEC:
8231 case X86ISD::OR:
8232 case X86ISD::XOR:
8233 case X86ISD::AND:
8234 return SDValue(Op.getNode(), 1);
8235 default:
8236 default_case:
8237 break;
Dan Gohman076aee32009-03-04 19:44:21 +00008238 }
8239
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008240 if (Opcode == 0)
8241 // Emit a CMP with 0, which is the TEST pattern.
8242 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8243 DAG.getConstant(0, Op.getValueType()));
8244
8245 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
8246 SmallVector<SDValue, 4> Ops;
8247 for (unsigned i = 0; i != NumOperands; ++i)
8248 Ops.push_back(Op.getOperand(i));
8249
8250 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
8251 DAG.ReplaceAllUsesWith(Op, New);
8252 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00008253}
8254
8255/// Emit nodes that will be selected as "cmp Op0,Op1", or something
8256/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008257SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008258 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008259 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
8260 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00008261 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00008262
8263 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00008264 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00008265}
8266
Evan Chengd40d03e2010-01-06 19:38:29 +00008267/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
8268/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00008269SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
8270 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008271 SDValue Op0 = And.getOperand(0);
8272 SDValue Op1 = And.getOperand(1);
8273 if (Op0.getOpcode() == ISD::TRUNCATE)
8274 Op0 = Op0.getOperand(0);
8275 if (Op1.getOpcode() == ISD::TRUNCATE)
8276 Op1 = Op1.getOperand(0);
8277
Evan Chengd40d03e2010-01-06 19:38:29 +00008278 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008279 if (Op1.getOpcode() == ISD::SHL)
8280 std::swap(Op0, Op1);
8281 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008282 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
8283 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008284 // If we looked past a truncate, check that it's only truncating away
8285 // known zeros.
8286 unsigned BitWidth = Op0.getValueSizeInBits();
8287 unsigned AndBitWidth = And.getValueSizeInBits();
8288 if (BitWidth > AndBitWidth) {
8289 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
8290 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
8291 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
8292 return SDValue();
8293 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008294 LHS = Op1;
8295 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00008296 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008297 } else if (Op1.getOpcode() == ISD::Constant) {
8298 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
8299 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00008300 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
8301 LHS = AndLHS.getOperand(0);
8302 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008303 }
Evan Chengd40d03e2010-01-06 19:38:29 +00008304 }
Evan Cheng0488db92007-09-25 01:57:46 +00008305
Evan Chengd40d03e2010-01-06 19:38:29 +00008306 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00008307 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00008308 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00008309 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00008310 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00008311 // Also promote i16 to i32 for performance / code size reason.
8312 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00008313 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00008314 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00008315
Evan Chengd40d03e2010-01-06 19:38:29 +00008316 // If the operand types disagree, extend the shift amount to match. Since
8317 // BT ignores high bits (like shifts) we can use anyextend.
8318 if (LHS.getValueType() != RHS.getValueType())
8319 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008320
Evan Chengd40d03e2010-01-06 19:38:29 +00008321 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
8322 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
8323 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8324 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00008325 }
8326
Evan Cheng54de3ea2010-01-05 06:52:31 +00008327 return SDValue();
8328}
8329
Dan Gohmand858e902010-04-17 15:26:15 +00008330SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Duncan Sands28b77e92011-09-06 19:07:46 +00008331
8332 if (Op.getValueType().isVector()) return LowerVSETCC(Op, DAG);
8333
Evan Cheng54de3ea2010-01-05 06:52:31 +00008334 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
8335 SDValue Op0 = Op.getOperand(0);
8336 SDValue Op1 = Op.getOperand(1);
8337 DebugLoc dl = Op.getDebugLoc();
8338 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
8339
8340 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00008341 // Lower (X & (1 << N)) == 0 to BT(X, N).
8342 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
8343 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00008344 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008345 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00008346 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008347 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
8348 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
8349 if (NewSetCC.getNode())
8350 return NewSetCC;
8351 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00008352
Chris Lattner481eebc2010-12-19 21:23:48 +00008353 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
8354 // these.
8355 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00008356 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00008357 cast<ConstantSDNode>(Op1)->isNullValue()) &&
8358 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008359
Chris Lattner481eebc2010-12-19 21:23:48 +00008360 // If the input is a setcc, then reuse the input setcc or use a new one with
8361 // the inverted condition.
8362 if (Op0.getOpcode() == X86ISD::SETCC) {
8363 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
8364 bool Invert = (CC == ISD::SETNE) ^
8365 cast<ConstantSDNode>(Op1)->isNullValue();
8366 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008367
Evan Cheng2c755ba2010-02-27 07:36:59 +00008368 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00008369 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8370 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
8371 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008372 }
8373
Evan Chenge5b51ac2010-04-17 06:13:15 +00008374 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00008375 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008376 if (X86CC == X86::COND_INVALID)
8377 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008378
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008379 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00008380 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008381 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00008382}
8383
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008384// Lower256IntVETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
8385// ones, and then concatenate the result back.
8386static SDValue Lower256IntVETCC(SDValue Op, SelectionDAG &DAG) {
8387 EVT VT = Op.getValueType();
8388
Duncan Sands28b77e92011-09-06 19:07:46 +00008389 assert(VT.getSizeInBits() == 256 && Op.getOpcode() == ISD::SETCC &&
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008390 "Unsupported value type for operation");
8391
8392 int NumElems = VT.getVectorNumElements();
8393 DebugLoc dl = Op.getDebugLoc();
8394 SDValue CC = Op.getOperand(2);
8395 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
8396 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
8397
8398 // Extract the LHS vectors
8399 SDValue LHS = Op.getOperand(0);
8400 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
8401 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
8402
8403 // Extract the RHS vectors
8404 SDValue RHS = Op.getOperand(1);
8405 SDValue RHS1 = Extract128BitVector(RHS, Idx0, DAG, dl);
8406 SDValue RHS2 = Extract128BitVector(RHS, Idx1, DAG, dl);
8407
8408 // Issue the operation on the smaller types and concatenate the result back
8409 MVT EltVT = VT.getVectorElementType().getSimpleVT();
8410 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
8411 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
8412 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
8413 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
8414}
8415
8416
Dan Gohmand858e902010-04-17 15:26:15 +00008417SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008418 SDValue Cond;
8419 SDValue Op0 = Op.getOperand(0);
8420 SDValue Op1 = Op.getOperand(1);
8421 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00008422 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00008423 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
8424 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008425 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00008426
8427 if (isFP) {
8428 unsigned SSECC = 8;
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008429 EVT EltVT = Op0.getValueType().getVectorElementType();
8430 assert(EltVT == MVT::f32 || EltVT == MVT::f64);
8431
8432 unsigned Opc = EltVT == MVT::f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00008433 bool Swap = false;
8434
8435 switch (SetCCOpcode) {
8436 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008437 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00008438 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00008439 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00008440 case ISD::SETGT: Swap = true; // Fallthrough
8441 case ISD::SETLT:
8442 case ISD::SETOLT: SSECC = 1; break;
8443 case ISD::SETOGE:
8444 case ISD::SETGE: Swap = true; // Fallthrough
8445 case ISD::SETLE:
8446 case ISD::SETOLE: SSECC = 2; break;
8447 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008448 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00008449 case ISD::SETNE: SSECC = 4; break;
8450 case ISD::SETULE: Swap = true;
8451 case ISD::SETUGE: SSECC = 5; break;
8452 case ISD::SETULT: Swap = true;
8453 case ISD::SETUGT: SSECC = 6; break;
8454 case ISD::SETO: SSECC = 7; break;
8455 }
8456 if (Swap)
8457 std::swap(Op0, Op1);
8458
Nate Begemanfb8ead02008-07-25 19:05:58 +00008459 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00008460 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00008461 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00008462 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00008463 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
8464 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00008465 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00008466 }
8467 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00008468 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00008469 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
8470 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00008471 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00008472 }
Torok Edwinc23197a2009-07-14 16:55:14 +00008473 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00008474 }
8475 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00008476 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00008477 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008478
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008479 // Break 256-bit integer vector compare into smaller ones.
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008480 if (!isFP && VT.getSizeInBits() == 256)
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008481 return Lower256IntVETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008482
Nate Begeman30a0de92008-07-17 16:51:19 +00008483 // We are handling one of the integer comparisons here. Since SSE only has
8484 // GT and EQ comparisons for integer, swapping operands and multiple
8485 // operations may be required for some comparisons.
8486 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
8487 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00008488
Owen Anderson825b72b2009-08-11 20:47:22 +00008489 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00008490 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00008491 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00008492 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00008493 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
8494 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008495 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008496
Nate Begeman30a0de92008-07-17 16:51:19 +00008497 switch (SetCCOpcode) {
8498 default: break;
8499 case ISD::SETNE: Invert = true;
8500 case ISD::SETEQ: Opc = EQOpc; break;
8501 case ISD::SETLT: Swap = true;
8502 case ISD::SETGT: Opc = GTOpc; break;
8503 case ISD::SETGE: Swap = true;
8504 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
8505 case ISD::SETULT: Swap = true;
8506 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
8507 case ISD::SETUGE: Swap = true;
8508 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
8509 }
8510 if (Swap)
8511 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008512
Nate Begeman30a0de92008-07-17 16:51:19 +00008513 // Since SSE has no unsigned integer comparisons, we need to flip the sign
8514 // bits of the inputs before performing those operations.
8515 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00008516 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00008517 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
8518 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00008519 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00008520 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
8521 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00008522 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
8523 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00008524 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008525
Dale Johannesenace16102009-02-03 19:33:06 +00008526 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00008527
8528 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00008529 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00008530 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00008531
Nate Begeman30a0de92008-07-17 16:51:19 +00008532 return Result;
8533}
Evan Cheng0488db92007-09-25 01:57:46 +00008534
Evan Cheng370e5342008-12-03 08:38:43 +00008535// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00008536static bool isX86LogicalCmp(SDValue Op) {
8537 unsigned Opc = Op.getNode()->getOpcode();
8538 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
8539 return true;
8540 if (Op.getResNo() == 1 &&
8541 (Opc == X86ISD::ADD ||
8542 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00008543 Opc == X86ISD::ADC ||
8544 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00008545 Opc == X86ISD::SMUL ||
8546 Opc == X86ISD::UMUL ||
8547 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00008548 Opc == X86ISD::DEC ||
8549 Opc == X86ISD::OR ||
8550 Opc == X86ISD::XOR ||
8551 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00008552 return true;
8553
Chris Lattner9637d5b2010-12-05 07:49:54 +00008554 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
8555 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008556
Dan Gohman076aee32009-03-04 19:44:21 +00008557 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00008558}
8559
Chris Lattnera2b56002010-12-05 01:23:24 +00008560static bool isZero(SDValue V) {
8561 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8562 return C && C->isNullValue();
8563}
8564
Chris Lattner96908b12010-12-05 02:00:51 +00008565static bool isAllOnes(SDValue V) {
8566 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8567 return C && C->isAllOnesValue();
8568}
8569
Dan Gohmand858e902010-04-17 15:26:15 +00008570SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008571 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008572 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00008573 SDValue Op1 = Op.getOperand(1);
8574 SDValue Op2 = Op.getOperand(2);
8575 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008576 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00008577
Dan Gohman1a492952009-10-20 16:22:37 +00008578 if (Cond.getOpcode() == ISD::SETCC) {
8579 SDValue NewCond = LowerSETCC(Cond, DAG);
8580 if (NewCond.getNode())
8581 Cond = NewCond;
8582 }
Evan Cheng734503b2006-09-11 02:19:56 +00008583
Chris Lattnera2b56002010-12-05 01:23:24 +00008584 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008585 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00008586 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008587 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008588 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00008589 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
8590 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008591 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008592
Chris Lattnera2b56002010-12-05 01:23:24 +00008593 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008594
8595 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00008596 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
8597 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00008598
8599 SDValue CmpOp0 = Cmp.getOperand(0);
8600 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
8601 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008602
Chris Lattner96908b12010-12-05 02:00:51 +00008603 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00008604 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8605 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008606
Chris Lattner96908b12010-12-05 02:00:51 +00008607 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
8608 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008609
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008610 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00008611 if (N2C == 0 || !N2C->isNullValue())
8612 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
8613 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008614 }
8615 }
8616
Chris Lattnera2b56002010-12-05 01:23:24 +00008617 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00008618 if (Cond.getOpcode() == ISD::AND &&
8619 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8620 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008621 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008622 Cond = Cond.getOperand(0);
8623 }
8624
Evan Cheng3f41d662007-10-08 22:16:29 +00008625 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8626 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00008627 if (Cond.getOpcode() == X86ISD::SETCC ||
8628 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008629 CC = Cond.getOperand(0);
8630
Dan Gohman475871a2008-07-27 21:46:04 +00008631 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008632 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00008633 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00008634
Evan Cheng3f41d662007-10-08 22:16:29 +00008635 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008636 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00008637 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00008638 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00008639
Chris Lattnerd1980a52009-03-12 06:52:53 +00008640 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
8641 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00008642 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008643 addTest = false;
8644 }
8645 }
8646
8647 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008648 // Look pass the truncate.
8649 if (Cond.getOpcode() == ISD::TRUNCATE)
8650 Cond = Cond.getOperand(0);
8651
8652 // We know the result of AND is compared against zero. Try to match
8653 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00008654 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00008655 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00008656 if (NewSetCC.getNode()) {
8657 CC = NewSetCC.getOperand(0);
8658 Cond = NewSetCC.getOperand(1);
8659 addTest = false;
8660 }
8661 }
8662 }
8663
8664 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008665 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00008666 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00008667 }
8668
Benjamin Kramere915ff32010-12-22 23:09:28 +00008669 // a < b ? -1 : 0 -> RES = ~setcc_carry
8670 // a < b ? 0 : -1 -> RES = setcc_carry
8671 // a >= b ? -1 : 0 -> RES = setcc_carry
8672 // a >= b ? 0 : -1 -> RES = ~setcc_carry
8673 if (Cond.getOpcode() == X86ISD::CMP) {
8674 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
8675
8676 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
8677 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
8678 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8679 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
8680 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
8681 return DAG.getNOT(DL, Res, Res.getValueType());
8682 return Res;
8683 }
8684 }
8685
Evan Cheng0488db92007-09-25 01:57:46 +00008686 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
8687 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008688 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008689 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00008690 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00008691}
8692
Nadav Rotemffe3e7d2011-09-08 08:11:19 +00008693SDValue X86TargetLowering::LowerVSELECT(SDValue Op, SelectionDAG &DAG) const {
8694 SDValue Cond = Op.getOperand(0);
8695 SDValue Op1 = Op.getOperand(1);
8696 SDValue Op2 = Op.getOperand(2);
8697 DebugLoc DL = Op.getDebugLoc();
8698
Bruno Cardoso Lopes814c6ce2011-09-08 18:05:08 +00008699 SDValue Ops[] = {Op1, Op2, Cond};
Nadav Rotemffe3e7d2011-09-08 08:11:19 +00008700
8701 assert(Op1.getValueType().isVector() && "Op1 must be a vector");
8702 assert(Op2.getValueType().isVector() && "Op2 must be a vector");
8703 assert(Cond.getValueType().isVector() && "Cond must be a vector");
8704 assert(Op1.getValueType() == Op2.getValueType() && "Type mismatch");
Nadav Rotemee64be92011-09-08 22:17:35 +00008705
8706 EVT VT = Op1.getValueType();
8707 switch (VT.getSimpleVT().SimpleTy) {
Nadav Rotemffe3e7d2011-09-08 08:11:19 +00008708 default: break;
Nadav Rotemee64be92011-09-08 22:17:35 +00008709 case MVT::v2i64:
8710 case MVT::v2f64:
8711 return DAG.getNode(X86ISD::BLENDVPD, DL, VT, Ops, array_lengthof(Ops));
8712 case MVT::v4i32:
8713 case MVT::v4f32:
8714 return DAG.getNode(X86ISD::BLENDVPS, DL, VT , Ops, array_lengthof(Ops));
8715 case MVT::v16i8:
8716 return DAG.getNode(X86ISD::PBLENDVB, DL, VT , Ops, array_lengthof(Ops));
Nadav Rotemffe3e7d2011-09-08 08:11:19 +00008717 }
Nadav Rotemee64be92011-09-08 22:17:35 +00008718
Nadav Rotemffe3e7d2011-09-08 08:11:19 +00008719 return SDValue();
8720}
8721
8722
Evan Cheng370e5342008-12-03 08:38:43 +00008723// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
8724// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
8725// from the AND / OR.
8726static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
8727 Opc = Op.getOpcode();
8728 if (Opc != ISD::OR && Opc != ISD::AND)
8729 return false;
8730 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8731 Op.getOperand(0).hasOneUse() &&
8732 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
8733 Op.getOperand(1).hasOneUse());
8734}
8735
Evan Cheng961d6d42009-02-02 08:19:07 +00008736// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
8737// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00008738static bool isXor1OfSetCC(SDValue Op) {
8739 if (Op.getOpcode() != ISD::XOR)
8740 return false;
8741 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
8742 if (N1C && N1C->getAPIntValue() == 1) {
8743 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8744 Op.getOperand(0).hasOneUse();
8745 }
8746 return false;
8747}
8748
Dan Gohmand858e902010-04-17 15:26:15 +00008749SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008750 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008751 SDValue Chain = Op.getOperand(0);
8752 SDValue Cond = Op.getOperand(1);
8753 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008754 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008755 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00008756
Dan Gohman1a492952009-10-20 16:22:37 +00008757 if (Cond.getOpcode() == ISD::SETCC) {
8758 SDValue NewCond = LowerSETCC(Cond, DAG);
8759 if (NewCond.getNode())
8760 Cond = NewCond;
8761 }
Chris Lattnere55484e2008-12-25 05:34:37 +00008762#if 0
8763 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00008764 else if (Cond.getOpcode() == X86ISD::ADD ||
8765 Cond.getOpcode() == X86ISD::SUB ||
8766 Cond.getOpcode() == X86ISD::SMUL ||
8767 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00008768 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00008769#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00008770
Evan Chengad9c0a32009-12-15 00:53:42 +00008771 // Look pass (and (setcc_carry (cmp ...)), 1).
8772 if (Cond.getOpcode() == ISD::AND &&
8773 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8774 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008775 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008776 Cond = Cond.getOperand(0);
8777 }
8778
Evan Cheng3f41d662007-10-08 22:16:29 +00008779 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8780 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00008781 if (Cond.getOpcode() == X86ISD::SETCC ||
8782 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008783 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008784
Dan Gohman475871a2008-07-27 21:46:04 +00008785 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008786 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00008787 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00008788 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00008789 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008790 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00008791 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00008792 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00008793 default: break;
8794 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00008795 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00008796 // These can only come from an arithmetic instruction with overflow,
8797 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00008798 Cond = Cond.getNode()->getOperand(1);
8799 addTest = false;
8800 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00008801 }
Evan Cheng0488db92007-09-25 01:57:46 +00008802 }
Evan Cheng370e5342008-12-03 08:38:43 +00008803 } else {
8804 unsigned CondOpc;
8805 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
8806 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00008807 if (CondOpc == ISD::OR) {
8808 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
8809 // two branches instead of an explicit OR instruction with a
8810 // separate test.
8811 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00008812 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00008813 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008814 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00008815 Chain, Dest, CC, Cmp);
8816 CC = Cond.getOperand(1).getOperand(0);
8817 Cond = Cmp;
8818 addTest = false;
8819 }
8820 } else { // ISD::AND
8821 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
8822 // two branches instead of an explicit AND instruction with a
8823 // separate test. However, we only do this if this block doesn't
8824 // have a fall-through edge, because this requires an explicit
8825 // jmp when the condition is false.
8826 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00008827 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00008828 Op.getNode()->hasOneUse()) {
8829 X86::CondCode CCode =
8830 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
8831 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008832 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00008833 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00008834 // Look for an unconditional branch following this conditional branch.
8835 // We need this because we need to reverse the successors in order
8836 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00008837 if (User->getOpcode() == ISD::BR) {
8838 SDValue FalseBB = User->getOperand(1);
8839 SDNode *NewBR =
8840 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00008841 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00008842 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00008843 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00008844
Dale Johannesene4d209d2009-02-03 20:21:25 +00008845 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00008846 Chain, Dest, CC, Cmp);
8847 X86::CondCode CCode =
8848 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
8849 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008850 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00008851 Cond = Cmp;
8852 addTest = false;
8853 }
8854 }
Dan Gohman279c22e2008-10-21 03:29:32 +00008855 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00008856 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
8857 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
8858 // It should be transformed during dag combiner except when the condition
8859 // is set by a arithmetics with overflow node.
8860 X86::CondCode CCode =
8861 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
8862 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008863 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00008864 Cond = Cond.getOperand(0).getOperand(1);
8865 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00008866 }
Evan Cheng0488db92007-09-25 01:57:46 +00008867 }
8868
8869 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008870 // Look pass the truncate.
8871 if (Cond.getOpcode() == ISD::TRUNCATE)
8872 Cond = Cond.getOperand(0);
8873
8874 // We know the result of AND is compared against zero. Try to match
8875 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00008876 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008877 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
8878 if (NewSetCC.getNode()) {
8879 CC = NewSetCC.getOperand(0);
8880 Cond = NewSetCC.getOperand(1);
8881 addTest = false;
8882 }
8883 }
8884 }
8885
8886 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008887 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00008888 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00008889 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00008890 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00008891 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00008892}
8893
Anton Korobeynikove060b532007-04-17 19:34:00 +00008894
8895// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
8896// Calls to _alloca is needed to probe the stack when allocating more than 4k
8897// bytes in one go. Touching the stack at 4K increments is necessary to ensure
8898// that the guard pages used by the OS virtual memory manager are allocated in
8899// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00008900SDValue
8901X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008902 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008903 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
8904 EnableSegmentedStacks) &&
8905 "This should be used only on Windows targets or when segmented stacks "
Rafael Espindola96428ce2011-09-06 18:43:08 +00008906 "are being used");
8907 assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008908 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008909
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008910 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00008911 SDValue Chain = Op.getOperand(0);
8912 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008913 // FIXME: Ensure alignment here
8914
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008915 bool Is64Bit = Subtarget->is64Bit();
8916 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008917
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008918 if (EnableSegmentedStacks) {
8919 MachineFunction &MF = DAG.getMachineFunction();
8920 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008921
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008922 if (Is64Bit) {
8923 // The 64 bit implementation of segmented stacks needs to clobber both r10
Rafael Espindola96428ce2011-09-06 18:43:08 +00008924 // r11. This makes it impossible to use it along with nested parameters.
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008925 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00008926
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008927 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
8928 I != E; I++)
8929 if (I->hasNestAttr())
8930 report_fatal_error("Cannot use segmented stacks with functions that "
8931 "have nested arguments.");
8932 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00008933
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008934 const TargetRegisterClass *AddrRegClass =
8935 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
8936 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
8937 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
8938 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
8939 DAG.getRegister(Vreg, SPTy));
8940 SDValue Ops1[2] = { Value, Chain };
8941 return DAG.getMergeValues(Ops1, 2, dl);
8942 } else {
8943 SDValue Flag;
8944 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008945
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008946 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
8947 Flag = Chain.getValue(1);
8948 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008949
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008950 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
8951 Flag = Chain.getValue(1);
8952
8953 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
8954
8955 SDValue Ops1[2] = { Chain.getValue(0), Chain };
8956 return DAG.getMergeValues(Ops1, 2, dl);
8957 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008958}
8959
Dan Gohmand858e902010-04-17 15:26:15 +00008960SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00008961 MachineFunction &MF = DAG.getMachineFunction();
8962 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
8963
Dan Gohman69de1932008-02-06 22:27:42 +00008964 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00008965 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00008966
Anton Korobeynikove7beda12010-10-03 22:52:07 +00008967 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00008968 // vastart just stores the address of the VarArgsFrameIndex slot into the
8969 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00008970 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
8971 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00008972 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
8973 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008974 }
8975
8976 // __va_list_tag:
8977 // gp_offset (0 - 6 * 8)
8978 // fp_offset (48 - 48 + 8 * 16)
8979 // overflow_arg_area (point to parameters coming in memory).
8980 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00008981 SmallVector<SDValue, 8> MemOps;
8982 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00008983 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00008984 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00008985 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
8986 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008987 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008988 MemOps.push_back(Store);
8989
8990 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00008991 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008992 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008993 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00008994 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
8995 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008996 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008997 MemOps.push_back(Store);
8998
8999 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00009000 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009001 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00009002 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9003 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009004 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
9005 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00009006 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009007 MemOps.push_back(Store);
9008
9009 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00009010 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009011 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00009012 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
9013 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009014 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
9015 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009016 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009017 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00009018 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00009019}
9020
Dan Gohmand858e902010-04-17 15:26:15 +00009021SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00009022 assert(Subtarget->is64Bit() &&
9023 "LowerVAARG only handles 64-bit va_arg!");
9024 assert((Subtarget->isTargetLinux() ||
9025 Subtarget->isTargetDarwin()) &&
9026 "Unhandled target in LowerVAARG");
9027 assert(Op.getNode()->getNumOperands() == 4);
9028 SDValue Chain = Op.getOperand(0);
9029 SDValue SrcPtr = Op.getOperand(1);
9030 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
9031 unsigned Align = Op.getConstantOperandVal(3);
9032 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00009033
Dan Gohman320afb82010-10-12 18:00:49 +00009034 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009035 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Dan Gohman320afb82010-10-12 18:00:49 +00009036 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
9037 uint8_t ArgMode;
9038
9039 // Decide which area this value should be read from.
9040 // TODO: Implement the AMD64 ABI in its entirety. This simple
9041 // selection mechanism works only for the basic types.
9042 if (ArgVT == MVT::f80) {
9043 llvm_unreachable("va_arg for f80 not yet implemented");
9044 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
9045 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
9046 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
9047 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
9048 } else {
9049 llvm_unreachable("Unhandled argument type in LowerVAARG");
9050 }
9051
9052 if (ArgMode == 2) {
9053 // Sanity Check: Make sure using fp_offset makes sense.
Michael J. Spencer87b86652010-10-19 07:32:42 +00009054 assert(!UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00009055 !(DAG.getMachineFunction()
9056 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00009057 Subtarget->hasXMM());
Dan Gohman320afb82010-10-12 18:00:49 +00009058 }
9059
9060 // Insert VAARG_64 node into the DAG
9061 // VAARG_64 returns two values: Variable Argument Address, Chain
9062 SmallVector<SDValue, 11> InstOps;
9063 InstOps.push_back(Chain);
9064 InstOps.push_back(SrcPtr);
9065 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
9066 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
9067 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
9068 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
9069 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
9070 VTs, &InstOps[0], InstOps.size(),
9071 MVT::i64,
9072 MachinePointerInfo(SV),
9073 /*Align=*/0,
9074 /*Volatile=*/false,
9075 /*ReadMem=*/true,
9076 /*WriteMem=*/true);
9077 Chain = VAARG.getValue(1);
9078
9079 // Load the next argument and return it
9080 return DAG.getLoad(ArgVT, dl,
9081 Chain,
9082 VAARG,
9083 MachinePointerInfo(),
9084 false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00009085}
9086
Dan Gohmand858e902010-04-17 15:26:15 +00009087SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00009088 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00009089 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00009090 SDValue Chain = Op.getOperand(0);
9091 SDValue DstPtr = Op.getOperand(1);
9092 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00009093 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
9094 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00009095 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00009096
Chris Lattnere72f2022010-09-21 05:40:29 +00009097 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00009098 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00009099 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00009100 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00009101}
9102
Dan Gohman475871a2008-07-27 21:46:04 +00009103SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00009104X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009105 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009106 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00009107 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00009108 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00009109 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00009110 case Intrinsic::x86_sse_comieq_ss:
9111 case Intrinsic::x86_sse_comilt_ss:
9112 case Intrinsic::x86_sse_comile_ss:
9113 case Intrinsic::x86_sse_comigt_ss:
9114 case Intrinsic::x86_sse_comige_ss:
9115 case Intrinsic::x86_sse_comineq_ss:
9116 case Intrinsic::x86_sse_ucomieq_ss:
9117 case Intrinsic::x86_sse_ucomilt_ss:
9118 case Intrinsic::x86_sse_ucomile_ss:
9119 case Intrinsic::x86_sse_ucomigt_ss:
9120 case Intrinsic::x86_sse_ucomige_ss:
9121 case Intrinsic::x86_sse_ucomineq_ss:
9122 case Intrinsic::x86_sse2_comieq_sd:
9123 case Intrinsic::x86_sse2_comilt_sd:
9124 case Intrinsic::x86_sse2_comile_sd:
9125 case Intrinsic::x86_sse2_comigt_sd:
9126 case Intrinsic::x86_sse2_comige_sd:
9127 case Intrinsic::x86_sse2_comineq_sd:
9128 case Intrinsic::x86_sse2_ucomieq_sd:
9129 case Intrinsic::x86_sse2_ucomilt_sd:
9130 case Intrinsic::x86_sse2_ucomile_sd:
9131 case Intrinsic::x86_sse2_ucomigt_sd:
9132 case Intrinsic::x86_sse2_ucomige_sd:
9133 case Intrinsic::x86_sse2_ucomineq_sd: {
9134 unsigned Opc = 0;
9135 ISD::CondCode CC = ISD::SETCC_INVALID;
9136 switch (IntNo) {
9137 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009138 case Intrinsic::x86_sse_comieq_ss:
9139 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009140 Opc = X86ISD::COMI;
9141 CC = ISD::SETEQ;
9142 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009143 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009144 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009145 Opc = X86ISD::COMI;
9146 CC = ISD::SETLT;
9147 break;
9148 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009149 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009150 Opc = X86ISD::COMI;
9151 CC = ISD::SETLE;
9152 break;
9153 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009154 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009155 Opc = X86ISD::COMI;
9156 CC = ISD::SETGT;
9157 break;
9158 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009159 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009160 Opc = X86ISD::COMI;
9161 CC = ISD::SETGE;
9162 break;
9163 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009164 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009165 Opc = X86ISD::COMI;
9166 CC = ISD::SETNE;
9167 break;
9168 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009169 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009170 Opc = X86ISD::UCOMI;
9171 CC = ISD::SETEQ;
9172 break;
9173 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009174 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009175 Opc = X86ISD::UCOMI;
9176 CC = ISD::SETLT;
9177 break;
9178 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009179 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009180 Opc = X86ISD::UCOMI;
9181 CC = ISD::SETLE;
9182 break;
9183 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009184 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009185 Opc = X86ISD::UCOMI;
9186 CC = ISD::SETGT;
9187 break;
9188 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009189 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009190 Opc = X86ISD::UCOMI;
9191 CC = ISD::SETGE;
9192 break;
9193 case Intrinsic::x86_sse_ucomineq_ss:
9194 case Intrinsic::x86_sse2_ucomineq_sd:
9195 Opc = X86ISD::UCOMI;
9196 CC = ISD::SETNE;
9197 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009198 }
Evan Cheng734503b2006-09-11 02:19:56 +00009199
Dan Gohman475871a2008-07-27 21:46:04 +00009200 SDValue LHS = Op.getOperand(1);
9201 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00009202 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00009203 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00009204 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
9205 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9206 DAG.getConstant(X86CC, MVT::i8), Cond);
9207 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00009208 }
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009209 // ptest and testp intrinsics. The intrinsic these come from are designed to
9210 // return an integer value, not just an instruction so lower it to the ptest
9211 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00009212 case Intrinsic::x86_sse41_ptestz:
9213 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009214 case Intrinsic::x86_sse41_ptestnzc:
9215 case Intrinsic::x86_avx_ptestz_256:
9216 case Intrinsic::x86_avx_ptestc_256:
9217 case Intrinsic::x86_avx_ptestnzc_256:
9218 case Intrinsic::x86_avx_vtestz_ps:
9219 case Intrinsic::x86_avx_vtestc_ps:
9220 case Intrinsic::x86_avx_vtestnzc_ps:
9221 case Intrinsic::x86_avx_vtestz_pd:
9222 case Intrinsic::x86_avx_vtestc_pd:
9223 case Intrinsic::x86_avx_vtestnzc_pd:
9224 case Intrinsic::x86_avx_vtestz_ps_256:
9225 case Intrinsic::x86_avx_vtestc_ps_256:
9226 case Intrinsic::x86_avx_vtestnzc_ps_256:
9227 case Intrinsic::x86_avx_vtestz_pd_256:
9228 case Intrinsic::x86_avx_vtestc_pd_256:
9229 case Intrinsic::x86_avx_vtestnzc_pd_256: {
9230 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00009231 unsigned X86CC = 0;
9232 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00009233 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009234 case Intrinsic::x86_avx_vtestz_ps:
9235 case Intrinsic::x86_avx_vtestz_pd:
9236 case Intrinsic::x86_avx_vtestz_ps_256:
9237 case Intrinsic::x86_avx_vtestz_pd_256:
9238 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009239 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009240 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009241 // ZF = 1
9242 X86CC = X86::COND_E;
9243 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009244 case Intrinsic::x86_avx_vtestc_ps:
9245 case Intrinsic::x86_avx_vtestc_pd:
9246 case Intrinsic::x86_avx_vtestc_ps_256:
9247 case Intrinsic::x86_avx_vtestc_pd_256:
9248 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009249 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009250 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009251 // CF = 1
9252 X86CC = X86::COND_B;
9253 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009254 case Intrinsic::x86_avx_vtestnzc_ps:
9255 case Intrinsic::x86_avx_vtestnzc_pd:
9256 case Intrinsic::x86_avx_vtestnzc_ps_256:
9257 case Intrinsic::x86_avx_vtestnzc_pd_256:
9258 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00009259 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009260 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009261 // ZF and CF = 0
9262 X86CC = X86::COND_A;
9263 break;
9264 }
Eric Christopherfd179292009-08-27 18:07:15 +00009265
Eric Christopher71c67532009-07-29 00:28:05 +00009266 SDValue LHS = Op.getOperand(1);
9267 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009268 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
9269 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00009270 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
9271 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
9272 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00009273 }
Evan Cheng5759f972008-05-04 09:15:50 +00009274
9275 // Fix vector shift instructions where the last operand is a non-immediate
9276 // i32 value.
9277 case Intrinsic::x86_sse2_pslli_w:
9278 case Intrinsic::x86_sse2_pslli_d:
9279 case Intrinsic::x86_sse2_pslli_q:
9280 case Intrinsic::x86_sse2_psrli_w:
9281 case Intrinsic::x86_sse2_psrli_d:
9282 case Intrinsic::x86_sse2_psrli_q:
9283 case Intrinsic::x86_sse2_psrai_w:
9284 case Intrinsic::x86_sse2_psrai_d:
9285 case Intrinsic::x86_mmx_pslli_w:
9286 case Intrinsic::x86_mmx_pslli_d:
9287 case Intrinsic::x86_mmx_pslli_q:
9288 case Intrinsic::x86_mmx_psrli_w:
9289 case Intrinsic::x86_mmx_psrli_d:
9290 case Intrinsic::x86_mmx_psrli_q:
9291 case Intrinsic::x86_mmx_psrai_w:
9292 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00009293 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00009294 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00009295 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00009296
9297 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00009298 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00009299 switch (IntNo) {
9300 case Intrinsic::x86_sse2_pslli_w:
9301 NewIntNo = Intrinsic::x86_sse2_psll_w;
9302 break;
9303 case Intrinsic::x86_sse2_pslli_d:
9304 NewIntNo = Intrinsic::x86_sse2_psll_d;
9305 break;
9306 case Intrinsic::x86_sse2_pslli_q:
9307 NewIntNo = Intrinsic::x86_sse2_psll_q;
9308 break;
9309 case Intrinsic::x86_sse2_psrli_w:
9310 NewIntNo = Intrinsic::x86_sse2_psrl_w;
9311 break;
9312 case Intrinsic::x86_sse2_psrli_d:
9313 NewIntNo = Intrinsic::x86_sse2_psrl_d;
9314 break;
9315 case Intrinsic::x86_sse2_psrli_q:
9316 NewIntNo = Intrinsic::x86_sse2_psrl_q;
9317 break;
9318 case Intrinsic::x86_sse2_psrai_w:
9319 NewIntNo = Intrinsic::x86_sse2_psra_w;
9320 break;
9321 case Intrinsic::x86_sse2_psrai_d:
9322 NewIntNo = Intrinsic::x86_sse2_psra_d;
9323 break;
9324 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00009325 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00009326 switch (IntNo) {
9327 case Intrinsic::x86_mmx_pslli_w:
9328 NewIntNo = Intrinsic::x86_mmx_psll_w;
9329 break;
9330 case Intrinsic::x86_mmx_pslli_d:
9331 NewIntNo = Intrinsic::x86_mmx_psll_d;
9332 break;
9333 case Intrinsic::x86_mmx_pslli_q:
9334 NewIntNo = Intrinsic::x86_mmx_psll_q;
9335 break;
9336 case Intrinsic::x86_mmx_psrli_w:
9337 NewIntNo = Intrinsic::x86_mmx_psrl_w;
9338 break;
9339 case Intrinsic::x86_mmx_psrli_d:
9340 NewIntNo = Intrinsic::x86_mmx_psrl_d;
9341 break;
9342 case Intrinsic::x86_mmx_psrli_q:
9343 NewIntNo = Intrinsic::x86_mmx_psrl_q;
9344 break;
9345 case Intrinsic::x86_mmx_psrai_w:
9346 NewIntNo = Intrinsic::x86_mmx_psra_w;
9347 break;
9348 case Intrinsic::x86_mmx_psrai_d:
9349 NewIntNo = Intrinsic::x86_mmx_psra_d;
9350 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00009351 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00009352 }
9353 break;
9354 }
9355 }
Mon P Wangefa42202009-09-03 19:56:25 +00009356
9357 // The vector shift intrinsics with scalars uses 32b shift amounts but
9358 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
9359 // to be zero.
9360 SDValue ShOps[4];
9361 ShOps[0] = ShAmt;
9362 ShOps[1] = DAG.getConstant(0, MVT::i32);
9363 if (ShAmtVT == MVT::v4i32) {
9364 ShOps[2] = DAG.getUNDEF(MVT::i32);
9365 ShOps[3] = DAG.getUNDEF(MVT::i32);
9366 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
9367 } else {
9368 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00009369// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00009370 }
9371
Owen Andersone50ed302009-08-10 22:56:29 +00009372 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009373 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009374 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009375 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00009376 Op.getOperand(1), ShAmt);
9377 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00009378 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00009379}
Evan Cheng72261582005-12-20 06:22:03 +00009380
Dan Gohmand858e902010-04-17 15:26:15 +00009381SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
9382 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00009383 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9384 MFI->setReturnAddressIsTaken(true);
9385
Bill Wendling64e87322009-01-16 19:25:27 +00009386 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009387 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00009388
9389 if (Depth > 0) {
9390 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
9391 SDValue Offset =
9392 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00009393 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009394 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00009395 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009396 FrameAddr, Offset),
Chris Lattner51abfe42010-09-21 06:02:19 +00009397 MachinePointerInfo(), false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00009398 }
9399
9400 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00009401 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00009402 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Chris Lattner51abfe42010-09-21 06:02:19 +00009403 RetAddrFI, MachinePointerInfo(), false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00009404}
9405
Dan Gohmand858e902010-04-17 15:26:15 +00009406SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00009407 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9408 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00009409
Owen Andersone50ed302009-08-10 22:56:29 +00009410 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009411 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00009412 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
9413 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00009414 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00009415 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00009416 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
9417 MachinePointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +00009418 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00009419 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00009420}
9421
Dan Gohman475871a2008-07-27 21:46:04 +00009422SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009423 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00009424 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009425}
9426
Dan Gohmand858e902010-04-17 15:26:15 +00009427SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009428 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00009429 SDValue Chain = Op.getOperand(0);
9430 SDValue Offset = Op.getOperand(1);
9431 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009432 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009433
Dan Gohmand8816272010-08-11 18:14:00 +00009434 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
9435 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
9436 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009437 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009438
Dan Gohmand8816272010-08-11 18:14:00 +00009439 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
9440 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009441 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009442 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
9443 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00009444 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009445 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009446
Dale Johannesene4d209d2009-02-03 20:21:25 +00009447 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00009448 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009449 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009450}
9451
Duncan Sands4a544a72011-09-06 13:37:06 +00009452SDValue X86TargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op,
9453 SelectionDAG &DAG) const {
9454 return Op.getOperand(0);
9455}
9456
9457SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
9458 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00009459 SDValue Root = Op.getOperand(0);
9460 SDValue Trmp = Op.getOperand(1); // trampoline
9461 SDValue FPtr = Op.getOperand(2); // nested function
9462 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009463 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009464
Dan Gohman69de1932008-02-06 22:27:42 +00009465 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009466
9467 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00009468 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00009469
9470 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00009471 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
9472 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00009473
Evan Cheng0e6a0522011-07-18 20:57:22 +00009474 const unsigned char N86R10 = X86_MC::getX86RegNum(X86::R10);
9475 const unsigned char N86R11 = X86_MC::getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00009476
9477 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
9478
9479 // Load the pointer to the nested function into R11.
9480 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00009481 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00009482 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009483 Addr, MachinePointerInfo(TrmpAddr),
9484 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009485
Owen Anderson825b72b2009-08-11 20:47:22 +00009486 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9487 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009488 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
9489 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00009490 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00009491
9492 // Load the 'nest' parameter value into R10.
9493 // R10 is specified in X86CallingConv.td
9494 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00009495 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9496 DAG.getConstant(10, MVT::i64));
9497 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009498 Addr, MachinePointerInfo(TrmpAddr, 10),
9499 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009500
Owen Anderson825b72b2009-08-11 20:47:22 +00009501 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9502 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009503 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
9504 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00009505 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00009506
9507 // Jump to the nested function.
9508 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00009509 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9510 DAG.getConstant(20, MVT::i64));
9511 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009512 Addr, MachinePointerInfo(TrmpAddr, 20),
9513 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009514
9515 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00009516 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9517 DAG.getConstant(22, MVT::i64));
9518 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00009519 MachinePointerInfo(TrmpAddr, 22),
9520 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009521
Duncan Sands4a544a72011-09-06 13:37:06 +00009522 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009523 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00009524 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00009525 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00009526 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00009527 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009528
9529 switch (CC) {
9530 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00009531 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00009532 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00009533 case CallingConv::X86_StdCall: {
9534 // Pass 'nest' parameter in ECX.
9535 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00009536 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009537
9538 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009539 FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00009540 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009541
Chris Lattner58d74912008-03-12 17:45:29 +00009542 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00009543 unsigned InRegCount = 0;
9544 unsigned Idx = 1;
9545
9546 for (FunctionType::param_iterator I = FTy->param_begin(),
9547 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00009548 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00009549 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00009550 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009551
9552 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00009553 report_fatal_error("Nest register in use - reduce number of inreg"
9554 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00009555 }
9556 }
9557 break;
9558 }
9559 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00009560 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00009561 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00009562 // Pass 'nest' parameter in EAX.
9563 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00009564 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009565 break;
9566 }
9567
Dan Gohman475871a2008-07-27 21:46:04 +00009568 SDValue OutChains[4];
9569 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009570
Owen Anderson825b72b2009-08-11 20:47:22 +00009571 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9572 DAG.getConstant(10, MVT::i32));
9573 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009574
Chris Lattnera62fe662010-02-05 19:20:30 +00009575 // This is storing the opcode for MOV32ri.
9576 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Evan Cheng0e6a0522011-07-18 20:57:22 +00009577 const unsigned char N86Reg = X86_MC::getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009578 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00009579 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009580 Trmp, MachinePointerInfo(TrmpAddr),
9581 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009582
Owen Anderson825b72b2009-08-11 20:47:22 +00009583 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9584 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009585 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
9586 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00009587 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009588
Chris Lattnera62fe662010-02-05 19:20:30 +00009589 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00009590 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9591 DAG.getConstant(5, MVT::i32));
9592 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00009593 MachinePointerInfo(TrmpAddr, 5),
9594 false, false, 1);
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(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009598 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
9599 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00009600 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009601
Duncan Sands4a544a72011-09-06 13:37:06 +00009602 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009603 }
9604}
9605
Dan Gohmand858e902010-04-17 15:26:15 +00009606SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
9607 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009608 /*
9609 The rounding mode is in bits 11:10 of FPSR, and has the following
9610 settings:
9611 00 Round to nearest
9612 01 Round to -inf
9613 10 Round to +inf
9614 11 Round to 0
9615
9616 FLT_ROUNDS, on the other hand, expects the following:
9617 -1 Undefined
9618 0 Round to 0
9619 1 Round to nearest
9620 2 Round to +inf
9621 3 Round to -inf
9622
9623 To perform the conversion, we do:
9624 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
9625 */
9626
9627 MachineFunction &MF = DAG.getMachineFunction();
9628 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00009629 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009630 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00009631 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00009632 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009633
9634 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00009635 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00009636 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009637
Michael J. Spencerec38de22010-10-10 22:04:20 +00009638
Chris Lattner2156b792010-09-22 01:11:26 +00009639 MachineMemOperand *MMO =
9640 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
9641 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009642
Chris Lattner2156b792010-09-22 01:11:26 +00009643 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
9644 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
9645 DAG.getVTList(MVT::Other),
9646 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009647
9648 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00009649 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Chris Lattner51abfe42010-09-21 06:02:19 +00009650 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009651
9652 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00009653 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00009654 DAG.getNode(ISD::SRL, DL, MVT::i16,
9655 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00009656 CWD, DAG.getConstant(0x800, MVT::i16)),
9657 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00009658 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00009659 DAG.getNode(ISD::SRL, DL, MVT::i16,
9660 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00009661 CWD, DAG.getConstant(0x400, MVT::i16)),
9662 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009663
Dan Gohman475871a2008-07-27 21:46:04 +00009664 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00009665 DAG.getNode(ISD::AND, DL, MVT::i16,
9666 DAG.getNode(ISD::ADD, DL, MVT::i16,
9667 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00009668 DAG.getConstant(1, MVT::i16)),
9669 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009670
9671
Duncan Sands83ec4b62008-06-06 12:08:01 +00009672 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00009673 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009674}
9675
Dan Gohmand858e902010-04-17 15:26:15 +00009676SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009677 EVT VT = Op.getValueType();
9678 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009679 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009680 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00009681
9682 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009683 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00009684 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00009685 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00009686 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009687 }
Evan Cheng18efe262007-12-14 02:13:44 +00009688
Evan Cheng152804e2007-12-14 08:30:15 +00009689 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00009690 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009691 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00009692
9693 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00009694 SDValue Ops[] = {
9695 Op,
9696 DAG.getConstant(NumBits+NumBits-1, OpVT),
9697 DAG.getConstant(X86::COND_E, MVT::i8),
9698 Op.getValue(1)
9699 };
9700 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00009701
9702 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00009703 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00009704
Owen Anderson825b72b2009-08-11 20:47:22 +00009705 if (VT == MVT::i8)
9706 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009707 return Op;
9708}
9709
Dan Gohmand858e902010-04-17 15:26:15 +00009710SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009711 EVT VT = Op.getValueType();
9712 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009713 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009714 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00009715
9716 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009717 if (VT == MVT::i8) {
9718 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00009719 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009720 }
Evan Cheng152804e2007-12-14 08:30:15 +00009721
9722 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00009723 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009724 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00009725
9726 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00009727 SDValue Ops[] = {
9728 Op,
9729 DAG.getConstant(NumBits, OpVT),
9730 DAG.getConstant(X86::COND_E, MVT::i8),
9731 Op.getValue(1)
9732 };
9733 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00009734
Owen Anderson825b72b2009-08-11 20:47:22 +00009735 if (VT == MVT::i8)
9736 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009737 return Op;
9738}
9739
Craig Topper13894fa2011-08-24 06:14:18 +00009740// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
9741// ones, and then concatenate the result back.
9742static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00009743 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +00009744
9745 assert(VT.getSizeInBits() == 256 && VT.isInteger() &&
9746 "Unsupported value type for operation");
9747
9748 int NumElems = VT.getVectorNumElements();
9749 DebugLoc dl = Op.getDebugLoc();
9750 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
9751 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
9752
9753 // Extract the LHS vectors
9754 SDValue LHS = Op.getOperand(0);
9755 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
9756 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
9757
9758 // Extract the RHS vectors
9759 SDValue RHS = Op.getOperand(1);
9760 SDValue RHS1 = Extract128BitVector(RHS, Idx0, DAG, dl);
9761 SDValue RHS2 = Extract128BitVector(RHS, Idx1, DAG, dl);
9762
9763 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9764 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
9765
9766 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
9767 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
9768 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
9769}
9770
9771SDValue X86TargetLowering::LowerADD(SDValue Op, SelectionDAG &DAG) const {
9772 assert(Op.getValueType().getSizeInBits() == 256 &&
9773 Op.getValueType().isInteger() &&
9774 "Only handle AVX 256-bit vector integer operation");
9775 return Lower256IntArith(Op, DAG);
9776}
9777
9778SDValue X86TargetLowering::LowerSUB(SDValue Op, SelectionDAG &DAG) const {
9779 assert(Op.getValueType().getSizeInBits() == 256 &&
9780 Op.getValueType().isInteger() &&
9781 "Only handle AVX 256-bit vector integer operation");
9782 return Lower256IntArith(Op, DAG);
9783}
9784
9785SDValue X86TargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
9786 EVT VT = Op.getValueType();
9787
9788 // Decompose 256-bit ops into smaller 128-bit ops.
9789 if (VT.getSizeInBits() == 256)
9790 return Lower256IntArith(Op, DAG);
9791
Owen Anderson825b72b2009-08-11 20:47:22 +00009792 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009793 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00009794
Mon P Wangaf9b9522008-12-18 21:42:19 +00009795 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
9796 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
9797 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
9798 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
9799 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
9800 //
9801 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
9802 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
9803 // return AloBlo + AloBhi + AhiBlo;
9804
9805 SDValue A = Op.getOperand(0);
9806 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009807
Dale Johannesene4d209d2009-02-03 20:21:25 +00009808 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009809 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
9810 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009811 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009812 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
9813 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009814 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009815 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00009816 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009817 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009818 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00009819 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009820 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009821 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00009822 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009823 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009824 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
9825 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009826 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009827 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
9828 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009829 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
9830 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00009831 return Res;
9832}
9833
Nadav Rotem43012222011-05-11 08:12:09 +00009834SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
9835
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009836 EVT VT = Op.getValueType();
9837 DebugLoc dl = Op.getDebugLoc();
9838 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +00009839 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009840 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009841
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00009842 if (!(Subtarget->hasSSE2() || Subtarget->hasAVX()))
9843 return SDValue();
9844
9845 // Decompose 256-bit shifts into smaller 128-bit shifts.
9846 if (VT.getSizeInBits() == 256) {
9847 int NumElems = VT.getVectorNumElements();
9848 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9849 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
9850
9851 // Extract the two vectors
9852 SDValue V1 = Extract128BitVector(R, DAG.getConstant(0, MVT::i32), DAG, dl);
9853 SDValue V2 = Extract128BitVector(R, DAG.getConstant(NumElems/2, MVT::i32),
9854 DAG, dl);
9855
9856 // Recreate the shift amount vectors
Bruno Cardoso Lopes0dd80b02011-08-17 22:12:20 +00009857 SDValue Amt1, Amt2;
9858 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
9859 // Constant shift amount
9860 SmallVector<SDValue, 4> Amt1Csts;
9861 SmallVector<SDValue, 4> Amt2Csts;
9862 for (int i = 0; i < NumElems/2; ++i)
9863 Amt1Csts.push_back(Amt->getOperand(i));
9864 for (int i = NumElems/2; i < NumElems; ++i)
9865 Amt2Csts.push_back(Amt->getOperand(i));
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00009866
Bruno Cardoso Lopes0dd80b02011-08-17 22:12:20 +00009867 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
9868 &Amt1Csts[0], NumElems/2);
9869 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
9870 &Amt2Csts[0], NumElems/2);
9871 } else {
9872 // Variable shift amount
9873 Amt1 = Extract128BitVector(Amt, DAG.getConstant(0, MVT::i32), DAG, dl);
9874 Amt2 = Extract128BitVector(Amt, DAG.getConstant(NumElems/2, MVT::i32),
9875 DAG, dl);
9876 }
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00009877
9878 // Issue new vector shifts for the smaller types
9879 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
9880 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
9881
9882 // Concatenate the result back
9883 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
9884 }
Nate Begeman51409212010-07-28 00:21:48 +00009885
Nadav Rotem43012222011-05-11 08:12:09 +00009886 // Optimize shl/srl/sra with constant shift amount.
9887 if (isSplatVector(Amt.getNode())) {
9888 SDValue SclrAmt = Amt->getOperand(0);
9889 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
9890 uint64_t ShiftAmt = C->getZExtValue();
9891
9892 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SHL)
9893 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9894 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
9895 R, DAG.getConstant(ShiftAmt, MVT::i32));
9896
9897 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SHL)
9898 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9899 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
9900 R, DAG.getConstant(ShiftAmt, MVT::i32));
9901
9902 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SHL)
9903 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9904 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
9905 R, DAG.getConstant(ShiftAmt, MVT::i32));
9906
9907 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SRL)
9908 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9909 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
9910 R, DAG.getConstant(ShiftAmt, MVT::i32));
9911
9912 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRL)
9913 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9914 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
9915 R, DAG.getConstant(ShiftAmt, MVT::i32));
9916
9917 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRL)
9918 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9919 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
9920 R, DAG.getConstant(ShiftAmt, MVT::i32));
9921
9922 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRA)
9923 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9924 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
9925 R, DAG.getConstant(ShiftAmt, MVT::i32));
9926
9927 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRA)
9928 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9929 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
9930 R, DAG.getConstant(ShiftAmt, MVT::i32));
9931 }
9932 }
9933
9934 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +00009935 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +00009936 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9937 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
9938 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
9939
9940 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009941
Nate Begeman51409212010-07-28 00:21:48 +00009942 std::vector<Constant*> CV(4, CI);
9943 Constant *C = ConstantVector::get(CV);
9944 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
9945 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00009946 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00009947 false, false, 16);
9948
9949 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009950 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +00009951 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
9952 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
9953 }
Nadav Rotem43012222011-05-11 08:12:09 +00009954 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +00009955 // a = a << 5;
9956 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9957 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
9958 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
9959
9960 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
9961 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
9962
9963 std::vector<Constant*> CVM1(16, CM1);
9964 std::vector<Constant*> CVM2(16, CM2);
9965 Constant *C = ConstantVector::get(CVM1);
9966 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
9967 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00009968 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00009969 false, false, 16);
9970
9971 // r = pblendv(r, psllw(r & (char16)15, 4), a);
9972 M = DAG.getNode(ISD::AND, dl, VT, R, M);
9973 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9974 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
9975 DAG.getConstant(4, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00009976 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00009977 // a += a
9978 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009979
Nate Begeman51409212010-07-28 00:21:48 +00009980 C = ConstantVector::get(CVM2);
9981 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
9982 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00009983 MachinePointerInfo::getConstantPool(),
Chris Lattner51abfe42010-09-21 06:02:19 +00009984 false, false, 16);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009985
Nate Begeman51409212010-07-28 00:21:48 +00009986 // r = pblendv(r, psllw(r & (char16)63, 2), a);
9987 M = DAG.getNode(ISD::AND, dl, VT, R, M);
9988 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9989 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
9990 DAG.getConstant(2, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00009991 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00009992 // a += a
9993 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009994
Nate Begeman51409212010-07-28 00:21:48 +00009995 // return pblendv(r, r+r, a);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009996 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT,
Nate Begeman51409212010-07-28 00:21:48 +00009997 R, DAG.getNode(ISD::ADD, dl, VT, R, R), Op);
9998 return R;
9999 }
10000 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010001}
Mon P Wangaf9b9522008-12-18 21:42:19 +000010002
Dan Gohmand858e902010-04-17 15:26:15 +000010003SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +000010004 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
10005 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +000010006 // looks for this combo and may remove the "setcc" instruction if the "setcc"
10007 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +000010008 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +000010009 SDValue LHS = N->getOperand(0);
10010 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +000010011 unsigned BaseOp = 0;
10012 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010013 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +000010014 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010015 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +000010016 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +000010017 // A subtract of one will be selected as a INC. Note that INC doesn't
10018 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010019 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10020 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010021 BaseOp = X86ISD::INC;
10022 Cond = X86::COND_O;
10023 break;
10024 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010025 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +000010026 Cond = X86::COND_O;
10027 break;
10028 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010029 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +000010030 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010031 break;
10032 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +000010033 // A subtract of one will be selected as a DEC. Note that DEC doesn't
10034 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010035 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10036 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010037 BaseOp = X86ISD::DEC;
10038 Cond = X86::COND_O;
10039 break;
10040 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010041 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +000010042 Cond = X86::COND_O;
10043 break;
10044 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010045 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +000010046 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010047 break;
10048 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000010049 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000010050 Cond = X86::COND_O;
10051 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010052 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
10053 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
10054 MVT::i32);
10055 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010056
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010057 SDValue SetCC =
10058 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10059 DAG.getConstant(X86::COND_O, MVT::i32),
10060 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010061
Dan Gohman6e5fda22011-07-22 18:45:15 +000010062 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010063 }
Bill Wendling74c37652008-12-09 22:08:41 +000010064 }
Bill Wendling3fafd932008-11-26 22:37:40 +000010065
Bill Wendling61edeb52008-12-02 01:06:39 +000010066 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010067 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010068 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000010069
Bill Wendling61edeb52008-12-02 01:06:39 +000010070 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010071 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
10072 DAG.getConstant(Cond, MVT::i32),
10073 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000010074
Dan Gohman6e5fda22011-07-22 18:45:15 +000010075 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000010076}
10077
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010078SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op, SelectionDAG &DAG) const{
10079 DebugLoc dl = Op.getDebugLoc();
10080 SDNode* Node = Op.getNode();
10081 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
10082 EVT VT = Node->getValueType(0);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010083 if (Subtarget->hasSSE2() && VT.isVector()) {
10084 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
10085 ExtraVT.getScalarType().getSizeInBits();
10086 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
10087
10088 unsigned SHLIntrinsicsID = 0;
10089 unsigned SRAIntrinsicsID = 0;
10090 switch (VT.getSimpleVT().SimpleTy) {
10091 default:
10092 return SDValue();
10093 case MVT::v2i64: {
10094 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_q;
10095 SRAIntrinsicsID = 0;
10096 break;
10097 }
10098 case MVT::v4i32: {
10099 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_d;
10100 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_d;
10101 break;
10102 }
10103 case MVT::v8i16: {
10104 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_w;
10105 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_w;
10106 break;
10107 }
10108 }
10109
10110 SDValue Tmp1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10111 DAG.getConstant(SHLIntrinsicsID, MVT::i32),
10112 Node->getOperand(0), ShAmt);
10113
10114 // In case of 1 bit sext, no need to shr
10115 if (ExtraVT.getScalarType().getSizeInBits() == 1) return Tmp1;
10116
10117 if (SRAIntrinsicsID) {
10118 Tmp1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10119 DAG.getConstant(SRAIntrinsicsID, MVT::i32),
10120 Tmp1, ShAmt);
10121 }
10122 return Tmp1;
10123 }
10124
10125 return SDValue();
10126}
10127
10128
Eric Christopher9a9d2752010-07-22 02:48:34 +000010129SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
10130 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010131
Eric Christopher77ed1352011-07-08 00:04:56 +000010132 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
10133 // There isn't any reason to disable it if the target processor supports it.
10134 if (!Subtarget->hasSSE2() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +000010135 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +000010136 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +000010137 SDValue Ops[] = {
10138 DAG.getRegister(X86::ESP, MVT::i32), // Base
10139 DAG.getTargetConstant(1, MVT::i8), // Scale
10140 DAG.getRegister(0, MVT::i32), // Index
10141 DAG.getTargetConstant(0, MVT::i32), // Disp
10142 DAG.getRegister(0, MVT::i32), // Segment.
10143 Zero,
10144 Chain
10145 };
Michael J. Spencerec38de22010-10-10 22:04:20 +000010146 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +000010147 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
10148 array_lengthof(Ops));
10149 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +000010150 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010151
Eric Christopher9a9d2752010-07-22 02:48:34 +000010152 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +000010153 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +000010154 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010155
Chris Lattner132929a2010-08-14 17:26:09 +000010156 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10157 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
10158 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
10159 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010160
Chris Lattner132929a2010-08-14 17:26:09 +000010161 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
10162 if (!Op1 && !Op2 && !Op3 && Op4)
10163 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010164
Chris Lattner132929a2010-08-14 17:26:09 +000010165 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
10166 if (Op1 && !Op2 && !Op3 && !Op4)
10167 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010168
10169 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +000010170 // (MFENCE)>;
10171 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +000010172}
10173
Eli Friedman14648462011-07-27 22:21:52 +000010174SDValue X86TargetLowering::LowerATOMIC_FENCE(SDValue Op,
10175 SelectionDAG &DAG) const {
10176 DebugLoc dl = Op.getDebugLoc();
10177 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
10178 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
10179 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
10180 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
10181
10182 // The only fence that needs an instruction is a sequentially-consistent
10183 // cross-thread fence.
10184 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
10185 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
10186 // no-sse2). There isn't any reason to disable it if the target processor
10187 // supports it.
10188 if (Subtarget->hasSSE2() || Subtarget->is64Bit())
10189 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
10190
10191 SDValue Chain = Op.getOperand(0);
10192 SDValue Zero = DAG.getConstant(0, MVT::i32);
10193 SDValue Ops[] = {
10194 DAG.getRegister(X86::ESP, MVT::i32), // Base
10195 DAG.getTargetConstant(1, MVT::i8), // Scale
10196 DAG.getRegister(0, MVT::i32), // Index
10197 DAG.getTargetConstant(0, MVT::i32), // Disp
10198 DAG.getRegister(0, MVT::i32), // Segment.
10199 Zero,
10200 Chain
10201 };
10202 SDNode *Res =
10203 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
10204 array_lengthof(Ops));
10205 return SDValue(Res, 0);
10206 }
10207
10208 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
10209 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
10210}
10211
10212
Dan Gohmand858e902010-04-17 15:26:15 +000010213SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010214 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010215 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000010216 unsigned Reg = 0;
10217 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000010218 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000010219 default:
10220 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000010221 case MVT::i8: Reg = X86::AL; size = 1; break;
10222 case MVT::i16: Reg = X86::AX; size = 2; break;
10223 case MVT::i32: Reg = X86::EAX; size = 4; break;
10224 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000010225 assert(Subtarget->is64Bit() && "Node not type legal!");
10226 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000010227 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000010228 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010229 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000010230 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000010231 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000010232 Op.getOperand(1),
10233 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000010234 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000010235 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010236 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010237 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
10238 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
10239 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000010240 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010241 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000010242 return cpOut;
10243}
10244
Duncan Sands1607f052008-12-01 11:39:25 +000010245SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010246 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +000010247 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010248 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000010249 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010250 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +000010251 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010252 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
10253 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000010254 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000010255 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
10256 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000010257 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000010258 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000010259 rdx.getValue(1)
10260 };
Dale Johannesene4d209d2009-02-03 20:21:25 +000010261 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010262}
10263
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010264SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +000010265 SelectionDAG &DAG) const {
10266 EVT SrcVT = Op.getOperand(0).getValueType();
10267 EVT DstVT = Op.getValueType();
Chris Lattner2a786eb2010-12-19 20:19:20 +000010268 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
10269 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000010270 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000010271 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010272 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000010273 // i64 <=> MMX conversions are Legal.
10274 if (SrcVT==MVT::i64 && DstVT.isVector())
10275 return Op;
10276 if (DstVT==MVT::i64 && SrcVT.isVector())
10277 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000010278 // MMX <=> MMX conversions are Legal.
10279 if (SrcVT.isVector() && DstVT.isVector())
10280 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000010281 // All other conversions need to be expanded.
10282 return SDValue();
10283}
Chris Lattner5b856542010-12-20 00:59:46 +000010284
Dan Gohmand858e902010-04-17 15:26:15 +000010285SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000010286 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +000010287 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000010288 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010289 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000010290 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010291 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010292 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000010293 Node->getOperand(0),
10294 Node->getOperand(1), negOp,
10295 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000010296 cast<AtomicSDNode>(Node)->getAlignment(),
10297 cast<AtomicSDNode>(Node)->getOrdering(),
10298 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000010299}
10300
Eli Friedman327236c2011-08-24 20:50:09 +000010301static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
10302 SDNode *Node = Op.getNode();
10303 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010304 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000010305
10306 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010307 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
10308 // FIXME: On 32-bit, store -> fist or movq would be more efficient
10309 // (The only way to get a 16-byte store is cmpxchg16b)
10310 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
10311 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
10312 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000010313 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
10314 cast<AtomicSDNode>(Node)->getMemoryVT(),
10315 Node->getOperand(0),
10316 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010317 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000010318 cast<AtomicSDNode>(Node)->getOrdering(),
10319 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000010320 return Swap.getValue(1);
10321 }
10322 // Other atomic stores have a simple pattern.
10323 return Op;
10324}
10325
Chris Lattner5b856542010-12-20 00:59:46 +000010326static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
10327 EVT VT = Op.getNode()->getValueType(0);
10328
10329 // Let legalize expand this if it isn't a legal type yet.
10330 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
10331 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010332
Chris Lattner5b856542010-12-20 00:59:46 +000010333 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010334
Chris Lattner5b856542010-12-20 00:59:46 +000010335 unsigned Opc;
10336 bool ExtraOp = false;
10337 switch (Op.getOpcode()) {
10338 default: assert(0 && "Invalid code");
10339 case ISD::ADDC: Opc = X86ISD::ADD; break;
10340 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
10341 case ISD::SUBC: Opc = X86ISD::SUB; break;
10342 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
10343 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010344
Chris Lattner5b856542010-12-20 00:59:46 +000010345 if (!ExtraOp)
10346 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
10347 Op.getOperand(1));
10348 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
10349 Op.getOperand(1), Op.getOperand(2));
10350}
10351
Evan Cheng0db9fe62006-04-25 20:13:52 +000010352/// LowerOperation - Provide custom lowering hooks for some operations.
10353///
Dan Gohmand858e902010-04-17 15:26:15 +000010354SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000010355 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010356 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010357 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Eric Christopher9a9d2752010-07-22 02:48:34 +000010358 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Eli Friedman14648462011-07-27 22:21:52 +000010359 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010360 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
10361 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000010362 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010363 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000010364 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010365 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
10366 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
10367 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +000010368 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +000010369 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010370 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
10371 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
10372 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000010373 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000010374 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000010375 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010376 case ISD::SHL_PARTS:
10377 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000010378 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010379 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000010380 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010381 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000010382 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010383 case ISD::FABS: return LowerFABS(Op, DAG);
10384 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000010385 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000010386 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000010387 case ISD::SETCC: return LowerSETCC(Op, DAG);
10388 case ISD::SELECT: return LowerSELECT(Op, DAG);
Nadav Rotemffe3e7d2011-09-08 08:11:19 +000010389 case ISD::VSELECT: return LowerVSELECT(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000010390 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010391 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010392 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000010393 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +000010394 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010395 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000010396 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
10397 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010398 case ISD::FRAME_TO_ARGS_OFFSET:
10399 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010400 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010401 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +000010402 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
10403 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000010404 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000010405 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
10406 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000010407 case ISD::MUL: return LowerMUL(Op, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000010408 case ISD::SRA:
10409 case ISD::SRL:
10410 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000010411 case ISD::SADDO:
10412 case ISD::UADDO:
10413 case ISD::SSUBO:
10414 case ISD::USUBO:
10415 case ISD::SMULO:
10416 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +000010417 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010418 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000010419 case ISD::ADDC:
10420 case ISD::ADDE:
10421 case ISD::SUBC:
10422 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000010423 case ISD::ADD: return LowerADD(Op, DAG);
10424 case ISD::SUB: return LowerSUB(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010425 }
Chris Lattner27a6c732007-11-24 07:07:01 +000010426}
10427
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010428static void ReplaceATOMIC_LOAD(SDNode *Node,
10429 SmallVectorImpl<SDValue> &Results,
10430 SelectionDAG &DAG) {
10431 DebugLoc dl = Node->getDebugLoc();
10432 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
10433
10434 // Convert wide load -> cmpxchg8b/cmpxchg16b
10435 // FIXME: On 32-bit, load -> fild or movq would be more efficient
10436 // (The only way to get a 16-byte load is cmpxchg16b)
10437 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000010438 SDValue Zero = DAG.getConstant(0, VT);
10439 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010440 Node->getOperand(0),
10441 Node->getOperand(1), Zero, Zero,
10442 cast<AtomicSDNode>(Node)->getMemOperand(),
10443 cast<AtomicSDNode>(Node)->getOrdering(),
10444 cast<AtomicSDNode>(Node)->getSynchScope());
10445 Results.push_back(Swap.getValue(0));
10446 Results.push_back(Swap.getValue(1));
10447}
10448
Duncan Sands1607f052008-12-01 11:39:25 +000010449void X86TargetLowering::
10450ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000010451 SelectionDAG &DAG, unsigned NewOp) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010452 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010453 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +000010454 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000010455
10456 SDValue Chain = Node->getOperand(0);
10457 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010458 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010459 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000010460 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010461 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000010462 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000010463 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000010464 SDValue Result =
10465 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
10466 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000010467 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000010468 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010469 Results.push_back(Result.getValue(2));
10470}
10471
Duncan Sands126d9072008-07-04 11:47:58 +000010472/// ReplaceNodeResults - Replace a node with an illegal result type
10473/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000010474void X86TargetLowering::ReplaceNodeResults(SDNode *N,
10475 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000010476 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010477 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +000010478 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000010479 default:
Duncan Sands1607f052008-12-01 11:39:25 +000010480 assert(false && "Do not know how to custom type legalize this operation!");
10481 return;
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010482 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000010483 case ISD::ADDC:
10484 case ISD::ADDE:
10485 case ISD::SUBC:
10486 case ISD::SUBE:
10487 // We don't want to expand or promote these.
10488 return;
Duncan Sands1607f052008-12-01 11:39:25 +000010489 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +000010490 std::pair<SDValue,SDValue> Vals =
10491 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +000010492 SDValue FIST = Vals.first, StackSlot = Vals.second;
10493 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000010494 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000010495 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +000010496 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
10497 MachinePointerInfo(), false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +000010498 }
10499 return;
10500 }
10501 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010502 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000010503 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010504 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010505 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000010506 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000010507 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010508 eax.getValue(2));
10509 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
10510 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +000010511 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010512 Results.push_back(edx.getValue(1));
10513 return;
10514 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010515 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000010516 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000010517 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000010518 bool Regs64bit = T == MVT::i128;
10519 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000010520 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000010521 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
10522 DAG.getConstant(0, HalfT));
10523 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
10524 DAG.getConstant(1, HalfT));
10525 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
10526 Regs64bit ? X86::RAX : X86::EAX,
10527 cpInL, SDValue());
10528 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
10529 Regs64bit ? X86::RDX : X86::EDX,
10530 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000010531 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000010532 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
10533 DAG.getConstant(0, HalfT));
10534 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
10535 DAG.getConstant(1, HalfT));
10536 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
10537 Regs64bit ? X86::RBX : X86::EBX,
10538 swapInL, cpInH.getValue(1));
10539 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
10540 Regs64bit ? X86::RCX : X86::ECX,
10541 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000010542 SDValue Ops[] = { swapInH.getValue(0),
10543 N->getOperand(1),
10544 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010545 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000010546 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000010547 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
10548 X86ISD::LCMPXCHG8_DAG;
10549 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000010550 Ops, 3, T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000010551 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
10552 Regs64bit ? X86::RAX : X86::EAX,
10553 HalfT, Result.getValue(1));
10554 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
10555 Regs64bit ? X86::RDX : X86::EDX,
10556 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000010557 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000010558 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010559 Results.push_back(cpOutH.getValue(1));
10560 return;
10561 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010562 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +000010563 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
10564 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010565 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +000010566 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
10567 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010568 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +000010569 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
10570 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010571 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +000010572 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
10573 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010574 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +000010575 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
10576 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010577 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +000010578 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
10579 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010580 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +000010581 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
10582 return;
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010583 case ISD::ATOMIC_LOAD:
10584 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000010585 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000010586}
10587
Evan Cheng72261582005-12-20 06:22:03 +000010588const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
10589 switch (Opcode) {
10590 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000010591 case X86ISD::BSF: return "X86ISD::BSF";
10592 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000010593 case X86ISD::SHLD: return "X86ISD::SHLD";
10594 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000010595 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000010596 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000010597 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000010598 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000010599 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000010600 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000010601 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
10602 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
10603 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000010604 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000010605 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000010606 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000010607 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000010608 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000010609 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000010610 case X86ISD::COMI: return "X86ISD::COMI";
10611 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000010612 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000010613 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000010614 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
10615 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000010616 case X86ISD::CMOV: return "X86ISD::CMOV";
10617 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000010618 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000010619 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
10620 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000010621 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000010622 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000010623 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000010624 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000010625 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000010626 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
10627 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000010628 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000010629 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000010630 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Nate Begemanb65c1752010-12-17 22:55:37 +000010631 case X86ISD::PSIGNB: return "X86ISD::PSIGNB";
10632 case X86ISD::PSIGNW: return "X86ISD::PSIGNW";
10633 case X86ISD::PSIGND: return "X86ISD::PSIGND";
Nate Begeman672fb622010-12-20 22:04:24 +000010634 case X86ISD::PBLENDVB: return "X86ISD::PBLENDVB";
Evan Cheng8ca29322006-11-10 21:43:37 +000010635 case X86ISD::FMAX: return "X86ISD::FMAX";
10636 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +000010637 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
10638 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000010639 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000010640 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010641 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000010642 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010643 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +000010644 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
10645 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010646 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
10647 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
10648 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
10649 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
10650 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
10651 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000010652 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
10653 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +000010654 case X86ISD::VSHL: return "X86ISD::VSHL";
10655 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +000010656 case X86ISD::CMPPD: return "X86ISD::CMPPD";
10657 case X86ISD::CMPPS: return "X86ISD::CMPPS";
10658 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
10659 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
10660 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
10661 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
10662 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
10663 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
10664 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
10665 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010666 case X86ISD::ADD: return "X86ISD::ADD";
10667 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000010668 case X86ISD::ADC: return "X86ISD::ADC";
10669 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000010670 case X86ISD::SMUL: return "X86ISD::SMUL";
10671 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000010672 case X86ISD::INC: return "X86ISD::INC";
10673 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000010674 case X86ISD::OR: return "X86ISD::OR";
10675 case X86ISD::XOR: return "X86ISD::XOR";
10676 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +000010677 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000010678 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010679 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010680 case X86ISD::PALIGN: return "X86ISD::PALIGN";
10681 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
10682 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
10683 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
10684 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
10685 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
10686 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
10687 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
10688 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010689 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000010690 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010691 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000010692 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
10693 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010694 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
10695 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
10696 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
10697 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
10698 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
10699 case X86ISD::MOVSD: return "X86ISD::MOVSD";
10700 case X86ISD::MOVSS: return "X86ISD::MOVSS";
10701 case X86ISD::UNPCKLPS: return "X86ISD::UNPCKLPS";
10702 case X86ISD::UNPCKLPD: return "X86ISD::UNPCKLPD";
David Greenefbf05d32011-02-22 23:31:46 +000010703 case X86ISD::VUNPCKLPDY: return "X86ISD::VUNPCKLPDY";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010704 case X86ISD::UNPCKHPS: return "X86ISD::UNPCKHPS";
10705 case X86ISD::UNPCKHPD: return "X86ISD::UNPCKHPD";
10706 case X86ISD::PUNPCKLBW: return "X86ISD::PUNPCKLBW";
10707 case X86ISD::PUNPCKLWD: return "X86ISD::PUNPCKLWD";
10708 case X86ISD::PUNPCKLDQ: return "X86ISD::PUNPCKLDQ";
10709 case X86ISD::PUNPCKLQDQ: return "X86ISD::PUNPCKLQDQ";
10710 case X86ISD::PUNPCKHBW: return "X86ISD::PUNPCKHBW";
10711 case X86ISD::PUNPCKHWD: return "X86ISD::PUNPCKHWD";
10712 case X86ISD::PUNPCKHDQ: return "X86ISD::PUNPCKHDQ";
10713 case X86ISD::PUNPCKHQDQ: return "X86ISD::PUNPCKHQDQ";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000010714 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +000010715 case X86ISD::VPERMILPS: return "X86ISD::VPERMILPS";
10716 case X86ISD::VPERMILPSY: return "X86ISD::VPERMILPSY";
10717 case X86ISD::VPERMILPD: return "X86ISD::VPERMILPD";
10718 case X86ISD::VPERMILPDY: return "X86ISD::VPERMILPDY";
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +000010719 case X86ISD::VPERM2F128: return "X86ISD::VPERM2F128";
Dan Gohmand6708ea2009-08-15 01:38:56 +000010720 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000010721 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010722 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000010723 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000010724 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +000010725 }
10726}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010727
Chris Lattnerc9addb72007-03-30 23:15:24 +000010728// isLegalAddressingMode - Return true if the addressing mode represented
10729// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000010730bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010731 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000010732 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010733 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000010734 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000010735
Chris Lattnerc9addb72007-03-30 23:15:24 +000010736 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010737 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000010738 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000010739
Chris Lattnerc9addb72007-03-30 23:15:24 +000010740 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000010741 unsigned GVFlags =
10742 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010743
Chris Lattnerdfed4132009-07-10 07:38:24 +000010744 // If a reference to this global requires an extra load, we can't fold it.
10745 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000010746 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010747
Chris Lattnerdfed4132009-07-10 07:38:24 +000010748 // If BaseGV requires a register for the PIC base, we cannot also have a
10749 // BaseReg specified.
10750 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000010751 return false;
Evan Cheng52787842007-08-01 23:46:47 +000010752
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010753 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000010754 if ((M != CodeModel::Small || R != Reloc::Static) &&
10755 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010756 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000010757 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010758
Chris Lattnerc9addb72007-03-30 23:15:24 +000010759 switch (AM.Scale) {
10760 case 0:
10761 case 1:
10762 case 2:
10763 case 4:
10764 case 8:
10765 // These scales always work.
10766 break;
10767 case 3:
10768 case 5:
10769 case 9:
10770 // These scales are formed with basereg+scalereg. Only accept if there is
10771 // no basereg yet.
10772 if (AM.HasBaseReg)
10773 return false;
10774 break;
10775 default: // Other stuff never works.
10776 return false;
10777 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010778
Chris Lattnerc9addb72007-03-30 23:15:24 +000010779 return true;
10780}
10781
10782
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010783bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000010784 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000010785 return false;
Evan Chenge127a732007-10-29 07:57:50 +000010786 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
10787 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000010788 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +000010789 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000010790 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +000010791}
10792
Owen Andersone50ed302009-08-10 22:56:29 +000010793bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000010794 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000010795 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000010796 unsigned NumBits1 = VT1.getSizeInBits();
10797 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000010798 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +000010799 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000010800 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000010801}
Evan Cheng2bd122c2007-10-26 01:56:11 +000010802
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010803bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000010804 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000010805 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000010806}
10807
Owen Andersone50ed302009-08-10 22:56:29 +000010808bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000010809 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000010810 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000010811}
10812
Owen Andersone50ed302009-08-10 22:56:29 +000010813bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000010814 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000010815 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000010816}
10817
Evan Cheng60c07e12006-07-05 22:17:51 +000010818/// isShuffleMaskLegal - Targets can use this to indicate that they only
10819/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
10820/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
10821/// are assumed to be legal.
10822bool
Eric Christopherfd179292009-08-27 18:07:15 +000010823X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000010824 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000010825 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000010826 if (VT.getSizeInBits() == 64)
Eric Christophercff6f852010-04-15 01:40:20 +000010827 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3());
Nate Begeman9008ca62009-04-27 18:41:29 +000010828
Nate Begemana09008b2009-10-19 02:17:23 +000010829 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000010830 return (VT.getVectorNumElements() == 2 ||
10831 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
10832 isMOVLMask(M, VT) ||
10833 isSHUFPMask(M, VT) ||
10834 isPSHUFDMask(M, VT) ||
10835 isPSHUFHWMask(M, VT) ||
10836 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +000010837 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000010838 isUNPCKLMask(M, VT) ||
10839 isUNPCKHMask(M, VT) ||
10840 isUNPCKL_v_undef_Mask(M, VT) ||
10841 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +000010842}
10843
Dan Gohman7d8143f2008-04-09 20:09:42 +000010844bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000010845X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000010846 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000010847 unsigned NumElts = VT.getVectorNumElements();
10848 // FIXME: This collection of masks seems suspect.
10849 if (NumElts == 2)
10850 return true;
10851 if (NumElts == 4 && VT.getSizeInBits() == 128) {
10852 return (isMOVLMask(Mask, VT) ||
10853 isCommutedMOVLMask(Mask, VT, true) ||
10854 isSHUFPMask(Mask, VT) ||
10855 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +000010856 }
10857 return false;
10858}
10859
10860//===----------------------------------------------------------------------===//
10861// X86 Scheduler Hooks
10862//===----------------------------------------------------------------------===//
10863
Mon P Wang63307c32008-05-05 19:05:59 +000010864// private utility function
10865MachineBasicBlock *
10866X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
10867 MachineBasicBlock *MBB,
10868 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010869 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010870 unsigned LoadOpc,
10871 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010872 unsigned notOpc,
10873 unsigned EAXreg,
10874 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000010875 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000010876 // For the atomic bitwise operator, we generate
10877 // thisMBB:
10878 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000010879 // ld t1 = [bitinstr.addr]
10880 // op t2 = t1, [bitinstr.val]
10881 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000010882 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
10883 // bz newMBB
10884 // fallthrough -->nextMBB
10885 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10886 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000010887 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000010888 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000010889
Mon P Wang63307c32008-05-05 19:05:59 +000010890 /// First build the CFG
10891 MachineFunction *F = MBB->getParent();
10892 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000010893 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
10894 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
10895 F->insert(MBBIter, newMBB);
10896 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010897
Dan Gohman14152b42010-07-06 20:24:04 +000010898 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
10899 nextMBB->splice(nextMBB->begin(), thisMBB,
10900 llvm::next(MachineBasicBlock::iterator(bInstr)),
10901 thisMBB->end());
10902 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010903
Mon P Wang63307c32008-05-05 19:05:59 +000010904 // Update thisMBB to fall through to newMBB
10905 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010906
Mon P Wang63307c32008-05-05 19:05:59 +000010907 // newMBB jumps to itself and fall through to nextMBB
10908 newMBB->addSuccessor(nextMBB);
10909 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010910
Mon P Wang63307c32008-05-05 19:05:59 +000010911 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010912 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000010913 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +000010914 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000010915 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010916 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000010917 int numArgs = bInstr->getNumOperands() - 1;
10918 for (int i=0; i < numArgs; ++i)
10919 argOpers[i] = &bInstr->getOperand(i+1);
10920
10921 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010922 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000010923 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000010924
Dale Johannesen140be2d2008-08-19 18:47:28 +000010925 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010926 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000010927 for (int i=0; i <= lastAddrIndx; ++i)
10928 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010929
Dale Johannesen140be2d2008-08-19 18:47:28 +000010930 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010931 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010932 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010933 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010934 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010935 tt = t1;
10936
Dale Johannesen140be2d2008-08-19 18:47:28 +000010937 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +000010938 assert((argOpers[valArgIndx]->isReg() ||
10939 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000010940 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +000010941 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000010942 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000010943 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000010944 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010945 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +000010946 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010947
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010948 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +000010949 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +000010950
Dale Johannesene4d209d2009-02-03 20:21:25 +000010951 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +000010952 for (int i=0; i <= lastAddrIndx; ++i)
10953 (*MIB).addOperand(*argOpers[i]);
10954 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +000010955 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000010956 (*MIB).setMemRefs(bInstr->memoperands_begin(),
10957 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +000010958
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010959 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +000010960 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +000010961
Mon P Wang63307c32008-05-05 19:05:59 +000010962 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000010963 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000010964
Dan Gohman14152b42010-07-06 20:24:04 +000010965 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000010966 return nextMBB;
10967}
10968
Dale Johannesen1b54c7f2008-10-03 19:41:08 +000010969// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +000010970MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010971X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
10972 MachineBasicBlock *MBB,
10973 unsigned regOpcL,
10974 unsigned regOpcH,
10975 unsigned immOpcL,
10976 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000010977 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010978 // For the atomic bitwise operator, we generate
10979 // thisMBB (instructions are in pairs, except cmpxchg8b)
10980 // ld t1,t2 = [bitinstr.addr]
10981 // newMBB:
10982 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
10983 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +000010984 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010985 // mov ECX, EBX <- t5, t6
10986 // mov EAX, EDX <- t1, t2
10987 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
10988 // mov t3, t4 <- EAX, EDX
10989 // bz newMBB
10990 // result in out1, out2
10991 // fallthrough -->nextMBB
10992
10993 const TargetRegisterClass *RC = X86::GR32RegisterClass;
10994 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010995 const unsigned NotOpc = X86::NOT32r;
10996 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10997 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
10998 MachineFunction::iterator MBBIter = MBB;
10999 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011000
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011001 /// First build the CFG
11002 MachineFunction *F = MBB->getParent();
11003 MachineBasicBlock *thisMBB = MBB;
11004 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11005 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11006 F->insert(MBBIter, newMBB);
11007 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011008
Dan Gohman14152b42010-07-06 20:24:04 +000011009 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11010 nextMBB->splice(nextMBB->begin(), thisMBB,
11011 llvm::next(MachineBasicBlock::iterator(bInstr)),
11012 thisMBB->end());
11013 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011014
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011015 // Update thisMBB to fall through to newMBB
11016 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011017
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011018 // newMBB jumps to itself and fall through to nextMBB
11019 newMBB->addSuccessor(nextMBB);
11020 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011021
Dale Johannesene4d209d2009-02-03 20:21:25 +000011022 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011023 // Insert instructions into newMBB based on incoming instruction
11024 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011025 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011026 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011027 MachineOperand& dest1Oper = bInstr->getOperand(0);
11028 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011029 MachineOperand* argOpers[2 + X86::AddrNumOperands];
11030 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011031 argOpers[i] = &bInstr->getOperand(i+2);
11032
Dan Gohman71ea4e52010-05-14 21:01:44 +000011033 // We use some of the operands multiple times, so conservatively just
11034 // clear any kill flags that might be present.
11035 if (argOpers[i]->isReg() && argOpers[i]->isUse())
11036 argOpers[i]->setIsKill(false);
11037 }
11038
Evan Chengad5b52f2010-01-08 19:14:57 +000011039 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011040 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +000011041
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011042 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011043 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011044 for (int i=0; i <= lastAddrIndx; ++i)
11045 (*MIB).addOperand(*argOpers[i]);
11046 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011047 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +000011048 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +000011049 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011050 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +000011051 MachineOperand newOp3 = *(argOpers[3]);
11052 if (newOp3.isImm())
11053 newOp3.setImm(newOp3.getImm()+4);
11054 else
11055 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011056 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +000011057 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011058
11059 // t3/4 are defined later, at the bottom of the loop
11060 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
11061 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011062 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011063 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011064 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011065 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
11066
Evan Cheng306b4ca2010-01-08 23:41:50 +000011067 // The subsequent operations should be using the destination registers of
11068 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +000011069 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +000011070 t1 = F->getRegInfo().createVirtualRegister(RC);
11071 t2 = F->getRegInfo().createVirtualRegister(RC);
11072 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
11073 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011074 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +000011075 t1 = dest1Oper.getReg();
11076 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011077 }
11078
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011079 int valArgIndx = lastAddrIndx + 1;
11080 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +000011081 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011082 "invalid operand");
11083 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
11084 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011085 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011086 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011087 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011088 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +000011089 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011090 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011091 (*MIB).addOperand(*argOpers[valArgIndx]);
11092 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011093 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011094 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011095 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011096 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011097 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011098 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011099 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +000011100 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011101 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011102 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011103
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011104 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011105 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011106 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011107 MIB.addReg(t2);
11108
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011109 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011110 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011111 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011112 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +000011113
Dale Johannesene4d209d2009-02-03 20:21:25 +000011114 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011115 for (int i=0; i <= lastAddrIndx; ++i)
11116 (*MIB).addOperand(*argOpers[i]);
11117
11118 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011119 (*MIB).setMemRefs(bInstr->memoperands_begin(),
11120 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011121
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011122 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011123 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011124 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011125 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000011126
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011127 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011128 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011129
Dan Gohman14152b42010-07-06 20:24:04 +000011130 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011131 return nextMBB;
11132}
11133
11134// private utility function
11135MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +000011136X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
11137 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011138 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000011139 // For the atomic min/max operator, we generate
11140 // thisMBB:
11141 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000011142 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +000011143 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +000011144 // cmp t1, t2
11145 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +000011146 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000011147 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
11148 // bz newMBB
11149 // fallthrough -->nextMBB
11150 //
11151 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11152 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011153 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000011154 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011155
Mon P Wang63307c32008-05-05 19:05:59 +000011156 /// First build the CFG
11157 MachineFunction *F = MBB->getParent();
11158 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011159 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11160 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11161 F->insert(MBBIter, newMBB);
11162 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011163
Dan Gohman14152b42010-07-06 20:24:04 +000011164 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11165 nextMBB->splice(nextMBB->begin(), thisMBB,
11166 llvm::next(MachineBasicBlock::iterator(mInstr)),
11167 thisMBB->end());
11168 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011169
Mon P Wang63307c32008-05-05 19:05:59 +000011170 // Update thisMBB to fall through to newMBB
11171 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011172
Mon P Wang63307c32008-05-05 19:05:59 +000011173 // newMBB jumps to newMBB and fall through to nextMBB
11174 newMBB->addSuccessor(nextMBB);
11175 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011176
Dale Johannesene4d209d2009-02-03 20:21:25 +000011177 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000011178 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011179 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011180 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +000011181 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011182 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000011183 int numArgs = mInstr->getNumOperands() - 1;
11184 for (int i=0; i < numArgs; ++i)
11185 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +000011186
Mon P Wang63307c32008-05-05 19:05:59 +000011187 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011188 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011189 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000011190
Mon P Wangab3e7472008-05-05 22:56:23 +000011191 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011192 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011193 for (int i=0; i <= lastAddrIndx; ++i)
11194 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +000011195
Mon P Wang63307c32008-05-05 19:05:59 +000011196 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +000011197 assert((argOpers[valArgIndx]->isReg() ||
11198 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000011199 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +000011200
11201 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +000011202 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011203 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +000011204 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011205 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000011206 (*MIB).addOperand(*argOpers[valArgIndx]);
11207
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011208 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +000011209 MIB.addReg(t1);
11210
Dale Johannesene4d209d2009-02-03 20:21:25 +000011211 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +000011212 MIB.addReg(t1);
11213 MIB.addReg(t2);
11214
11215 // Generate movc
11216 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011217 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +000011218 MIB.addReg(t2);
11219 MIB.addReg(t1);
11220
11221 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +000011222 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +000011223 for (int i=0; i <= lastAddrIndx; ++i)
11224 (*MIB).addOperand(*argOpers[i]);
11225 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +000011226 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011227 (*MIB).setMemRefs(mInstr->memoperands_begin(),
11228 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +000011229
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011230 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +000011231 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +000011232
Mon P Wang63307c32008-05-05 19:05:59 +000011233 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011234 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000011235
Dan Gohman14152b42010-07-06 20:24:04 +000011236 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000011237 return nextMBB;
11238}
11239
Eric Christopherf83a5de2009-08-27 18:08:16 +000011240// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011241// or XMM0_V32I8 in AVX all of this code can be replaced with that
11242// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +000011243MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +000011244X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +000011245 unsigned numArgs, bool memArg) const {
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011246 assert((Subtarget->hasSSE42() || Subtarget->hasAVX()) &&
11247 "Target must have SSE4.2 or AVX features enabled");
11248
Eric Christopherb120ab42009-08-18 22:50:32 +000011249 DebugLoc dl = MI->getDebugLoc();
11250 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +000011251 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011252 if (!Subtarget->hasAVX()) {
11253 if (memArg)
11254 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
11255 else
11256 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
11257 } else {
11258 if (memArg)
11259 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
11260 else
11261 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
11262 }
Eric Christopherb120ab42009-08-18 22:50:32 +000011263
Eric Christopher41c902f2010-11-30 08:20:21 +000011264 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +000011265 for (unsigned i = 0; i < numArgs; ++i) {
11266 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +000011267 if (!(Op.isReg() && Op.isImplicit()))
11268 MIB.addOperand(Op);
11269 }
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011270 BuildMI(*BB, MI, dl,
11271 TII->get(Subtarget->hasAVX() ? X86::VMOVAPSrr : X86::MOVAPSrr),
11272 MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000011273 .addReg(X86::XMM0);
11274
Dan Gohman14152b42010-07-06 20:24:04 +000011275 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000011276 return BB;
11277}
11278
11279MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +000011280X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000011281 DebugLoc dl = MI->getDebugLoc();
11282 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011283
Eric Christopher228232b2010-11-30 07:20:12 +000011284 // Address into RAX/EAX, other two args into ECX, EDX.
11285 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
11286 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
11287 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
11288 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000011289 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011290
Eric Christopher228232b2010-11-30 07:20:12 +000011291 unsigned ValOps = X86::AddrNumOperands;
11292 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
11293 .addReg(MI->getOperand(ValOps).getReg());
11294 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
11295 .addReg(MI->getOperand(ValOps+1).getReg());
11296
11297 // The instruction doesn't actually take any operands though.
11298 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011299
Eric Christopher228232b2010-11-30 07:20:12 +000011300 MI->eraseFromParent(); // The pseudo is gone now.
11301 return BB;
11302}
11303
11304MachineBasicBlock *
11305X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000011306 DebugLoc dl = MI->getDebugLoc();
11307 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011308
Eric Christopher228232b2010-11-30 07:20:12 +000011309 // First arg in ECX, the second in EAX.
11310 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
11311 .addReg(MI->getOperand(0).getReg());
11312 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
11313 .addReg(MI->getOperand(1).getReg());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011314
Eric Christopher228232b2010-11-30 07:20:12 +000011315 // The instruction doesn't actually take any operands though.
11316 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011317
Eric Christopher228232b2010-11-30 07:20:12 +000011318 MI->eraseFromParent(); // The pseudo is gone now.
11319 return BB;
11320}
11321
11322MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000011323X86TargetLowering::EmitVAARG64WithCustomInserter(
11324 MachineInstr *MI,
11325 MachineBasicBlock *MBB) const {
11326 // Emit va_arg instruction on X86-64.
11327
11328 // Operands to this pseudo-instruction:
11329 // 0 ) Output : destination address (reg)
11330 // 1-5) Input : va_list address (addr, i64mem)
11331 // 6 ) ArgSize : Size (in bytes) of vararg type
11332 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
11333 // 8 ) Align : Alignment of type
11334 // 9 ) EFLAGS (implicit-def)
11335
11336 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
11337 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
11338
11339 unsigned DestReg = MI->getOperand(0).getReg();
11340 MachineOperand &Base = MI->getOperand(1);
11341 MachineOperand &Scale = MI->getOperand(2);
11342 MachineOperand &Index = MI->getOperand(3);
11343 MachineOperand &Disp = MI->getOperand(4);
11344 MachineOperand &Segment = MI->getOperand(5);
11345 unsigned ArgSize = MI->getOperand(6).getImm();
11346 unsigned ArgMode = MI->getOperand(7).getImm();
11347 unsigned Align = MI->getOperand(8).getImm();
11348
11349 // Memory Reference
11350 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
11351 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
11352 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
11353
11354 // Machine Information
11355 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11356 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
11357 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
11358 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
11359 DebugLoc DL = MI->getDebugLoc();
11360
11361 // struct va_list {
11362 // i32 gp_offset
11363 // i32 fp_offset
11364 // i64 overflow_area (address)
11365 // i64 reg_save_area (address)
11366 // }
11367 // sizeof(va_list) = 24
11368 // alignment(va_list) = 8
11369
11370 unsigned TotalNumIntRegs = 6;
11371 unsigned TotalNumXMMRegs = 8;
11372 bool UseGPOffset = (ArgMode == 1);
11373 bool UseFPOffset = (ArgMode == 2);
11374 unsigned MaxOffset = TotalNumIntRegs * 8 +
11375 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
11376
11377 /* Align ArgSize to a multiple of 8 */
11378 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
11379 bool NeedsAlign = (Align > 8);
11380
11381 MachineBasicBlock *thisMBB = MBB;
11382 MachineBasicBlock *overflowMBB;
11383 MachineBasicBlock *offsetMBB;
11384 MachineBasicBlock *endMBB;
11385
11386 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
11387 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
11388 unsigned OffsetReg = 0;
11389
11390 if (!UseGPOffset && !UseFPOffset) {
11391 // If we only pull from the overflow region, we don't create a branch.
11392 // We don't need to alter control flow.
11393 OffsetDestReg = 0; // unused
11394 OverflowDestReg = DestReg;
11395
11396 offsetMBB = NULL;
11397 overflowMBB = thisMBB;
11398 endMBB = thisMBB;
11399 } else {
11400 // First emit code to check if gp_offset (or fp_offset) is below the bound.
11401 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
11402 // If not, pull from overflow_area. (branch to overflowMBB)
11403 //
11404 // thisMBB
11405 // | .
11406 // | .
11407 // offsetMBB overflowMBB
11408 // | .
11409 // | .
11410 // endMBB
11411
11412 // Registers for the PHI in endMBB
11413 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
11414 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
11415
11416 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11417 MachineFunction *MF = MBB->getParent();
11418 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11419 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11420 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11421
11422 MachineFunction::iterator MBBIter = MBB;
11423 ++MBBIter;
11424
11425 // Insert the new basic blocks
11426 MF->insert(MBBIter, offsetMBB);
11427 MF->insert(MBBIter, overflowMBB);
11428 MF->insert(MBBIter, endMBB);
11429
11430 // Transfer the remainder of MBB and its successor edges to endMBB.
11431 endMBB->splice(endMBB->begin(), thisMBB,
11432 llvm::next(MachineBasicBlock::iterator(MI)),
11433 thisMBB->end());
11434 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
11435
11436 // Make offsetMBB and overflowMBB successors of thisMBB
11437 thisMBB->addSuccessor(offsetMBB);
11438 thisMBB->addSuccessor(overflowMBB);
11439
11440 // endMBB is a successor of both offsetMBB and overflowMBB
11441 offsetMBB->addSuccessor(endMBB);
11442 overflowMBB->addSuccessor(endMBB);
11443
11444 // Load the offset value into a register
11445 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
11446 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
11447 .addOperand(Base)
11448 .addOperand(Scale)
11449 .addOperand(Index)
11450 .addDisp(Disp, UseFPOffset ? 4 : 0)
11451 .addOperand(Segment)
11452 .setMemRefs(MMOBegin, MMOEnd);
11453
11454 // Check if there is enough room left to pull this argument.
11455 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
11456 .addReg(OffsetReg)
11457 .addImm(MaxOffset + 8 - ArgSizeA8);
11458
11459 // Branch to "overflowMBB" if offset >= max
11460 // Fall through to "offsetMBB" otherwise
11461 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
11462 .addMBB(overflowMBB);
11463 }
11464
11465 // In offsetMBB, emit code to use the reg_save_area.
11466 if (offsetMBB) {
11467 assert(OffsetReg != 0);
11468
11469 // Read the reg_save_area address.
11470 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
11471 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
11472 .addOperand(Base)
11473 .addOperand(Scale)
11474 .addOperand(Index)
11475 .addDisp(Disp, 16)
11476 .addOperand(Segment)
11477 .setMemRefs(MMOBegin, MMOEnd);
11478
11479 // Zero-extend the offset
11480 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
11481 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
11482 .addImm(0)
11483 .addReg(OffsetReg)
11484 .addImm(X86::sub_32bit);
11485
11486 // Add the offset to the reg_save_area to get the final address.
11487 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
11488 .addReg(OffsetReg64)
11489 .addReg(RegSaveReg);
11490
11491 // Compute the offset for the next argument
11492 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
11493 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
11494 .addReg(OffsetReg)
11495 .addImm(UseFPOffset ? 16 : 8);
11496
11497 // Store it back into the va_list.
11498 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
11499 .addOperand(Base)
11500 .addOperand(Scale)
11501 .addOperand(Index)
11502 .addDisp(Disp, UseFPOffset ? 4 : 0)
11503 .addOperand(Segment)
11504 .addReg(NextOffsetReg)
11505 .setMemRefs(MMOBegin, MMOEnd);
11506
11507 // Jump to endMBB
11508 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
11509 .addMBB(endMBB);
11510 }
11511
11512 //
11513 // Emit code to use overflow area
11514 //
11515
11516 // Load the overflow_area address into a register.
11517 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
11518 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
11519 .addOperand(Base)
11520 .addOperand(Scale)
11521 .addOperand(Index)
11522 .addDisp(Disp, 8)
11523 .addOperand(Segment)
11524 .setMemRefs(MMOBegin, MMOEnd);
11525
11526 // If we need to align it, do so. Otherwise, just copy the address
11527 // to OverflowDestReg.
11528 if (NeedsAlign) {
11529 // Align the overflow address
11530 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
11531 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
11532
11533 // aligned_addr = (addr + (align-1)) & ~(align-1)
11534 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
11535 .addReg(OverflowAddrReg)
11536 .addImm(Align-1);
11537
11538 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
11539 .addReg(TmpReg)
11540 .addImm(~(uint64_t)(Align-1));
11541 } else {
11542 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
11543 .addReg(OverflowAddrReg);
11544 }
11545
11546 // Compute the next overflow address after this argument.
11547 // (the overflow address should be kept 8-byte aligned)
11548 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
11549 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
11550 .addReg(OverflowDestReg)
11551 .addImm(ArgSizeA8);
11552
11553 // Store the new overflow address.
11554 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
11555 .addOperand(Base)
11556 .addOperand(Scale)
11557 .addOperand(Index)
11558 .addDisp(Disp, 8)
11559 .addOperand(Segment)
11560 .addReg(NextAddrReg)
11561 .setMemRefs(MMOBegin, MMOEnd);
11562
11563 // If we branched, emit the PHI to the front of endMBB.
11564 if (offsetMBB) {
11565 BuildMI(*endMBB, endMBB->begin(), DL,
11566 TII->get(X86::PHI), DestReg)
11567 .addReg(OffsetDestReg).addMBB(offsetMBB)
11568 .addReg(OverflowDestReg).addMBB(overflowMBB);
11569 }
11570
11571 // Erase the pseudo instruction
11572 MI->eraseFromParent();
11573
11574 return endMBB;
11575}
11576
11577MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000011578X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
11579 MachineInstr *MI,
11580 MachineBasicBlock *MBB) const {
11581 // Emit code to save XMM registers to the stack. The ABI says that the
11582 // number of registers to save is given in %al, so it's theoretically
11583 // possible to do an indirect jump trick to avoid saving all of them,
11584 // however this code takes a simpler approach and just executes all
11585 // of the stores if %al is non-zero. It's less code, and it's probably
11586 // easier on the hardware branch predictor, and stores aren't all that
11587 // expensive anyway.
11588
11589 // Create the new basic blocks. One block contains all the XMM stores,
11590 // and one block is the final destination regardless of whether any
11591 // stores were performed.
11592 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11593 MachineFunction *F = MBB->getParent();
11594 MachineFunction::iterator MBBIter = MBB;
11595 ++MBBIter;
11596 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
11597 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
11598 F->insert(MBBIter, XMMSaveMBB);
11599 F->insert(MBBIter, EndMBB);
11600
Dan Gohman14152b42010-07-06 20:24:04 +000011601 // Transfer the remainder of MBB and its successor edges to EndMBB.
11602 EndMBB->splice(EndMBB->begin(), MBB,
11603 llvm::next(MachineBasicBlock::iterator(MI)),
11604 MBB->end());
11605 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
11606
Dan Gohmand6708ea2009-08-15 01:38:56 +000011607 // The original block will now fall through to the XMM save block.
11608 MBB->addSuccessor(XMMSaveMBB);
11609 // The XMMSaveMBB will fall through to the end block.
11610 XMMSaveMBB->addSuccessor(EndMBB);
11611
11612 // Now add the instructions.
11613 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11614 DebugLoc DL = MI->getDebugLoc();
11615
11616 unsigned CountReg = MI->getOperand(0).getReg();
11617 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
11618 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
11619
11620 if (!Subtarget->isTargetWin64()) {
11621 // If %al is 0, branch around the XMM save block.
11622 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011623 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000011624 MBB->addSuccessor(EndMBB);
11625 }
11626
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011627 unsigned MOVOpc = Subtarget->hasAVX() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000011628 // In the XMM save block, save all the XMM argument registers.
11629 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
11630 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000011631 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000011632 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000011633 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000011634 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000011635 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011636 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000011637 .addFrameIndex(RegSaveFrameIndex)
11638 .addImm(/*Scale=*/1)
11639 .addReg(/*IndexReg=*/0)
11640 .addImm(/*Disp=*/Offset)
11641 .addReg(/*Segment=*/0)
11642 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000011643 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000011644 }
11645
Dan Gohman14152b42010-07-06 20:24:04 +000011646 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000011647
11648 return EndMBB;
11649}
Mon P Wang63307c32008-05-05 19:05:59 +000011650
Evan Cheng60c07e12006-07-05 22:17:51 +000011651MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000011652X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000011653 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000011654 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11655 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000011656
Chris Lattner52600972009-09-02 05:57:00 +000011657 // To "insert" a SELECT_CC instruction, we actually have to insert the
11658 // diamond control-flow pattern. The incoming instruction knows the
11659 // destination vreg to set, the condition code register to branch on, the
11660 // true/false values to select between, and a branch opcode to use.
11661 const BasicBlock *LLVM_BB = BB->getBasicBlock();
11662 MachineFunction::iterator It = BB;
11663 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000011664
Chris Lattner52600972009-09-02 05:57:00 +000011665 // thisMBB:
11666 // ...
11667 // TrueVal = ...
11668 // cmpTY ccX, r1, r2
11669 // bCC copy1MBB
11670 // fallthrough --> copy0MBB
11671 MachineBasicBlock *thisMBB = BB;
11672 MachineFunction *F = BB->getParent();
11673 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
11674 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000011675 F->insert(It, copy0MBB);
11676 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000011677
Bill Wendling730c07e2010-06-25 20:48:10 +000011678 // If the EFLAGS register isn't dead in the terminator, then claim that it's
11679 // live into the sink and copy blocks.
Jakob Stoklund Olesen4a1b9d82011-09-02 23:52:49 +000011680 if (!MI->killsRegister(X86::EFLAGS)) {
11681 copy0MBB->addLiveIn(X86::EFLAGS);
11682 sinkMBB->addLiveIn(X86::EFLAGS);
Bill Wendling730c07e2010-06-25 20:48:10 +000011683 }
11684
Dan Gohman14152b42010-07-06 20:24:04 +000011685 // Transfer the remainder of BB and its successor edges to sinkMBB.
11686 sinkMBB->splice(sinkMBB->begin(), BB,
11687 llvm::next(MachineBasicBlock::iterator(MI)),
11688 BB->end());
11689 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
11690
11691 // Add the true and fallthrough blocks as its successors.
11692 BB->addSuccessor(copy0MBB);
11693 BB->addSuccessor(sinkMBB);
11694
11695 // Create the conditional branch instruction.
11696 unsigned Opc =
11697 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
11698 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
11699
Chris Lattner52600972009-09-02 05:57:00 +000011700 // copy0MBB:
11701 // %FalseValue = ...
11702 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000011703 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000011704
Chris Lattner52600972009-09-02 05:57:00 +000011705 // sinkMBB:
11706 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
11707 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000011708 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
11709 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000011710 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
11711 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
11712
Dan Gohman14152b42010-07-06 20:24:04 +000011713 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000011714 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000011715}
11716
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011717MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000011718X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
11719 bool Is64Bit) const {
11720 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11721 DebugLoc DL = MI->getDebugLoc();
11722 MachineFunction *MF = BB->getParent();
11723 const BasicBlock *LLVM_BB = BB->getBasicBlock();
11724
11725 assert(EnableSegmentedStacks);
11726
11727 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
11728 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
11729
11730 // BB:
11731 // ... [Till the alloca]
11732 // If stacklet is not large enough, jump to mallocMBB
11733 //
11734 // bumpMBB:
11735 // Allocate by subtracting from RSP
11736 // Jump to continueMBB
11737 //
11738 // mallocMBB:
11739 // Allocate by call to runtime
11740 //
11741 // continueMBB:
11742 // ...
11743 // [rest of original BB]
11744 //
11745
11746 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11747 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11748 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11749
11750 MachineRegisterInfo &MRI = MF->getRegInfo();
11751 const TargetRegisterClass *AddrRegClass =
11752 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
11753
11754 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
11755 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
11756 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
11757 sizeVReg = MI->getOperand(1).getReg(),
11758 physSPReg = Is64Bit ? X86::RSP : X86::ESP;
11759
11760 MachineFunction::iterator MBBIter = BB;
11761 ++MBBIter;
11762
11763 MF->insert(MBBIter, bumpMBB);
11764 MF->insert(MBBIter, mallocMBB);
11765 MF->insert(MBBIter, continueMBB);
11766
11767 continueMBB->splice(continueMBB->begin(), BB, llvm::next
11768 (MachineBasicBlock::iterator(MI)), BB->end());
11769 continueMBB->transferSuccessorsAndUpdatePHIs(BB);
11770
11771 // Add code to the main basic block to check if the stack limit has been hit,
11772 // and if so, jump to mallocMBB otherwise to bumpMBB.
11773 BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
11774 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), tmpSPVReg)
11775 .addReg(tmpSPVReg).addReg(sizeVReg);
11776 BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
11777 .addReg(0).addImm(0).addReg(0).addImm(TlsOffset).addReg(TlsReg)
11778 .addReg(tmpSPVReg);
11779 BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
11780
11781 // bumpMBB simply decreases the stack pointer, since we know the current
11782 // stacklet has enough space.
11783 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
11784 .addReg(tmpSPVReg);
11785 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
11786 .addReg(tmpSPVReg);
11787 BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
11788
11789 // Calls into a routine in libgcc to allocate more space from the heap.
11790 if (Is64Bit) {
11791 BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
11792 .addReg(sizeVReg);
11793 BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
11794 .addExternalSymbol("__morestack_allocate_stack_space").addReg(X86::RDI);
11795 } else {
11796 BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
11797 .addImm(12);
11798 BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
11799 BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
11800 .addExternalSymbol("__morestack_allocate_stack_space");
11801 }
11802
11803 if (!Is64Bit)
11804 BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
11805 .addImm(16);
11806
11807 BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
11808 .addReg(Is64Bit ? X86::RAX : X86::EAX);
11809 BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
11810
11811 // Set up the CFG correctly.
11812 BB->addSuccessor(bumpMBB);
11813 BB->addSuccessor(mallocMBB);
11814 mallocMBB->addSuccessor(continueMBB);
11815 bumpMBB->addSuccessor(continueMBB);
11816
11817 // Take care of the PHI nodes.
11818 BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
11819 MI->getOperand(0).getReg())
11820 .addReg(mallocPtrVReg).addMBB(mallocMBB)
11821 .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
11822
11823 // Delete the original pseudo instruction.
11824 MI->eraseFromParent();
11825
11826 // And we're done.
11827 return continueMBB;
11828}
11829
11830MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000011831X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000011832 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011833 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11834 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011835
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000011836 assert(!Subtarget->isTargetEnvMacho());
11837
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011838 // The lowering is pretty easy: we're just emitting the call to _alloca. The
11839 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011840
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000011841 if (Subtarget->isTargetWin64()) {
11842 if (Subtarget->isTargetCygMing()) {
11843 // ___chkstk(Mingw64):
11844 // Clobbers R10, R11, RAX and EFLAGS.
11845 // Updates RSP.
11846 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
11847 .addExternalSymbol("___chkstk")
11848 .addReg(X86::RAX, RegState::Implicit)
11849 .addReg(X86::RSP, RegState::Implicit)
11850 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
11851 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
11852 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
11853 } else {
11854 // __chkstk(MSVCRT): does not update stack pointer.
11855 // Clobbers R10, R11 and EFLAGS.
11856 // FIXME: RAX(allocated size) might be reused and not killed.
11857 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
11858 .addExternalSymbol("__chkstk")
11859 .addReg(X86::RAX, RegState::Implicit)
11860 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
11861 // RAX has the offset to subtracted from RSP.
11862 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
11863 .addReg(X86::RSP)
11864 .addReg(X86::RAX);
11865 }
11866 } else {
11867 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000011868 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
11869
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000011870 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
11871 .addExternalSymbol(StackProbeSymbol)
11872 .addReg(X86::EAX, RegState::Implicit)
11873 .addReg(X86::ESP, RegState::Implicit)
11874 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
11875 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
11876 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
11877 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011878
Dan Gohman14152b42010-07-06 20:24:04 +000011879 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011880 return BB;
11881}
Chris Lattner52600972009-09-02 05:57:00 +000011882
11883MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000011884X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
11885 MachineBasicBlock *BB) const {
11886 // This is pretty easy. We're taking the value that we received from
11887 // our load from the relocation, sticking it in either RDI (x86-64)
11888 // or EAX and doing an indirect call. The return value will then
11889 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000011890 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000011891 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000011892 DebugLoc DL = MI->getDebugLoc();
11893 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000011894
11895 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000011896 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000011897
Eric Christopher30ef0e52010-06-03 04:07:48 +000011898 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000011899 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
11900 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000011901 .addReg(X86::RIP)
11902 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000011903 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000011904 MI->getOperand(3).getTargetFlags())
11905 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000011906 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000011907 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +000011908 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000011909 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
11910 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000011911 .addReg(0)
11912 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000011913 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000011914 MI->getOperand(3).getTargetFlags())
11915 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000011916 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000011917 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000011918 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000011919 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
11920 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000011921 .addReg(TII->getGlobalBaseReg(F))
11922 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000011923 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000011924 MI->getOperand(3).getTargetFlags())
11925 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000011926 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000011927 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000011928 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000011929
Dan Gohman14152b42010-07-06 20:24:04 +000011930 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000011931 return BB;
11932}
11933
11934MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000011935X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000011936 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000011937 switch (MI->getOpcode()) {
11938 default: assert(false && "Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000011939 case X86::TAILJMPd64:
11940 case X86::TAILJMPr64:
11941 case X86::TAILJMPm64:
11942 assert(!"TAILJMP64 would not be touched here.");
11943 case X86::TCRETURNdi64:
11944 case X86::TCRETURNri64:
11945 case X86::TCRETURNmi64:
11946 // Defs of TCRETURNxx64 has Win64's callee-saved registers, as subset.
11947 // On AMD64, additional defs should be added before register allocation.
11948 if (!Subtarget->isTargetWin64()) {
11949 MI->addRegisterDefined(X86::RSI);
11950 MI->addRegisterDefined(X86::RDI);
11951 MI->addRegisterDefined(X86::XMM6);
11952 MI->addRegisterDefined(X86::XMM7);
11953 MI->addRegisterDefined(X86::XMM8);
11954 MI->addRegisterDefined(X86::XMM9);
11955 MI->addRegisterDefined(X86::XMM10);
11956 MI->addRegisterDefined(X86::XMM11);
11957 MI->addRegisterDefined(X86::XMM12);
11958 MI->addRegisterDefined(X86::XMM13);
11959 MI->addRegisterDefined(X86::XMM14);
11960 MI->addRegisterDefined(X86::XMM15);
11961 }
11962 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000011963 case X86::WIN_ALLOCA:
11964 return EmitLoweredWinAlloca(MI, BB);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000011965 case X86::SEG_ALLOCA_32:
11966 return EmitLoweredSegAlloca(MI, BB, false);
11967 case X86::SEG_ALLOCA_64:
11968 return EmitLoweredSegAlloca(MI, BB, true);
Eric Christopher30ef0e52010-06-03 04:07:48 +000011969 case X86::TLSCall_32:
11970 case X86::TLSCall_64:
11971 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000011972 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000011973 case X86::CMOV_FR32:
11974 case X86::CMOV_FR64:
11975 case X86::CMOV_V4F32:
11976 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000011977 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000011978 case X86::CMOV_V8F32:
11979 case X86::CMOV_V4F64:
11980 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000011981 case X86::CMOV_GR16:
11982 case X86::CMOV_GR32:
11983 case X86::CMOV_RFP32:
11984 case X86::CMOV_RFP64:
11985 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000011986 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000011987
Dale Johannesen849f2142007-07-03 00:53:03 +000011988 case X86::FP32_TO_INT16_IN_MEM:
11989 case X86::FP32_TO_INT32_IN_MEM:
11990 case X86::FP32_TO_INT64_IN_MEM:
11991 case X86::FP64_TO_INT16_IN_MEM:
11992 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000011993 case X86::FP64_TO_INT64_IN_MEM:
11994 case X86::FP80_TO_INT16_IN_MEM:
11995 case X86::FP80_TO_INT32_IN_MEM:
11996 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000011997 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11998 DebugLoc DL = MI->getDebugLoc();
11999
Evan Cheng60c07e12006-07-05 22:17:51 +000012000 // Change the floating point control register to use "round towards zero"
12001 // mode when truncating to an integer value.
12002 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000012003 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000012004 addFrameReference(BuildMI(*BB, MI, DL,
12005 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012006
12007 // Load the old value of the high byte of the control word...
12008 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000012009 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000012010 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000012011 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012012
12013 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000012014 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012015 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000012016
12017 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000012018 addFrameReference(BuildMI(*BB, MI, DL,
12019 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012020
12021 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000012022 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012023 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000012024
12025 // Get the X86 opcode to use.
12026 unsigned Opc;
12027 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000012028 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000012029 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
12030 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
12031 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
12032 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
12033 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
12034 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000012035 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
12036 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
12037 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000012038 }
12039
12040 X86AddressMode AM;
12041 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000012042 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012043 AM.BaseType = X86AddressMode::RegBase;
12044 AM.Base.Reg = Op.getReg();
12045 } else {
12046 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000012047 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000012048 }
12049 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000012050 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012051 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012052 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000012053 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012054 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012055 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000012056 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012057 AM.GV = Op.getGlobal();
12058 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000012059 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012060 }
Dan Gohman14152b42010-07-06 20:24:04 +000012061 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000012062 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000012063
12064 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000012065 addFrameReference(BuildMI(*BB, MI, DL,
12066 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012067
Dan Gohman14152b42010-07-06 20:24:04 +000012068 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000012069 return BB;
12070 }
Eric Christopherb120ab42009-08-18 22:50:32 +000012071 // String/text processing lowering.
12072 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012073 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000012074 return EmitPCMP(MI, BB, 3, false /* in-mem */);
12075 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012076 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000012077 return EmitPCMP(MI, BB, 3, true /* in-mem */);
12078 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012079 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000012080 return EmitPCMP(MI, BB, 5, false /* in mem */);
12081 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012082 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000012083 return EmitPCMP(MI, BB, 5, true /* in mem */);
12084
Eric Christopher228232b2010-11-30 07:20:12 +000012085 // Thread synchronization.
12086 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012087 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000012088 case X86::MWAIT:
12089 return EmitMwait(MI, BB);
12090
Eric Christopherb120ab42009-08-18 22:50:32 +000012091 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000012092 case X86::ATOMAND32:
12093 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012094 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012095 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012096 X86::NOT32r, X86::EAX,
12097 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000012098 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000012099 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
12100 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012101 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012102 X86::NOT32r, X86::EAX,
12103 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000012104 case X86::ATOMXOR32:
12105 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012106 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012107 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012108 X86::NOT32r, X86::EAX,
12109 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000012110 case X86::ATOMNAND32:
12111 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012112 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012113 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012114 X86::NOT32r, X86::EAX,
12115 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000012116 case X86::ATOMMIN32:
12117 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
12118 case X86::ATOMMAX32:
12119 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
12120 case X86::ATOMUMIN32:
12121 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
12122 case X86::ATOMUMAX32:
12123 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000012124
12125 case X86::ATOMAND16:
12126 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
12127 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012128 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012129 X86::NOT16r, X86::AX,
12130 X86::GR16RegisterClass);
12131 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000012132 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012133 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012134 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012135 X86::NOT16r, X86::AX,
12136 X86::GR16RegisterClass);
12137 case X86::ATOMXOR16:
12138 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
12139 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012140 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012141 X86::NOT16r, X86::AX,
12142 X86::GR16RegisterClass);
12143 case X86::ATOMNAND16:
12144 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
12145 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012146 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012147 X86::NOT16r, X86::AX,
12148 X86::GR16RegisterClass, true);
12149 case X86::ATOMMIN16:
12150 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
12151 case X86::ATOMMAX16:
12152 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
12153 case X86::ATOMUMIN16:
12154 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
12155 case X86::ATOMUMAX16:
12156 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
12157
12158 case X86::ATOMAND8:
12159 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
12160 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012161 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012162 X86::NOT8r, X86::AL,
12163 X86::GR8RegisterClass);
12164 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000012165 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012166 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012167 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012168 X86::NOT8r, X86::AL,
12169 X86::GR8RegisterClass);
12170 case X86::ATOMXOR8:
12171 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
12172 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012173 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012174 X86::NOT8r, X86::AL,
12175 X86::GR8RegisterClass);
12176 case X86::ATOMNAND8:
12177 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
12178 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012179 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012180 X86::NOT8r, X86::AL,
12181 X86::GR8RegisterClass, true);
12182 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012183 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000012184 case X86::ATOMAND64:
12185 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012186 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012187 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012188 X86::NOT64r, X86::RAX,
12189 X86::GR64RegisterClass);
12190 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000012191 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
12192 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012193 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012194 X86::NOT64r, X86::RAX,
12195 X86::GR64RegisterClass);
12196 case X86::ATOMXOR64:
12197 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012198 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012199 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012200 X86::NOT64r, X86::RAX,
12201 X86::GR64RegisterClass);
12202 case X86::ATOMNAND64:
12203 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
12204 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012205 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012206 X86::NOT64r, X86::RAX,
12207 X86::GR64RegisterClass, true);
12208 case X86::ATOMMIN64:
12209 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
12210 case X86::ATOMMAX64:
12211 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
12212 case X86::ATOMUMIN64:
12213 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
12214 case X86::ATOMUMAX64:
12215 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012216
12217 // This group does 64-bit operations on a 32-bit host.
12218 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012219 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012220 X86::AND32rr, X86::AND32rr,
12221 X86::AND32ri, X86::AND32ri,
12222 false);
12223 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012224 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012225 X86::OR32rr, X86::OR32rr,
12226 X86::OR32ri, X86::OR32ri,
12227 false);
12228 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012229 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012230 X86::XOR32rr, X86::XOR32rr,
12231 X86::XOR32ri, X86::XOR32ri,
12232 false);
12233 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012234 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012235 X86::AND32rr, X86::AND32rr,
12236 X86::AND32ri, X86::AND32ri,
12237 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012238 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012239 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012240 X86::ADD32rr, X86::ADC32rr,
12241 X86::ADD32ri, X86::ADC32ri,
12242 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012243 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012244 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012245 X86::SUB32rr, X86::SBB32rr,
12246 X86::SUB32ri, X86::SBB32ri,
12247 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000012248 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012249 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000012250 X86::MOV32rr, X86::MOV32rr,
12251 X86::MOV32ri, X86::MOV32ri,
12252 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012253 case X86::VASTART_SAVE_XMM_REGS:
12254 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000012255
12256 case X86::VAARG_64:
12257 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000012258 }
12259}
12260
12261//===----------------------------------------------------------------------===//
12262// X86 Optimization Hooks
12263//===----------------------------------------------------------------------===//
12264
Dan Gohman475871a2008-07-27 21:46:04 +000012265void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000012266 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000012267 APInt &KnownZero,
12268 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000012269 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000012270 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012271 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000012272 assert((Opc >= ISD::BUILTIN_OP_END ||
12273 Opc == ISD::INTRINSIC_WO_CHAIN ||
12274 Opc == ISD::INTRINSIC_W_CHAIN ||
12275 Opc == ISD::INTRINSIC_VOID) &&
12276 "Should use MaskedValueIsZero if you don't know whether Op"
12277 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012278
Dan Gohmanf4f92f52008-02-13 23:07:24 +000012279 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012280 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000012281 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012282 case X86ISD::ADD:
12283 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000012284 case X86ISD::ADC:
12285 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012286 case X86ISD::SMUL:
12287 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000012288 case X86ISD::INC:
12289 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000012290 case X86ISD::OR:
12291 case X86ISD::XOR:
12292 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012293 // These nodes' second result is a boolean.
12294 if (Op.getResNo() == 0)
12295 break;
12296 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012297 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000012298 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
12299 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000012300 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012301 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012302}
Chris Lattner259e97c2006-01-31 19:43:35 +000012303
Owen Andersonbc146b02010-09-21 20:42:50 +000012304unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
12305 unsigned Depth) const {
12306 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
12307 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
12308 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000012309
Owen Andersonbc146b02010-09-21 20:42:50 +000012310 // Fallback case.
12311 return 1;
12312}
12313
Evan Cheng206ee9d2006-07-07 08:33:52 +000012314/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000012315/// node is a GlobalAddress + offset.
12316bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000012317 const GlobalValue* &GA,
12318 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000012319 if (N->getOpcode() == X86ISD::Wrapper) {
12320 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000012321 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000012322 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000012323 return true;
12324 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000012325 }
Evan Chengad4196b2008-05-12 19:56:52 +000012326 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000012327}
12328
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012329/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
12330/// same as extracting the high 128-bit part of 256-bit vector and then
12331/// inserting the result into the low part of a new 256-bit vector
12332static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
12333 EVT VT = SVOp->getValueType(0);
12334 int NumElems = VT.getVectorNumElements();
12335
12336 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
12337 for (int i = 0, j = NumElems/2; i < NumElems/2; ++i, ++j)
12338 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
12339 SVOp->getMaskElt(j) >= 0)
12340 return false;
12341
12342 return true;
12343}
12344
12345/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
12346/// same as extracting the low 128-bit part of 256-bit vector and then
12347/// inserting the result into the high part of a new 256-bit vector
12348static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
12349 EVT VT = SVOp->getValueType(0);
12350 int NumElems = VT.getVectorNumElements();
12351
12352 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
12353 for (int i = NumElems/2, j = 0; i < NumElems; ++i, ++j)
12354 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
12355 SVOp->getMaskElt(j) >= 0)
12356 return false;
12357
12358 return true;
12359}
12360
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012361/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
12362static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
12363 TargetLowering::DAGCombinerInfo &DCI) {
12364 DebugLoc dl = N->getDebugLoc();
12365 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
12366 SDValue V1 = SVOp->getOperand(0);
12367 SDValue V2 = SVOp->getOperand(1);
12368 EVT VT = SVOp->getValueType(0);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012369 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012370
12371 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
12372 V2.getOpcode() == ISD::CONCAT_VECTORS) {
12373 //
12374 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000012375 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012376 // V UNDEF BUILD_VECTOR UNDEF
12377 // \ / \ /
12378 // CONCAT_VECTOR CONCAT_VECTOR
12379 // \ /
12380 // \ /
12381 // RESULT: V + zero extended
12382 //
12383 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
12384 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
12385 V1.getOperand(1).getOpcode() != ISD::UNDEF)
12386 return SDValue();
12387
12388 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
12389 return SDValue();
12390
12391 // To match the shuffle mask, the first half of the mask should
12392 // be exactly the first vector, and all the rest a splat with the
12393 // first element of the second one.
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012394 for (int i = 0; i < NumElems/2; ++i)
12395 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
12396 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
12397 return SDValue();
12398
12399 // Emit a zeroed vector and insert the desired subvector on its
12400 // first half.
12401 SDValue Zeros = getZeroVector(VT, true /* HasSSE2 */, DAG, dl);
12402 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0),
12403 DAG.getConstant(0, MVT::i32), DAG, dl);
12404 return DCI.CombineTo(N, InsV);
12405 }
12406
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012407 //===--------------------------------------------------------------------===//
12408 // Combine some shuffles into subvector extracts and inserts:
12409 //
12410
12411 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
12412 if (isShuffleHigh128VectorInsertLow(SVOp)) {
12413 SDValue V = Extract128BitVector(V1, DAG.getConstant(NumElems/2, MVT::i32),
12414 DAG, dl);
12415 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT),
12416 V, DAG.getConstant(0, MVT::i32), DAG, dl);
12417 return DCI.CombineTo(N, InsV);
12418 }
12419
12420 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
12421 if (isShuffleLow128VectorInsertHigh(SVOp)) {
12422 SDValue V = Extract128BitVector(V1, DAG.getConstant(0, MVT::i32), DAG, dl);
12423 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT),
12424 V, DAG.getConstant(NumElems/2, MVT::i32), DAG, dl);
12425 return DCI.CombineTo(N, InsV);
12426 }
12427
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012428 return SDValue();
12429}
12430
12431/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000012432static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000012433 TargetLowering::DAGCombinerInfo &DCI,
12434 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000012435 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000012436 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000012437
Mon P Wanga0fd0d52010-12-19 23:55:53 +000012438 // Don't create instructions with illegal types after legalize types has run.
12439 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12440 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
12441 return SDValue();
12442
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000012443 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
12444 if (Subtarget->hasAVX() && VT.getSizeInBits() == 256 &&
12445 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012446 return PerformShuffleCombine256(N, DAG, DCI);
12447
12448 // Only handle 128 wide vector from here on.
12449 if (VT.getSizeInBits() != 128)
12450 return SDValue();
12451
12452 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
12453 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
12454 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000012455 SmallVector<SDValue, 16> Elts;
12456 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012457 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000012458
Nate Begemanfdea31a2010-03-24 20:49:50 +000012459 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000012460}
Evan Chengd880b972008-05-09 21:53:03 +000012461
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000012462/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
12463/// generation and convert it from being a bunch of shuffles and extracts
12464/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012465static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
12466 const TargetLowering &TLI) {
12467 SDValue InputVector = N->getOperand(0);
12468
12469 // Only operate on vectors of 4 elements, where the alternative shuffling
12470 // gets to be more expensive.
12471 if (InputVector.getValueType() != MVT::v4i32)
12472 return SDValue();
12473
12474 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
12475 // single use which is a sign-extend or zero-extend, and all elements are
12476 // used.
12477 SmallVector<SDNode *, 4> Uses;
12478 unsigned ExtractedElements = 0;
12479 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
12480 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
12481 if (UI.getUse().getResNo() != InputVector.getResNo())
12482 return SDValue();
12483
12484 SDNode *Extract = *UI;
12485 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
12486 return SDValue();
12487
12488 if (Extract->getValueType(0) != MVT::i32)
12489 return SDValue();
12490 if (!Extract->hasOneUse())
12491 return SDValue();
12492 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
12493 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
12494 return SDValue();
12495 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
12496 return SDValue();
12497
12498 // Record which element was extracted.
12499 ExtractedElements |=
12500 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
12501
12502 Uses.push_back(Extract);
12503 }
12504
12505 // If not all the elements were used, this may not be worthwhile.
12506 if (ExtractedElements != 15)
12507 return SDValue();
12508
12509 // Ok, we've now decided to do the transformation.
12510 DebugLoc dl = InputVector.getDebugLoc();
12511
12512 // Store the value to a temporary stack slot.
12513 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000012514 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
12515 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012516
12517 // Replace each use (extract) with a load of the appropriate element.
12518 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
12519 UE = Uses.end(); UI != UE; ++UI) {
12520 SDNode *Extract = *UI;
12521
Nadav Rotem86694292011-05-17 08:31:57 +000012522 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012523 SDValue Idx = Extract->getOperand(1);
12524 unsigned EltSize =
12525 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
12526 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
12527 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
12528
Nadav Rotem86694292011-05-17 08:31:57 +000012529 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000012530 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012531
12532 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000012533 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000012534 ScalarAddr, MachinePointerInfo(),
12535 false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012536
12537 // Replace the exact with the load.
12538 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
12539 }
12540
12541 // The replacement was made in place; don't return anything.
12542 return SDValue();
12543}
12544
Chris Lattner83e6c992006-10-04 06:57:07 +000012545/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000012546static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000012547 const X86Subtarget *Subtarget) {
12548 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000012549 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000012550 // Get the LHS/RHS of the select.
12551 SDValue LHS = N->getOperand(1);
12552 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +000012553
Dan Gohman670e5392009-09-21 18:03:22 +000012554 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000012555 // instructions match the semantics of the common C idiom x<y?x:y but not
12556 // x<=y?x:y, because of how they handle negative zero (which can be
12557 // ignored in unsafe-math mode).
Chris Lattner83e6c992006-10-04 06:57:07 +000012558 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +000012559 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +000012560 Cond.getOpcode() == ISD::SETCC) {
12561 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012562
Chris Lattner47b4ce82009-03-11 05:48:52 +000012563 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000012564 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000012565 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
12566 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000012567 switch (CC) {
12568 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000012569 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000012570 // Converting this to a min would handle NaNs incorrectly, and swapping
12571 // the operands would cause it to handle comparisons between positive
12572 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000012573 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000012574 if (!UnsafeFPMath &&
12575 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
12576 break;
12577 std::swap(LHS, RHS);
12578 }
Dan Gohman670e5392009-09-21 18:03:22 +000012579 Opcode = X86ISD::FMIN;
12580 break;
12581 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000012582 // Converting this to a min would handle comparisons between positive
12583 // and negative zero incorrectly.
12584 if (!UnsafeFPMath &&
12585 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
12586 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012587 Opcode = X86ISD::FMIN;
12588 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000012589 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000012590 // Converting this to a min would handle both negative zeros and NaNs
12591 // incorrectly, but we can swap the operands to fix both.
12592 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012593 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012594 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000012595 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012596 Opcode = X86ISD::FMIN;
12597 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012598
Dan Gohman670e5392009-09-21 18:03:22 +000012599 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012600 // Converting this to a max would handle comparisons between positive
12601 // and negative zero incorrectly.
12602 if (!UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000012603 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012604 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012605 Opcode = X86ISD::FMAX;
12606 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000012607 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000012608 // Converting this to a max would handle NaNs incorrectly, and swapping
12609 // the operands would cause it to handle comparisons between positive
12610 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000012611 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000012612 if (!UnsafeFPMath &&
12613 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
12614 break;
12615 std::swap(LHS, RHS);
12616 }
Dan Gohman670e5392009-09-21 18:03:22 +000012617 Opcode = X86ISD::FMAX;
12618 break;
12619 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012620 // Converting this to a max would handle both negative zeros and NaNs
12621 // incorrectly, but we can swap the operands to fix both.
12622 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012623 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012624 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012625 case ISD::SETGE:
12626 Opcode = X86ISD::FMAX;
12627 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000012628 }
Dan Gohman670e5392009-09-21 18:03:22 +000012629 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000012630 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
12631 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000012632 switch (CC) {
12633 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000012634 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012635 // Converting this to a min would handle comparisons between positive
12636 // and negative zero incorrectly, and swapping the operands would
12637 // cause it to handle NaNs incorrectly.
12638 if (!UnsafeFPMath &&
12639 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000012640 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012641 break;
12642 std::swap(LHS, RHS);
12643 }
Dan Gohman670e5392009-09-21 18:03:22 +000012644 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000012645 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012646 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000012647 // Converting this to a min would handle NaNs incorrectly.
12648 if (!UnsafeFPMath &&
12649 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
12650 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012651 Opcode = X86ISD::FMIN;
12652 break;
12653 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012654 // Converting this to a min would handle both negative zeros and NaNs
12655 // incorrectly, but we can swap the operands to fix both.
12656 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012657 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012658 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012659 case ISD::SETGE:
12660 Opcode = X86ISD::FMIN;
12661 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012662
Dan Gohman670e5392009-09-21 18:03:22 +000012663 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000012664 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000012665 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012666 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012667 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000012668 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012669 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000012670 // Converting this to a max would handle comparisons between positive
12671 // and negative zero incorrectly, and swapping the operands would
12672 // cause it to handle NaNs incorrectly.
12673 if (!UnsafeFPMath &&
12674 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000012675 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012676 break;
12677 std::swap(LHS, RHS);
12678 }
Dan Gohman670e5392009-09-21 18:03:22 +000012679 Opcode = X86ISD::FMAX;
12680 break;
12681 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000012682 // Converting this to a max would handle both negative zeros and NaNs
12683 // incorrectly, but we can swap the operands to fix both.
12684 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012685 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012686 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000012687 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012688 Opcode = X86ISD::FMAX;
12689 break;
12690 }
Chris Lattner83e6c992006-10-04 06:57:07 +000012691 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012692
Chris Lattner47b4ce82009-03-11 05:48:52 +000012693 if (Opcode)
12694 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000012695 }
Eric Christopherfd179292009-08-27 18:07:15 +000012696
Chris Lattnerd1980a52009-03-12 06:52:53 +000012697 // If this is a select between two integer constants, try to do some
12698 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000012699 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
12700 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000012701 // Don't do this for crazy integer types.
12702 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
12703 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000012704 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000012705 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000012706
Chris Lattnercee56e72009-03-13 05:53:31 +000012707 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000012708 // Efficiently invertible.
12709 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
12710 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
12711 isa<ConstantSDNode>(Cond.getOperand(1))))) {
12712 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000012713 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000012714 }
Eric Christopherfd179292009-08-27 18:07:15 +000012715
Chris Lattnerd1980a52009-03-12 06:52:53 +000012716 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000012717 if (FalseC->getAPIntValue() == 0 &&
12718 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000012719 if (NeedsCondInvert) // Invert the condition if needed.
12720 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
12721 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000012722
Chris Lattnerd1980a52009-03-12 06:52:53 +000012723 // Zero extend the condition if needed.
12724 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000012725
Chris Lattnercee56e72009-03-13 05:53:31 +000012726 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000012727 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000012728 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000012729 }
Eric Christopherfd179292009-08-27 18:07:15 +000012730
Chris Lattner97a29a52009-03-13 05:22:11 +000012731 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000012732 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000012733 if (NeedsCondInvert) // Invert the condition if needed.
12734 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
12735 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000012736
Chris Lattner97a29a52009-03-13 05:22:11 +000012737 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000012738 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
12739 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000012740 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000012741 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000012742 }
Eric Christopherfd179292009-08-27 18:07:15 +000012743
Chris Lattnercee56e72009-03-13 05:53:31 +000012744 // Optimize cases that will turn into an LEA instruction. This requires
12745 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000012746 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000012747 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000012748 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000012749
Chris Lattnercee56e72009-03-13 05:53:31 +000012750 bool isFastMultiplier = false;
12751 if (Diff < 10) {
12752 switch ((unsigned char)Diff) {
12753 default: break;
12754 case 1: // result = add base, cond
12755 case 2: // result = lea base( , cond*2)
12756 case 3: // result = lea base(cond, cond*2)
12757 case 4: // result = lea base( , cond*4)
12758 case 5: // result = lea base(cond, cond*4)
12759 case 8: // result = lea base( , cond*8)
12760 case 9: // result = lea base(cond, cond*8)
12761 isFastMultiplier = true;
12762 break;
12763 }
12764 }
Eric Christopherfd179292009-08-27 18:07:15 +000012765
Chris Lattnercee56e72009-03-13 05:53:31 +000012766 if (isFastMultiplier) {
12767 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
12768 if (NeedsCondInvert) // Invert the condition if needed.
12769 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
12770 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000012771
Chris Lattnercee56e72009-03-13 05:53:31 +000012772 // Zero extend the condition if needed.
12773 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
12774 Cond);
12775 // Scale the condition by the difference.
12776 if (Diff != 1)
12777 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
12778 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000012779
Chris Lattnercee56e72009-03-13 05:53:31 +000012780 // Add the base if non-zero.
12781 if (FalseC->getAPIntValue() != 0)
12782 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
12783 SDValue(FalseC, 0));
12784 return Cond;
12785 }
Eric Christopherfd179292009-08-27 18:07:15 +000012786 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000012787 }
12788 }
Eric Christopherfd179292009-08-27 18:07:15 +000012789
Dan Gohman475871a2008-07-27 21:46:04 +000012790 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000012791}
12792
Chris Lattnerd1980a52009-03-12 06:52:53 +000012793/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
12794static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
12795 TargetLowering::DAGCombinerInfo &DCI) {
12796 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000012797
Chris Lattnerd1980a52009-03-12 06:52:53 +000012798 // If the flag operand isn't dead, don't touch this CMOV.
12799 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
12800 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000012801
Evan Chengb5a55d92011-05-24 01:48:22 +000012802 SDValue FalseOp = N->getOperand(0);
12803 SDValue TrueOp = N->getOperand(1);
12804 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
12805 SDValue Cond = N->getOperand(3);
12806 if (CC == X86::COND_E || CC == X86::COND_NE) {
12807 switch (Cond.getOpcode()) {
12808 default: break;
12809 case X86ISD::BSR:
12810 case X86ISD::BSF:
12811 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
12812 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
12813 return (CC == X86::COND_E) ? FalseOp : TrueOp;
12814 }
12815 }
12816
Chris Lattnerd1980a52009-03-12 06:52:53 +000012817 // If this is a select between two integer constants, try to do some
12818 // optimizations. Note that the operands are ordered the opposite of SELECT
12819 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000012820 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
12821 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000012822 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
12823 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000012824 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
12825 CC = X86::GetOppositeBranchCondition(CC);
12826 std::swap(TrueC, FalseC);
12827 }
Eric Christopherfd179292009-08-27 18:07:15 +000012828
Chris Lattnerd1980a52009-03-12 06:52:53 +000012829 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000012830 // This is efficient for any integer data type (including i8/i16) and
12831 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000012832 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012833 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
12834 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000012835
Chris Lattnerd1980a52009-03-12 06:52:53 +000012836 // Zero extend the condition if needed.
12837 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000012838
Chris Lattnerd1980a52009-03-12 06:52:53 +000012839 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
12840 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000012841 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000012842 if (N->getNumValues() == 2) // Dead flag value?
12843 return DCI.CombineTo(N, Cond, SDValue());
12844 return Cond;
12845 }
Eric Christopherfd179292009-08-27 18:07:15 +000012846
Chris Lattnercee56e72009-03-13 05:53:31 +000012847 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
12848 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000012849 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012850 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
12851 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000012852
Chris Lattner97a29a52009-03-13 05:22:11 +000012853 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000012854 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
12855 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000012856 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
12857 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000012858
Chris Lattner97a29a52009-03-13 05:22:11 +000012859 if (N->getNumValues() == 2) // Dead flag value?
12860 return DCI.CombineTo(N, Cond, SDValue());
12861 return Cond;
12862 }
Eric Christopherfd179292009-08-27 18:07:15 +000012863
Chris Lattnercee56e72009-03-13 05:53:31 +000012864 // Optimize cases that will turn into an LEA instruction. This requires
12865 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000012866 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000012867 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000012868 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000012869
Chris Lattnercee56e72009-03-13 05:53:31 +000012870 bool isFastMultiplier = false;
12871 if (Diff < 10) {
12872 switch ((unsigned char)Diff) {
12873 default: break;
12874 case 1: // result = add base, cond
12875 case 2: // result = lea base( , cond*2)
12876 case 3: // result = lea base(cond, cond*2)
12877 case 4: // result = lea base( , cond*4)
12878 case 5: // result = lea base(cond, cond*4)
12879 case 8: // result = lea base( , cond*8)
12880 case 9: // result = lea base(cond, cond*8)
12881 isFastMultiplier = true;
12882 break;
12883 }
12884 }
Eric Christopherfd179292009-08-27 18:07:15 +000012885
Chris Lattnercee56e72009-03-13 05:53:31 +000012886 if (isFastMultiplier) {
12887 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000012888 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
12889 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000012890 // Zero extend the condition if needed.
12891 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
12892 Cond);
12893 // Scale the condition by the difference.
12894 if (Diff != 1)
12895 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
12896 DAG.getConstant(Diff, Cond.getValueType()));
12897
12898 // Add the base if non-zero.
12899 if (FalseC->getAPIntValue() != 0)
12900 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
12901 SDValue(FalseC, 0));
12902 if (N->getNumValues() == 2) // Dead flag value?
12903 return DCI.CombineTo(N, Cond, SDValue());
12904 return Cond;
12905 }
Eric Christopherfd179292009-08-27 18:07:15 +000012906 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000012907 }
12908 }
12909 return SDValue();
12910}
12911
12912
Evan Cheng0b0cd912009-03-28 05:57:29 +000012913/// PerformMulCombine - Optimize a single multiply with constant into two
12914/// in order to implement it with two cheaper instructions, e.g.
12915/// LEA + SHL, LEA + LEA.
12916static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
12917 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000012918 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
12919 return SDValue();
12920
Owen Andersone50ed302009-08-10 22:56:29 +000012921 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012922 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000012923 return SDValue();
12924
12925 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
12926 if (!C)
12927 return SDValue();
12928 uint64_t MulAmt = C->getZExtValue();
12929 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
12930 return SDValue();
12931
12932 uint64_t MulAmt1 = 0;
12933 uint64_t MulAmt2 = 0;
12934 if ((MulAmt % 9) == 0) {
12935 MulAmt1 = 9;
12936 MulAmt2 = MulAmt / 9;
12937 } else if ((MulAmt % 5) == 0) {
12938 MulAmt1 = 5;
12939 MulAmt2 = MulAmt / 5;
12940 } else if ((MulAmt % 3) == 0) {
12941 MulAmt1 = 3;
12942 MulAmt2 = MulAmt / 3;
12943 }
12944 if (MulAmt2 &&
12945 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
12946 DebugLoc DL = N->getDebugLoc();
12947
12948 if (isPowerOf2_64(MulAmt2) &&
12949 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
12950 // If second multiplifer is pow2, issue it first. We want the multiply by
12951 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
12952 // is an add.
12953 std::swap(MulAmt1, MulAmt2);
12954
12955 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000012956 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000012957 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000012958 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000012959 else
Evan Cheng73f24c92009-03-30 21:36:47 +000012960 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000012961 DAG.getConstant(MulAmt1, VT));
12962
Eric Christopherfd179292009-08-27 18:07:15 +000012963 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000012964 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000012965 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000012966 else
Evan Cheng73f24c92009-03-30 21:36:47 +000012967 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000012968 DAG.getConstant(MulAmt2, VT));
12969
12970 // Do not add new nodes to DAG combiner worklist.
12971 DCI.CombineTo(N, NewMul, false);
12972 }
12973 return SDValue();
12974}
12975
Evan Chengad9c0a32009-12-15 00:53:42 +000012976static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
12977 SDValue N0 = N->getOperand(0);
12978 SDValue N1 = N->getOperand(1);
12979 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
12980 EVT VT = N0.getValueType();
12981
12982 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
12983 // since the result of setcc_c is all zero's or all ones.
12984 if (N1C && N0.getOpcode() == ISD::AND &&
12985 N0.getOperand(1).getOpcode() == ISD::Constant) {
12986 SDValue N00 = N0.getOperand(0);
12987 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
12988 ((N00.getOpcode() == ISD::ANY_EXTEND ||
12989 N00.getOpcode() == ISD::ZERO_EXTEND) &&
12990 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
12991 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
12992 APInt ShAmt = N1C->getAPIntValue();
12993 Mask = Mask.shl(ShAmt);
12994 if (Mask != 0)
12995 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
12996 N00, DAG.getConstant(Mask, VT));
12997 }
12998 }
12999
13000 return SDValue();
13001}
Evan Cheng0b0cd912009-03-28 05:57:29 +000013002
Nate Begeman740ab032009-01-26 00:52:55 +000013003/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
13004/// when possible.
13005static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
13006 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000013007 EVT VT = N->getValueType(0);
13008 if (!VT.isVector() && VT.isInteger() &&
13009 N->getOpcode() == ISD::SHL)
13010 return PerformSHLCombine(N, DAG);
13011
Nate Begeman740ab032009-01-26 00:52:55 +000013012 // On X86 with SSE2 support, we can transform this to a vector shift if
13013 // all elements are shifted by the same amount. We can't do this in legalize
13014 // because the a constant vector is typically transformed to a constant pool
13015 // so we have no knowledge of the shift amount.
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +000013016 if (!(Subtarget->hasSSE2() || Subtarget->hasAVX()))
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013017 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000013018
Owen Anderson825b72b2009-08-11 20:47:22 +000013019 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013020 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000013021
Mon P Wang3becd092009-01-28 08:12:05 +000013022 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000013023 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000013024 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000013025 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000013026 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
13027 unsigned NumElts = VT.getVectorNumElements();
13028 unsigned i = 0;
13029 for (; i != NumElts; ++i) {
13030 SDValue Arg = ShAmtOp.getOperand(i);
13031 if (Arg.getOpcode() == ISD::UNDEF) continue;
13032 BaseShAmt = Arg;
13033 break;
13034 }
13035 for (; i != NumElts; ++i) {
13036 SDValue Arg = ShAmtOp.getOperand(i);
13037 if (Arg.getOpcode() == ISD::UNDEF) continue;
13038 if (Arg != BaseShAmt) {
13039 return SDValue();
13040 }
13041 }
13042 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000013043 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000013044 SDValue InVec = ShAmtOp.getOperand(0);
13045 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
13046 unsigned NumElts = InVec.getValueType().getVectorNumElements();
13047 unsigned i = 0;
13048 for (; i != NumElts; ++i) {
13049 SDValue Arg = InVec.getOperand(i);
13050 if (Arg.getOpcode() == ISD::UNDEF) continue;
13051 BaseShAmt = Arg;
13052 break;
13053 }
13054 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
13055 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000013056 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000013057 if (C->getZExtValue() == SplatIdx)
13058 BaseShAmt = InVec.getOperand(1);
13059 }
13060 }
13061 if (BaseShAmt.getNode() == 0)
13062 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
13063 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000013064 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013065 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000013066
Mon P Wangefa42202009-09-03 19:56:25 +000013067 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000013068 if (EltVT.bitsGT(MVT::i32))
13069 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
13070 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000013071 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000013072
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013073 // The shift amount is identical so we can do a vector shift.
13074 SDValue ValOp = N->getOperand(0);
13075 switch (N->getOpcode()) {
13076 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000013077 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013078 break;
13079 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000013080 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013081 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013082 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013083 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013084 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013085 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013086 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013087 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013088 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013089 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013090 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013091 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013092 break;
13093 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000013094 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013095 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013096 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013097 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013098 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013099 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013100 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013101 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013102 break;
13103 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000013104 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013105 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013106 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013107 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013108 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013109 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013110 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013111 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013112 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013113 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013114 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013115 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013116 break;
Nate Begeman740ab032009-01-26 00:52:55 +000013117 }
13118 return SDValue();
13119}
13120
Nate Begemanb65c1752010-12-17 22:55:37 +000013121
Stuart Hastings865f0932011-06-03 23:53:54 +000013122// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
13123// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
13124// and friends. Likewise for OR -> CMPNEQSS.
13125static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
13126 TargetLowering::DAGCombinerInfo &DCI,
13127 const X86Subtarget *Subtarget) {
13128 unsigned opcode;
13129
13130 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
13131 // we're requiring SSE2 for both.
13132 if (Subtarget->hasSSE2() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
13133 SDValue N0 = N->getOperand(0);
13134 SDValue N1 = N->getOperand(1);
13135 SDValue CMP0 = N0->getOperand(1);
13136 SDValue CMP1 = N1->getOperand(1);
13137 DebugLoc DL = N->getDebugLoc();
13138
13139 // The SETCCs should both refer to the same CMP.
13140 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
13141 return SDValue();
13142
13143 SDValue CMP00 = CMP0->getOperand(0);
13144 SDValue CMP01 = CMP0->getOperand(1);
13145 EVT VT = CMP00.getValueType();
13146
13147 if (VT == MVT::f32 || VT == MVT::f64) {
13148 bool ExpectingFlags = false;
13149 // Check for any users that want flags:
13150 for (SDNode::use_iterator UI = N->use_begin(),
13151 UE = N->use_end();
13152 !ExpectingFlags && UI != UE; ++UI)
13153 switch (UI->getOpcode()) {
13154 default:
13155 case ISD::BR_CC:
13156 case ISD::BRCOND:
13157 case ISD::SELECT:
13158 ExpectingFlags = true;
13159 break;
13160 case ISD::CopyToReg:
13161 case ISD::SIGN_EXTEND:
13162 case ISD::ZERO_EXTEND:
13163 case ISD::ANY_EXTEND:
13164 break;
13165 }
13166
13167 if (!ExpectingFlags) {
13168 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
13169 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
13170
13171 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
13172 X86::CondCode tmp = cc0;
13173 cc0 = cc1;
13174 cc1 = tmp;
13175 }
13176
13177 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
13178 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
13179 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
13180 X86ISD::NodeType NTOperator = is64BitFP ?
13181 X86ISD::FSETCCsd : X86ISD::FSETCCss;
13182 // FIXME: need symbolic constants for these magic numbers.
13183 // See X86ATTInstPrinter.cpp:printSSECC().
13184 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
13185 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
13186 DAG.getConstant(x86cc, MVT::i8));
13187 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
13188 OnesOrZeroesF);
13189 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
13190 DAG.getConstant(1, MVT::i32));
13191 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
13192 return OneBitOfTruth;
13193 }
13194 }
13195 }
13196 }
13197 return SDValue();
13198}
13199
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013200/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
13201/// so it can be folded inside ANDNP.
13202static bool CanFoldXORWithAllOnes(const SDNode *N) {
13203 EVT VT = N->getValueType(0);
13204
13205 // Match direct AllOnes for 128 and 256-bit vectors
13206 if (ISD::isBuildVectorAllOnes(N))
13207 return true;
13208
13209 // Look through a bit convert.
13210 if (N->getOpcode() == ISD::BITCAST)
13211 N = N->getOperand(0).getNode();
13212
13213 // Sometimes the operand may come from a insert_subvector building a 256-bit
13214 // allones vector
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013215 if (VT.getSizeInBits() == 256 &&
Bill Wendling456a9252011-08-04 00:32:58 +000013216 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
13217 SDValue V1 = N->getOperand(0);
13218 SDValue V2 = N->getOperand(1);
13219
13220 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
13221 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
13222 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
13223 ISD::isBuildVectorAllOnes(V2.getNode()))
13224 return true;
13225 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013226
13227 return false;
13228}
13229
Nate Begemanb65c1752010-12-17 22:55:37 +000013230static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
13231 TargetLowering::DAGCombinerInfo &DCI,
13232 const X86Subtarget *Subtarget) {
13233 if (DCI.isBeforeLegalizeOps())
13234 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013235
Stuart Hastings865f0932011-06-03 23:53:54 +000013236 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
13237 if (R.getNode())
13238 return R;
13239
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000013240 // Want to form ANDNP nodes:
13241 // 1) In the hopes of then easily combining them with OR and AND nodes
13242 // to form PBLEND/PSIGN.
13243 // 2) To match ANDN packed intrinsics
Nate Begemanb65c1752010-12-17 22:55:37 +000013244 EVT VT = N->getValueType(0);
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000013245 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000013246 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013247
Nate Begemanb65c1752010-12-17 22:55:37 +000013248 SDValue N0 = N->getOperand(0);
13249 SDValue N1 = N->getOperand(1);
13250 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013251
Nate Begemanb65c1752010-12-17 22:55:37 +000013252 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013253 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013254 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
13255 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013256 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000013257
13258 // Check RHS for vnot
13259 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013260 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
13261 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013262 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013263
Nate Begemanb65c1752010-12-17 22:55:37 +000013264 return SDValue();
13265}
13266
Evan Cheng760d1942010-01-04 21:22:48 +000013267static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000013268 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000013269 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000013270 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000013271 return SDValue();
13272
Stuart Hastings865f0932011-06-03 23:53:54 +000013273 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
13274 if (R.getNode())
13275 return R;
13276
Evan Cheng760d1942010-01-04 21:22:48 +000013277 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000013278 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64 && VT != MVT::v2i64)
Evan Cheng760d1942010-01-04 21:22:48 +000013279 return SDValue();
13280
Evan Cheng760d1942010-01-04 21:22:48 +000013281 SDValue N0 = N->getOperand(0);
13282 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013283
Nate Begemanb65c1752010-12-17 22:55:37 +000013284 // look for psign/blend
13285 if (Subtarget->hasSSSE3()) {
13286 if (VT == MVT::v2i64) {
13287 // Canonicalize pandn to RHS
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013288 if (N0.getOpcode() == X86ISD::ANDNP)
Nate Begemanb65c1752010-12-17 22:55:37 +000013289 std::swap(N0, N1);
13290 // or (and (m, x), (pandn m, y))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013291 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
Nate Begemanb65c1752010-12-17 22:55:37 +000013292 SDValue Mask = N1.getOperand(0);
13293 SDValue X = N1.getOperand(1);
13294 SDValue Y;
13295 if (N0.getOperand(0) == Mask)
13296 Y = N0.getOperand(1);
13297 if (N0.getOperand(1) == Mask)
13298 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013299
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013300 // Check to see if the mask appeared in both the AND and ANDNP and
Nate Begemanb65c1752010-12-17 22:55:37 +000013301 if (!Y.getNode())
13302 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013303
Nate Begemanb65c1752010-12-17 22:55:37 +000013304 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
13305 if (Mask.getOpcode() != ISD::BITCAST ||
13306 X.getOpcode() != ISD::BITCAST ||
13307 Y.getOpcode() != ISD::BITCAST)
13308 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013309
Nate Begemanb65c1752010-12-17 22:55:37 +000013310 // Look through mask bitcast.
13311 Mask = Mask.getOperand(0);
13312 EVT MaskVT = Mask.getValueType();
13313
13314 // Validate that the Mask operand is a vector sra node. The sra node
13315 // will be an intrinsic.
13316 if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
13317 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013318
Nate Begemanb65c1752010-12-17 22:55:37 +000013319 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
13320 // there is no psrai.b
13321 switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
13322 case Intrinsic::x86_sse2_psrai_w:
13323 case Intrinsic::x86_sse2_psrai_d:
13324 break;
13325 default: return SDValue();
13326 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013327
Nate Begemanb65c1752010-12-17 22:55:37 +000013328 // Check that the SRA is all signbits.
13329 SDValue SraC = Mask.getOperand(2);
13330 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
13331 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
13332 if ((SraAmt + 1) != EltBits)
13333 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013334
Nate Begemanb65c1752010-12-17 22:55:37 +000013335 DebugLoc DL = N->getDebugLoc();
13336
13337 // Now we know we at least have a plendvb with the mask val. See if
13338 // we can form a psignb/w/d.
13339 // psign = x.type == y.type == mask.type && y = sub(0, x);
13340 X = X.getOperand(0);
13341 Y = Y.getOperand(0);
13342 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
13343 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
13344 X.getValueType() == MaskVT && X.getValueType() == Y.getValueType()){
13345 unsigned Opc = 0;
13346 switch (EltBits) {
13347 case 8: Opc = X86ISD::PSIGNB; break;
13348 case 16: Opc = X86ISD::PSIGNW; break;
13349 case 32: Opc = X86ISD::PSIGND; break;
13350 default: break;
13351 }
13352 if (Opc) {
13353 SDValue Sign = DAG.getNode(Opc, DL, MaskVT, X, Mask.getOperand(1));
13354 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Sign);
13355 }
13356 }
13357 // PBLENDVB only available on SSE 4.1
13358 if (!Subtarget->hasSSE41())
13359 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013360
Nate Begemanb65c1752010-12-17 22:55:37 +000013361 X = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, X);
13362 Y = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Y);
13363 Mask = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Mask);
Nate Begeman672fb622010-12-20 22:04:24 +000013364 Mask = DAG.getNode(X86ISD::PBLENDVB, DL, MVT::v16i8, X, Y, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000013365 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Mask);
13366 }
13367 }
13368 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013369
Nate Begemanb65c1752010-12-17 22:55:37 +000013370 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000013371 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
13372 std::swap(N0, N1);
13373 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
13374 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000013375 if (!N0.hasOneUse() || !N1.hasOneUse())
13376 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000013377
13378 SDValue ShAmt0 = N0.getOperand(1);
13379 if (ShAmt0.getValueType() != MVT::i8)
13380 return SDValue();
13381 SDValue ShAmt1 = N1.getOperand(1);
13382 if (ShAmt1.getValueType() != MVT::i8)
13383 return SDValue();
13384 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
13385 ShAmt0 = ShAmt0.getOperand(0);
13386 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
13387 ShAmt1 = ShAmt1.getOperand(0);
13388
13389 DebugLoc DL = N->getDebugLoc();
13390 unsigned Opc = X86ISD::SHLD;
13391 SDValue Op0 = N0.getOperand(0);
13392 SDValue Op1 = N1.getOperand(0);
13393 if (ShAmt0.getOpcode() == ISD::SUB) {
13394 Opc = X86ISD::SHRD;
13395 std::swap(Op0, Op1);
13396 std::swap(ShAmt0, ShAmt1);
13397 }
13398
Evan Cheng8b1190a2010-04-28 01:18:01 +000013399 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000013400 if (ShAmt1.getOpcode() == ISD::SUB) {
13401 SDValue Sum = ShAmt1.getOperand(0);
13402 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000013403 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
13404 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
13405 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
13406 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000013407 return DAG.getNode(Opc, DL, VT,
13408 Op0, Op1,
13409 DAG.getNode(ISD::TRUNCATE, DL,
13410 MVT::i8, ShAmt0));
13411 }
13412 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
13413 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
13414 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000013415 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000013416 return DAG.getNode(Opc, DL, VT,
13417 N0.getOperand(0), N1.getOperand(0),
13418 DAG.getNode(ISD::TRUNCATE, DL,
13419 MVT::i8, ShAmt0));
13420 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013421
Evan Cheng760d1942010-01-04 21:22:48 +000013422 return SDValue();
13423}
13424
Chris Lattner149a4e52008-02-22 02:09:43 +000013425/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000013426static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000013427 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000013428 StoreSDNode *St = cast<StoreSDNode>(N);
13429 EVT VT = St->getValue().getValueType();
13430 EVT StVT = St->getMemoryVT();
13431 DebugLoc dl = St->getDebugLoc();
Nadav Rotem5e742a32011-08-11 16:41:21 +000013432 SDValue StoredVal = St->getOperand(1);
13433 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13434
13435 // If we are saving a concatination of two XMM registers, perform two stores.
Nadav Rotem6236f7f2011-08-11 17:05:47 +000013436 // This is better in Sandy Bridge cause one 256-bit mem op is done via two
13437 // 128-bit ones. If in the future the cost becomes only one memory access the
13438 // first version would be better.
Nadav Rotem5e742a32011-08-11 16:41:21 +000013439 if (VT.getSizeInBits() == 256 &&
13440 StoredVal.getNode()->getOpcode() == ISD::CONCAT_VECTORS &&
13441 StoredVal.getNumOperands() == 2) {
13442
13443 SDValue Value0 = StoredVal.getOperand(0);
13444 SDValue Value1 = StoredVal.getOperand(1);
13445
13446 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
13447 SDValue Ptr0 = St->getBasePtr();
13448 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
13449
13450 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
13451 St->getPointerInfo(), St->isVolatile(),
13452 St->isNonTemporal(), St->getAlignment());
13453 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
13454 St->getPointerInfo(), St->isVolatile(),
13455 St->isNonTemporal(), St->getAlignment());
13456 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
13457 }
Nadav Rotem614061b2011-08-10 19:30:14 +000013458
13459 // Optimize trunc store (of multiple scalars) to shuffle and store.
13460 // First, pack all of the elements in one place. Next, store to memory
13461 // in fewer chunks.
13462 if (St->isTruncatingStore() && VT.isVector()) {
13463 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13464 unsigned NumElems = VT.getVectorNumElements();
13465 assert(StVT != VT && "Cannot truncate to the same type");
13466 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
13467 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
13468
13469 // From, To sizes and ElemCount must be pow of two
13470 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
13471 // We are going to use the original vector elt for storing.
13472 // accumulated smaller vector elements must be a multiple of bigger size.
13473 if (0 != (NumElems * ToSz) % FromSz) return SDValue();
13474 unsigned SizeRatio = FromSz / ToSz;
13475
13476 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
13477
13478 // Create a type on which we perform the shuffle
13479 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
13480 StVT.getScalarType(), NumElems*SizeRatio);
13481
13482 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
13483
13484 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
13485 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
13486 for (unsigned i = 0; i < NumElems; i++ ) ShuffleVec[i] = i * SizeRatio;
13487
13488 // Can't shuffle using an illegal type
13489 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
13490
13491 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
13492 DAG.getUNDEF(WideVec.getValueType()),
13493 ShuffleVec.data());
13494 // At this point all of the data is stored at the bottom of the
13495 // register. We now need to save it to mem.
13496
13497 // Find the largest store unit
13498 MVT StoreType = MVT::i8;
13499 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
13500 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
13501 MVT Tp = (MVT::SimpleValueType)tp;
13502 if (TLI.isTypeLegal(Tp) && StoreType.getSizeInBits() < NumElems * ToSz)
13503 StoreType = Tp;
13504 }
13505
13506 // Bitcast the original vector into a vector of store-size units
13507 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
13508 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
13509 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
13510 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
13511 SmallVector<SDValue, 8> Chains;
13512 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
13513 TLI.getPointerTy());
13514 SDValue Ptr = St->getBasePtr();
13515
13516 // Perform one or more big stores into memory.
13517 for (unsigned i = 0; i < (ToSz*NumElems)/StoreType.getSizeInBits() ; i++) {
13518 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
13519 StoreType, ShuffWide,
13520 DAG.getIntPtrConstant(i));
13521 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
13522 St->getPointerInfo(), St->isVolatile(),
13523 St->isNonTemporal(), St->getAlignment());
13524 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
13525 Chains.push_back(Ch);
13526 }
13527
13528 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
13529 Chains.size());
13530 }
13531
13532
Chris Lattner149a4e52008-02-22 02:09:43 +000013533 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
13534 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000013535 // A preferable solution to the general problem is to figure out the right
13536 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000013537
13538 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000013539 if (VT.getSizeInBits() != 64)
13540 return SDValue();
13541
Devang Patel578efa92009-06-05 21:57:13 +000013542 const Function *F = DAG.getMachineFunction().getFunction();
13543 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +000013544 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +000013545 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000013546 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000013547 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000013548 isa<LoadSDNode>(St->getValue()) &&
13549 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
13550 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000013551 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000013552 LoadSDNode *Ld = 0;
13553 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000013554 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000013555 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000013556 // Must be a store of a load. We currently handle two cases: the load
13557 // is a direct child, and it's under an intervening TokenFactor. It is
13558 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000013559 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000013560 Ld = cast<LoadSDNode>(St->getChain());
13561 else if (St->getValue().hasOneUse() &&
13562 ChainVal->getOpcode() == ISD::TokenFactor) {
13563 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000013564 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000013565 TokenFactorIndex = i;
13566 Ld = cast<LoadSDNode>(St->getValue());
13567 } else
13568 Ops.push_back(ChainVal->getOperand(i));
13569 }
13570 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000013571
Evan Cheng536e6672009-03-12 05:59:15 +000013572 if (!Ld || !ISD::isNormalLoad(Ld))
13573 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000013574
Evan Cheng536e6672009-03-12 05:59:15 +000013575 // If this is not the MMX case, i.e. we are just turning i64 load/store
13576 // into f64 load/store, avoid the transformation if there are multiple
13577 // uses of the loaded value.
13578 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
13579 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000013580
Evan Cheng536e6672009-03-12 05:59:15 +000013581 DebugLoc LdDL = Ld->getDebugLoc();
13582 DebugLoc StDL = N->getDebugLoc();
13583 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
13584 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
13585 // pair instead.
13586 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000013587 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000013588 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
13589 Ld->getPointerInfo(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000013590 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000013591 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000013592 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000013593 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000013594 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000013595 Ops.size());
13596 }
Evan Cheng536e6672009-03-12 05:59:15 +000013597 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000013598 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000013599 St->isVolatile(), St->isNonTemporal(),
13600 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000013601 }
Evan Cheng536e6672009-03-12 05:59:15 +000013602
13603 // Otherwise, lower to two pairs of 32-bit loads / stores.
13604 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000013605 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
13606 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000013607
Owen Anderson825b72b2009-08-11 20:47:22 +000013608 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000013609 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000013610 Ld->isVolatile(), Ld->isNonTemporal(),
13611 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000013612 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000013613 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000013614 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000013615 MinAlign(Ld->getAlignment(), 4));
13616
13617 SDValue NewChain = LoLd.getValue(1);
13618 if (TokenFactorIndex != -1) {
13619 Ops.push_back(LoLd);
13620 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000013621 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000013622 Ops.size());
13623 }
13624
13625 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000013626 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
13627 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000013628
13629 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000013630 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000013631 St->isVolatile(), St->isNonTemporal(),
13632 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000013633 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000013634 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000013635 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000013636 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000013637 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000013638 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000013639 }
Dan Gohman475871a2008-07-27 21:46:04 +000013640 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000013641}
13642
Chris Lattner6cf73262008-01-25 06:14:17 +000013643/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
13644/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000013645static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000013646 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
13647 // F[X]OR(0.0, x) -> x
13648 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000013649 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
13650 if (C->getValueAPF().isPosZero())
13651 return N->getOperand(1);
13652 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
13653 if (C->getValueAPF().isPosZero())
13654 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000013655 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000013656}
13657
13658/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000013659static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000013660 // FAND(0.0, x) -> 0.0
13661 // FAND(x, 0.0) -> 0.0
13662 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
13663 if (C->getValueAPF().isPosZero())
13664 return N->getOperand(0);
13665 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
13666 if (C->getValueAPF().isPosZero())
13667 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000013668 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000013669}
13670
Dan Gohmane5af2d32009-01-29 01:59:02 +000013671static SDValue PerformBTCombine(SDNode *N,
13672 SelectionDAG &DAG,
13673 TargetLowering::DAGCombinerInfo &DCI) {
13674 // BT ignores high bits in the bit index operand.
13675 SDValue Op1 = N->getOperand(1);
13676 if (Op1.hasOneUse()) {
13677 unsigned BitWidth = Op1.getValueSizeInBits();
13678 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
13679 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000013680 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
13681 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000013682 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000013683 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
13684 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
13685 DCI.CommitTargetLoweringOpt(TLO);
13686 }
13687 return SDValue();
13688}
Chris Lattner83e6c992006-10-04 06:57:07 +000013689
Eli Friedman7a5e5552009-06-07 06:52:44 +000013690static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
13691 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000013692 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000013693 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000013694 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000013695 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000013696 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000013697 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000013698 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000013699 }
13700 return SDValue();
13701}
13702
Evan Cheng2e489c42009-12-16 00:53:11 +000013703static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
13704 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
13705 // (and (i32 x86isd::setcc_carry), 1)
13706 // This eliminates the zext. This transformation is necessary because
13707 // ISD::SETCC is always legalized to i8.
13708 DebugLoc dl = N->getDebugLoc();
13709 SDValue N0 = N->getOperand(0);
13710 EVT VT = N->getValueType(0);
13711 if (N0.getOpcode() == ISD::AND &&
13712 N0.hasOneUse() &&
13713 N0.getOperand(0).hasOneUse()) {
13714 SDValue N00 = N0.getOperand(0);
13715 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
13716 return SDValue();
13717 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
13718 if (!C || C->getZExtValue() != 1)
13719 return SDValue();
13720 return DAG.getNode(ISD::AND, dl, VT,
13721 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
13722 N00.getOperand(0), N00.getOperand(1)),
13723 DAG.getConstant(1, VT));
13724 }
13725
13726 return SDValue();
13727}
13728
Chris Lattnerc19d1c32010-12-19 22:08:31 +000013729// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
13730static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
13731 unsigned X86CC = N->getConstantOperandVal(0);
13732 SDValue EFLAG = N->getOperand(1);
13733 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013734
Chris Lattnerc19d1c32010-12-19 22:08:31 +000013735 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
13736 // a zext and produces an all-ones bit which is more useful than 0/1 in some
13737 // cases.
13738 if (X86CC == X86::COND_B)
13739 return DAG.getNode(ISD::AND, DL, MVT::i8,
13740 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
13741 DAG.getConstant(X86CC, MVT::i8), EFLAG),
13742 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013743
Chris Lattnerc19d1c32010-12-19 22:08:31 +000013744 return SDValue();
13745}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013746
Benjamin Kramer1396c402011-06-18 11:09:41 +000013747static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
13748 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000013749 SDValue Op0 = N->getOperand(0);
13750 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
13751 // a 32-bit target where SSE doesn't support i64->FP operations.
13752 if (Op0.getOpcode() == ISD::LOAD) {
13753 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
13754 EVT VT = Ld->getValueType(0);
13755 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
13756 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
13757 !XTLI->getSubtarget()->is64Bit() &&
13758 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000013759 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
13760 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000013761 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
13762 return FILDChain;
13763 }
13764 }
13765 return SDValue();
13766}
13767
Chris Lattner23a01992010-12-20 01:37:09 +000013768// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
13769static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
13770 X86TargetLowering::DAGCombinerInfo &DCI) {
13771 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
13772 // the result is either zero or one (depending on the input carry bit).
13773 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
13774 if (X86::isZeroNode(N->getOperand(0)) &&
13775 X86::isZeroNode(N->getOperand(1)) &&
13776 // We don't have a good way to replace an EFLAGS use, so only do this when
13777 // dead right now.
13778 SDValue(N, 1).use_empty()) {
13779 DebugLoc DL = N->getDebugLoc();
13780 EVT VT = N->getValueType(0);
13781 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
13782 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
13783 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
13784 DAG.getConstant(X86::COND_B,MVT::i8),
13785 N->getOperand(2)),
13786 DAG.getConstant(1, VT));
13787 return DCI.CombineTo(N, Res1, CarryOut);
13788 }
13789
13790 return SDValue();
13791}
13792
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000013793// fold (add Y, (sete X, 0)) -> adc 0, Y
13794// (add Y, (setne X, 0)) -> sbb -1, Y
13795// (sub (sete X, 0), Y) -> sbb 0, Y
13796// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000013797static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000013798 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013799
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000013800 // Look through ZExts.
13801 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
13802 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
13803 return SDValue();
13804
13805 SDValue SetCC = Ext.getOperand(0);
13806 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
13807 return SDValue();
13808
13809 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
13810 if (CC != X86::COND_E && CC != X86::COND_NE)
13811 return SDValue();
13812
13813 SDValue Cmp = SetCC.getOperand(1);
13814 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000013815 !X86::isZeroNode(Cmp.getOperand(1)) ||
13816 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000013817 return SDValue();
13818
13819 SDValue CmpOp0 = Cmp.getOperand(0);
13820 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
13821 DAG.getConstant(1, CmpOp0.getValueType()));
13822
13823 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
13824 if (CC == X86::COND_NE)
13825 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
13826 DL, OtherVal.getValueType(), OtherVal,
13827 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
13828 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
13829 DL, OtherVal.getValueType(), OtherVal,
13830 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
13831}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000013832
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000013833static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG) {
13834 SDValue Op0 = N->getOperand(0);
13835 SDValue Op1 = N->getOperand(1);
13836
13837 // X86 can't encode an immediate LHS of a sub. See if we can push the
13838 // negation into a preceding instruction.
13839 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000013840 // If the RHS of the sub is a XOR with one use and a constant, invert the
13841 // immediate. Then add one to the LHS of the sub so we can turn
13842 // X-Y -> X+~Y+1, saving one register.
13843 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
13844 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000013845 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000013846 EVT VT = Op0.getValueType();
13847 SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
13848 Op1.getOperand(0),
13849 DAG.getConstant(~XorC, VT));
13850 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000013851 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000013852 }
13853 }
13854
13855 return OptimizeConditionalInDecrement(N, DAG);
13856}
13857
Dan Gohman475871a2008-07-27 21:46:04 +000013858SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000013859 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000013860 SelectionDAG &DAG = DCI.DAG;
13861 switch (N->getOpcode()) {
13862 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013863 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000013864 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +000013865 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000013866 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000013867 case ISD::ADD: return OptimizeConditionalInDecrement(N, DAG);
13868 case ISD::SUB: return PerformSubCombine(N, DAG);
Chris Lattner23a01992010-12-20 01:37:09 +000013869 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000013870 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000013871 case ISD::SHL:
13872 case ISD::SRA:
13873 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000013874 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000013875 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000013876 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000013877 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Chris Lattner6cf73262008-01-25 06:14:17 +000013878 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000013879 case X86ISD::FOR: return PerformFORCombine(N, DAG);
13880 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000013881 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000013882 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000013883 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000013884 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000013885 case X86ISD::SHUFPS: // Handle all target specific shuffles
13886 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000013887 case X86ISD::PALIGN:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000013888 case X86ISD::PUNPCKHBW:
13889 case X86ISD::PUNPCKHWD:
13890 case X86ISD::PUNPCKHDQ:
13891 case X86ISD::PUNPCKHQDQ:
13892 case X86ISD::UNPCKHPS:
13893 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +000013894 case X86ISD::VUNPCKHPSY:
13895 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000013896 case X86ISD::PUNPCKLBW:
13897 case X86ISD::PUNPCKLWD:
13898 case X86ISD::PUNPCKLDQ:
13899 case X86ISD::PUNPCKLQDQ:
13900 case X86ISD::UNPCKLPS:
13901 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +000013902 case X86ISD::VUNPCKLPSY:
13903 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000013904 case X86ISD::MOVHLPS:
13905 case X86ISD::MOVLHPS:
13906 case X86ISD::PSHUFD:
13907 case X86ISD::PSHUFHW:
13908 case X86ISD::PSHUFLW:
13909 case X86ISD::MOVSS:
13910 case X86ISD::MOVSD:
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +000013911 case X86ISD::VPERMILPS:
13912 case X86ISD::VPERMILPSY:
13913 case X86ISD::VPERMILPD:
13914 case X86ISD::VPERMILPDY:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +000013915 case X86ISD::VPERM2F128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000013916 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000013917 }
13918
Dan Gohman475871a2008-07-27 21:46:04 +000013919 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000013920}
13921
Evan Chenge5b51ac2010-04-17 06:13:15 +000013922/// isTypeDesirableForOp - Return true if the target has native support for
13923/// the specified value type and it is 'desirable' to use the type for the
13924/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
13925/// instruction encodings are longer and some i16 instructions are slow.
13926bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
13927 if (!isTypeLegal(VT))
13928 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000013929 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000013930 return true;
13931
13932 switch (Opc) {
13933 default:
13934 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000013935 case ISD::LOAD:
13936 case ISD::SIGN_EXTEND:
13937 case ISD::ZERO_EXTEND:
13938 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000013939 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000013940 case ISD::SRL:
13941 case ISD::SUB:
13942 case ISD::ADD:
13943 case ISD::MUL:
13944 case ISD::AND:
13945 case ISD::OR:
13946 case ISD::XOR:
13947 return false;
13948 }
13949}
13950
13951/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000013952/// beneficial for dag combiner to promote the specified node. If true, it
13953/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000013954bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000013955 EVT VT = Op.getValueType();
13956 if (VT != MVT::i16)
13957 return false;
13958
Evan Cheng4c26e932010-04-19 19:29:22 +000013959 bool Promote = false;
13960 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000013961 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000013962 default: break;
13963 case ISD::LOAD: {
13964 LoadSDNode *LD = cast<LoadSDNode>(Op);
13965 // If the non-extending load has a single use and it's not live out, then it
13966 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000013967 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
13968 Op.hasOneUse()*/) {
13969 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
13970 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
13971 // The only case where we'd want to promote LOAD (rather then it being
13972 // promoted as an operand is when it's only use is liveout.
13973 if (UI->getOpcode() != ISD::CopyToReg)
13974 return false;
13975 }
13976 }
Evan Cheng4c26e932010-04-19 19:29:22 +000013977 Promote = true;
13978 break;
13979 }
13980 case ISD::SIGN_EXTEND:
13981 case ISD::ZERO_EXTEND:
13982 case ISD::ANY_EXTEND:
13983 Promote = true;
13984 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000013985 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000013986 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000013987 SDValue N0 = Op.getOperand(0);
13988 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000013989 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000013990 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000013991 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000013992 break;
13993 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000013994 case ISD::ADD:
13995 case ISD::MUL:
13996 case ISD::AND:
13997 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000013998 case ISD::XOR:
13999 Commute = true;
14000 // fallthrough
14001 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000014002 SDValue N0 = Op.getOperand(0);
14003 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000014004 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014005 return false;
14006 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000014007 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014008 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000014009 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014010 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000014011 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014012 }
14013 }
14014
14015 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000014016 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014017}
14018
Evan Cheng60c07e12006-07-05 22:17:51 +000014019//===----------------------------------------------------------------------===//
14020// X86 Inline Assembly Support
14021//===----------------------------------------------------------------------===//
14022
Chris Lattnerb8105652009-07-20 17:51:36 +000014023bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
14024 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000014025
14026 std::string AsmStr = IA->getAsmString();
14027
14028 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000014029 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000014030 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000014031
14032 switch (AsmPieces.size()) {
14033 default: return false;
14034 case 1:
14035 AsmStr = AsmPieces[0];
14036 AsmPieces.clear();
14037 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
14038
Chris Lattner7a2bdde2011-04-15 05:18:47 +000014039 // FIXME: this should verify that we are targeting a 486 or better. If not,
Evan Cheng55d42002011-01-08 01:24:27 +000014040 // we will turn this bswap into something that will be lowered to logical ops
14041 // instead of emitting the bswap asm. For now, we don't support 486 or lower
14042 // so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000014043 // bswap $0
14044 if (AsmPieces.size() == 2 &&
14045 (AsmPieces[0] == "bswap" ||
14046 AsmPieces[0] == "bswapq" ||
14047 AsmPieces[0] == "bswapl") &&
14048 (AsmPieces[1] == "$0" ||
14049 AsmPieces[1] == "${0:q}")) {
14050 // No need to check constraints, nothing other than the equivalent of
14051 // "=r,0" would be valid here.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014052 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014053 if (!Ty || Ty->getBitWidth() % 16 != 0)
14054 return false;
14055 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000014056 }
14057 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000014058 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000014059 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000014060 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000014061 AsmPieces[1] == "$$8," &&
14062 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000014063 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
14064 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014065 const std::string &ConstraintsStr = IA->getConstraintString();
14066 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000014067 std::sort(AsmPieces.begin(), AsmPieces.end());
14068 if (AsmPieces.size() == 4 &&
14069 AsmPieces[0] == "~{cc}" &&
14070 AsmPieces[1] == "~{dirflag}" &&
14071 AsmPieces[2] == "~{flags}" &&
14072 AsmPieces[3] == "~{fpsr}") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014073 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014074 if (!Ty || Ty->getBitWidth() % 16 != 0)
14075 return false;
14076 return IntrinsicLowering::LowerToByteSwap(CI);
Dan Gohman0ef701e2010-03-04 19:58:08 +000014077 }
Chris Lattnerb8105652009-07-20 17:51:36 +000014078 }
14079 break;
14080 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000014081 if (CI->getType()->isIntegerTy(32) &&
14082 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
14083 SmallVector<StringRef, 4> Words;
14084 SplitString(AsmPieces[0], Words, " \t,");
14085 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
14086 Words[2] == "${0:w}") {
14087 Words.clear();
14088 SplitString(AsmPieces[1], Words, " \t,");
14089 if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
14090 Words[2] == "$0") {
14091 Words.clear();
14092 SplitString(AsmPieces[2], Words, " \t,");
14093 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
14094 Words[2] == "${0:w}") {
14095 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014096 const std::string &ConstraintsStr = IA->getConstraintString();
14097 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Peter Collingbourne948cf022010-11-13 19:54:30 +000014098 std::sort(AsmPieces.begin(), AsmPieces.end());
14099 if (AsmPieces.size() == 4 &&
14100 AsmPieces[0] == "~{cc}" &&
14101 AsmPieces[1] == "~{dirflag}" &&
14102 AsmPieces[2] == "~{flags}" &&
14103 AsmPieces[3] == "~{fpsr}") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014104 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014105 if (!Ty || Ty->getBitWidth() % 16 != 0)
14106 return false;
14107 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000014108 }
14109 }
14110 }
14111 }
14112 }
Evan Cheng55d42002011-01-08 01:24:27 +000014113
14114 if (CI->getType()->isIntegerTy(64)) {
14115 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
14116 if (Constraints.size() >= 2 &&
14117 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
14118 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
14119 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
14120 SmallVector<StringRef, 4> Words;
14121 SplitString(AsmPieces[0], Words, " \t");
14122 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
Chris Lattnerb8105652009-07-20 17:51:36 +000014123 Words.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014124 SplitString(AsmPieces[1], Words, " \t");
14125 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
14126 Words.clear();
14127 SplitString(AsmPieces[2], Words, " \t,");
14128 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
14129 Words[2] == "%edx") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014130 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014131 if (!Ty || Ty->getBitWidth() % 16 != 0)
14132 return false;
14133 return IntrinsicLowering::LowerToByteSwap(CI);
14134 }
Chris Lattnerb8105652009-07-20 17:51:36 +000014135 }
14136 }
14137 }
14138 }
14139 break;
14140 }
14141 return false;
14142}
14143
14144
14145
Chris Lattnerf4dff842006-07-11 02:54:03 +000014146/// getConstraintType - Given a constraint letter, return the type of
14147/// constraint it is for this target.
14148X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000014149X86TargetLowering::getConstraintType(const std::string &Constraint) const {
14150 if (Constraint.size() == 1) {
14151 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000014152 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000014153 case 'q':
14154 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000014155 case 'f':
14156 case 't':
14157 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000014158 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000014159 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000014160 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000014161 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000014162 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000014163 case 'a':
14164 case 'b':
14165 case 'c':
14166 case 'd':
14167 case 'S':
14168 case 'D':
14169 case 'A':
14170 return C_Register;
14171 case 'I':
14172 case 'J':
14173 case 'K':
14174 case 'L':
14175 case 'M':
14176 case 'N':
14177 case 'G':
14178 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000014179 case 'e':
14180 case 'Z':
14181 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000014182 default:
14183 break;
14184 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000014185 }
Chris Lattner4234f572007-03-25 02:14:49 +000014186 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000014187}
14188
John Thompson44ab89e2010-10-29 17:29:13 +000014189/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000014190/// This object must already have been set up with the operand type
14191/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000014192TargetLowering::ConstraintWeight
14193 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000014194 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000014195 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014196 Value *CallOperandVal = info.CallOperandVal;
14197 // If we don't have a value, we can't do a match,
14198 // but allow it at the lowest weight.
14199 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000014200 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014201 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000014202 // Look at the constraint type.
14203 switch (*constraint) {
14204 default:
John Thompson44ab89e2010-10-29 17:29:13 +000014205 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
14206 case 'R':
14207 case 'q':
14208 case 'Q':
14209 case 'a':
14210 case 'b':
14211 case 'c':
14212 case 'd':
14213 case 'S':
14214 case 'D':
14215 case 'A':
14216 if (CallOperandVal->getType()->isIntegerTy())
14217 weight = CW_SpecificReg;
14218 break;
14219 case 'f':
14220 case 't':
14221 case 'u':
14222 if (type->isFloatingPointTy())
14223 weight = CW_SpecificReg;
14224 break;
14225 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000014226 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000014227 weight = CW_SpecificReg;
14228 break;
14229 case 'x':
14230 case 'Y':
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014231 if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
John Thompson44ab89e2010-10-29 17:29:13 +000014232 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014233 break;
14234 case 'I':
14235 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
14236 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000014237 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014238 }
14239 break;
John Thompson44ab89e2010-10-29 17:29:13 +000014240 case 'J':
14241 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14242 if (C->getZExtValue() <= 63)
14243 weight = CW_Constant;
14244 }
14245 break;
14246 case 'K':
14247 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14248 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
14249 weight = CW_Constant;
14250 }
14251 break;
14252 case 'L':
14253 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14254 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
14255 weight = CW_Constant;
14256 }
14257 break;
14258 case 'M':
14259 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14260 if (C->getZExtValue() <= 3)
14261 weight = CW_Constant;
14262 }
14263 break;
14264 case 'N':
14265 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14266 if (C->getZExtValue() <= 0xff)
14267 weight = CW_Constant;
14268 }
14269 break;
14270 case 'G':
14271 case 'C':
14272 if (dyn_cast<ConstantFP>(CallOperandVal)) {
14273 weight = CW_Constant;
14274 }
14275 break;
14276 case 'e':
14277 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14278 if ((C->getSExtValue() >= -0x80000000LL) &&
14279 (C->getSExtValue() <= 0x7fffffffLL))
14280 weight = CW_Constant;
14281 }
14282 break;
14283 case 'Z':
14284 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14285 if (C->getZExtValue() <= 0xffffffff)
14286 weight = CW_Constant;
14287 }
14288 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014289 }
14290 return weight;
14291}
14292
Dale Johannesenba2a0b92008-01-29 02:21:21 +000014293/// LowerXConstraint - try to replace an X constraint, which matches anything,
14294/// with another that has more specific requirements based on the type of the
14295/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000014296const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000014297LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000014298 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
14299 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000014300 if (ConstraintVT.isFloatingPoint()) {
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014301 if (Subtarget->hasXMMInt())
Chris Lattner5e764232008-04-26 23:02:14 +000014302 return "Y";
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014303 if (Subtarget->hasXMM())
Chris Lattner5e764232008-04-26 23:02:14 +000014304 return "x";
14305 }
Scott Michelfdc40a02009-02-17 22:15:04 +000014306
Chris Lattner5e764232008-04-26 23:02:14 +000014307 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000014308}
14309
Chris Lattner48884cd2007-08-25 00:47:38 +000014310/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
14311/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000014312void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000014313 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000014314 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000014315 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000014316 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000014317
Eric Christopher100c8332011-06-02 23:16:42 +000014318 // Only support length 1 constraints for now.
14319 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000014320
Eric Christopher100c8332011-06-02 23:16:42 +000014321 char ConstraintLetter = Constraint[0];
14322 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000014323 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000014324 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000014325 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000014326 if (C->getZExtValue() <= 31) {
14327 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000014328 break;
14329 }
Devang Patel84f7fd22007-03-17 00:13:28 +000014330 }
Chris Lattner48884cd2007-08-25 00:47:38 +000014331 return;
Evan Cheng364091e2008-09-22 23:57:37 +000014332 case 'J':
14333 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000014334 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000014335 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
14336 break;
14337 }
14338 }
14339 return;
14340 case 'K':
14341 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000014342 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000014343 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
14344 break;
14345 }
14346 }
14347 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000014348 case 'N':
14349 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000014350 if (C->getZExtValue() <= 255) {
14351 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000014352 break;
14353 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000014354 }
Chris Lattner48884cd2007-08-25 00:47:38 +000014355 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000014356 case 'e': {
14357 // 32-bit signed value
14358 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000014359 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
14360 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000014361 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000014362 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000014363 break;
14364 }
14365 // FIXME gcc accepts some relocatable values here too, but only in certain
14366 // memory models; it's complicated.
14367 }
14368 return;
14369 }
14370 case 'Z': {
14371 // 32-bit unsigned value
14372 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000014373 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
14374 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000014375 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
14376 break;
14377 }
14378 }
14379 // FIXME gcc accepts some relocatable values here too, but only in certain
14380 // memory models; it's complicated.
14381 return;
14382 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000014383 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000014384 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000014385 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000014386 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000014387 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000014388 break;
14389 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014390
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000014391 // In any sort of PIC mode addresses need to be computed at runtime by
14392 // adding in a register or some sort of table lookup. These can't
14393 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000014394 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000014395 return;
14396
Chris Lattnerdc43a882007-05-03 16:52:29 +000014397 // If we are in non-pic codegen mode, we allow the address of a global (with
14398 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000014399 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000014400 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000014401
Chris Lattner49921962009-05-08 18:23:14 +000014402 // Match either (GA), (GA+C), (GA+C1+C2), etc.
14403 while (1) {
14404 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
14405 Offset += GA->getOffset();
14406 break;
14407 } else if (Op.getOpcode() == ISD::ADD) {
14408 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
14409 Offset += C->getZExtValue();
14410 Op = Op.getOperand(0);
14411 continue;
14412 }
14413 } else if (Op.getOpcode() == ISD::SUB) {
14414 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
14415 Offset += -C->getZExtValue();
14416 Op = Op.getOperand(0);
14417 continue;
14418 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000014419 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000014420
Chris Lattner49921962009-05-08 18:23:14 +000014421 // Otherwise, this isn't something we can handle, reject it.
14422 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000014423 }
Eric Christopherfd179292009-08-27 18:07:15 +000014424
Dan Gohman46510a72010-04-15 01:51:59 +000014425 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000014426 // If we require an extra load to get this address, as in PIC mode, we
14427 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000014428 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
14429 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000014430 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000014431
Devang Patel0d881da2010-07-06 22:08:15 +000014432 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
14433 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000014434 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000014435 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000014436 }
Scott Michelfdc40a02009-02-17 22:15:04 +000014437
Gabor Greifba36cb52008-08-28 21:40:38 +000014438 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000014439 Ops.push_back(Result);
14440 return;
14441 }
Dale Johannesen1784d162010-06-25 21:55:36 +000014442 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000014443}
14444
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014445std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000014446X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000014447 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000014448 // First, see if this is a constraint that directly corresponds to an LLVM
14449 // register class.
14450 if (Constraint.size() == 1) {
14451 // GCC Constraint Letters
14452 switch (Constraint[0]) {
14453 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000014454 // TODO: Slight differences here in allocation order and leaving
14455 // RIP in the class. Do they matter any more here than they do
14456 // in the normal allocation?
14457 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
14458 if (Subtarget->is64Bit()) {
Nick Lewycky9bf45d02011-07-08 00:19:27 +000014459 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000014460 return std::make_pair(0U, X86::GR32RegisterClass);
14461 else if (VT == MVT::i16)
14462 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000014463 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000014464 return std::make_pair(0U, X86::GR8RegisterClass);
Nick Lewycky9bf45d02011-07-08 00:19:27 +000014465 else if (VT == MVT::i64 || VT == MVT::f64)
Eric Christopherd176af82011-06-29 17:23:50 +000014466 return std::make_pair(0U, X86::GR64RegisterClass);
14467 break;
14468 }
14469 // 32-bit fallthrough
14470 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000014471 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000014472 return std::make_pair(0U, X86::GR32_ABCDRegisterClass);
14473 else if (VT == MVT::i16)
14474 return std::make_pair(0U, X86::GR16_ABCDRegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000014475 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000014476 return std::make_pair(0U, X86::GR8_ABCD_LRegisterClass);
14477 else if (VT == MVT::i64)
14478 return std::make_pair(0U, X86::GR64_ABCDRegisterClass);
14479 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000014480 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000014481 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000014482 if (VT == MVT::i8 || VT == MVT::i1)
Chris Lattner0f65cad2007-04-09 05:49:22 +000014483 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000014484 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000014485 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000014486 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000014487 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000014488 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000014489 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000014490 if (VT == MVT::i8 || VT == MVT::i1)
Dale Johannesen5f3663e2009-10-07 22:47:20 +000014491 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
14492 if (VT == MVT::i16)
14493 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
14494 if (VT == MVT::i32 || !Subtarget->is64Bit())
14495 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
14496 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000014497 case 'f': // FP Stack registers.
14498 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
14499 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000014500 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000014501 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000014502 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000014503 return std::make_pair(0U, X86::RFP64RegisterClass);
14504 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000014505 case 'y': // MMX_REGS if MMX allowed.
14506 if (!Subtarget->hasMMX()) break;
14507 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000014508 case 'Y': // SSE_REGS if SSE2 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014509 if (!Subtarget->hasXMMInt()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000014510 // FALL THROUGH.
14511 case 'x': // SSE_REGS if SSE1 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014512 if (!Subtarget->hasXMM()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000014513
Owen Anderson825b72b2009-08-11 20:47:22 +000014514 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000014515 default: break;
14516 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000014517 case MVT::f32:
14518 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000014519 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000014520 case MVT::f64:
14521 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000014522 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000014523 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000014524 case MVT::v16i8:
14525 case MVT::v8i16:
14526 case MVT::v4i32:
14527 case MVT::v2i64:
14528 case MVT::v4f32:
14529 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000014530 return std::make_pair(0U, X86::VR128RegisterClass);
14531 }
Chris Lattnerad043e82007-04-09 05:11:28 +000014532 break;
14533 }
14534 }
Scott Michelfdc40a02009-02-17 22:15:04 +000014535
Chris Lattnerf76d1802006-07-31 23:26:50 +000014536 // Use the default implementation in TargetLowering to convert the register
14537 // constraint into a member of a register class.
14538 std::pair<unsigned, const TargetRegisterClass*> Res;
14539 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000014540
14541 // Not found as a standard register?
14542 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000014543 // Map st(0) -> st(7) -> ST0
14544 if (Constraint.size() == 7 && Constraint[0] == '{' &&
14545 tolower(Constraint[1]) == 's' &&
14546 tolower(Constraint[2]) == 't' &&
14547 Constraint[3] == '(' &&
14548 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
14549 Constraint[5] == ')' &&
14550 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000014551
Chris Lattner56d77c72009-09-13 22:41:48 +000014552 Res.first = X86::ST0+Constraint[4]-'0';
14553 Res.second = X86::RFP80RegisterClass;
14554 return Res;
14555 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000014556
Chris Lattner56d77c72009-09-13 22:41:48 +000014557 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000014558 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000014559 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000014560 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000014561 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000014562 }
Chris Lattner56d77c72009-09-13 22:41:48 +000014563
14564 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000014565 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000014566 Res.first = X86::EFLAGS;
14567 Res.second = X86::CCRRegisterClass;
14568 return Res;
14569 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000014570
Dale Johannesen330169f2008-11-13 21:52:36 +000014571 // 'A' means EAX + EDX.
14572 if (Constraint == "A") {
14573 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000014574 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000014575 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000014576 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000014577 return Res;
14578 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014579
Chris Lattnerf76d1802006-07-31 23:26:50 +000014580 // Otherwise, check to see if this is a register class of the wrong value
14581 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
14582 // turn into {ax},{dx}.
14583 if (Res.second->hasType(VT))
14584 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014585
Chris Lattnerf76d1802006-07-31 23:26:50 +000014586 // All of the single-register GCC register classes map their values onto
14587 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
14588 // really want an 8-bit or 32-bit register, map to the appropriate register
14589 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000014590 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000014591 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000014592 unsigned DestReg = 0;
14593 switch (Res.first) {
14594 default: break;
14595 case X86::AX: DestReg = X86::AL; break;
14596 case X86::DX: DestReg = X86::DL; break;
14597 case X86::CX: DestReg = X86::CL; break;
14598 case X86::BX: DestReg = X86::BL; break;
14599 }
14600 if (DestReg) {
14601 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000014602 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000014603 }
Owen Anderson825b72b2009-08-11 20:47:22 +000014604 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000014605 unsigned DestReg = 0;
14606 switch (Res.first) {
14607 default: break;
14608 case X86::AX: DestReg = X86::EAX; break;
14609 case X86::DX: DestReg = X86::EDX; break;
14610 case X86::CX: DestReg = X86::ECX; break;
14611 case X86::BX: DestReg = X86::EBX; break;
14612 case X86::SI: DestReg = X86::ESI; break;
14613 case X86::DI: DestReg = X86::EDI; break;
14614 case X86::BP: DestReg = X86::EBP; break;
14615 case X86::SP: DestReg = X86::ESP; break;
14616 }
14617 if (DestReg) {
14618 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000014619 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000014620 }
Owen Anderson825b72b2009-08-11 20:47:22 +000014621 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000014622 unsigned DestReg = 0;
14623 switch (Res.first) {
14624 default: break;
14625 case X86::AX: DestReg = X86::RAX; break;
14626 case X86::DX: DestReg = X86::RDX; break;
14627 case X86::CX: DestReg = X86::RCX; break;
14628 case X86::BX: DestReg = X86::RBX; break;
14629 case X86::SI: DestReg = X86::RSI; break;
14630 case X86::DI: DestReg = X86::RDI; break;
14631 case X86::BP: DestReg = X86::RBP; break;
14632 case X86::SP: DestReg = X86::RSP; break;
14633 }
14634 if (DestReg) {
14635 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000014636 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000014637 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000014638 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000014639 } else if (Res.second == X86::FR32RegisterClass ||
14640 Res.second == X86::FR64RegisterClass ||
14641 Res.second == X86::VR128RegisterClass) {
14642 // Handle references to XMM physical registers that got mapped into the
14643 // wrong class. This can happen with constraints like {xmm0} where the
14644 // target independent register mapper will just pick the first match it can
14645 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000014646 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000014647 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000014648 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000014649 Res.second = X86::FR64RegisterClass;
14650 else if (X86::VR128RegisterClass->hasType(VT))
14651 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000014652 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014653
Chris Lattnerf76d1802006-07-31 23:26:50 +000014654 return Res;
14655}