blob: 2ec08140db51db92b29083ae404d10a8d0bfe7cf [file] [log] [blame]
Arnold Schwaighofer92226dd2007-10-12 21:53:12 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
Evan Chengb1712452010-01-27 06:25:16 +000015#define DEBUG_TYPE "x86-isel"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000016#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000017#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000018#include "X86ISelLowering.h"
19#include "X86TargetMachine.h"
Chris Lattner8c6ed052009-09-16 01:46:41 +000020#include "X86TargetObjectFile.h"
David Greene583b68f2011-02-17 19:18:59 +000021#include "Utils/X86ShuffleDecode.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000022#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000023#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000024#include "llvm/DerivedTypes.h"
Chris Lattnerb903bed2009-06-26 21:20:29 +000025#include "llvm/GlobalAlias.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000026#include "llvm/GlobalVariable.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000027#include "llvm/Function.h"
Chris Lattnerb8105652009-07-20 17:51:36 +000028#include "llvm/Instructions.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000029#include "llvm/Intrinsics.h"
Owen Andersona90b3dc2009-07-15 21:51:10 +000030#include "llvm/LLVMContext.h"
Evan Cheng55d42002011-01-08 01:24:27 +000031#include "llvm/CodeGen/IntrinsicLowering.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000032#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000033#include "llvm/CodeGen/MachineFunction.h"
34#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner5e1df8d2010-01-25 23:38:14 +000035#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000036#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000037#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000038#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattner589c6f62010-01-26 06:28:43 +000039#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000040#include "llvm/MC/MCContext.h"
Daniel Dunbar4e815f82010-03-15 23:51:06 +000041#include "llvm/MC/MCExpr.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000042#include "llvm/MC/MCSymbol.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000043#include "llvm/ADT/BitVector.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000044#include "llvm/ADT/SmallSet.h"
Evan Chengb1712452010-01-27 06:25:16 +000045#include "llvm/ADT/Statistic.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000046#include "llvm/ADT/StringExtras.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000047#include "llvm/ADT/VectorExtras.h"
Evan Cheng485fafc2011-03-21 01:19:09 +000048#include "llvm/Support/CallSite.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000049#include "llvm/Support/Debug.h"
Bill Wendlingec041eb2010-03-12 19:20:40 +000050#include "llvm/Support/Dwarf.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000051#include "llvm/Support/ErrorHandling.h"
52#include "llvm/Support/MathExtras.h"
Torok Edwindac237e2009-07-08 20:53:28 +000053#include "llvm/Support/raw_ostream.h"
Rafael Espindola151ab3e2011-08-30 19:47:04 +000054#include "llvm/Target/TargetOptions.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000055using namespace llvm;
Bill Wendlingec041eb2010-03-12 19:20:40 +000056using namespace dwarf;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000057
Evan Chengb1712452010-01-27 06:25:16 +000058STATISTIC(NumTailCalls, "Number of tail calls");
59
Evan Cheng10e86422008-04-25 19:11:04 +000060// Forward declarations.
Owen Andersone50ed302009-08-10 22:56:29 +000061static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +000062 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000063
David Greenea5f26012011-02-07 19:36:54 +000064static SDValue Insert128BitVector(SDValue Result,
65 SDValue Vec,
66 SDValue Idx,
67 SelectionDAG &DAG,
68 DebugLoc dl);
David Greenef125a292011-02-08 19:04:41 +000069
David Greenea5f26012011-02-07 19:36:54 +000070static SDValue Extract128BitVector(SDValue Vec,
71 SDValue Idx,
72 SelectionDAG &DAG,
73 DebugLoc dl);
74
75/// Generate a DAG to grab 128-bits from a vector > 128 bits. This
76/// sets things up to match to an AVX VEXTRACTF128 instruction or a
David Greene74a579d2011-02-10 16:57:36 +000077/// simple subregister reference. Idx is an index in the 128 bits we
78/// want. It need not be aligned to a 128-bit bounday. That makes
79/// lowering EXTRACT_VECTOR_ELT operations easier.
David Greenea5f26012011-02-07 19:36:54 +000080static SDValue Extract128BitVector(SDValue Vec,
81 SDValue Idx,
82 SelectionDAG &DAG,
83 DebugLoc dl) {
84 EVT VT = Vec.getValueType();
85 assert(VT.getSizeInBits() == 256 && "Unexpected vector size!");
David Greenea5f26012011-02-07 19:36:54 +000086 EVT ElVT = VT.getVectorElementType();
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +000087 int Factor = VT.getSizeInBits()/128;
88 EVT ResultVT = EVT::getVectorVT(*DAG.getContext(), ElVT,
89 VT.getVectorNumElements()/Factor);
David Greenea5f26012011-02-07 19:36:54 +000090
91 // Extract from UNDEF is UNDEF.
92 if (Vec.getOpcode() == ISD::UNDEF)
93 return DAG.getNode(ISD::UNDEF, dl, ResultVT);
94
95 if (isa<ConstantSDNode>(Idx)) {
96 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
97
98 // Extract the relevant 128 bits. Generate an EXTRACT_SUBVECTOR
99 // we can match to VEXTRACTF128.
100 unsigned ElemsPerChunk = 128 / ElVT.getSizeInBits();
101
102 // This is the index of the first element of the 128-bit chunk
103 // we want.
104 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits()) / 128)
105 * ElemsPerChunk);
106
107 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
David Greenea5f26012011-02-07 19:36:54 +0000108 SDValue Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, ResultVT, Vec,
109 VecIdx);
110
111 return Result;
112 }
113
114 return SDValue();
115}
116
117/// Generate a DAG to put 128-bits into a vector > 128 bits. This
118/// sets things up to match to an AVX VINSERTF128 instruction or a
David Greene6b381262011-02-09 15:32:06 +0000119/// simple superregister reference. Idx is an index in the 128 bits
120/// we want. It need not be aligned to a 128-bit bounday. That makes
121/// lowering INSERT_VECTOR_ELT operations easier.
David Greenea5f26012011-02-07 19:36:54 +0000122static SDValue Insert128BitVector(SDValue Result,
123 SDValue Vec,
124 SDValue Idx,
125 SelectionDAG &DAG,
126 DebugLoc dl) {
127 if (isa<ConstantSDNode>(Idx)) {
128 EVT VT = Vec.getValueType();
129 assert(VT.getSizeInBits() == 128 && "Unexpected vector size!");
130
131 EVT ElVT = VT.getVectorElementType();
David Greenea5f26012011-02-07 19:36:54 +0000132 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
David Greenea5f26012011-02-07 19:36:54 +0000133 EVT ResultVT = Result.getValueType();
134
135 // Insert the relevant 128 bits.
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +0000136 unsigned ElemsPerChunk = 128/ElVT.getSizeInBits();
David Greenea5f26012011-02-07 19:36:54 +0000137
138 // This is the index of the first element of the 128-bit chunk
139 // we want.
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +0000140 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits())/128)
David Greenea5f26012011-02-07 19:36:54 +0000141 * ElemsPerChunk);
142
143 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
David Greenea5f26012011-02-07 19:36:54 +0000144 Result = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResultVT, Result, Vec,
145 VecIdx);
146 return Result;
147 }
148
149 return SDValue();
150}
151
Chris Lattnerf0144122009-07-28 03:13:23 +0000152static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
Evan Cheng2bffee22011-02-01 01:14:13 +0000153 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
154 bool is64Bit = Subtarget->is64Bit();
NAKAMURA Takumi27635382011-02-05 15:10:54 +0000155
Evan Cheng2bffee22011-02-01 01:14:13 +0000156 if (Subtarget->isTargetEnvMacho()) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000157 if (is64Bit)
158 return new X8664_MachoTargetObjectFile();
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000159 return new TargetLoweringObjectFileMachO();
Michael J. Spencerec38de22010-10-10 22:04:20 +0000160 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000161
Evan Cheng203576a2011-07-20 19:50:42 +0000162 if (Subtarget->isTargetELF())
163 return new TargetLoweringObjectFileELF();
Evan Cheng2bffee22011-02-01 01:14:13 +0000164 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
Chris Lattnere019ec12010-12-19 20:07:10 +0000165 return new TargetLoweringObjectFileCOFF();
Eric Christopher62f35a22010-07-05 19:26:33 +0000166 llvm_unreachable("unknown subtarget type");
Chris Lattnerf0144122009-07-28 03:13:23 +0000167}
168
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +0000169X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +0000170 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +0000171 Subtarget = &TM.getSubtarget<X86Subtarget>();
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +0000172 X86ScalarSSEf64 = Subtarget->hasXMMInt();
173 X86ScalarSSEf32 = Subtarget->hasXMM();
Evan Cheng25ab6902006-09-08 06:48:29 +0000174 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000175
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000176 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000177 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000178
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000179 // Set up the TargetLowering object.
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000180 static MVT IntVTs[] = { MVT::i8, MVT::i16, MVT::i32, MVT::i64 };
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000181
182 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Duncan Sands03228082008-11-23 15:47:28 +0000183 setBooleanContents(ZeroOrOneBooleanContent);
Duncan Sands28b77e92011-09-06 19:07:46 +0000184 // X86-SSE is even stranger. It uses -1 or 0 for vector masks.
185 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Eric Christopher471e4222011-06-08 23:55:35 +0000186
Eric Christopherde5e1012011-03-11 01:05:58 +0000187 // For 64-bit since we have so many registers use the ILP scheduler, for
188 // 32-bit code use the register pressure specific scheduling.
189 if (Subtarget->is64Bit())
190 setSchedulingPreference(Sched::ILP);
191 else
192 setSchedulingPreference(Sched::RegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +0000193 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +0000194
Michael J. Spencer92bf38c2010-10-10 23:11:06 +0000195 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing()) {
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000196 // Setup Windows compiler runtime calls.
197 setLibcallName(RTLIB::SDIV_I64, "_alldiv");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000198 setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
Julien Lerougef2960822011-07-08 21:40:25 +0000199 setLibcallName(RTLIB::SREM_I64, "_allrem");
200 setLibcallName(RTLIB::UREM_I64, "_aullrem");
201 setLibcallName(RTLIB::MUL_I64, "_allmul");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000202 setLibcallName(RTLIB::FPTOUINT_F64_I64, "_ftol2");
Michael J. Spencer94f7eeb2010-10-19 07:32:52 +0000203 setLibcallName(RTLIB::FPTOUINT_F32_I64, "_ftol2");
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000204 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer335b8062010-10-11 05:29:15 +0000205 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
Julien Lerougef2960822011-07-08 21:40:25 +0000206 setLibcallCallingConv(RTLIB::SREM_I64, CallingConv::X86_StdCall);
207 setLibcallCallingConv(RTLIB::UREM_I64, CallingConv::X86_StdCall);
208 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::X86_StdCall);
Michael J. Spencer6dad10e2010-10-27 18:52:38 +0000209 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::C);
210 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::C);
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000211 }
212
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000213 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000214 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000215 setUseUnderscoreSetJmp(false);
216 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000217 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000218 // MS runtime is weird: it exports _setjmp, but longjmp!
219 setUseUnderscoreSetJmp(true);
220 setUseUnderscoreLongJmp(false);
221 } else {
222 setUseUnderscoreSetJmp(true);
223 setUseUnderscoreLongJmp(true);
224 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000225
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000226 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000227 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
Dan Gohman71edb242010-04-30 18:30:26 +0000228 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000229 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000230 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000231 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000232
Owen Anderson825b72b2009-08-11 20:47:22 +0000233 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000234
Scott Michelfdc40a02009-02-17 22:15:04 +0000235 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000236 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000237 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000238 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000239 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000240 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
241 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000242
243 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000244 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
245 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
246 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
247 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
248 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
249 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000250
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000251 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
252 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000253 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
254 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
255 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000256
Evan Cheng25ab6902006-09-08 06:48:29 +0000257 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000258 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
259 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Eli Friedman948e95a2009-05-23 09:59:16 +0000260 } else if (!UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000261 // We have an algorithm for SSE2->double, and we turn this into a
262 // 64-bit FILD followed by conditional FADD for other targets.
263 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000264 // We have an algorithm for SSE2, and we turn this into a 64-bit
265 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000266 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000267 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000268
269 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
270 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000271 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
272 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000273
Devang Patel6a784892009-06-05 18:48:29 +0000274 if (!UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000275 // SSE has no i16 to fp conversion, only i32
276 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000277 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000278 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000279 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000280 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000281 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
282 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000283 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000284 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000285 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
286 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000287 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000288
Dale Johannesen73328d12007-09-19 23:55:34 +0000289 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
290 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000291 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
292 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000293
Evan Cheng02568ff2006-01-30 22:13:22 +0000294 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
295 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000296 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
297 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000298
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000299 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000300 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000301 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000302 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000303 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000304 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
305 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000306 }
307
308 // Handle FP_TO_UINT by promoting the destination to a larger signed
309 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000310 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
311 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
312 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000313
Evan Cheng25ab6902006-09-08 06:48:29 +0000314 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000315 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
316 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman948e95a2009-05-23 09:59:16 +0000317 } else if (!UseSoftFloat) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +0000318 // Since AVX is a superset of SSE3, only check for SSE here.
319 if (Subtarget->hasSSE1() && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000320 // Expand FP_TO_UINT into a select.
321 // FIXME: We would like to use a Custom expander here eventually to do
322 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000323 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000324 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000325 // With SSE3 we can use fisttpll to convert to a signed i64; without
326 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000327 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000328 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000329
Chris Lattner399610a2006-12-05 18:22:22 +0000330 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000331 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000332 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
333 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000334 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000335 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000336 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000337 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000338 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000339 }
Chris Lattner21f66852005-12-23 05:15:23 +0000340
Dan Gohmanb00ee212008-02-18 19:34:53 +0000341 // Scalar integer divide and remainder are lowered to use operations that
342 // produce two results, to match the available instructions. This exposes
343 // the two-result form to trivial CSE, which is able to combine x/y and x%y
344 // into a single instruction.
345 //
346 // Scalar integer multiply-high is also lowered to use two-result
347 // operations, to match the available instructions. However, plain multiply
348 // (low) operations are left as Legal, as there are single-result
349 // instructions for this in x86. Using the two-result multiply instructions
350 // when both high and low results are needed must be arranged by dagcombine.
Chris Lattnere019ec12010-12-19 20:07:10 +0000351 for (unsigned i = 0, e = 4; i != e; ++i) {
352 MVT VT = IntVTs[i];
353 setOperationAction(ISD::MULHS, VT, Expand);
354 setOperationAction(ISD::MULHU, VT, Expand);
355 setOperationAction(ISD::SDIV, VT, Expand);
356 setOperationAction(ISD::UDIV, VT, Expand);
357 setOperationAction(ISD::SREM, VT, Expand);
358 setOperationAction(ISD::UREM, VT, Expand);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000359
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000360 // Add/Sub overflow ops with MVT::Glues are lowered to EFLAGS dependences.
Chris Lattnerd8ff7ec2010-12-20 01:03:27 +0000361 setOperationAction(ISD::ADDC, VT, Custom);
362 setOperationAction(ISD::ADDE, VT, Custom);
363 setOperationAction(ISD::SUBC, VT, Custom);
364 setOperationAction(ISD::SUBE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000365 }
Dan Gohmana37c9f72007-09-25 18:23:27 +0000366
Owen Anderson825b72b2009-08-11 20:47:22 +0000367 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
368 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
369 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
370 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000371 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000372 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
373 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
374 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
375 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
376 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
377 setOperationAction(ISD::FREM , MVT::f32 , Expand);
378 setOperationAction(ISD::FREM , MVT::f64 , Expand);
379 setOperationAction(ISD::FREM , MVT::f80 , Expand);
380 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000381
Craig Topper909652f2011-10-14 03:21:46 +0000382 if (Subtarget->hasBMI()) {
383 setOperationAction(ISD::CTTZ , MVT::i8 , Promote);
384 } else {
385 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
386 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
387 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
388 if (Subtarget->is64Bit())
389 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
390 }
Craig Topper37f21672011-10-11 06:44:02 +0000391
392 if (Subtarget->hasLZCNT()) {
393 setOperationAction(ISD::CTLZ , MVT::i8 , Promote);
394 } else {
395 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
396 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
397 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
398 if (Subtarget->is64Bit())
399 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000400 }
401
Benjamin Kramer1292c222010-12-04 20:32:23 +0000402 if (Subtarget->hasPOPCNT()) {
403 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
404 } else {
405 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
406 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
407 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
408 if (Subtarget->is64Bit())
409 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
410 }
411
Owen Anderson825b72b2009-08-11 20:47:22 +0000412 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
413 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000414
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000415 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000416 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000417 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000418 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000419 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000420 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
421 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
422 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
423 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
424 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000425 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000426 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
427 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
428 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
429 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000430 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000431 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
Andrew Trickf6c39412011-03-23 23:11:02 +0000432 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000433 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000434 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000435
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000436 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000437 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
438 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
439 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
440 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000441 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000442 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
443 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000444 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000445 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000446 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
447 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
448 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
449 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000450 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000451 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000452 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000453 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
454 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
455 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000456 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000457 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
458 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
459 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000460 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000461
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000462 if (Subtarget->hasXMM())
Owen Anderson825b72b2009-08-11 20:47:22 +0000463 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000464
Eric Christopher9a9d2752010-07-22 02:48:34 +0000465 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000466 setOperationAction(ISD::ATOMIC_FENCE , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000467
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000468 // On X86 and X86-64, atomic operations are lowered to locked instructions.
469 // Locked instructions, in turn, have implicit fence semantics (all memory
470 // operations are flushed before issuing the locked instruction, and they
471 // are not buffered), so we can fold away the common pattern of
472 // fence-atomic-fence.
473 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000474
Mon P Wang63307c32008-05-05 19:05:59 +0000475 // Expand certain atomics
Chris Lattnere019ec12010-12-19 20:07:10 +0000476 for (unsigned i = 0, e = 4; i != e; ++i) {
477 MVT VT = IntVTs[i];
478 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
479 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
Eli Friedman327236c2011-08-24 20:50:09 +0000480 setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000481 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000482
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000483 if (!Subtarget->is64Bit()) {
Eli Friedmanf8f90f02011-08-24 22:33:28 +0000484 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000485 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
486 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
487 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
488 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
489 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
490 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
491 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000492 }
493
Eli Friedman43f51ae2011-08-26 21:21:21 +0000494 if (Subtarget->hasCmpxchg16b()) {
495 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i128, Custom);
496 }
497
Evan Cheng3c992d22006-03-07 02:02:57 +0000498 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000499 if (!Subtarget->isTargetDarwin() &&
500 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000501 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000502 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000503 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000504
Owen Anderson825b72b2009-08-11 20:47:22 +0000505 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
506 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
507 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
508 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000509 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000510 setExceptionPointerRegister(X86::RAX);
511 setExceptionSelectorRegister(X86::RDX);
512 } else {
513 setExceptionPointerRegister(X86::EAX);
514 setExceptionSelectorRegister(X86::EDX);
515 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000516 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
517 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000518
Duncan Sands4a544a72011-09-06 13:37:06 +0000519 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
520 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000521
Owen Anderson825b72b2009-08-11 20:47:22 +0000522 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000523
Nate Begemanacc398c2006-01-25 18:21:52 +0000524 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000525 setOperationAction(ISD::VASTART , MVT::Other, Custom);
526 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000527 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000528 setOperationAction(ISD::VAARG , MVT::Other, Custom);
529 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000530 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000531 setOperationAction(ISD::VAARG , MVT::Other, Expand);
532 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000533 }
Evan Chengae642192007-03-02 23:16:35 +0000534
Owen Anderson825b72b2009-08-11 20:47:22 +0000535 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
536 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eric Christopherc967ad82011-08-31 04:17:21 +0000537
538 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
539 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
540 MVT::i64 : MVT::i32, Custom);
541 else if (EnableSegmentedStacks)
542 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
543 MVT::i64 : MVT::i32, Custom);
544 else
545 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
546 MVT::i64 : MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000547
Evan Chengc7ce29b2009-02-13 22:36:38 +0000548 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000549 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000550 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000551 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
552 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000553
Evan Cheng223547a2006-01-31 22:28:30 +0000554 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000555 setOperationAction(ISD::FABS , MVT::f64, Custom);
556 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000557
558 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000559 setOperationAction(ISD::FNEG , MVT::f64, Custom);
560 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000561
Evan Cheng68c47cb2007-01-05 07:55:56 +0000562 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000563 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
564 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000565
Stuart Hastings4fd0dee2011-06-01 04:39:42 +0000566 // Lower this to FGETSIGNx86 plus an AND.
567 setOperationAction(ISD::FGETSIGN, MVT::i64, Custom);
568 setOperationAction(ISD::FGETSIGN, MVT::i32, Custom);
569
Evan Chengd25e9e82006-02-02 00:28:23 +0000570 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000571 setOperationAction(ISD::FSIN , MVT::f64, Expand);
572 setOperationAction(ISD::FCOS , MVT::f64, Expand);
573 setOperationAction(ISD::FSIN , MVT::f32, Expand);
574 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000575
Chris Lattnera54aa942006-01-29 06:26:08 +0000576 // Expand FP immediates into loads from the stack, except for the special
577 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000578 addLegalFPImmediate(APFloat(+0.0)); // xorpd
579 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000580 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000581 // Use SSE for f32, x87 for f64.
582 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000583 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
584 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000585
586 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000587 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000588
589 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000590 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000591
Owen Anderson825b72b2009-08-11 20:47:22 +0000592 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000593
594 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000595 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
596 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000597
598 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000599 setOperationAction(ISD::FSIN , MVT::f32, Expand);
600 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000601
Nate Begemane1795842008-02-14 08:57:00 +0000602 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000603 addLegalFPImmediate(APFloat(+0.0f)); // xorps
604 addLegalFPImmediate(APFloat(+0.0)); // FLD0
605 addLegalFPImmediate(APFloat(+1.0)); // FLD1
606 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
607 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
608
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000609 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000610 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
611 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000612 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000613 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000614 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000615 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000616 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
617 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000618
Owen Anderson825b72b2009-08-11 20:47:22 +0000619 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
620 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
621 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
622 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000623
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000624 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000625 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
626 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000627 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000628 addLegalFPImmediate(APFloat(+0.0)); // FLD0
629 addLegalFPImmediate(APFloat(+1.0)); // FLD1
630 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
631 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000632 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
633 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
634 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
635 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000636 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000637
Cameron Zwarich33390842011-07-08 21:39:21 +0000638 // We don't support FMA.
639 setOperationAction(ISD::FMA, MVT::f64, Expand);
640 setOperationAction(ISD::FMA, MVT::f32, Expand);
641
Dale Johannesen59a58732007-08-05 18:49:15 +0000642 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000643 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000644 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
645 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
646 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000647 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000648 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000649 addLegalFPImmediate(TmpFlt); // FLD0
650 TmpFlt.changeSign();
651 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000652
653 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000654 APFloat TmpFlt2(+1.0);
655 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
656 &ignored);
657 addLegalFPImmediate(TmpFlt2); // FLD1
658 TmpFlt2.changeSign();
659 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
660 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000661
Evan Chengc7ce29b2009-02-13 22:36:38 +0000662 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000663 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
664 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000665 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000666
667 setOperationAction(ISD::FMA, MVT::f80, Expand);
Dale Johannesen2f429012007-09-26 21:10:55 +0000668 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000669
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000670 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000671 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
672 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
673 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000674
Owen Anderson825b72b2009-08-11 20:47:22 +0000675 setOperationAction(ISD::FLOG, MVT::f80, Expand);
676 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
677 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
678 setOperationAction(ISD::FEXP, MVT::f80, Expand);
679 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000680
Mon P Wangf007a8b2008-11-06 05:31:54 +0000681 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000682 // (for widening) or expand (for scalarization). Then we will selectively
683 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000684 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
685 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
686 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
687 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
688 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
689 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
690 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
691 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
692 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
693 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
694 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
695 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
696 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
697 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
698 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
699 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
700 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000701 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
David Greenecfe33c42011-01-26 19:13:22 +0000702 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
703 setOperationAction(ISD::INSERT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000704 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
705 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
706 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
707 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
708 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
709 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
710 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
711 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
712 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
713 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
714 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
715 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
716 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
717 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
718 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
719 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
720 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
721 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
722 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
723 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
724 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
Duncan Sands28b77e92011-09-06 19:07:46 +0000725 setOperationAction(ISD::SETCC, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000726 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
727 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
728 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
729 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
730 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
731 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
732 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
733 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
734 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000735 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000736 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
737 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
738 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
739 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
Nadav Rotemaec58612011-09-13 19:17:42 +0000740 setOperationAction(ISD::VSELECT, (MVT::SimpleValueType)VT, Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000741 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
742 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
743 setTruncStoreAction((MVT::SimpleValueType)VT,
744 (MVT::SimpleValueType)InnerVT, Expand);
745 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
746 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
747 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000748 }
749
Evan Chengc7ce29b2009-02-13 22:36:38 +0000750 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
751 // with -msoft-float, disable use of MMX as well.
Chris Lattner2a786eb2010-12-19 20:19:20 +0000752 if (!UseSoftFloat && Subtarget->hasMMX()) {
Dale Johannesene93d99c2010-10-20 21:32:10 +0000753 addRegisterClass(MVT::x86mmx, X86::VR64RegisterClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000754 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000755 }
756
Dale Johannesen0488fb62010-09-30 23:57:10 +0000757 // MMX-sized vectors (other than x86mmx) are expected to be expanded
758 // into smaller operations.
759 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
760 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
761 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
762 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
763 setOperationAction(ISD::AND, MVT::v8i8, Expand);
764 setOperationAction(ISD::AND, MVT::v4i16, Expand);
765 setOperationAction(ISD::AND, MVT::v2i32, Expand);
766 setOperationAction(ISD::AND, MVT::v1i64, Expand);
767 setOperationAction(ISD::OR, MVT::v8i8, Expand);
768 setOperationAction(ISD::OR, MVT::v4i16, Expand);
769 setOperationAction(ISD::OR, MVT::v2i32, Expand);
770 setOperationAction(ISD::OR, MVT::v1i64, Expand);
771 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
772 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
773 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
774 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
775 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
776 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
777 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
778 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
779 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
780 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
781 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
782 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
783 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000784 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
785 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
786 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
787 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000788
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000789 if (!UseSoftFloat && Subtarget->hasXMM()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000790 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000791
Owen Anderson825b72b2009-08-11 20:47:22 +0000792 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
793 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
794 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
795 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
796 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
797 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
798 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
799 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
800 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
801 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
802 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000803 setOperationAction(ISD::SETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000804 }
805
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000806 if (!UseSoftFloat && Subtarget->hasXMMInt()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000807 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000808
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000809 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
810 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000811 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
812 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
813 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
814 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000815
Owen Anderson825b72b2009-08-11 20:47:22 +0000816 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
817 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
818 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
819 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
820 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
821 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
822 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
823 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
824 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
825 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
826 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
827 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
828 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
829 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
830 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
831 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000832
Nadav Rotem354efd82011-09-18 14:57:03 +0000833 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000834 setOperationAction(ISD::SETCC, MVT::v16i8, Custom);
835 setOperationAction(ISD::SETCC, MVT::v8i16, Custom);
836 setOperationAction(ISD::SETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000837
Owen Anderson825b72b2009-08-11 20:47:22 +0000838 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
839 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
840 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
841 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
842 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000843
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000844 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
845 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
846 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
847 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
848 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
849
Evan Cheng2c3ae372006-04-12 21:21:57 +0000850 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000851 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
852 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000853 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000854 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000855 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000856 // Do not attempt to custom lower non-128-bit vectors
857 if (!VT.is128BitVector())
858 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000859 setOperationAction(ISD::BUILD_VECTOR,
860 VT.getSimpleVT().SimpleTy, Custom);
861 setOperationAction(ISD::VECTOR_SHUFFLE,
862 VT.getSimpleVT().SimpleTy, Custom);
863 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
864 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000865 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000866
Owen Anderson825b72b2009-08-11 20:47:22 +0000867 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
868 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
869 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
870 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
871 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
872 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000873
Nate Begemancdd1eec2008-02-12 22:51:28 +0000874 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000875 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
876 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000877 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000878
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000879 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000880 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
881 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000882 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000883
884 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000885 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000886 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000887
Owen Andersond6662ad2009-08-10 20:46:15 +0000888 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000889 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000890 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000891 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000892 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000893 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000894 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000895 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000896 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000897 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000898 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000899
Owen Anderson825b72b2009-08-11 20:47:22 +0000900 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000901
Evan Cheng2c3ae372006-04-12 21:21:57 +0000902 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000903 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
904 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
905 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
906 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000907
Owen Anderson825b72b2009-08-11 20:47:22 +0000908 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
909 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000910 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000911
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000912 if (Subtarget->hasSSE41() || Subtarget->hasAVX()) {
Dale Johannesen54feef22010-05-27 20:12:41 +0000913 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
914 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
915 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
916 setOperationAction(ISD::FRINT, MVT::f32, Legal);
917 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
918 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
919 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
920 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
921 setOperationAction(ISD::FRINT, MVT::f64, Legal);
922 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
923
Nate Begeman14d12ca2008-02-11 04:19:36 +0000924 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000925 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000926
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000927 // Can turn SHL into an integer multiply.
928 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
Nate Begeman51409212010-07-28 00:21:48 +0000929 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000930
Nadav Rotemfbad25e2011-09-11 15:02:23 +0000931 setOperationAction(ISD::VSELECT, MVT::v2f64, Legal);
932 setOperationAction(ISD::VSELECT, MVT::v2i64, Legal);
933 setOperationAction(ISD::VSELECT, MVT::v16i8, Legal);
934 setOperationAction(ISD::VSELECT, MVT::v4i32, Legal);
935 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
Nadav Rotemffe3e7d2011-09-08 08:11:19 +0000936
Nate Begeman14d12ca2008-02-11 04:19:36 +0000937 // i8 and i16 vectors are custom , because the source register and source
938 // source memory operand types are not the same width. f32 vectors are
939 // custom since the immediate controlling the insert encodes additional
940 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000941 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
942 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
943 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
944 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000945
Owen Anderson825b72b2009-08-11 20:47:22 +0000946 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
947 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
948 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
949 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000950
951 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000952 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
953 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000954 }
955 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000956
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +0000957 if (Subtarget->hasXMMInt()) {
Nadav Rotem43012222011-05-11 08:12:09 +0000958 setOperationAction(ISD::SRL, MVT::v2i64, Custom);
959 setOperationAction(ISD::SRL, MVT::v4i32, Custom);
960 setOperationAction(ISD::SRL, MVT::v16i8, Custom);
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000961 setOperationAction(ISD::SRL, MVT::v8i16, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +0000962
963 setOperationAction(ISD::SHL, MVT::v2i64, Custom);
964 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
965 setOperationAction(ISD::SHL, MVT::v8i16, Custom);
966
967 setOperationAction(ISD::SRA, MVT::v4i32, Custom);
968 setOperationAction(ISD::SRA, MVT::v8i16, Custom);
969 }
970
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000971 if (Subtarget->hasSSE42() || Subtarget->hasAVX())
Duncan Sands28b77e92011-09-06 19:07:46 +0000972 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000973
David Greene9b9838d2009-06-29 16:47:10 +0000974 if (!UseSoftFloat && Subtarget->hasAVX()) {
Bruno Cardoso Lopesdbd4fe22011-07-21 02:24:08 +0000975 addRegisterClass(MVT::v32i8, X86::VR256RegisterClass);
976 addRegisterClass(MVT::v16i16, X86::VR256RegisterClass);
977 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
978 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
979 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
980 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000981
Owen Anderson825b72b2009-08-11 20:47:22 +0000982 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +0000983 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
984 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +0000985
Owen Anderson825b72b2009-08-11 20:47:22 +0000986 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
987 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
988 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
989 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
990 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
991 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000992
Owen Anderson825b72b2009-08-11 20:47:22 +0000993 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
994 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
995 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
996 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
997 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
998 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000999
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001000 setOperationAction(ISD::FP_TO_SINT, MVT::v8i32, Legal);
1001 setOperationAction(ISD::SINT_TO_FP, MVT::v8i32, Legal);
Bruno Cardoso Lopes55244ce2011-08-01 21:54:09 +00001002 setOperationAction(ISD::FP_ROUND, MVT::v4f32, Legal);
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001003
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00001004 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4f64, Custom);
1005 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i64, Custom);
1006 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8f32, Custom);
1007 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i32, Custom);
1008 setOperationAction(ISD::CONCAT_VECTORS, MVT::v32i8, Custom);
1009 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i16, Custom);
1010
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001011 setOperationAction(ISD::SRL, MVT::v4i64, Custom);
1012 setOperationAction(ISD::SRL, MVT::v8i32, Custom);
1013 setOperationAction(ISD::SRL, MVT::v16i16, Custom);
1014 setOperationAction(ISD::SRL, MVT::v32i8, Custom);
1015
1016 setOperationAction(ISD::SHL, MVT::v4i64, Custom);
1017 setOperationAction(ISD::SHL, MVT::v8i32, Custom);
1018 setOperationAction(ISD::SHL, MVT::v16i16, Custom);
1019 setOperationAction(ISD::SHL, MVT::v32i8, Custom);
1020
1021 setOperationAction(ISD::SRA, MVT::v8i32, Custom);
1022 setOperationAction(ISD::SRA, MVT::v16i16, Custom);
1023
Duncan Sands28b77e92011-09-06 19:07:46 +00001024 setOperationAction(ISD::SETCC, MVT::v32i8, Custom);
1025 setOperationAction(ISD::SETCC, MVT::v16i16, Custom);
1026 setOperationAction(ISD::SETCC, MVT::v8i32, Custom);
1027 setOperationAction(ISD::SETCC, MVT::v4i64, Custom);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00001028
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +00001029 setOperationAction(ISD::SELECT, MVT::v4f64, Custom);
1030 setOperationAction(ISD::SELECT, MVT::v4i64, Custom);
1031 setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
1032
Nadav Rotemfbad25e2011-09-11 15:02:23 +00001033 setOperationAction(ISD::VSELECT, MVT::v4f64, Legal);
1034 setOperationAction(ISD::VSELECT, MVT::v4i64, Legal);
1035 setOperationAction(ISD::VSELECT, MVT::v8i32, Legal);
1036 setOperationAction(ISD::VSELECT, MVT::v8f32, Legal);
Nadav Rotem8ffad562011-09-09 20:29:17 +00001037
Craig Topper13894fa2011-08-24 06:14:18 +00001038 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
1039 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
1040 setOperationAction(ISD::ADD, MVT::v16i16, Custom);
1041 setOperationAction(ISD::ADD, MVT::v32i8, Custom);
1042
1043 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
1044 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
1045 setOperationAction(ISD::SUB, MVT::v16i16, Custom);
1046 setOperationAction(ISD::SUB, MVT::v32i8, Custom);
1047
1048 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1049 setOperationAction(ISD::MUL, MVT::v8i32, Custom);
1050 setOperationAction(ISD::MUL, MVT::v16i16, Custom);
1051 // Don't lower v32i8 because there is no 128-bit byte mul
1052
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001053 // Custom lower several nodes for 256-bit types.
David Greene54d8eba2011-01-27 22:38:56 +00001054 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001055 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
1056 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
1057 EVT VT = SVT;
1058
1059 // Extract subvector is special because the value type
1060 // (result) is 128-bit but the source is 256-bit wide.
1061 if (VT.is128BitVector())
1062 setOperationAction(ISD::EXTRACT_SUBVECTOR, SVT, Custom);
1063
1064 // Do not attempt to custom lower other non-256-bit vectors
1065 if (!VT.is256BitVector())
David Greene9b9838d2009-06-29 16:47:10 +00001066 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001067
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001068 setOperationAction(ISD::BUILD_VECTOR, SVT, Custom);
1069 setOperationAction(ISD::VECTOR_SHUFFLE, SVT, Custom);
1070 setOperationAction(ISD::INSERT_VECTOR_ELT, SVT, Custom);
1071 setOperationAction(ISD::EXTRACT_VECTOR_ELT, SVT, Custom);
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00001072 setOperationAction(ISD::SCALAR_TO_VECTOR, SVT, Custom);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001073 setOperationAction(ISD::INSERT_SUBVECTOR, SVT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001074 }
1075
David Greene54d8eba2011-01-27 22:38:56 +00001076 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001077 for (unsigned i = (unsigned)MVT::v32i8; i != (unsigned)MVT::v4i64; ++i) {
1078 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
1079 EVT VT = SVT;
David Greene54d8eba2011-01-27 22:38:56 +00001080
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001081 // Do not attempt to promote non-256-bit vectors
1082 if (!VT.is256BitVector())
David Greene54d8eba2011-01-27 22:38:56 +00001083 continue;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001084
1085 setOperationAction(ISD::AND, SVT, Promote);
1086 AddPromotedToType (ISD::AND, SVT, MVT::v4i64);
1087 setOperationAction(ISD::OR, SVT, Promote);
1088 AddPromotedToType (ISD::OR, SVT, MVT::v4i64);
1089 setOperationAction(ISD::XOR, SVT, Promote);
1090 AddPromotedToType (ISD::XOR, SVT, MVT::v4i64);
1091 setOperationAction(ISD::LOAD, SVT, Promote);
1092 AddPromotedToType (ISD::LOAD, SVT, MVT::v4i64);
1093 setOperationAction(ISD::SELECT, SVT, Promote);
1094 AddPromotedToType (ISD::SELECT, SVT, MVT::v4i64);
David Greene54d8eba2011-01-27 22:38:56 +00001095 }
David Greene9b9838d2009-06-29 16:47:10 +00001096 }
1097
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001098 // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
1099 // of this type with custom code.
1100 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1101 VT != (unsigned)MVT::LAST_VECTOR_VALUETYPE; VT++) {
1102 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT, Custom);
1103 }
1104
Evan Cheng6be2c582006-04-05 23:38:46 +00001105 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001106 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001107
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001108
Eli Friedman962f5492010-06-02 19:35:46 +00001109 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1110 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001111 //
Eli Friedman962f5492010-06-02 19:35:46 +00001112 // FIXME: We really should do custom legalization for addition and
1113 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1114 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001115 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1116 // Add/Sub/Mul with overflow operations are custom lowered.
1117 MVT VT = IntVTs[i];
1118 setOperationAction(ISD::SADDO, VT, Custom);
1119 setOperationAction(ISD::UADDO, VT, Custom);
1120 setOperationAction(ISD::SSUBO, VT, Custom);
1121 setOperationAction(ISD::USUBO, VT, Custom);
1122 setOperationAction(ISD::SMULO, VT, Custom);
1123 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001124 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001125
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001126 // There are no 8-bit 3-address imul/mul instructions
1127 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1128 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001129
Evan Chengd54f2d52009-03-31 19:38:51 +00001130 if (!Subtarget->is64Bit()) {
1131 // These libcalls are not available in 32-bit.
1132 setLibcallName(RTLIB::SHL_I128, 0);
1133 setLibcallName(RTLIB::SRL_I128, 0);
1134 setLibcallName(RTLIB::SRA_I128, 0);
1135 }
1136
Evan Cheng206ee9d2006-07-07 08:33:52 +00001137 // We have target-specific dag combine patterns for the following nodes:
1138 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001139 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +00001140 setTargetDAGCombine(ISD::BUILD_VECTOR);
Duncan Sands6bcd2192011-09-17 16:49:39 +00001141 setTargetDAGCombine(ISD::VSELECT);
Chris Lattner83e6c992006-10-04 06:57:07 +00001142 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001143 setTargetDAGCombine(ISD::SHL);
1144 setTargetDAGCombine(ISD::SRA);
1145 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001146 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001147 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001148 setTargetDAGCombine(ISD::ADD);
Duncan Sands17470be2011-09-22 20:15:48 +00001149 setTargetDAGCombine(ISD::FADD);
1150 setTargetDAGCombine(ISD::FSUB);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001151 setTargetDAGCombine(ISD::SUB);
Nadav Rotem91e43fd2011-09-18 10:39:32 +00001152 setTargetDAGCombine(ISD::LOAD);
Chris Lattner149a4e52008-02-22 02:09:43 +00001153 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001154 setTargetDAGCombine(ISD::ZERO_EXTEND);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001155 setTargetDAGCombine(ISD::SINT_TO_FP);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001156 if (Subtarget->is64Bit())
1157 setTargetDAGCombine(ISD::MUL);
Craig Topperb4c94572011-10-21 06:55:01 +00001158 if (Subtarget->hasBMI())
1159 setTargetDAGCombine(ISD::XOR);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001160
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001161 computeRegisterProperties();
1162
Evan Cheng05219282011-01-06 06:52:41 +00001163 // On Darwin, -Os means optimize for size without hurting performance,
1164 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001165 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001166 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001167 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001168 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1169 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1170 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengfb8075d2008-02-28 00:43:03 +00001171 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001172 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001173
1174 setPrefFunctionAlignment(4);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001175}
1176
Scott Michel5b8f82e2008-03-10 15:42:14 +00001177
Duncan Sands28b77e92011-09-06 19:07:46 +00001178EVT X86TargetLowering::getSetCCResultType(EVT VT) const {
1179 if (!VT.isVector()) return MVT::i8;
1180 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +00001181}
1182
1183
Evan Cheng29286502008-01-23 23:17:41 +00001184/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1185/// the desired ByVal argument alignment.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001186static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
Evan Cheng29286502008-01-23 23:17:41 +00001187 if (MaxAlign == 16)
1188 return;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001189 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001190 if (VTy->getBitWidth() == 128)
1191 MaxAlign = 16;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001192 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001193 unsigned EltAlign = 0;
1194 getMaxByValAlign(ATy->getElementType(), EltAlign);
1195 if (EltAlign > MaxAlign)
1196 MaxAlign = EltAlign;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001197 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001198 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1199 unsigned EltAlign = 0;
1200 getMaxByValAlign(STy->getElementType(i), EltAlign);
1201 if (EltAlign > MaxAlign)
1202 MaxAlign = EltAlign;
1203 if (MaxAlign == 16)
1204 break;
1205 }
1206 }
1207 return;
1208}
1209
1210/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1211/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001212/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1213/// are at 4-byte boundaries.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001214unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001215 if (Subtarget->is64Bit()) {
1216 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001217 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001218 if (TyAlign > 8)
1219 return TyAlign;
1220 return 8;
1221 }
1222
Evan Cheng29286502008-01-23 23:17:41 +00001223 unsigned Align = 4;
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001224 if (Subtarget->hasXMM())
Dale Johannesen0c191872008-02-08 19:48:20 +00001225 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001226 return Align;
1227}
Chris Lattner2b02a442007-02-25 08:29:00 +00001228
Evan Chengf0df0312008-05-15 08:39:06 +00001229/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001230/// and store operations as a result of memset, memcpy, and memmove
1231/// lowering. If DstAlign is zero that means it's safe to destination
1232/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1233/// means there isn't a need to check it against alignment requirement,
1234/// probably because the source does not need to be loaded. If
Lang Hames15701f82011-10-26 23:50:43 +00001235/// 'IsZeroVal' is true, that means it's safe to return a
Evan Chengc3b0c342010-04-08 07:37:57 +00001236/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1237/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1238/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001239/// It returns EVT::Other if the type should be determined using generic
1240/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001241EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001242X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1243 unsigned DstAlign, unsigned SrcAlign,
Lang Hames15701f82011-10-26 23:50:43 +00001244 bool IsZeroVal,
Evan Chengc3b0c342010-04-08 07:37:57 +00001245 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001246 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001247 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1248 // linux. This is because the stack realignment code can't handle certain
1249 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001250 const Function *F = MF.getFunction();
Lang Hames15701f82011-10-26 23:50:43 +00001251 if (IsZeroVal &&
Evan Chenga5e13622011-01-07 19:35:30 +00001252 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001253 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001254 (Subtarget->isUnalignedMemAccessFast() ||
1255 ((DstAlign == 0 || DstAlign >= 16) &&
1256 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001257 Subtarget->getStackAlignment() >= 16) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00001258 if (Subtarget->hasAVX() &&
1259 Subtarget->getStackAlignment() >= 32)
1260 return MVT::v8f32;
1261 if (Subtarget->hasXMMInt())
Evan Cheng255f20f2010-04-01 06:04:33 +00001262 return MVT::v4i32;
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00001263 if (Subtarget->hasXMM())
Evan Cheng255f20f2010-04-01 06:04:33 +00001264 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001265 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001266 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001267 Subtarget->getStackAlignment() >= 8 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001268 Subtarget->hasXMMInt()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001269 // Do not use f64 to lower memcpy if source is string constant. It's
1270 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001271 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001272 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001273 }
Evan Chengf0df0312008-05-15 08:39:06 +00001274 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001275 return MVT::i64;
1276 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001277}
1278
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001279/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1280/// current function. The returned value is a member of the
1281/// MachineJumpTableInfo::JTEntryKind enum.
1282unsigned X86TargetLowering::getJumpTableEncoding() const {
1283 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1284 // symbol.
1285 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1286 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001287 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001288
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001289 // Otherwise, use the normal jump table encoding heuristics.
1290 return TargetLowering::getJumpTableEncoding();
1291}
1292
Chris Lattnerc64daab2010-01-26 05:02:42 +00001293const MCExpr *
1294X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1295 const MachineBasicBlock *MBB,
1296 unsigned uid,MCContext &Ctx) const{
1297 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1298 Subtarget->isPICStyleGOT());
1299 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1300 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001301 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1302 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001303}
1304
Evan Chengcc415862007-11-09 01:32:10 +00001305/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1306/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001307SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001308 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001309 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001310 // This doesn't have DebugLoc associated with it, but is not really the
1311 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001312 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001313 return Table;
1314}
1315
Chris Lattner589c6f62010-01-26 06:28:43 +00001316/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1317/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1318/// MCExpr.
1319const MCExpr *X86TargetLowering::
1320getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1321 MCContext &Ctx) const {
1322 // X86-64 uses RIP relative addressing based on the jump table label.
1323 if (Subtarget->isPICStyleRIPRel())
1324 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1325
1326 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001327 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001328}
1329
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001330// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001331std::pair<const TargetRegisterClass*, uint8_t>
1332X86TargetLowering::findRepresentativeClass(EVT VT) const{
1333 const TargetRegisterClass *RRC = 0;
1334 uint8_t Cost = 1;
1335 switch (VT.getSimpleVT().SimpleTy) {
1336 default:
1337 return TargetLowering::findRepresentativeClass(VT);
1338 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
1339 RRC = (Subtarget->is64Bit()
1340 ? X86::GR64RegisterClass : X86::GR32RegisterClass);
1341 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001342 case MVT::x86mmx:
Evan Chengdee81012010-07-26 21:50:05 +00001343 RRC = X86::VR64RegisterClass;
1344 break;
1345 case MVT::f32: case MVT::f64:
1346 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1347 case MVT::v4f32: case MVT::v2f64:
1348 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1349 case MVT::v4f64:
1350 RRC = X86::VR128RegisterClass;
1351 break;
1352 }
1353 return std::make_pair(RRC, Cost);
1354}
1355
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001356bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1357 unsigned &Offset) const {
1358 if (!Subtarget->isTargetLinux())
1359 return false;
1360
1361 if (Subtarget->is64Bit()) {
1362 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1363 Offset = 0x28;
1364 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1365 AddressSpace = 256;
1366 else
1367 AddressSpace = 257;
1368 } else {
1369 // %gs:0x14 on i386
1370 Offset = 0x14;
1371 AddressSpace = 256;
1372 }
1373 return true;
1374}
1375
1376
Chris Lattner2b02a442007-02-25 08:29:00 +00001377//===----------------------------------------------------------------------===//
1378// Return Value Calling Convention Implementation
1379//===----------------------------------------------------------------------===//
1380
Chris Lattner59ed56b2007-02-28 04:55:35 +00001381#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001382
Michael J. Spencerec38de22010-10-10 22:04:20 +00001383bool
Eric Christopher471e4222011-06-08 23:55:35 +00001384X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
1385 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001386 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001387 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001388 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001389 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001390 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001391 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001392}
1393
Dan Gohman98ca4f22009-08-05 01:29:28 +00001394SDValue
1395X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001396 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001397 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001398 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001399 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001400 MachineFunction &MF = DAG.getMachineFunction();
1401 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001402
Chris Lattner9774c912007-02-27 05:28:59 +00001403 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001404 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001405 RVLocs, *DAG.getContext());
1406 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001407
Evan Chengdcea1632010-02-04 02:40:39 +00001408 // Add the regs to the liveout set for the function.
1409 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1410 for (unsigned i = 0; i != RVLocs.size(); ++i)
1411 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1412 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001413
Dan Gohman475871a2008-07-27 21:46:04 +00001414 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001415
Dan Gohman475871a2008-07-27 21:46:04 +00001416 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001417 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1418 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001419 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1420 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001421
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001422 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001423 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1424 CCValAssign &VA = RVLocs[i];
1425 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001426 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001427 EVT ValVT = ValToCopy.getValueType();
1428
Dale Johannesenc4510512010-09-24 19:05:48 +00001429 // If this is x86-64, and we disabled SSE, we can't return FP values,
1430 // or SSE or MMX vectors.
1431 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1432 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001433 (Subtarget->is64Bit() && !Subtarget->hasXMM())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001434 report_fatal_error("SSE register return with SSE disabled");
1435 }
1436 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1437 // llvm-gcc has never done it right and no one has noticed, so this
1438 // should be OK for now.
1439 if (ValVT == MVT::f64 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001440 (Subtarget->is64Bit() && !Subtarget->hasXMMInt()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001441 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001442
Chris Lattner447ff682008-03-11 03:23:40 +00001443 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1444 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001445 if (VA.getLocReg() == X86::ST0 ||
1446 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001447 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1448 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001449 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001450 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001451 RetOps.push_back(ValToCopy);
1452 // Don't emit a copytoreg.
1453 continue;
1454 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001455
Evan Cheng242b38b2009-02-23 09:03:22 +00001456 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1457 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001458 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001459 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001460 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001461 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001462 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1463 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001464 // If we don't have SSE2 available, convert to v4f32 so the generated
1465 // register is legal.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00001466 if (!Subtarget->hasXMMInt())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001467 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001468 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001469 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001470 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001471
Dale Johannesendd64c412009-02-04 00:33:20 +00001472 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001473 Flag = Chain.getValue(1);
1474 }
Dan Gohman61a92132008-04-21 23:59:07 +00001475
1476 // The x86-64 ABI for returning structs by value requires that we copy
1477 // the sret argument into %rax for the return. We saved the argument into
1478 // a virtual register in the entry block, so now we copy the value out
1479 // and into %rax.
1480 if (Subtarget->is64Bit() &&
1481 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1482 MachineFunction &MF = DAG.getMachineFunction();
1483 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1484 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001485 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001486 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001487 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001488
Dale Johannesendd64c412009-02-04 00:33:20 +00001489 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001490 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001491
1492 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001493 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001494 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001495
Chris Lattner447ff682008-03-11 03:23:40 +00001496 RetOps[0] = Chain; // Update chain.
1497
1498 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001499 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001500 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001501
1502 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001503 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001504}
1505
Evan Cheng3d2125c2010-11-30 23:55:39 +00001506bool X86TargetLowering::isUsedByReturnOnly(SDNode *N) const {
1507 if (N->getNumValues() != 1)
1508 return false;
1509 if (!N->hasNUsesOfValue(1, 0))
1510 return false;
1511
1512 SDNode *Copy = *N->use_begin();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001513 if (Copy->getOpcode() != ISD::CopyToReg &&
1514 Copy->getOpcode() != ISD::FP_EXTEND)
Evan Cheng3d2125c2010-11-30 23:55:39 +00001515 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001516
1517 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001518 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001519 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001520 if (UI->getOpcode() != X86ISD::RET_FLAG)
1521 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001522 HasRet = true;
1523 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001524
Evan Cheng1bf891a2010-12-01 22:59:46 +00001525 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001526}
1527
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001528EVT
1529X86TargetLowering::getTypeForExtArgOrReturn(LLVMContext &Context, EVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001530 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001531 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001532 // TODO: Is this also valid on 32-bit?
1533 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001534 ReturnMVT = MVT::i8;
1535 else
1536 ReturnMVT = MVT::i32;
1537
1538 EVT MinVT = getRegisterType(Context, ReturnMVT);
1539 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001540}
1541
Dan Gohman98ca4f22009-08-05 01:29:28 +00001542/// LowerCallResult - Lower the result values of a call into the
1543/// appropriate copies out of appropriate physical registers.
1544///
1545SDValue
1546X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001547 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001548 const SmallVectorImpl<ISD::InputArg> &Ins,
1549 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001550 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001551
Chris Lattnere32bbf62007-02-28 07:09:55 +00001552 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001553 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001554 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001555 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1556 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001557 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001558
Chris Lattner3085e152007-02-25 08:59:22 +00001559 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001560 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001561 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001562 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001563
Torok Edwin3f142c32009-02-01 18:15:56 +00001564 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001565 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001566 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasXMM())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001567 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001568 }
1569
Evan Cheng79fb3b42009-02-20 20:43:02 +00001570 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001571
1572 // If this is a call to a function that returns an fp value on the floating
1573 // point stack, we must guarantee the the value is popped from the stack, so
1574 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001575 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001576 // instead.
1577 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1578 // If we prefer to use the value in xmm registers, copy it out as f80 and
1579 // use a truncate to move it from fp stack reg to xmm reg.
1580 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001581 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001582 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1583 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001584 Val = Chain.getValue(0);
1585
1586 // Round the f80 to the right size, which also moves it to the appropriate
1587 // xmm register.
1588 if (CopyVT != VA.getValVT())
1589 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1590 // This truncation won't change the value.
1591 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001592 } else {
1593 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1594 CopyVT, InFlag).getValue(1);
1595 Val = Chain.getValue(0);
1596 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001597 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001598 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001599 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001600
Dan Gohman98ca4f22009-08-05 01:29:28 +00001601 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001602}
1603
1604
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001605//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001606// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001607//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001608// StdCall calling convention seems to be standard for many Windows' API
1609// routines and around. It differs from C calling convention just a little:
1610// callee should clean up the stack, not caller. Symbols should be also
1611// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001612// For info on fast calling convention see Fast Calling Convention (tail call)
1613// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001614
Dan Gohman98ca4f22009-08-05 01:29:28 +00001615/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001616/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001617static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1618 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001619 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001620
Dan Gohman98ca4f22009-08-05 01:29:28 +00001621 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001622}
1623
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001624/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001625/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001626static bool
1627ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1628 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001629 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001630
Dan Gohman98ca4f22009-08-05 01:29:28 +00001631 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001632}
1633
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001634/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1635/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001636/// the specific parameter attribute. The copy will be passed as a byval
1637/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001638static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001639CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001640 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1641 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001642 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001643
Dale Johannesendd64c412009-02-04 00:33:20 +00001644 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001645 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001646 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001647}
1648
Chris Lattner29689432010-03-11 00:22:57 +00001649/// IsTailCallConvention - Return true if the calling convention is one that
1650/// supports tail call optimization.
1651static bool IsTailCallConvention(CallingConv::ID CC) {
1652 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1653}
1654
Evan Cheng485fafc2011-03-21 01:19:09 +00001655bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1656 if (!CI->isTailCall())
1657 return false;
1658
1659 CallSite CS(CI);
1660 CallingConv::ID CalleeCC = CS.getCallingConv();
1661 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1662 return false;
1663
1664 return true;
1665}
1666
Evan Cheng0c439eb2010-01-27 00:07:07 +00001667/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1668/// a tailcall target by changing its ABI.
1669static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001670 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001671}
1672
Dan Gohman98ca4f22009-08-05 01:29:28 +00001673SDValue
1674X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001675 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001676 const SmallVectorImpl<ISD::InputArg> &Ins,
1677 DebugLoc dl, SelectionDAG &DAG,
1678 const CCValAssign &VA,
1679 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001680 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001681 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001682 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001683 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001684 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001685 EVT ValVT;
1686
1687 // If value is passed by pointer we have address passed instead of the value
1688 // itself.
1689 if (VA.getLocInfo() == CCValAssign::Indirect)
1690 ValVT = VA.getLocVT();
1691 else
1692 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001693
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001694 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001695 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001696 // In case of tail call optimization mark all arguments mutable. Since they
1697 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001698 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001699 unsigned Bytes = Flags.getByValSize();
1700 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1701 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001702 return DAG.getFrameIndex(FI, getPointerTy());
1703 } else {
1704 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001705 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001706 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1707 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001708 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00001709 false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001710 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001711}
1712
Dan Gohman475871a2008-07-27 21:46:04 +00001713SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001714X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001715 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001716 bool isVarArg,
1717 const SmallVectorImpl<ISD::InputArg> &Ins,
1718 DebugLoc dl,
1719 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001720 SmallVectorImpl<SDValue> &InVals)
1721 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001722 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001723 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001724
Gordon Henriksen86737662008-01-05 16:56:59 +00001725 const Function* Fn = MF.getFunction();
1726 if (Fn->hasExternalLinkage() &&
1727 Subtarget->isTargetCygMing() &&
1728 Fn->getName() == "main")
1729 FuncInfo->setForceFramePointer(true);
1730
Evan Cheng1bc78042006-04-26 01:20:17 +00001731 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001732 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001733 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001734
Chris Lattner29689432010-03-11 00:22:57 +00001735 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1736 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001737
Chris Lattner638402b2007-02-28 07:00:42 +00001738 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001739 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001740 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001741 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001742
1743 // Allocate shadow area for Win64
1744 if (IsWin64) {
1745 CCInfo.AllocateStack(32, 8);
1746 }
1747
Duncan Sands45907662010-10-31 13:21:44 +00001748 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001749
Chris Lattnerf39f7712007-02-28 05:46:49 +00001750 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001751 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001752 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1753 CCValAssign &VA = ArgLocs[i];
1754 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1755 // places.
1756 assert(VA.getValNo() != LastVal &&
1757 "Don't support value assigned to multiple locs yet");
Duncan Sands17001ce2011-10-18 12:44:00 +00001758 (void)LastVal;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001759 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001760
Chris Lattnerf39f7712007-02-28 05:46:49 +00001761 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001762 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001763 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001764 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001765 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001766 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001767 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001768 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001769 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001770 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001771 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001772 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1773 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001774 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001775 RC = X86::VR128RegisterClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001776 else if (RegVT == MVT::x86mmx)
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001777 RC = X86::VR64RegisterClass;
1778 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001779 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001780
Devang Patel68e6bee2011-02-21 23:21:26 +00001781 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001782 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001783
Chris Lattnerf39f7712007-02-28 05:46:49 +00001784 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1785 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1786 // right size.
1787 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001788 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001789 DAG.getValueType(VA.getValVT()));
1790 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001791 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001792 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001793 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001794 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001795
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001796 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001797 // Handle MMX values passed in XMM regs.
1798 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001799 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1800 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001801 } else
1802 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001803 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001804 } else {
1805 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001806 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001807 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001808
1809 // If value is passed via pointer - do a load.
1810 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001811 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
1812 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001813
Dan Gohman98ca4f22009-08-05 01:29:28 +00001814 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001815 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001816
Dan Gohman61a92132008-04-21 23:59:07 +00001817 // The x86-64 ABI for returning structs by value requires that we copy
1818 // the sret argument into %rax for the return. Save the argument into
1819 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001820 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001821 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1822 unsigned Reg = FuncInfo->getSRetReturnReg();
1823 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001824 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001825 FuncInfo->setSRetReturnReg(Reg);
1826 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001827 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001828 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001829 }
1830
Chris Lattnerf39f7712007-02-28 05:46:49 +00001831 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001832 // Align stack specially for tail calls.
1833 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001834 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001835
Evan Cheng1bc78042006-04-26 01:20:17 +00001836 // If the function takes variable number of arguments, make a frame index for
1837 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001838 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001839 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1840 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001841 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001842 }
1843 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001844 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1845
1846 // FIXME: We should really autogenerate these arrays
1847 static const unsigned GPR64ArgRegsWin64[] = {
1848 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001849 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001850 static const unsigned GPR64ArgRegs64Bit[] = {
1851 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1852 };
1853 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001854 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1855 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1856 };
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001857 const unsigned *GPR64ArgRegs;
1858 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001859
1860 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001861 // The XMM registers which might contain var arg parameters are shadowed
1862 // in their paired GPR. So we only need to save the GPR to their home
1863 // slots.
1864 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001865 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001866 } else {
1867 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1868 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001869
1870 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit, TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001871 }
1872 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1873 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001874
Devang Patel578efa92009-06-05 21:57:13 +00001875 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001876 assert(!(NumXMMRegs && !Subtarget->hasXMM()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001877 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001878 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001879 "SSE register cannot be used when SSE is disabled!");
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001880 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasXMM())
Torok Edwin3f142c32009-02-01 18:15:56 +00001881 // Kernel mode asks for SSE to be disabled, so don't push them
1882 // on the stack.
1883 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001884
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001885 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001886 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001887 // Get to the caller-allocated home save location. Add 8 to account
1888 // for the return address.
1889 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001890 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001891 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001892 // Fixup to set vararg frame on shadow area (4 x i64).
1893 if (NumIntRegs < 4)
1894 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001895 } else {
1896 // For X86-64, if there are vararg parameters that are passed via
1897 // registers, then we must store them to their spots on the stack so they
1898 // may be loaded by deferencing the result of va_next.
1899 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1900 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1901 FuncInfo->setRegSaveFrameIndex(
1902 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00001903 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001904 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001905
Gordon Henriksen86737662008-01-05 16:56:59 +00001906 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001907 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001908 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1909 getPointerTy());
1910 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001911 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001912 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1913 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001914 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001915 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001916 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001917 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001918 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001919 MachinePointerInfo::getFixedStack(
1920 FuncInfo->getRegSaveFrameIndex(), Offset),
1921 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001922 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001923 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001924 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001925
Dan Gohmanface41a2009-08-16 21:24:25 +00001926 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1927 // Now store the XMM (fp + vector) parameter registers.
1928 SmallVector<SDValue, 11> SaveXMMOps;
1929 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001930
Devang Patel68e6bee2011-02-21 23:21:26 +00001931 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001932 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1933 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001934
Dan Gohman1e93df62010-04-17 14:41:14 +00001935 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1936 FuncInfo->getRegSaveFrameIndex()));
1937 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1938 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001939
Dan Gohmanface41a2009-08-16 21:24:25 +00001940 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001941 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001942 X86::VR128RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001943 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1944 SaveXMMOps.push_back(Val);
1945 }
1946 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1947 MVT::Other,
1948 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001949 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001950
1951 if (!MemOps.empty())
1952 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1953 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001954 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001955 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001956
Gordon Henriksen86737662008-01-05 16:56:59 +00001957 // Some CCs need callee pop.
Evan Chengef41ff62011-06-23 17:54:54 +00001958 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg, GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001959 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001960 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001961 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001962 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001963 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001964 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001965 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001966
Gordon Henriksen86737662008-01-05 16:56:59 +00001967 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001968 // RegSaveFrameIndex is X86-64 only.
1969 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001970 if (CallConv == CallingConv::X86_FastCall ||
1971 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001972 // fastcc functions can't have varargs.
1973 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001974 }
Evan Cheng25caf632006-05-23 21:06:34 +00001975
Rafael Espindola76927d752011-08-30 19:39:58 +00001976 FuncInfo->setArgumentStackSize(StackSize);
1977
Dan Gohman98ca4f22009-08-05 01:29:28 +00001978 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001979}
1980
Dan Gohman475871a2008-07-27 21:46:04 +00001981SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001982X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1983 SDValue StackPtr, SDValue Arg,
1984 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001985 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001986 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001987 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001988 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001989 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001990 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00001991 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001992
1993 return DAG.getStore(Chain, dl, Arg, PtrOff,
1994 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00001995 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001996}
1997
Bill Wendling64e87322009-01-16 19:25:27 +00001998/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001999/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00002000SDValue
2001X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00002002 SDValue &OutRetAddr, SDValue Chain,
2003 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00002004 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002005 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00002006 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002007 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00002008
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002009 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00002010 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
2011 false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00002012 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002013}
2014
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002015/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002016/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00002017static SDValue
2018EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00002019 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00002020 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002021 // Store the return address to the appropriate stack slot.
2022 if (!FPDiff) return Chain;
2023 // Calculate the new stack slot for the return address.
2024 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00002025 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00002026 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00002027 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00002028 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002029 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00002030 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00002031 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002032 return Chain;
2033}
2034
Dan Gohman98ca4f22009-08-05 01:29:28 +00002035SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00002036X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002037 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00002038 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002039 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002040 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002041 const SmallVectorImpl<ISD::InputArg> &Ins,
2042 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002043 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002044 MachineFunction &MF = DAG.getMachineFunction();
2045 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002046 bool IsWin64 = Subtarget->isTargetWin64();
Dan Gohman98ca4f22009-08-05 01:29:28 +00002047 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00002048 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002049
Evan Cheng5f941932010-02-05 02:21:12 +00002050 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002051 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00002052 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
2053 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00002054 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00002055
2056 // Sibcalls are automatically detected tailcalls which do not require
2057 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00002058 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002059 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002060
2061 if (isTailCall)
2062 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002063 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002064
Chris Lattner29689432010-03-11 00:22:57 +00002065 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
2066 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002067
Chris Lattner638402b2007-02-28 07:00:42 +00002068 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002069 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002070 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002071 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002072
2073 // Allocate shadow area for Win64
2074 if (IsWin64) {
2075 CCInfo.AllocateStack(32, 8);
2076 }
2077
Duncan Sands45907662010-10-31 13:21:44 +00002078 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002079
Chris Lattner423c5f42007-02-28 05:31:48 +00002080 // Get a count of how many bytes are to be pushed on the stack.
2081 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002082 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002083 // This is a sibcall. The memory operands are available in caller's
2084 // own caller's stack.
2085 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00002086 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002087 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002088
Gordon Henriksen86737662008-01-05 16:56:59 +00002089 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002090 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002091 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00002092 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00002093 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2094 FPDiff = NumBytesCallerPushed - NumBytes;
2095
2096 // Set the delta of movement of the returnaddr stackslot.
2097 // But only set if delta is greater than previous delta.
2098 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2099 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2100 }
2101
Evan Chengf22f9b32010-02-06 03:28:46 +00002102 if (!IsSibcall)
2103 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002104
Dan Gohman475871a2008-07-27 21:46:04 +00002105 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002106 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002107 if (isTailCall && FPDiff)
2108 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2109 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002110
Dan Gohman475871a2008-07-27 21:46:04 +00002111 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2112 SmallVector<SDValue, 8> MemOpChains;
2113 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002114
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002115 // Walk the register/memloc assignments, inserting copies/loads. In the case
2116 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002117 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2118 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002119 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002120 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002121 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002122 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002123
Chris Lattner423c5f42007-02-28 05:31:48 +00002124 // Promote the value if needed.
2125 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002126 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002127 case CCValAssign::Full: break;
2128 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002129 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002130 break;
2131 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002132 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002133 break;
2134 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002135 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
2136 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002137 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002138 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2139 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002140 } else
2141 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2142 break;
2143 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002144 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002145 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002146 case CCValAssign::Indirect: {
2147 // Store the argument.
2148 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002149 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002150 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002151 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002152 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002153 Arg = SpillSlot;
2154 break;
2155 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002156 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002157
Chris Lattner423c5f42007-02-28 05:31:48 +00002158 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002159 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2160 if (isVarArg && IsWin64) {
2161 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2162 // shadow reg if callee is a varargs function.
2163 unsigned ShadowReg = 0;
2164 switch (VA.getLocReg()) {
2165 case X86::XMM0: ShadowReg = X86::RCX; break;
2166 case X86::XMM1: ShadowReg = X86::RDX; break;
2167 case X86::XMM2: ShadowReg = X86::R8; break;
2168 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002169 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002170 if (ShadowReg)
2171 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002172 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002173 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002174 assert(VA.isMemLoc());
2175 if (StackPtr.getNode() == 0)
2176 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2177 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2178 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002179 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002180 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002181
Evan Cheng32fe1032006-05-25 00:59:30 +00002182 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002183 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002184 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002185
Evan Cheng347d5f72006-04-28 21:29:37 +00002186 // Build a sequence of copy-to-reg nodes chained together with token chain
2187 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002188 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002189 // Tail call byval lowering might overwrite argument registers so in case of
2190 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002191 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002192 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002193 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002194 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002195 InFlag = Chain.getValue(1);
2196 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002197
Chris Lattner88e1fd52009-07-09 04:24:46 +00002198 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002199 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2200 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002201 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002202 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2203 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00002204 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002205 InFlag);
2206 InFlag = Chain.getValue(1);
2207 } else {
2208 // If we are tail calling and generating PIC/GOT style code load the
2209 // address of the callee into ECX. The value in ecx is used as target of
2210 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2211 // for tail calls on PIC/GOT architectures. Normally we would just put the
2212 // address of GOT into ebx and then call target@PLT. But for tail calls
2213 // ebx would be restored (since ebx is callee saved) before jumping to the
2214 // target@PLT.
2215
2216 // Note: The actual moving to ECX is done further down.
2217 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2218 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2219 !G->getGlobal()->hasProtectedVisibility())
2220 Callee = LowerGlobalAddress(Callee, DAG);
2221 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002222 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002223 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002224 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002225
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002226 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002227 // From AMD64 ABI document:
2228 // For calls that may call functions that use varargs or stdargs
2229 // (prototype-less calls or calls to functions containing ellipsis (...) in
2230 // the declaration) %al is used as hidden argument to specify the number
2231 // of SSE registers used. The contents of %al do not need to match exactly
2232 // the number of registers, but must be an ubound on the number of SSE
2233 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002234
Gordon Henriksen86737662008-01-05 16:56:59 +00002235 // Count the number of XMM registers allocated.
2236 static const unsigned XMMArgRegs[] = {
2237 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2238 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2239 };
2240 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00002241 assert((Subtarget->hasXMM() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002242 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002243
Dale Johannesendd64c412009-02-04 00:33:20 +00002244 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002245 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002246 InFlag = Chain.getValue(1);
2247 }
2248
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002249
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002250 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002251 if (isTailCall) {
2252 // Force all the incoming stack arguments to be loaded from the stack
2253 // before any new outgoing arguments are stored to the stack, because the
2254 // outgoing stack slots may alias the incoming argument stack slots, and
2255 // the alias isn't otherwise explicit. This is slightly more conservative
2256 // than necessary, because it means that each store effectively depends
2257 // on every argument instead of just those arguments it would clobber.
2258 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2259
Dan Gohman475871a2008-07-27 21:46:04 +00002260 SmallVector<SDValue, 8> MemOpChains2;
2261 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002262 int FI = 0;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002263 // Do not flag preceding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002264 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002265 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002266 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2267 CCValAssign &VA = ArgLocs[i];
2268 if (VA.isRegLoc())
2269 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002270 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002271 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002272 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002273 // Create frame index.
2274 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002275 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002276 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002277 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002278
Duncan Sands276dcbd2008-03-21 09:14:45 +00002279 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002280 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002281 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002282 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002283 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002284 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002285 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002286
Dan Gohman98ca4f22009-08-05 01:29:28 +00002287 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2288 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002289 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002290 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002291 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002292 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002293 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002294 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002295 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002296 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002297 }
2298 }
2299
2300 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002301 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002302 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002303
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002304 // Copy arguments to their registers.
2305 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002306 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002307 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002308 InFlag = Chain.getValue(1);
2309 }
Dan Gohman475871a2008-07-27 21:46:04 +00002310 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002311
Gordon Henriksen86737662008-01-05 16:56:59 +00002312 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002313 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002314 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002315 }
2316
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002317 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2318 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2319 // In the 64-bit large code model, we have to make all calls
2320 // through a register, since the call instruction's 32-bit
2321 // pc-relative offset may not be large enough to hold the whole
2322 // address.
2323 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002324 // If the callee is a GlobalAddress node (quite common, every direct call
2325 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2326 // it.
2327
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002328 // We should use extra load for direct calls to dllimported functions in
2329 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002330 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002331 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002332 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002333 bool ExtraLoad = false;
2334 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002335
Chris Lattner48a7d022009-07-09 05:02:21 +00002336 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2337 // external symbols most go through the PLT in PIC mode. If the symbol
2338 // has hidden or protected visibility, or if it is static or local, then
2339 // we don't need to use the PLT - we can directly call it.
2340 if (Subtarget->isTargetELF() &&
2341 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002342 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002343 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002344 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002345 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002346 (!Subtarget->getTargetTriple().isMacOSX() ||
2347 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002348 // PC-relative references to external symbols should go through $stub,
2349 // unless we're building with the leopard linker or later, which
2350 // automatically synthesizes these stubs.
2351 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002352 } else if (Subtarget->isPICStyleRIPRel() &&
2353 isa<Function>(GV) &&
2354 cast<Function>(GV)->hasFnAttr(Attribute::NonLazyBind)) {
2355 // If the function is marked as non-lazy, generate an indirect call
2356 // which loads from the GOT directly. This avoids runtime overhead
2357 // at the cost of eager binding (and one extra byte of encoding).
2358 OpFlags = X86II::MO_GOTPCREL;
2359 WrapperKind = X86ISD::WrapperRIP;
2360 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002361 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002362
Devang Patel0d881da2010-07-06 22:08:15 +00002363 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002364 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002365
2366 // Add a wrapper if needed.
2367 if (WrapperKind != ISD::DELETED_NODE)
2368 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2369 // Add extra indirection if needed.
2370 if (ExtraLoad)
2371 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2372 MachinePointerInfo::getGOT(),
2373 false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002374 }
Bill Wendling056292f2008-09-16 21:48:12 +00002375 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002376 unsigned char OpFlags = 0;
2377
Evan Cheng1bf891a2010-12-01 22:59:46 +00002378 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2379 // external symbols should go through the PLT.
2380 if (Subtarget->isTargetELF() &&
2381 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2382 OpFlags = X86II::MO_PLT;
2383 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002384 (!Subtarget->getTargetTriple().isMacOSX() ||
2385 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002386 // PC-relative references to external symbols should go through $stub,
2387 // unless we're building with the leopard linker or later, which
2388 // automatically synthesizes these stubs.
2389 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002390 }
Eric Christopherfd179292009-08-27 18:07:15 +00002391
Chris Lattner48a7d022009-07-09 05:02:21 +00002392 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2393 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002394 }
2395
Chris Lattnerd96d0722007-02-25 06:40:16 +00002396 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002397 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002398 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002399
Evan Chengf22f9b32010-02-06 03:28:46 +00002400 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002401 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2402 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002403 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002404 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002405
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002406 Ops.push_back(Chain);
2407 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002408
Dan Gohman98ca4f22009-08-05 01:29:28 +00002409 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002410 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002411
Gordon Henriksen86737662008-01-05 16:56:59 +00002412 // Add argument registers to the end of the list so that they are known live
2413 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002414 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2415 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2416 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002417
Evan Cheng586ccac2008-03-18 23:36:35 +00002418 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002419 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002420 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2421
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002422 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002423 if (Is64Bit && isVarArg && !IsWin64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002424 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002425
Gabor Greifba36cb52008-08-28 21:40:38 +00002426 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002427 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002428
Dan Gohman98ca4f22009-08-05 01:29:28 +00002429 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002430 // We used to do:
2431 //// If this is the first return lowered for this function, add the regs
2432 //// to the liveout set for the function.
2433 // This isn't right, although it's probably harmless on x86; liveouts
2434 // should be computed from returns not tail calls. Consider a void
2435 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002436 return DAG.getNode(X86ISD::TC_RETURN, dl,
2437 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002438 }
2439
Dale Johannesenace16102009-02-03 19:33:06 +00002440 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002441 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002442
Chris Lattner2d297092006-05-23 18:50:38 +00002443 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002444 unsigned NumBytesForCalleeToPush;
Evan Chengef41ff62011-06-23 17:54:54 +00002445 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg, GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002446 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002447 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002448 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002449 // pops the hidden struct pointer, so we have to push it back.
2450 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002451 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002452 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002453 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002454
Gordon Henriksenae636f82008-01-03 16:47:34 +00002455 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002456 if (!IsSibcall) {
2457 Chain = DAG.getCALLSEQ_END(Chain,
2458 DAG.getIntPtrConstant(NumBytes, true),
2459 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2460 true),
2461 InFlag);
2462 InFlag = Chain.getValue(1);
2463 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002464
Chris Lattner3085e152007-02-25 08:59:22 +00002465 // Handle result values, copying them out of physregs into vregs that we
2466 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002467 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2468 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002469}
2470
Evan Cheng25ab6902006-09-08 06:48:29 +00002471
2472//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002473// Fast Calling Convention (tail call) implementation
2474//===----------------------------------------------------------------------===//
2475
2476// Like std call, callee cleans arguments, convention except that ECX is
2477// reserved for storing the tail called function address. Only 2 registers are
2478// free for argument passing (inreg). Tail call optimization is performed
2479// provided:
2480// * tailcallopt is enabled
2481// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002482// On X86_64 architecture with GOT-style position independent code only local
2483// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002484// To keep the stack aligned according to platform abi the function
2485// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2486// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002487// If a tail called function callee has more arguments than the caller the
2488// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002489// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002490// original REtADDR, but before the saved framepointer or the spilled registers
2491// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2492// stack layout:
2493// arg1
2494// arg2
2495// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002496// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002497// move area ]
2498// (possible EBP)
2499// ESI
2500// EDI
2501// local1 ..
2502
2503/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2504/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002505unsigned
2506X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2507 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002508 MachineFunction &MF = DAG.getMachineFunction();
2509 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002510 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002511 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002512 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002513 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002514 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002515 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2516 // Number smaller than 12 so just add the difference.
2517 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2518 } else {
2519 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002520 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002521 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002522 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002523 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002524}
2525
Evan Cheng5f941932010-02-05 02:21:12 +00002526/// MatchingStackOffset - Return true if the given stack call argument is
2527/// already available in the same position (relatively) of the caller's
2528/// incoming argument stack.
2529static
2530bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2531 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2532 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002533 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2534 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002535 if (Arg.getOpcode() == ISD::CopyFromReg) {
2536 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002537 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002538 return false;
2539 MachineInstr *Def = MRI->getVRegDef(VR);
2540 if (!Def)
2541 return false;
2542 if (!Flags.isByVal()) {
2543 if (!TII->isLoadFromStackSlot(Def, FI))
2544 return false;
2545 } else {
2546 unsigned Opcode = Def->getOpcode();
2547 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2548 Def->getOperand(1).isFI()) {
2549 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002550 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002551 } else
2552 return false;
2553 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002554 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2555 if (Flags.isByVal())
2556 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002557 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002558 // define @foo(%struct.X* %A) {
2559 // tail call @bar(%struct.X* byval %A)
2560 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002561 return false;
2562 SDValue Ptr = Ld->getBasePtr();
2563 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2564 if (!FINode)
2565 return false;
2566 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002567 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002568 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002569 FI = FINode->getIndex();
2570 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002571 } else
2572 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002573
Evan Cheng4cae1332010-03-05 08:38:04 +00002574 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002575 if (!MFI->isFixedObjectIndex(FI))
2576 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002577 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002578}
2579
Dan Gohman98ca4f22009-08-05 01:29:28 +00002580/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2581/// for tail call optimization. Targets which want to do tail call
2582/// optimization should implement this function.
2583bool
2584X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002585 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002586 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002587 bool isCalleeStructRet,
2588 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002589 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002590 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002591 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002592 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002593 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002594 CalleeCC != CallingConv::C)
2595 return false;
2596
Evan Cheng7096ae42010-01-29 06:45:59 +00002597 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002598 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002599 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002600 CallingConv::ID CallerCC = CallerF->getCallingConv();
2601 bool CCMatch = CallerCC == CalleeCC;
2602
Dan Gohman1797ed52010-02-08 20:27:50 +00002603 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002604 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002605 return true;
2606 return false;
2607 }
2608
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002609 // Look for obvious safe cases to perform tail call optimization that do not
2610 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002611
Evan Cheng2c12cb42010-03-26 16:26:03 +00002612 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2613 // emit a special epilogue.
2614 if (RegInfo->needsStackRealignment(MF))
2615 return false;
2616
Evan Chenga375d472010-03-15 18:54:48 +00002617 // Also avoid sibcall optimization if either caller or callee uses struct
2618 // return semantics.
2619 if (isCalleeStructRet || isCallerStructRet)
2620 return false;
2621
Chad Rosier2416da32011-06-24 21:15:36 +00002622 // An stdcall caller is expected to clean up its arguments; the callee
2623 // isn't going to do that.
2624 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2625 return false;
2626
Chad Rosier871f6642011-05-18 19:59:50 +00002627 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002628 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002629 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002630
2631 // Optimizing for varargs on Win64 is unlikely to be safe without
2632 // additional testing.
2633 if (Subtarget->isTargetWin64())
2634 return false;
2635
Chad Rosier871f6642011-05-18 19:59:50 +00002636 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002637 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2638 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002639
Chad Rosier871f6642011-05-18 19:59:50 +00002640 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2641 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2642 if (!ArgLocs[i].isRegLoc())
2643 return false;
2644 }
2645
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002646 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2647 // Therefore if it's not used by the call it is not safe to optimize this into
2648 // a sibcall.
2649 bool Unused = false;
2650 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2651 if (!Ins[i].Used) {
2652 Unused = true;
2653 break;
2654 }
2655 }
2656 if (Unused) {
2657 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002658 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
2659 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002660 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002661 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002662 CCValAssign &VA = RVLocs[i];
2663 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2664 return false;
2665 }
2666 }
2667
Evan Cheng13617962010-04-30 01:12:32 +00002668 // If the calling conventions do not match, then we'd better make sure the
2669 // results are returned in the same way as what the caller expects.
2670 if (!CCMatch) {
2671 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002672 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
2673 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002674 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2675
2676 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002677 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
2678 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002679 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2680
2681 if (RVLocs1.size() != RVLocs2.size())
2682 return false;
2683 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2684 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2685 return false;
2686 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2687 return false;
2688 if (RVLocs1[i].isRegLoc()) {
2689 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2690 return false;
2691 } else {
2692 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2693 return false;
2694 }
2695 }
2696 }
2697
Evan Chenga6bff982010-01-30 01:22:00 +00002698 // If the callee takes no arguments then go on to check the results of the
2699 // call.
2700 if (!Outs.empty()) {
2701 // Check if stack adjustment is needed. For now, do not do this if any
2702 // argument is passed on the stack.
2703 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002704 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2705 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002706
2707 // Allocate shadow area for Win64
2708 if (Subtarget->isTargetWin64()) {
2709 CCInfo.AllocateStack(32, 8);
2710 }
2711
Duncan Sands45907662010-10-31 13:21:44 +00002712 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002713 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002714 MachineFunction &MF = DAG.getMachineFunction();
2715 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2716 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002717
2718 // Check if the arguments are already laid out in the right way as
2719 // the caller's fixed stack objects.
2720 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002721 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2722 const X86InstrInfo *TII =
2723 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002724 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2725 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002726 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002727 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002728 if (VA.getLocInfo() == CCValAssign::Indirect)
2729 return false;
2730 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002731 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2732 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002733 return false;
2734 }
2735 }
2736 }
Evan Cheng9c044672010-05-29 01:35:22 +00002737
2738 // If the tailcall address may be in a register, then make sure it's
2739 // possible to register allocate for it. In 32-bit, the call address can
2740 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002741 // callee-saved registers are restored. These happen to be the same
2742 // registers used to pass 'inreg' arguments so watch out for those.
2743 if (!Subtarget->is64Bit() &&
2744 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002745 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002746 unsigned NumInRegs = 0;
2747 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2748 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002749 if (!VA.isRegLoc())
2750 continue;
2751 unsigned Reg = VA.getLocReg();
2752 switch (Reg) {
2753 default: break;
2754 case X86::EAX: case X86::EDX: case X86::ECX:
2755 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002756 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002757 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002758 }
2759 }
2760 }
Evan Chenga6bff982010-01-30 01:22:00 +00002761 }
Evan Chengb1712452010-01-27 06:25:16 +00002762
Evan Cheng86809cc2010-02-03 03:28:02 +00002763 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002764}
2765
Dan Gohman3df24e62008-09-03 23:12:08 +00002766FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002767X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2768 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002769}
2770
2771
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002772//===----------------------------------------------------------------------===//
2773// Other Lowering Hooks
2774//===----------------------------------------------------------------------===//
2775
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002776static bool MayFoldLoad(SDValue Op) {
2777 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2778}
2779
2780static bool MayFoldIntoStore(SDValue Op) {
2781 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2782}
2783
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002784static bool isTargetShuffle(unsigned Opcode) {
2785 switch(Opcode) {
2786 default: return false;
2787 case X86ISD::PSHUFD:
2788 case X86ISD::PSHUFHW:
2789 case X86ISD::PSHUFLW:
2790 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002791 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002792 case X86ISD::SHUFPS:
2793 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002794 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002795 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002796 case X86ISD::MOVLPS:
2797 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002798 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002799 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002800 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002801 case X86ISD::MOVSS:
2802 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002803 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002804 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002805 case X86ISD::VUNPCKLPSY:
2806 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002807 case X86ISD::PUNPCKLWD:
2808 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002809 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002810 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002811 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002812 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00002813 case X86ISD::VUNPCKHPSY:
2814 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002815 case X86ISD::PUNPCKHWD:
2816 case X86ISD::PUNPCKHBW:
2817 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002818 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00002819 case X86ISD::VPERMILPS:
2820 case X86ISD::VPERMILPSY:
2821 case X86ISD::VPERMILPD:
2822 case X86ISD::VPERMILPDY:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00002823 case X86ISD::VPERM2F128:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002824 return true;
2825 }
2826 return false;
2827}
2828
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002829static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002830 SDValue V1, SelectionDAG &DAG) {
2831 switch(Opc) {
2832 default: llvm_unreachable("Unknown x86 shuffle node");
2833 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002834 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002835 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002836 return DAG.getNode(Opc, dl, VT, V1);
2837 }
2838
2839 return SDValue();
2840}
2841
2842static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002843 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002844 switch(Opc) {
2845 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002846 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002847 case X86ISD::PSHUFHW:
2848 case X86ISD::PSHUFLW:
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00002849 case X86ISD::VPERMILPS:
2850 case X86ISD::VPERMILPSY:
2851 case X86ISD::VPERMILPD:
2852 case X86ISD::VPERMILPDY:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002853 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2854 }
2855
2856 return SDValue();
2857}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002858
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002859static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2860 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2861 switch(Opc) {
2862 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002863 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002864 case X86ISD::SHUFPD:
2865 case X86ISD::SHUFPS:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00002866 case X86ISD::VPERM2F128:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002867 return DAG.getNode(Opc, dl, VT, V1, V2,
2868 DAG.getConstant(TargetMask, MVT::i8));
2869 }
2870 return SDValue();
2871}
2872
2873static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2874 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2875 switch(Opc) {
2876 default: llvm_unreachable("Unknown x86 shuffle node");
2877 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002878 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002879 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002880 case X86ISD::MOVLPS:
2881 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002882 case X86ISD::MOVSS:
2883 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002884 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002885 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002886 case X86ISD::VUNPCKLPSY:
2887 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002888 case X86ISD::PUNPCKLWD:
2889 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002890 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002891 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002892 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002893 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00002894 case X86ISD::VUNPCKHPSY:
2895 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002896 case X86ISD::PUNPCKHWD:
2897 case X86ISD::PUNPCKHBW:
2898 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002899 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002900 return DAG.getNode(Opc, dl, VT, V1, V2);
2901 }
2902 return SDValue();
2903}
2904
Dan Gohmand858e902010-04-17 15:26:15 +00002905SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002906 MachineFunction &MF = DAG.getMachineFunction();
2907 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2908 int ReturnAddrIndex = FuncInfo->getRAIndex();
2909
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002910 if (ReturnAddrIndex == 0) {
2911 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002912 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002913 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002914 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002915 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002916 }
2917
Evan Cheng25ab6902006-09-08 06:48:29 +00002918 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002919}
2920
2921
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002922bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2923 bool hasSymbolicDisplacement) {
2924 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002925 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002926 return false;
2927
2928 // If we don't have a symbolic displacement - we don't have any extra
2929 // restrictions.
2930 if (!hasSymbolicDisplacement)
2931 return true;
2932
2933 // FIXME: Some tweaks might be needed for medium code model.
2934 if (M != CodeModel::Small && M != CodeModel::Kernel)
2935 return false;
2936
2937 // For small code model we assume that latest object is 16MB before end of 31
2938 // bits boundary. We may also accept pretty large negative constants knowing
2939 // that all objects are in the positive half of address space.
2940 if (M == CodeModel::Small && Offset < 16*1024*1024)
2941 return true;
2942
2943 // For kernel code model we know that all object resist in the negative half
2944 // of 32bits address space. We may not accept negative offsets, since they may
2945 // be just off and we may accept pretty large positive ones.
2946 if (M == CodeModel::Kernel && Offset > 0)
2947 return true;
2948
2949 return false;
2950}
2951
Evan Chengef41ff62011-06-23 17:54:54 +00002952/// isCalleePop - Determines whether the callee is required to pop its
2953/// own arguments. Callee pop is necessary to support tail calls.
2954bool X86::isCalleePop(CallingConv::ID CallingConv,
2955 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
2956 if (IsVarArg)
2957 return false;
2958
2959 switch (CallingConv) {
2960 default:
2961 return false;
2962 case CallingConv::X86_StdCall:
2963 return !is64Bit;
2964 case CallingConv::X86_FastCall:
2965 return !is64Bit;
2966 case CallingConv::X86_ThisCall:
2967 return !is64Bit;
2968 case CallingConv::Fast:
2969 return TailCallOpt;
2970 case CallingConv::GHC:
2971 return TailCallOpt;
2972 }
2973}
2974
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002975/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2976/// specific condition code, returning the condition code and the LHS/RHS of the
2977/// comparison to make.
2978static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2979 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002980 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002981 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2982 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2983 // X > -1 -> X == 0, jump !sign.
2984 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002985 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002986 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2987 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002988 return X86::COND_S;
Andrew Trickf6c39412011-03-23 23:11:02 +00002989 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002990 // X < 1 -> X <= 0
2991 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002992 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002993 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002994 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002995
Evan Chengd9558e02006-01-06 00:43:03 +00002996 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002997 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002998 case ISD::SETEQ: return X86::COND_E;
2999 case ISD::SETGT: return X86::COND_G;
3000 case ISD::SETGE: return X86::COND_GE;
3001 case ISD::SETLT: return X86::COND_L;
3002 case ISD::SETLE: return X86::COND_LE;
3003 case ISD::SETNE: return X86::COND_NE;
3004 case ISD::SETULT: return X86::COND_B;
3005 case ISD::SETUGT: return X86::COND_A;
3006 case ISD::SETULE: return X86::COND_BE;
3007 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00003008 }
Chris Lattner4c78e022008-12-23 23:42:27 +00003009 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003010
Chris Lattner4c78e022008-12-23 23:42:27 +00003011 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00003012
Chris Lattner4c78e022008-12-23 23:42:27 +00003013 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00003014 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
3015 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00003016 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
3017 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00003018 }
3019
Chris Lattner4c78e022008-12-23 23:42:27 +00003020 switch (SetCCOpcode) {
3021 default: break;
3022 case ISD::SETOLT:
3023 case ISD::SETOLE:
3024 case ISD::SETUGT:
3025 case ISD::SETUGE:
3026 std::swap(LHS, RHS);
3027 break;
3028 }
3029
3030 // On a floating point condition, the flags are set as follows:
3031 // ZF PF CF op
3032 // 0 | 0 | 0 | X > Y
3033 // 0 | 0 | 1 | X < Y
3034 // 1 | 0 | 0 | X == Y
3035 // 1 | 1 | 1 | unordered
3036 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003037 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00003038 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003039 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00003040 case ISD::SETOLT: // flipped
3041 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003042 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00003043 case ISD::SETOLE: // flipped
3044 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003045 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003046 case ISD::SETUGT: // flipped
3047 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003048 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00003049 case ISD::SETUGE: // flipped
3050 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003051 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003052 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003053 case ISD::SETNE: return X86::COND_NE;
3054 case ISD::SETUO: return X86::COND_P;
3055 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003056 case ISD::SETOEQ:
3057 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003058 }
Evan Chengd9558e02006-01-06 00:43:03 +00003059}
3060
Evan Cheng4a460802006-01-11 00:33:36 +00003061/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3062/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003063/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003064static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003065 switch (X86CC) {
3066 default:
3067 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003068 case X86::COND_B:
3069 case X86::COND_BE:
3070 case X86::COND_E:
3071 case X86::COND_P:
3072 case X86::COND_A:
3073 case X86::COND_AE:
3074 case X86::COND_NE:
3075 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003076 return true;
3077 }
3078}
3079
Evan Chengeb2f9692009-10-27 19:56:55 +00003080/// isFPImmLegal - Returns true if the target can instruction select the
3081/// specified FP immediate natively. If false, the legalizer will
3082/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003083bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003084 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3085 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3086 return true;
3087 }
3088 return false;
3089}
3090
Nate Begeman9008ca62009-04-27 18:41:29 +00003091/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3092/// the specified range (L, H].
3093static bool isUndefOrInRange(int Val, int Low, int Hi) {
3094 return (Val < 0) || (Val >= Low && Val < Hi);
3095}
3096
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00003097/// isUndefOrInRange - Return true if every element in Mask, begining
3098/// from position Pos and ending in Pos+Size, falls within the specified
3099/// range (L, L+Pos]. or is undef.
3100static bool isUndefOrInRange(const SmallVectorImpl<int> &Mask,
3101 int Pos, int Size, int Low, int Hi) {
3102 for (int i = Pos, e = Pos+Size; i != e; ++i)
3103 if (!isUndefOrInRange(Mask[i], Low, Hi))
3104 return false;
3105 return true;
3106}
3107
Nate Begeman9008ca62009-04-27 18:41:29 +00003108/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3109/// specified value.
3110static bool isUndefOrEqual(int Val, int CmpVal) {
3111 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003112 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003113 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00003114}
3115
Bruno Cardoso Lopes4002d7e2011-08-12 21:54:42 +00003116/// isSequentialOrUndefInRange - Return true if every element in Mask, begining
3117/// from position Pos and ending in Pos+Size, falls within the specified
3118/// sequential range (L, L+Pos]. or is undef.
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003119static bool isSequentialOrUndefInRange(const SmallVectorImpl<int> &Mask,
3120 int Pos, int Size, int Low) {
3121 for (int i = Pos, e = Pos+Size; i != e; ++i, ++Low)
3122 if (!isUndefOrEqual(Mask[i], Low))
3123 return false;
3124 return true;
3125}
3126
Nate Begeman9008ca62009-04-27 18:41:29 +00003127/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3128/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3129/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00003130static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003131 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003132 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003133 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003134 return (Mask[0] < 2 && Mask[1] < 2);
3135 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003136}
3137
Nate Begeman9008ca62009-04-27 18:41:29 +00003138bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003139 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003140 N->getMask(M);
3141 return ::isPSHUFDMask(M, N->getValueType(0));
3142}
Evan Cheng0188ecb2006-03-22 18:59:22 +00003143
Nate Begeman9008ca62009-04-27 18:41:29 +00003144/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3145/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00003146static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003147 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00003148 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003149
Nate Begeman9008ca62009-04-27 18:41:29 +00003150 // Lower quadword copied in order or undef.
3151 for (int i = 0; i != 4; ++i)
3152 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00003153 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003154
Evan Cheng506d3df2006-03-29 23:07:14 +00003155 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003156 for (int i = 4; i != 8; ++i)
3157 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00003158 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003159
Evan Cheng506d3df2006-03-29 23:07:14 +00003160 return true;
3161}
3162
Nate Begeman9008ca62009-04-27 18:41:29 +00003163bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003164 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003165 N->getMask(M);
3166 return ::isPSHUFHWMask(M, N->getValueType(0));
3167}
Evan Cheng506d3df2006-03-29 23:07:14 +00003168
Nate Begeman9008ca62009-04-27 18:41:29 +00003169/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3170/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00003171static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003172 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00003173 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003174
Rafael Espindola15684b22009-04-24 12:40:33 +00003175 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00003176 for (int i = 4; i != 8; ++i)
3177 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00003178 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003179
Rafael Espindola15684b22009-04-24 12:40:33 +00003180 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003181 for (int i = 0; i != 4; ++i)
3182 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00003183 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003184
Rafael Espindola15684b22009-04-24 12:40:33 +00003185 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003186}
3187
Nate Begeman9008ca62009-04-27 18:41:29 +00003188bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003189 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003190 N->getMask(M);
3191 return ::isPSHUFLWMask(M, N->getValueType(0));
3192}
3193
Nate Begemana09008b2009-10-19 02:17:23 +00003194/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3195/// is suitable for input to PALIGNR.
3196static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00003197 bool hasSSSE3OrAVX) {
Nate Begemana09008b2009-10-19 02:17:23 +00003198 int i, e = VT.getVectorNumElements();
Bruno Cardoso Lopes9065d4b2011-07-29 01:30:59 +00003199 if (VT.getSizeInBits() != 128 && VT.getSizeInBits() != 64)
3200 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003201
Nate Begemana09008b2009-10-19 02:17:23 +00003202 // Do not handle v2i64 / v2f64 shuffles with palignr.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00003203 if (e < 4 || !hasSSSE3OrAVX)
Nate Begemana09008b2009-10-19 02:17:23 +00003204 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003205
Nate Begemana09008b2009-10-19 02:17:23 +00003206 for (i = 0; i != e; ++i)
3207 if (Mask[i] >= 0)
3208 break;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003209
Nate Begemana09008b2009-10-19 02:17:23 +00003210 // All undef, not a palignr.
3211 if (i == e)
3212 return false;
3213
Eli Friedman63f8dde2011-07-25 21:36:45 +00003214 // Make sure we're shifting in the right direction.
3215 if (Mask[i] <= i)
3216 return false;
Nate Begemana09008b2009-10-19 02:17:23 +00003217
3218 int s = Mask[i] - i;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003219
Nate Begemana09008b2009-10-19 02:17:23 +00003220 // Check the rest of the elements to see if they are consecutive.
3221 for (++i; i != e; ++i) {
3222 int m = Mask[i];
Eli Friedman63f8dde2011-07-25 21:36:45 +00003223 if (m >= 0 && m != s+i)
Nate Begemana09008b2009-10-19 02:17:23 +00003224 return false;
3225 }
3226 return true;
3227}
3228
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003229/// isVSHUFPSYMask - Return true if the specified VECTOR_SHUFFLE operand
3230/// specifies a shuffle of elements that is suitable for input to 256-bit
3231/// VSHUFPSY.
3232static bool isVSHUFPSYMask(const SmallVectorImpl<int> &Mask, EVT VT,
3233 const X86Subtarget *Subtarget) {
3234 int NumElems = VT.getVectorNumElements();
3235
3236 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256)
3237 return false;
3238
3239 if (NumElems != 8)
3240 return false;
3241
3242 // VSHUFPSY divides the resulting vector into 4 chunks.
3243 // The sources are also splitted into 4 chunks, and each destination
3244 // chunk must come from a different source chunk.
3245 //
3246 // SRC1 => X7 X6 X5 X4 X3 X2 X1 X0
3247 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3248 //
3249 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3250 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3251 //
3252 int QuarterSize = NumElems/4;
3253 int HalfSize = QuarterSize*2;
3254 for (int i = 0; i < QuarterSize; ++i)
3255 if (!isUndefOrInRange(Mask[i], 0, HalfSize))
3256 return false;
3257 for (int i = QuarterSize; i < QuarterSize*2; ++i)
3258 if (!isUndefOrInRange(Mask[i], NumElems, NumElems+HalfSize))
3259 return false;
3260
3261 // The mask of the second half must be the same as the first but with
3262 // the appropriate offsets. This works in the same way as VPERMILPS
3263 // works with masks.
3264 for (int i = QuarterSize*2; i < QuarterSize*3; ++i) {
3265 if (!isUndefOrInRange(Mask[i], HalfSize, NumElems))
3266 return false;
3267 int FstHalfIdx = i-HalfSize;
3268 if (Mask[FstHalfIdx] < 0)
3269 continue;
3270 if (!isUndefOrEqual(Mask[i], Mask[FstHalfIdx]+HalfSize))
3271 return false;
3272 }
3273 for (int i = QuarterSize*3; i < NumElems; ++i) {
3274 if (!isUndefOrInRange(Mask[i], NumElems+HalfSize, NumElems*2))
3275 return false;
3276 int FstHalfIdx = i-HalfSize;
3277 if (Mask[FstHalfIdx] < 0)
3278 continue;
3279 if (!isUndefOrEqual(Mask[i], Mask[FstHalfIdx]+HalfSize))
3280 return false;
3281
3282 }
3283
3284 return true;
3285}
3286
3287/// getShuffleVSHUFPSYImmediate - Return the appropriate immediate to shuffle
3288/// the specified VECTOR_MASK mask with VSHUFPSY instruction.
3289static unsigned getShuffleVSHUFPSYImmediate(SDNode *N) {
3290 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3291 EVT VT = SVOp->getValueType(0);
3292 int NumElems = VT.getVectorNumElements();
3293
3294 assert(NumElems == 8 && VT.getSizeInBits() == 256 &&
3295 "Only supports v8i32 and v8f32 types");
3296
3297 int HalfSize = NumElems/2;
3298 unsigned Mask = 0;
3299 for (int i = 0; i != NumElems ; ++i) {
3300 if (SVOp->getMaskElt(i) < 0)
3301 continue;
3302 // The mask of the first half must be equal to the second one.
3303 unsigned Shamt = (i%HalfSize)*2;
3304 unsigned Elt = SVOp->getMaskElt(i) % HalfSize;
3305 Mask |= Elt << Shamt;
3306 }
3307
3308 return Mask;
3309}
3310
3311/// isVSHUFPDYMask - Return true if the specified VECTOR_SHUFFLE operand
3312/// specifies a shuffle of elements that is suitable for input to 256-bit
3313/// VSHUFPDY. This shuffle doesn't have the same restriction as the PS
3314/// version and the mask of the second half isn't binded with the first
3315/// one.
3316static bool isVSHUFPDYMask(const SmallVectorImpl<int> &Mask, EVT VT,
3317 const X86Subtarget *Subtarget) {
3318 int NumElems = VT.getVectorNumElements();
3319
3320 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256)
3321 return false;
3322
3323 if (NumElems != 4)
3324 return false;
3325
3326 // VSHUFPSY divides the resulting vector into 4 chunks.
3327 // The sources are also splitted into 4 chunks, and each destination
3328 // chunk must come from a different source chunk.
3329 //
3330 // SRC1 => X3 X2 X1 X0
3331 // SRC2 => Y3 Y2 Y1 Y0
3332 //
3333 // DST => Y2..Y3, X2..X3, Y1..Y0, X1..X0
3334 //
3335 int QuarterSize = NumElems/4;
3336 int HalfSize = QuarterSize*2;
3337 for (int i = 0; i < QuarterSize; ++i)
3338 if (!isUndefOrInRange(Mask[i], 0, HalfSize))
3339 return false;
3340 for (int i = QuarterSize; i < QuarterSize*2; ++i)
3341 if (!isUndefOrInRange(Mask[i], NumElems, NumElems+HalfSize))
3342 return false;
3343 for (int i = QuarterSize*2; i < QuarterSize*3; ++i)
3344 if (!isUndefOrInRange(Mask[i], HalfSize, NumElems))
3345 return false;
3346 for (int i = QuarterSize*3; i < NumElems; ++i)
3347 if (!isUndefOrInRange(Mask[i], NumElems+HalfSize, NumElems*2))
3348 return false;
3349
3350 return true;
3351}
3352
3353/// getShuffleVSHUFPDYImmediate - Return the appropriate immediate to shuffle
3354/// the specified VECTOR_MASK mask with VSHUFPDY instruction.
3355static unsigned getShuffleVSHUFPDYImmediate(SDNode *N) {
3356 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3357 EVT VT = SVOp->getValueType(0);
3358 int NumElems = VT.getVectorNumElements();
3359
3360 assert(NumElems == 4 && VT.getSizeInBits() == 256 &&
3361 "Only supports v4i64 and v4f64 types");
3362
3363 int HalfSize = NumElems/2;
3364 unsigned Mask = 0;
3365 for (int i = 0; i != NumElems ; ++i) {
3366 if (SVOp->getMaskElt(i) < 0)
3367 continue;
3368 int Elt = SVOp->getMaskElt(i) % HalfSize;
3369 Mask |= Elt << i;
3370 }
3371
3372 return Mask;
3373}
3374
Evan Cheng14aed5e2006-03-24 01:18:28 +00003375/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopesaf002d82011-08-24 23:17:55 +00003376/// specifies a shuffle of elements that is suitable for input to 128-bit
3377/// SHUFPS and SHUFPD.
Owen Andersone50ed302009-08-10 22:56:29 +00003378static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003379 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesaf002d82011-08-24 23:17:55 +00003380
3381 if (VT.getSizeInBits() != 128)
3382 return false;
3383
Nate Begeman9008ca62009-04-27 18:41:29 +00003384 if (NumElems != 2 && NumElems != 4)
3385 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003386
Nate Begeman9008ca62009-04-27 18:41:29 +00003387 int Half = NumElems / 2;
3388 for (int i = 0; i < Half; ++i)
3389 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003390 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003391 for (int i = Half; i < NumElems; ++i)
3392 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003393 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003394
Evan Cheng14aed5e2006-03-24 01:18:28 +00003395 return true;
3396}
3397
Nate Begeman9008ca62009-04-27 18:41:29 +00003398bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
3399 SmallVector<int, 8> M;
3400 N->getMask(M);
3401 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003402}
3403
Evan Cheng213d2cf2007-05-17 18:45:50 +00003404/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00003405/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
3406/// half elements to come from vector 1 (which would equal the dest.) and
3407/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00003408static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003409 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003410
3411 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00003412 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003413
Nate Begeman9008ca62009-04-27 18:41:29 +00003414 int Half = NumElems / 2;
3415 for (int i = 0; i < Half; ++i)
3416 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003417 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003418 for (int i = Half; i < NumElems; ++i)
3419 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003420 return false;
3421 return true;
3422}
3423
Nate Begeman9008ca62009-04-27 18:41:29 +00003424static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
3425 SmallVector<int, 8> M;
3426 N->getMask(M);
3427 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003428}
3429
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003430/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3431/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00003432bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003433 EVT VT = N->getValueType(0);
3434 unsigned NumElems = VT.getVectorNumElements();
3435
3436 if (VT.getSizeInBits() != 128)
3437 return false;
3438
3439 if (NumElems != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003440 return false;
3441
Evan Cheng2064a2b2006-03-28 06:50:32 +00003442 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00003443 return isUndefOrEqual(N->getMaskElt(0), 6) &&
3444 isUndefOrEqual(N->getMaskElt(1), 7) &&
3445 isUndefOrEqual(N->getMaskElt(2), 2) &&
3446 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003447}
3448
Nate Begeman0b10b912009-11-07 23:17:15 +00003449/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3450/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3451/// <2, 3, 2, 3>
3452bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003453 EVT VT = N->getValueType(0);
3454 unsigned NumElems = VT.getVectorNumElements();
3455
3456 if (VT.getSizeInBits() != 128)
3457 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003458
Nate Begeman0b10b912009-11-07 23:17:15 +00003459 if (NumElems != 4)
3460 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003461
Nate Begeman0b10b912009-11-07 23:17:15 +00003462 return isUndefOrEqual(N->getMaskElt(0), 2) &&
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003463 isUndefOrEqual(N->getMaskElt(1), 3) &&
3464 isUndefOrEqual(N->getMaskElt(2), 2) &&
3465 isUndefOrEqual(N->getMaskElt(3), 3);
Nate Begeman0b10b912009-11-07 23:17:15 +00003466}
3467
Evan Cheng5ced1d82006-04-06 23:23:56 +00003468/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3469/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003470bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3471 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003472
Evan Cheng5ced1d82006-04-06 23:23:56 +00003473 if (NumElems != 2 && NumElems != 4)
3474 return false;
3475
Evan Chengc5cdff22006-04-07 21:53:05 +00003476 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003477 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003478 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003479
Evan Chengc5cdff22006-04-07 21:53:05 +00003480 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003481 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003482 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003483
3484 return true;
3485}
3486
Nate Begeman0b10b912009-11-07 23:17:15 +00003487/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3488/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3489bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003490 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003491
David Greenea20244d2011-03-02 17:23:43 +00003492 if ((NumElems != 2 && NumElems != 4)
3493 || N->getValueType(0).getSizeInBits() > 128)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003494 return false;
3495
Evan Chengc5cdff22006-04-07 21:53:05 +00003496 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003497 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003498 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003499
Nate Begeman9008ca62009-04-27 18:41:29 +00003500 for (unsigned i = 0; i < NumElems/2; ++i)
3501 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003502 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003503
3504 return true;
3505}
3506
Evan Cheng0038e592006-03-28 00:39:58 +00003507/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3508/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003509static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003510 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003511 int NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003512
3513 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3514 "Unsupported vector type for unpckh");
3515
3516 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8)
Evan Cheng0038e592006-03-28 00:39:58 +00003517 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003518
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003519 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3520 // independently on 128-bit lanes.
3521 unsigned NumLanes = VT.getSizeInBits()/128;
3522 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003523
3524 unsigned Start = 0;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003525 unsigned End = NumLaneElts;
3526 for (unsigned s = 0; s < NumLanes; ++s) {
3527 for (unsigned i = Start, j = s * NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003528 i != End;
3529 i += 2, ++j) {
3530 int BitI = Mask[i];
3531 int BitI1 = Mask[i+1];
3532 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003533 return false;
David Greenea20244d2011-03-02 17:23:43 +00003534 if (V2IsSplat) {
3535 if (!isUndefOrEqual(BitI1, NumElts))
3536 return false;
3537 } else {
3538 if (!isUndefOrEqual(BitI1, j + NumElts))
3539 return false;
3540 }
Evan Cheng39623da2006-04-20 08:58:49 +00003541 }
David Greenea20244d2011-03-02 17:23:43 +00003542 // Process the next 128 bits.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003543 Start += NumLaneElts;
3544 End += NumLaneElts;
Evan Cheng0038e592006-03-28 00:39:58 +00003545 }
David Greenea20244d2011-03-02 17:23:43 +00003546
Evan Cheng0038e592006-03-28 00:39:58 +00003547 return true;
3548}
3549
Nate Begeman9008ca62009-04-27 18:41:29 +00003550bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3551 SmallVector<int, 8> M;
3552 N->getMask(M);
3553 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003554}
3555
Evan Cheng4fcb9222006-03-28 02:43:26 +00003556/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3557/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003558static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003559 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003560 int NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003561
3562 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3563 "Unsupported vector type for unpckh");
3564
3565 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8)
Evan Cheng4fcb9222006-03-28 02:43:26 +00003566 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003567
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003568 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3569 // independently on 128-bit lanes.
3570 unsigned NumLanes = VT.getSizeInBits()/128;
3571 unsigned NumLaneElts = NumElts/NumLanes;
3572
3573 unsigned Start = 0;
3574 unsigned End = NumLaneElts;
3575 for (unsigned l = 0; l != NumLanes; ++l) {
3576 for (unsigned i = Start, j = (l*NumLaneElts)+NumLaneElts/2;
3577 i != End; i += 2, ++j) {
3578 int BitI = Mask[i];
3579 int BitI1 = Mask[i+1];
3580 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003581 return false;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003582 if (V2IsSplat) {
3583 if (isUndefOrEqual(BitI1, NumElts))
3584 return false;
3585 } else {
3586 if (!isUndefOrEqual(BitI1, j+NumElts))
3587 return false;
3588 }
Evan Cheng39623da2006-04-20 08:58:49 +00003589 }
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003590 // Process the next 128 bits.
3591 Start += NumLaneElts;
3592 End += NumLaneElts;
Evan Cheng4fcb9222006-03-28 02:43:26 +00003593 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003594 return true;
3595}
3596
Nate Begeman9008ca62009-04-27 18:41:29 +00003597bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3598 SmallVector<int, 8> M;
3599 N->getMask(M);
3600 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003601}
3602
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003603/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3604/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3605/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003606static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003607 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003608 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003609 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003610
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003611 // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern
3612 // FIXME: Need a better way to get rid of this, there's no latency difference
3613 // between UNPCKLPD and MOVDDUP, the later should always be checked first and
3614 // the former later. We should also remove the "_undef" special mask.
3615 if (NumElems == 4 && VT.getSizeInBits() == 256)
3616 return false;
3617
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003618 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3619 // independently on 128-bit lanes.
3620 unsigned NumLanes = VT.getSizeInBits() / 128;
3621 unsigned NumLaneElts = NumElems / NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003622
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003623 for (unsigned s = 0; s < NumLanes; ++s) {
3624 for (unsigned i = s * NumLaneElts, j = s * NumLaneElts;
3625 i != NumLaneElts * (s + 1);
David Greenea20244d2011-03-02 17:23:43 +00003626 i += 2, ++j) {
3627 int BitI = Mask[i];
3628 int BitI1 = Mask[i+1];
3629
3630 if (!isUndefOrEqual(BitI, j))
3631 return false;
3632 if (!isUndefOrEqual(BitI1, j))
3633 return false;
3634 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003635 }
David Greenea20244d2011-03-02 17:23:43 +00003636
Rafael Espindola15684b22009-04-24 12:40:33 +00003637 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003638}
3639
Nate Begeman9008ca62009-04-27 18:41:29 +00003640bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3641 SmallVector<int, 8> M;
3642 N->getMask(M);
3643 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3644}
3645
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003646/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3647/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3648/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003649static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003650 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003651 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3652 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003653
Nate Begeman9008ca62009-04-27 18:41:29 +00003654 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3655 int BitI = Mask[i];
3656 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003657 if (!isUndefOrEqual(BitI, j))
3658 return false;
3659 if (!isUndefOrEqual(BitI1, j))
3660 return false;
3661 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003662 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003663}
3664
Nate Begeman9008ca62009-04-27 18:41:29 +00003665bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3666 SmallVector<int, 8> M;
3667 N->getMask(M);
3668 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3669}
3670
Evan Cheng017dcc62006-04-21 01:05:10 +00003671/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3672/// specifies a shuffle of elements that is suitable for input to MOVSS,
3673/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003674static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003675 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003676 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003677
3678 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003679
Nate Begeman9008ca62009-04-27 18:41:29 +00003680 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003681 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003682
Nate Begeman9008ca62009-04-27 18:41:29 +00003683 for (int i = 1; i < NumElts; ++i)
3684 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003685 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003686
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003687 return true;
3688}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003689
Nate Begeman9008ca62009-04-27 18:41:29 +00003690bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3691 SmallVector<int, 8> M;
3692 N->getMask(M);
3693 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003694}
3695
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003696/// isVPERM2F128Mask - Match 256-bit shuffles where the elements are considered
3697/// as permutations between 128-bit chunks or halves. As an example: this
3698/// shuffle bellow:
3699/// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
3700/// The first half comes from the second half of V1 and the second half from the
3701/// the second half of V2.
3702static bool isVPERM2F128Mask(const SmallVectorImpl<int> &Mask, EVT VT,
3703 const X86Subtarget *Subtarget) {
3704 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256)
3705 return false;
3706
3707 // The shuffle result is divided into half A and half B. In total the two
3708 // sources have 4 halves, namely: C, D, E, F. The final values of A and
3709 // B must come from C, D, E or F.
3710 int HalfSize = VT.getVectorNumElements()/2;
3711 bool MatchA = false, MatchB = false;
3712
3713 // Check if A comes from one of C, D, E, F.
3714 for (int Half = 0; Half < 4; ++Half) {
3715 if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
3716 MatchA = true;
3717 break;
3718 }
3719 }
3720
3721 // Check if B comes from one of C, D, E, F.
3722 for (int Half = 0; Half < 4; ++Half) {
3723 if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) {
3724 MatchB = true;
3725 break;
3726 }
3727 }
3728
3729 return MatchA && MatchB;
3730}
3731
3732/// getShuffleVPERM2F128Immediate - Return the appropriate immediate to shuffle
3733/// the specified VECTOR_MASK mask with VPERM2F128 instructions.
3734static unsigned getShuffleVPERM2F128Immediate(SDNode *N) {
3735 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3736 EVT VT = SVOp->getValueType(0);
3737
3738 int HalfSize = VT.getVectorNumElements()/2;
3739
3740 int FstHalf = 0, SndHalf = 0;
3741 for (int i = 0; i < HalfSize; ++i) {
3742 if (SVOp->getMaskElt(i) > 0) {
3743 FstHalf = SVOp->getMaskElt(i)/HalfSize;
3744 break;
3745 }
3746 }
3747 for (int i = HalfSize; i < HalfSize*2; ++i) {
3748 if (SVOp->getMaskElt(i) > 0) {
3749 SndHalf = SVOp->getMaskElt(i)/HalfSize;
3750 break;
3751 }
3752 }
3753
3754 return (FstHalf | (SndHalf << 4));
3755}
3756
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003757/// isVPERMILPDMask - Return true if the specified VECTOR_SHUFFLE operand
3758/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
3759/// Note that VPERMIL mask matching is different depending whether theunderlying
3760/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3761/// to the same elements of the low, but to the higher half of the source.
3762/// In VPERMILPD the two lanes could be shuffled independently of each other
3763/// with the same restriction that lanes can't be crossed.
3764static bool isVPERMILPDMask(const SmallVectorImpl<int> &Mask, EVT VT,
3765 const X86Subtarget *Subtarget) {
3766 int NumElts = VT.getVectorNumElements();
3767 int NumLanes = VT.getSizeInBits()/128;
3768
3769 if (!Subtarget->hasAVX())
3770 return false;
3771
Eli Friedmandca62d52011-10-10 22:28:47 +00003772 // Only match 256-bit with 64-bit types
3773 if (VT.getSizeInBits() != 256 || NumElts != 4)
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003774 return false;
3775
3776 // The mask on the high lane is independent of the low. Both can match
3777 // any element in inside its own lane, but can't cross.
3778 int LaneSize = NumElts/NumLanes;
3779 for (int l = 0; l < NumLanes; ++l)
3780 for (int i = l*LaneSize; i < LaneSize*(l+1); ++i) {
3781 int LaneStart = l*LaneSize;
3782 if (!isUndefOrInRange(Mask[i], LaneStart, LaneStart+LaneSize))
3783 return false;
3784 }
3785
3786 return true;
3787}
3788
3789/// isVPERMILPSMask - Return true if the specified VECTOR_SHUFFLE operand
3790/// specifies a shuffle of elements that is suitable for input to VPERMILPS*.
3791/// Note that VPERMIL mask matching is different depending whether theunderlying
3792/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3793/// to the same elements of the low, but to the higher half of the source.
3794/// In VPERMILPD the two lanes could be shuffled independently of each other
3795/// with the same restriction that lanes can't be crossed.
3796static bool isVPERMILPSMask(const SmallVectorImpl<int> &Mask, EVT VT,
3797 const X86Subtarget *Subtarget) {
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003798 unsigned NumElts = VT.getVectorNumElements();
3799 unsigned NumLanes = VT.getSizeInBits()/128;
3800
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003801 if (!Subtarget->hasAVX())
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003802 return false;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003803
Eli Friedmandca62d52011-10-10 22:28:47 +00003804 // Only match 256-bit with 32-bit types
3805 if (VT.getSizeInBits() != 256 || NumElts != 8)
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003806 return false;
3807
3808 // The mask on the high lane should be the same as the low. Actually,
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003809 // they can differ if any of the corresponding index in a lane is undef
3810 // and the other stays in range.
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003811 int LaneSize = NumElts/NumLanes;
3812 for (int i = 0; i < LaneSize; ++i) {
3813 int HighElt = i+LaneSize;
Bruno Cardoso Lopes155a92a2011-08-10 01:54:17 +00003814 bool HighValid = isUndefOrInRange(Mask[HighElt], LaneSize, NumElts);
3815 bool LowValid = isUndefOrInRange(Mask[i], 0, LaneSize);
3816
3817 if (!HighValid || !LowValid)
3818 return false;
3819 if (Mask[i] < 0 || Mask[HighElt] < 0)
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003820 continue;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003821 if (Mask[HighElt]-Mask[i] != LaneSize)
3822 return false;
3823 }
3824
3825 return true;
3826}
3827
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003828/// getShuffleVPERMILPSImmediate - Return the appropriate immediate to shuffle
3829/// the specified VECTOR_MASK mask with VPERMILPS* instructions.
3830static unsigned getShuffleVPERMILPSImmediate(SDNode *N) {
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003831 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3832 EVT VT = SVOp->getValueType(0);
3833
3834 int NumElts = VT.getVectorNumElements();
3835 int NumLanes = VT.getSizeInBits()/128;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003836 int LaneSize = NumElts/NumLanes;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003837
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003838 // Although the mask is equal for both lanes do it twice to get the cases
3839 // where a mask will match because the same mask element is undef on the
3840 // first half but valid on the second. This would get pathological cases
3841 // such as: shuffle <u, 0, 1, 2, 4, 4, 5, 6>, which is completely valid.
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003842 unsigned Mask = 0;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003843 for (int l = 0; l < NumLanes; ++l) {
3844 for (int i = 0; i < LaneSize; ++i) {
3845 int MaskElt = SVOp->getMaskElt(i+(l*LaneSize));
3846 if (MaskElt < 0)
3847 continue;
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003848 if (MaskElt >= LaneSize)
3849 MaskElt -= LaneSize;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003850 Mask |= MaskElt << (i*2);
3851 }
Bruno Cardoso Lopes377baa52011-07-29 01:31:04 +00003852 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003853
3854 return Mask;
3855}
3856
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003857/// getShuffleVPERMILPDImmediate - Return the appropriate immediate to shuffle
3858/// the specified VECTOR_MASK mask with VPERMILPD* instructions.
3859static unsigned getShuffleVPERMILPDImmediate(SDNode *N) {
3860 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3861 EVT VT = SVOp->getValueType(0);
3862
3863 int NumElts = VT.getVectorNumElements();
3864 int NumLanes = VT.getSizeInBits()/128;
3865
3866 unsigned Mask = 0;
3867 int LaneSize = NumElts/NumLanes;
3868 for (int l = 0; l < NumLanes; ++l)
Bruno Cardoso Lopes377baa52011-07-29 01:31:04 +00003869 for (int i = l*LaneSize; i < LaneSize*(l+1); ++i) {
3870 int MaskElt = SVOp->getMaskElt(i);
3871 if (MaskElt < 0)
3872 continue;
3873 Mask |= (MaskElt-l*LaneSize) << i;
3874 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003875
3876 return Mask;
3877}
3878
Evan Cheng017dcc62006-04-21 01:05:10 +00003879/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3880/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003881/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003882static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003883 bool V2IsSplat = false, bool V2IsUndef = false) {
3884 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003885 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003886 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003887
Nate Begeman9008ca62009-04-27 18:41:29 +00003888 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003889 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003890
Nate Begeman9008ca62009-04-27 18:41:29 +00003891 for (int i = 1; i < NumOps; ++i)
3892 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3893 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3894 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003895 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003896
Evan Cheng39623da2006-04-20 08:58:49 +00003897 return true;
3898}
3899
Nate Begeman9008ca62009-04-27 18:41:29 +00003900static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003901 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003902 SmallVector<int, 8> M;
3903 N->getMask(M);
3904 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003905}
3906
Evan Chengd9539472006-04-14 21:59:03 +00003907/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3908/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003909/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
3910bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N,
3911 const X86Subtarget *Subtarget) {
3912 if (!Subtarget->hasSSE3() && !Subtarget->hasAVX())
Evan Chengd9539472006-04-14 21:59:03 +00003913 return false;
3914
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003915 // The second vector must be undef
3916 if (N->getOperand(1).getOpcode() != ISD::UNDEF)
3917 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003918
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003919 EVT VT = N->getValueType(0);
3920 unsigned NumElems = VT.getVectorNumElements();
3921
3922 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3923 (VT.getSizeInBits() == 256 && NumElems != 8))
3924 return false;
3925
3926 // "i+1" is the value the indexed mask element must have
3927 for (unsigned i = 0; i < NumElems; i += 2)
3928 if (!isUndefOrEqual(N->getMaskElt(i), i+1) ||
3929 !isUndefOrEqual(N->getMaskElt(i+1), i+1))
Nate Begeman9008ca62009-04-27 18:41:29 +00003930 return false;
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003931
3932 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003933}
3934
3935/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3936/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003937/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
3938bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N,
3939 const X86Subtarget *Subtarget) {
3940 if (!Subtarget->hasSSE3() && !Subtarget->hasAVX())
Evan Chengd9539472006-04-14 21:59:03 +00003941 return false;
3942
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003943 // The second vector must be undef
3944 if (N->getOperand(1).getOpcode() != ISD::UNDEF)
3945 return false;
3946
3947 EVT VT = N->getValueType(0);
3948 unsigned NumElems = VT.getVectorNumElements();
3949
3950 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3951 (VT.getSizeInBits() == 256 && NumElems != 8))
3952 return false;
3953
3954 // "i" is the value the indexed mask element must have
3955 for (unsigned i = 0; i < NumElems; i += 2)
3956 if (!isUndefOrEqual(N->getMaskElt(i), i) ||
3957 !isUndefOrEqual(N->getMaskElt(i+1), i))
Nate Begeman9008ca62009-04-27 18:41:29 +00003958 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003959
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003960 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003961}
3962
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003963/// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand
3964/// specifies a shuffle of elements that is suitable for input to 256-bit
3965/// version of MOVDDUP.
3966static bool isMOVDDUPYMask(ShuffleVectorSDNode *N,
3967 const X86Subtarget *Subtarget) {
3968 EVT VT = N->getValueType(0);
3969 int NumElts = VT.getVectorNumElements();
3970 bool V2IsUndef = N->getOperand(1).getOpcode() == ISD::UNDEF;
3971
3972 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256 ||
3973 !V2IsUndef || NumElts != 4)
3974 return false;
3975
3976 for (int i = 0; i != NumElts/2; ++i)
3977 if (!isUndefOrEqual(N->getMaskElt(i), 0))
3978 return false;
3979 for (int i = NumElts/2; i != NumElts; ++i)
3980 if (!isUndefOrEqual(N->getMaskElt(i), NumElts/2))
3981 return false;
3982 return true;
3983}
3984
Evan Cheng0b457f02008-09-25 20:50:48 +00003985/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003986/// specifies a shuffle of elements that is suitable for input to 128-bit
3987/// version of MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003988bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003989 EVT VT = N->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00003990
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003991 if (VT.getSizeInBits() != 128)
3992 return false;
3993
3994 int e = VT.getVectorNumElements() / 2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003995 for (int i = 0; i < e; ++i)
3996 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003997 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003998 for (int i = 0; i < e; ++i)
3999 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00004000 return false;
4001 return true;
4002}
4003
David Greenec38a03e2011-02-03 15:50:00 +00004004/// isVEXTRACTF128Index - Return true if the specified
4005/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
4006/// suitable for input to VEXTRACTF128.
4007bool X86::isVEXTRACTF128Index(SDNode *N) {
4008 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4009 return false;
4010
4011 // The index should be aligned on a 128-bit boundary.
4012 uint64_t Index =
4013 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4014
4015 unsigned VL = N->getValueType(0).getVectorNumElements();
4016 unsigned VBits = N->getValueType(0).getSizeInBits();
4017 unsigned ElSize = VBits / VL;
4018 bool Result = (Index * ElSize) % 128 == 0;
4019
4020 return Result;
4021}
4022
David Greeneccacdc12011-02-04 16:08:29 +00004023/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
4024/// operand specifies a subvector insert that is suitable for input to
4025/// VINSERTF128.
4026bool X86::isVINSERTF128Index(SDNode *N) {
4027 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4028 return false;
4029
4030 // The index should be aligned on a 128-bit boundary.
4031 uint64_t Index =
4032 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
4033
4034 unsigned VL = N->getValueType(0).getVectorNumElements();
4035 unsigned VBits = N->getValueType(0).getSizeInBits();
4036 unsigned ElSize = VBits / VL;
4037 bool Result = (Index * ElSize) % 128 == 0;
4038
4039 return Result;
4040}
4041
Evan Cheng63d33002006-03-22 08:01:21 +00004042/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004043/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00004044unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004045 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
4046 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
4047
Evan Chengb9df0ca2006-03-22 02:53:00 +00004048 unsigned Shift = (NumOperands == 4) ? 2 : 1;
4049 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00004050 for (int i = 0; i < NumOperands; ++i) {
4051 int Val = SVOp->getMaskElt(NumOperands-i-1);
4052 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00004053 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00004054 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00004055 if (i != NumOperands - 1)
4056 Mask <<= Shift;
4057 }
Evan Cheng63d33002006-03-22 08:01:21 +00004058 return Mask;
4059}
4060
Evan Cheng506d3df2006-03-29 23:07:14 +00004061/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004062/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00004063unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004064 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00004065 unsigned Mask = 0;
4066 // 8 nodes, but we only care about the last 4.
4067 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004068 int Val = SVOp->getMaskElt(i);
4069 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00004070 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00004071 if (i != 4)
4072 Mask <<= 2;
4073 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004074 return Mask;
4075}
4076
4077/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004078/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00004079unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004080 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00004081 unsigned Mask = 0;
4082 // 8 nodes, but we only care about the first 4.
4083 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004084 int Val = SVOp->getMaskElt(i);
4085 if (Val >= 0)
4086 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00004087 if (i != 0)
4088 Mask <<= 2;
4089 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004090 return Mask;
4091}
4092
Nate Begemana09008b2009-10-19 02:17:23 +00004093/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
4094/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
4095unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
4096 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
4097 EVT VVT = N->getValueType(0);
4098 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
4099 int Val = 0;
4100
4101 unsigned i, e;
4102 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
4103 Val = SVOp->getMaskElt(i);
4104 if (Val >= 0)
4105 break;
4106 }
Eli Friedman63f8dde2011-07-25 21:36:45 +00004107 assert(Val - i > 0 && "PALIGNR imm should be positive");
Nate Begemana09008b2009-10-19 02:17:23 +00004108 return (Val - i) * EltSize;
4109}
4110
David Greenec38a03e2011-02-03 15:50:00 +00004111/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
4112/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
4113/// instructions.
4114unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
4115 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4116 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
4117
4118 uint64_t Index =
4119 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4120
4121 EVT VecVT = N->getOperand(0).getValueType();
4122 EVT ElVT = VecVT.getVectorElementType();
4123
4124 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004125 return Index / NumElemsPerChunk;
4126}
4127
David Greeneccacdc12011-02-04 16:08:29 +00004128/// getInsertVINSERTF128Immediate - Return the appropriate immediate
4129/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
4130/// instructions.
4131unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
4132 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4133 llvm_unreachable("Illegal insert subvector for VINSERTF128");
4134
4135 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00004136 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00004137
4138 EVT VecVT = N->getValueType(0);
4139 EVT ElVT = VecVT.getVectorElementType();
4140
4141 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004142 return Index / NumElemsPerChunk;
4143}
4144
Evan Cheng37b73872009-07-30 08:33:02 +00004145/// isZeroNode - Returns true if Elt is a constant zero or a floating point
4146/// constant +0.0.
4147bool X86::isZeroNode(SDValue Elt) {
4148 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00004149 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00004150 (isa<ConstantFPSDNode>(Elt) &&
4151 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
4152}
4153
Nate Begeman9008ca62009-04-27 18:41:29 +00004154/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
4155/// their permute mask.
4156static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
4157 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004158 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004159 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004160 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00004161
Nate Begeman5a5ca152009-04-29 05:20:52 +00004162 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004163 int idx = SVOp->getMaskElt(i);
4164 if (idx < 0)
4165 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004166 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00004167 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004168 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004169 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004170 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004171 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
4172 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004173}
4174
Evan Cheng779ccea2007-12-07 21:30:01 +00004175/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
4176/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00004177static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00004178 unsigned NumElems = VT.getVectorNumElements();
4179 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004180 int idx = Mask[i];
4181 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004182 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004183 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00004184 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004185 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004186 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004187 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004188}
4189
Evan Cheng533a0aa2006-04-19 20:35:22 +00004190/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
4191/// match movhlps. The lower half elements should come from upper half of
4192/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004193/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00004194static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004195 EVT VT = Op->getValueType(0);
4196 if (VT.getSizeInBits() != 128)
4197 return false;
4198 if (VT.getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00004199 return false;
4200 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004201 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004202 return false;
4203 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004204 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004205 return false;
4206 return true;
4207}
4208
Evan Cheng5ced1d82006-04-06 23:23:56 +00004209/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00004210/// is promoted to a vector. It also returns the LoadSDNode by reference if
4211/// required.
4212static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00004213 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
4214 return false;
4215 N = N->getOperand(0).getNode();
4216 if (!ISD::isNON_EXTLoad(N))
4217 return false;
4218 if (LD)
4219 *LD = cast<LoadSDNode>(N);
4220 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004221}
4222
Evan Cheng533a0aa2006-04-19 20:35:22 +00004223/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4224/// match movlp{s|d}. The lower half elements should come from lower half of
4225/// V1 (and in order), and the upper half elements should come from the upper
4226/// half of V2 (and in order). And since V1 will become the source of the
4227/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004228static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
4229 ShuffleVectorSDNode *Op) {
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004230 EVT VT = Op->getValueType(0);
4231 if (VT.getSizeInBits() != 128)
4232 return false;
4233
Evan Cheng466685d2006-10-09 20:57:25 +00004234 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004235 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00004236 // Is V2 is a vector load, don't do this transformation. We will try to use
4237 // load folding shufps op.
4238 if (ISD::isNON_EXTLoad(V2))
4239 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004240
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004241 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004242
Evan Cheng533a0aa2006-04-19 20:35:22 +00004243 if (NumElems != 2 && NumElems != 4)
4244 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004245 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004246 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004247 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004248 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004249 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004250 return false;
4251 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004252}
4253
Evan Cheng39623da2006-04-20 08:58:49 +00004254/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4255/// all the same.
4256static bool isSplatVector(SDNode *N) {
4257 if (N->getOpcode() != ISD::BUILD_VECTOR)
4258 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004259
Dan Gohman475871a2008-07-27 21:46:04 +00004260 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00004261 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4262 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00004263 return false;
4264 return true;
4265}
4266
Evan Cheng213d2cf2007-05-17 18:45:50 +00004267/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00004268/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00004269/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00004270static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00004271 SDValue V1 = N->getOperand(0);
4272 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004273 unsigned NumElems = N->getValueType(0).getVectorNumElements();
4274 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004275 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004276 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004277 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00004278 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4279 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004280 if (Opc != ISD::BUILD_VECTOR ||
4281 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00004282 return false;
4283 } else if (Idx >= 0) {
4284 unsigned Opc = V1.getOpcode();
4285 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4286 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004287 if (Opc != ISD::BUILD_VECTOR ||
4288 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00004289 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00004290 }
4291 }
4292 return true;
4293}
4294
4295/// getZeroVector - Returns a vector of specified type with all zero elements.
4296///
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004297static SDValue getZeroVector(EVT VT, bool HasXMMInt, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00004298 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004299 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004300
Dale Johannesen0488fb62010-09-30 23:57:10 +00004301 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004302 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00004303 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00004304 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004305 if (HasXMMInt) { // SSE2
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004306 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4307 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4308 } else { // SSE1
4309 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4310 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4311 }
4312 } else if (VT.getSizeInBits() == 256) { // AVX
4313 // 256-bit logic and arithmetic instructions in AVX are
4314 // all floating-point, no support for integer ops. Default
4315 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00004316 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004317 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4318 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00004319 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004320 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004321}
4322
Chris Lattner8a594482007-11-25 00:24:49 +00004323/// getOnesVector - Returns a vector of specified type with all bits set.
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004324/// Always build ones vectors as <4 x i32>. For 256-bit types, use two
4325/// <4 x i32> inserted in a <8 x i32> appropriately. Then bitcast to their
4326/// original type, ensuring they get CSE'd.
Owen Andersone50ed302009-08-10 22:56:29 +00004327static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004328 assert(VT.isVector() && "Expected a vector type");
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004329 assert((VT.is128BitVector() || VT.is256BitVector())
4330 && "Expected a 128-bit or 256-bit vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004331
Owen Anderson825b72b2009-08-11 20:47:22 +00004332 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004333 SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
4334 Cst, Cst, Cst, Cst);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004335
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004336 if (VT.is256BitVector()) {
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004337 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, MVT::v8i32),
4338 Vec, DAG.getConstant(0, MVT::i32), DAG, dl);
4339 Vec = Insert128BitVector(InsV, Vec,
4340 DAG.getConstant(4 /* NumElems/2 */, MVT::i32), DAG, dl);
4341 }
4342
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004343 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00004344}
4345
Evan Cheng39623da2006-04-20 08:58:49 +00004346/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4347/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00004348static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004349 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004350 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004351
Evan Cheng39623da2006-04-20 08:58:49 +00004352 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00004353 SmallVector<int, 8> MaskVec;
4354 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00004355
Nate Begeman5a5ca152009-04-29 05:20:52 +00004356 for (unsigned i = 0; i != NumElems; ++i) {
4357 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004358 MaskVec[i] = NumElems;
4359 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00004360 }
Evan Cheng39623da2006-04-20 08:58:49 +00004361 }
Evan Cheng39623da2006-04-20 08:58:49 +00004362 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00004363 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
4364 SVOp->getOperand(1), &MaskVec[0]);
4365 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00004366}
4367
Evan Cheng017dcc62006-04-21 01:05:10 +00004368/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4369/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00004370static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004371 SDValue V2) {
4372 unsigned NumElems = VT.getVectorNumElements();
4373 SmallVector<int, 8> Mask;
4374 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00004375 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004376 Mask.push_back(i);
4377 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00004378}
4379
Nate Begeman9008ca62009-04-27 18:41:29 +00004380/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004381static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004382 SDValue V2) {
4383 unsigned NumElems = VT.getVectorNumElements();
4384 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00004385 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004386 Mask.push_back(i);
4387 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00004388 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004389 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00004390}
4391
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004392/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004393static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004394 SDValue V2) {
4395 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00004396 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00004397 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00004398 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004399 Mask.push_back(i + Half);
4400 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00004401 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004402 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004403}
4404
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004405// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004406// a generic shuffle instruction because the target has no such instructions.
4407// Generate shuffles which repeat i16 and i8 several times until they can be
4408// represented by v4f32 and then be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004409static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004410 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004411 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004412 DebugLoc dl = V.getDebugLoc();
Rafael Espindola15684b22009-04-24 12:40:33 +00004413
Nate Begeman9008ca62009-04-27 18:41:29 +00004414 while (NumElems > 4) {
4415 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004416 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004417 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004418 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004419 EltNo -= NumElems/2;
4420 }
4421 NumElems >>= 1;
4422 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004423 return V;
4424}
Eric Christopherfd179292009-08-27 18:07:15 +00004425
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004426/// getLegalSplat - Generate a legal splat with supported x86 shuffles
4427static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
4428 EVT VT = V.getValueType();
4429 DebugLoc dl = V.getDebugLoc();
4430 assert((VT.getSizeInBits() == 128 || VT.getSizeInBits() == 256)
4431 && "Vector size not supported");
4432
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004433 if (VT.getSizeInBits() == 128) {
4434 V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004435 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004436 V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
4437 &SplatMask[0]);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004438 } else {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004439 // To use VPERMILPS to splat scalars, the second half of indicies must
4440 // refer to the higher part, which is a duplication of the lower one,
4441 // because VPERMILPS can only handle in-lane permutations.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004442 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
4443 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004444
4445 V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
4446 V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
4447 &SplatMask[0]);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004448 }
4449
4450 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4451}
4452
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004453/// PromoteSplat - Splat is promoted to target supported vector shuffles.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004454static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4455 EVT SrcVT = SV->getValueType(0);
4456 SDValue V1 = SV->getOperand(0);
4457 DebugLoc dl = SV->getDebugLoc();
4458
4459 int EltNo = SV->getSplatIndex();
4460 int NumElems = SrcVT.getVectorNumElements();
4461 unsigned Size = SrcVT.getSizeInBits();
4462
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004463 assert(((Size == 128 && NumElems > 4) || Size == 256) &&
4464 "Unknown how to promote splat for type");
4465
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004466 // Extract the 128-bit part containing the splat element and update
4467 // the splat element index when it refers to the higher register.
4468 if (Size == 256) {
4469 unsigned Idx = (EltNo > NumElems/2) ? NumElems/2 : 0;
4470 V1 = Extract128BitVector(V1, DAG.getConstant(Idx, MVT::i32), DAG, dl);
4471 if (Idx > 0)
4472 EltNo -= NumElems/2;
4473 }
4474
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004475 // All i16 and i8 vector types can't be used directly by a generic shuffle
4476 // instruction because the target has no such instruction. Generate shuffles
4477 // which repeat i16 and i8 several times until they fit in i32, and then can
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004478 // be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004479 EVT EltVT = SrcVT.getVectorElementType();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004480 if (EltVT == MVT::i8 || EltVT == MVT::i16)
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004481 V1 = PromoteSplati8i16(V1, DAG, EltNo);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004482
4483 // Recreate the 256-bit vector and place the same 128-bit vector
4484 // into the low and high part. This is necessary because we want
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004485 // to use VPERM* to shuffle the vectors
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004486 if (Size == 256) {
4487 SDValue InsV = Insert128BitVector(DAG.getUNDEF(SrcVT), V1,
4488 DAG.getConstant(0, MVT::i32), DAG, dl);
4489 V1 = Insert128BitVector(InsV, V1,
4490 DAG.getConstant(NumElems/2, MVT::i32), DAG, dl);
4491 }
4492
4493 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004494}
4495
Evan Chengba05f722006-04-21 23:03:30 +00004496/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004497/// vector of zero or undef vector. This produces a shuffle where the low
4498/// element of V2 is swizzled into the zero/undef vector, landing at element
4499/// 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 +00004500static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004501 bool isZero, bool HasXMMInt,
4502 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004503 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00004504 SDValue V1 = isZero
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004505 ? getZeroVector(VT, HasXMMInt, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00004506 unsigned NumElems = VT.getVectorNumElements();
4507 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004508 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004509 // If this is the insertion idx, put the low elt of V2 here.
4510 MaskVec.push_back(i == Idx ? NumElems : i);
4511 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004512}
4513
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004514/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4515/// element of the result of the vector shuffle.
Benjamin Kramer050db522011-03-26 12:38:19 +00004516static SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
4517 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004518 if (Depth == 6)
4519 return SDValue(); // Limit search depth.
4520
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004521 SDValue V = SDValue(N, 0);
4522 EVT VT = V.getValueType();
4523 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004524
4525 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4526 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
4527 Index = SV->getMaskElt(Index);
4528
4529 if (Index < 0)
4530 return DAG.getUNDEF(VT.getVectorElementType());
4531
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004532 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004533 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004534 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004535 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004536
4537 // Recurse into target specific vector shuffles to find scalars.
4538 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004539 int NumElems = VT.getVectorNumElements();
4540 SmallVector<unsigned, 16> ShuffleMask;
4541 SDValue ImmN;
4542
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004543 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004544 case X86ISD::SHUFPS:
4545 case X86ISD::SHUFPD:
4546 ImmN = N->getOperand(N->getNumOperands()-1);
4547 DecodeSHUFPSMask(NumElems,
4548 cast<ConstantSDNode>(ImmN)->getZExtValue(),
4549 ShuffleMask);
4550 break;
4551 case X86ISD::PUNPCKHBW:
4552 case X86ISD::PUNPCKHWD:
4553 case X86ISD::PUNPCKHDQ:
4554 case X86ISD::PUNPCKHQDQ:
4555 DecodePUNPCKHMask(NumElems, ShuffleMask);
4556 break;
4557 case X86ISD::UNPCKHPS:
4558 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00004559 case X86ISD::VUNPCKHPSY:
4560 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004561 DecodeUNPCKHPMask(NumElems, ShuffleMask);
4562 break;
4563 case X86ISD::PUNPCKLBW:
4564 case X86ISD::PUNPCKLWD:
4565 case X86ISD::PUNPCKLDQ:
4566 case X86ISD::PUNPCKLQDQ:
David Greenec4db4e52011-02-28 19:06:56 +00004567 DecodePUNPCKLMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004568 break;
4569 case X86ISD::UNPCKLPS:
4570 case X86ISD::UNPCKLPD:
David Greenec4db4e52011-02-28 19:06:56 +00004571 case X86ISD::VUNPCKLPSY:
4572 case X86ISD::VUNPCKLPDY:
4573 DecodeUNPCKLPMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004574 break;
4575 case X86ISD::MOVHLPS:
4576 DecodeMOVHLPSMask(NumElems, ShuffleMask);
4577 break;
4578 case X86ISD::MOVLHPS:
4579 DecodeMOVLHPSMask(NumElems, ShuffleMask);
4580 break;
4581 case X86ISD::PSHUFD:
4582 ImmN = N->getOperand(N->getNumOperands()-1);
4583 DecodePSHUFMask(NumElems,
4584 cast<ConstantSDNode>(ImmN)->getZExtValue(),
4585 ShuffleMask);
4586 break;
4587 case X86ISD::PSHUFHW:
4588 ImmN = N->getOperand(N->getNumOperands()-1);
4589 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4590 ShuffleMask);
4591 break;
4592 case X86ISD::PSHUFLW:
4593 ImmN = N->getOperand(N->getNumOperands()-1);
4594 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4595 ShuffleMask);
4596 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004597 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004598 case X86ISD::MOVSD: {
4599 // The index 0 always comes from the first element of the second source,
4600 // this is why MOVSS and MOVSD are used in the first place. The other
4601 // elements come from the other positions of the first source vector.
4602 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004603 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
4604 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004605 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00004606 case X86ISD::VPERMILPS:
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00004607 ImmN = N->getOperand(N->getNumOperands()-1);
Bruno Cardoso Lopes2eb4c2b2011-07-29 01:31:11 +00004608 DecodeVPERMILPSMask(4, cast<ConstantSDNode>(ImmN)->getZExtValue(),
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00004609 ShuffleMask);
Bruno Cardoso Lopes2eb4c2b2011-07-29 01:31:11 +00004610 break;
4611 case X86ISD::VPERMILPSY:
4612 ImmN = N->getOperand(N->getNumOperands()-1);
4613 DecodeVPERMILPSMask(8, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4614 ShuffleMask);
4615 break;
4616 case X86ISD::VPERMILPD:
4617 ImmN = N->getOperand(N->getNumOperands()-1);
4618 DecodeVPERMILPDMask(2, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4619 ShuffleMask);
4620 break;
4621 case X86ISD::VPERMILPDY:
4622 ImmN = N->getOperand(N->getNumOperands()-1);
4623 DecodeVPERMILPDMask(4, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4624 ShuffleMask);
4625 break;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00004626 case X86ISD::VPERM2F128:
4627 ImmN = N->getOperand(N->getNumOperands()-1);
4628 DecodeVPERM2F128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4629 ShuffleMask);
4630 break;
Eli Friedman106f6e72011-09-10 02:01:42 +00004631 case X86ISD::MOVDDUP:
4632 case X86ISD::MOVLHPD:
4633 case X86ISD::MOVLPD:
4634 case X86ISD::MOVLPS:
4635 case X86ISD::MOVSHDUP:
4636 case X86ISD::MOVSLDUP:
4637 case X86ISD::PALIGN:
4638 return SDValue(); // Not yet implemented.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004639 default:
Eli Friedman106f6e72011-09-10 02:01:42 +00004640 assert(0 && "unknown target shuffle node");
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004641 return SDValue();
4642 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004643
4644 Index = ShuffleMask[Index];
4645 if (Index < 0)
4646 return DAG.getUNDEF(VT.getVectorElementType());
4647
4648 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
4649 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
4650 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004651 }
4652
4653 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004654 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004655 V = V.getOperand(0);
4656 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004657 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004658
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004659 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004660 return SDValue();
4661 }
4662
4663 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4664 return (Index == 0) ? V.getOperand(0)
4665 : DAG.getUNDEF(VT.getVectorElementType());
4666
4667 if (V.getOpcode() == ISD::BUILD_VECTOR)
4668 return V.getOperand(Index);
4669
4670 return SDValue();
4671}
4672
4673/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4674/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004675/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004676static
4677unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
4678 bool ZerosFromLeft, SelectionDAG &DAG) {
4679 int i = 0;
4680
4681 while (i < NumElems) {
4682 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004683 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004684 if (!(Elt.getNode() &&
4685 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4686 break;
4687 ++i;
4688 }
4689
4690 return i;
4691}
4692
4693/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
4694/// MaskE correspond consecutively to elements from one of the vector operands,
4695/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4696static
4697bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
4698 int OpIdx, int NumElems, unsigned &OpNum) {
4699 bool SeenV1 = false;
4700 bool SeenV2 = false;
4701
4702 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
4703 int Idx = SVOp->getMaskElt(i);
4704 // Ignore undef indicies
4705 if (Idx < 0)
4706 continue;
4707
4708 if (Idx < NumElems)
4709 SeenV1 = true;
4710 else
4711 SeenV2 = true;
4712
4713 // Only accept consecutive elements from the same vector
4714 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4715 return false;
4716 }
4717
4718 OpNum = SeenV1 ? 0 : 1;
4719 return true;
4720}
4721
4722/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4723/// logical left shift of a vector.
4724static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4725 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4726 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4727 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4728 false /* check zeros from right */, DAG);
4729 unsigned OpSrc;
4730
4731 if (!NumZeros)
4732 return false;
4733
4734 // Considering the elements in the mask that are not consecutive zeros,
4735 // check if they consecutively come from only one of the source vectors.
4736 //
4737 // V1 = {X, A, B, C} 0
4738 // \ \ \ /
4739 // vector_shuffle V1, V2 <1, 2, 3, X>
4740 //
4741 if (!isShuffleMaskConsecutive(SVOp,
4742 0, // Mask Start Index
4743 NumElems-NumZeros-1, // Mask End Index
4744 NumZeros, // Where to start looking in the src vector
4745 NumElems, // Number of elements in vector
4746 OpSrc)) // Which source operand ?
4747 return false;
4748
4749 isLeft = false;
4750 ShAmt = NumZeros;
4751 ShVal = SVOp->getOperand(OpSrc);
4752 return true;
4753}
4754
4755/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4756/// logical left shift of a vector.
4757static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4758 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4759 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4760 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4761 true /* check zeros from left */, DAG);
4762 unsigned OpSrc;
4763
4764 if (!NumZeros)
4765 return false;
4766
4767 // Considering the elements in the mask that are not consecutive zeros,
4768 // check if they consecutively come from only one of the source vectors.
4769 //
4770 // 0 { A, B, X, X } = V2
4771 // / \ / /
4772 // vector_shuffle V1, V2 <X, X, 4, 5>
4773 //
4774 if (!isShuffleMaskConsecutive(SVOp,
4775 NumZeros, // Mask Start Index
4776 NumElems-1, // Mask End Index
4777 0, // Where to start looking in the src vector
4778 NumElems, // Number of elements in vector
4779 OpSrc)) // Which source operand ?
4780 return false;
4781
4782 isLeft = true;
4783 ShAmt = NumZeros;
4784 ShVal = SVOp->getOperand(OpSrc);
4785 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004786}
4787
4788/// isVectorShift - Returns true if the shuffle can be implemented as a
4789/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004790static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004791 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004792 // Although the logic below support any bitwidth size, there are no
4793 // shift instructions which handle more than 128-bit vectors.
4794 if (SVOp->getValueType(0).getSizeInBits() > 128)
4795 return false;
4796
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004797 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4798 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4799 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004800
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004801 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004802}
4803
Evan Chengc78d3b42006-04-24 18:01:45 +00004804/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4805///
Dan Gohman475871a2008-07-27 21:46:04 +00004806static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004807 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004808 SelectionDAG &DAG,
4809 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004810 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004811 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004812
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004813 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004814 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004815 bool First = true;
4816 for (unsigned i = 0; i < 16; ++i) {
4817 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4818 if (ThisIsNonZero && First) {
4819 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004820 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004821 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004822 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004823 First = false;
4824 }
4825
4826 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004827 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004828 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4829 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004830 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004831 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004832 }
4833 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004834 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4835 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4836 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004837 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004838 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004839 } else
4840 ThisElt = LastElt;
4841
Gabor Greifba36cb52008-08-28 21:40:38 +00004842 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004843 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004844 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004845 }
4846 }
4847
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004848 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004849}
4850
Bill Wendlinga348c562007-03-22 18:42:45 +00004851/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004852///
Dan Gohman475871a2008-07-27 21:46:04 +00004853static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004854 unsigned NumNonZero, unsigned NumZero,
4855 SelectionDAG &DAG,
4856 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004857 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004858 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004859
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004860 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004861 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004862 bool First = true;
4863 for (unsigned i = 0; i < 8; ++i) {
4864 bool isNonZero = (NonZeros & (1 << i)) != 0;
4865 if (isNonZero) {
4866 if (First) {
4867 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004868 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004869 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004870 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004871 First = false;
4872 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004873 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004874 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004875 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004876 }
4877 }
4878
4879 return V;
4880}
4881
Evan Chengf26ffe92008-05-29 08:22:04 +00004882/// getVShift - Return a vector logical shift node.
4883///
Owen Andersone50ed302009-08-10 22:56:29 +00004884static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004885 unsigned NumBits, SelectionDAG &DAG,
4886 const TargetLowering &TLI, DebugLoc dl) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004887 assert(VT.getSizeInBits() == 128 && "Unknown type for VShift");
Dale Johannesen0488fb62010-09-30 23:57:10 +00004888 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00004889 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004890 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4891 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004892 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004893 DAG.getConstant(NumBits,
4894 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004895}
4896
Dan Gohman475871a2008-07-27 21:46:04 +00004897SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004898X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004899 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004900
Evan Chengc3630942009-12-09 21:00:30 +00004901 // Check if the scalar load can be widened into a vector load. And if
4902 // the address is "base + cst" see if the cst can be "absorbed" into
4903 // the shuffle mask.
4904 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4905 SDValue Ptr = LD->getBasePtr();
4906 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4907 return SDValue();
4908 EVT PVT = LD->getValueType(0);
4909 if (PVT != MVT::i32 && PVT != MVT::f32)
4910 return SDValue();
4911
4912 int FI = -1;
4913 int64_t Offset = 0;
4914 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4915 FI = FINode->getIndex();
4916 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004917 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004918 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4919 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4920 Offset = Ptr.getConstantOperandVal(1);
4921 Ptr = Ptr.getOperand(0);
4922 } else {
4923 return SDValue();
4924 }
4925
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004926 // FIXME: 256-bit vector instructions don't require a strict alignment,
4927 // improve this code to support it better.
4928 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00004929 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004930 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00004931 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004932 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00004933 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004934 // Can't change the alignment. FIXME: It's possible to compute
4935 // the exact stack offset and reference FI + adjust offset instead.
4936 // If someone *really* cares about this. That's the way to implement it.
4937 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004938 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004939 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00004940 }
4941 }
4942
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004943 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00004944 // Ptr + (Offset & ~15).
4945 if (Offset < 0)
4946 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004947 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00004948 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004949 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00004950 if (StartOffset)
4951 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4952 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4953
4954 int EltNo = (Offset - StartOffset) >> 2;
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004955 int NumElems = VT.getVectorNumElements();
4956
4957 EVT CanonVT = VT.getSizeInBits() == 128 ? MVT::v4i32 : MVT::v8i32;
4958 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
4959 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00004960 LD->getPointerInfo().getWithOffset(StartOffset),
David Greene67c9d422010-02-15 16:53:33 +00004961 false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004962
4963 // Canonicalize it to a v4i32 or v8i32 shuffle.
4964 SmallVector<int, 8> Mask;
4965 for (int i = 0; i < NumElems; ++i)
4966 Mask.push_back(EltNo);
4967
4968 V1 = DAG.getNode(ISD::BITCAST, dl, CanonVT, V1);
4969 return DAG.getNode(ISD::BITCAST, dl, NVT,
4970 DAG.getVectorShuffle(CanonVT, dl, V1,
4971 DAG.getUNDEF(CanonVT),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00004972 }
4973
4974 return SDValue();
4975}
4976
Michael J. Spencerec38de22010-10-10 22:04:20 +00004977/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4978/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004979/// load which has the same value as a build_vector whose operands are 'elts'.
4980///
4981/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004982///
Nate Begeman1449f292010-03-24 22:19:06 +00004983/// FIXME: we'd also like to handle the case where the last elements are zero
4984/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4985/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004986static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004987 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004988 EVT EltVT = VT.getVectorElementType();
4989 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004990
Nate Begemanfdea31a2010-03-24 20:49:50 +00004991 LoadSDNode *LDBase = NULL;
4992 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004993
Nate Begeman1449f292010-03-24 22:19:06 +00004994 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004995 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004996 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004997 for (unsigned i = 0; i < NumElems; ++i) {
4998 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004999
Nate Begemanfdea31a2010-03-24 20:49:50 +00005000 if (!Elt.getNode() ||
5001 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
5002 return SDValue();
5003 if (!LDBase) {
5004 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
5005 return SDValue();
5006 LDBase = cast<LoadSDNode>(Elt.getNode());
5007 LastLoadedElt = i;
5008 continue;
5009 }
5010 if (Elt.getOpcode() == ISD::UNDEF)
5011 continue;
5012
5013 LoadSDNode *LD = cast<LoadSDNode>(Elt);
5014 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
5015 return SDValue();
5016 LastLoadedElt = i;
5017 }
Nate Begeman1449f292010-03-24 22:19:06 +00005018
5019 // If we have found an entire vector of loads and undefs, then return a large
5020 // load of the entire vector width starting at the base pointer. If we found
5021 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005022 if (LastLoadedElt == NumElems - 1) {
5023 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00005024 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005025 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00005026 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00005027 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005028 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00005029 LDBase->isVolatile(), LDBase->isNonTemporal(),
5030 LDBase->getAlignment());
Eli Friedman61cc47e2011-07-26 21:02:58 +00005031 } else if (NumElems == 4 && LastLoadedElt == 1 &&
5032 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005033 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
5034 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Eli Friedman322ea082011-09-14 23:42:45 +00005035 SDValue ResNode =
5036 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys, Ops, 2, MVT::i64,
5037 LDBase->getPointerInfo(),
5038 LDBase->getAlignment(),
5039 false/*isVolatile*/, true/*ReadMem*/,
5040 false/*WriteMem*/);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005041 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00005042 }
5043 return SDValue();
5044}
5045
Evan Chengc3630942009-12-09 21:00:30 +00005046SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005047X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005048 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00005049
David Greenef125a292011-02-08 19:04:41 +00005050 EVT VT = Op.getValueType();
5051 EVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00005052 unsigned NumElems = Op.getNumOperands();
5053
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005054 // Vectors containing all zeros can be matched by pxor and xorps later
5055 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5056 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
5057 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00005058 if (Op.getValueType() == MVT::v4i32 ||
5059 Op.getValueType() == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00005060 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005061
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005062 return getZeroVector(Op.getValueType(), Subtarget->hasXMMInt(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00005063 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005064
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005065 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
5066 // vectors or broken into v4i32 operations on 256-bit vectors.
5067 if (ISD::isBuildVectorAllOnes(Op.getNode())) {
5068 if (Op.getValueType() == MVT::v4i32)
5069 return Op;
5070
5071 return getOnesVector(Op.getValueType(), DAG, dl);
5072 }
5073
Owen Andersone50ed302009-08-10 22:56:29 +00005074 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005075
Evan Cheng0db9fe62006-04-25 20:13:52 +00005076 unsigned NumZero = 0;
5077 unsigned NumNonZero = 0;
5078 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005079 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005080 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005081 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005082 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00005083 if (Elt.getOpcode() == ISD::UNDEF)
5084 continue;
5085 Values.insert(Elt);
5086 if (Elt.getOpcode() != ISD::Constant &&
5087 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005088 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00005089 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00005090 NumZero++;
5091 else {
5092 NonZeros |= (1 << i);
5093 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005094 }
5095 }
5096
Chris Lattner97a2a562010-08-26 05:24:29 +00005097 // All undef vector. Return an UNDEF. All zero vectors were handled above.
5098 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00005099 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005100
Chris Lattner67f453a2008-03-09 05:42:06 +00005101 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00005102 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005103 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00005104 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005105
Chris Lattner62098042008-03-09 01:05:04 +00005106 // If this is an insertion of an i64 value on x86-32, and if the top bits of
5107 // the value are obviously zero, truncate the value to i32 and do the
5108 // insertion that way. Only do this if the value is non-constant or if the
5109 // value is a constant being inserted into element 0. It is cheaper to do
5110 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00005111 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00005112 (!IsAllConstants || Idx == 0)) {
5113 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00005114 // Handle SSE only.
5115 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
5116 EVT VecVT = MVT::v4i32;
5117 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00005118
Chris Lattner62098042008-03-09 01:05:04 +00005119 // Truncate the value (which may itself be a constant) to i32, and
5120 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00005121 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00005122 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00005123 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005124 Subtarget->hasXMMInt(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005125
Chris Lattner62098042008-03-09 01:05:04 +00005126 // Now we have our 32-bit value zero extended in the low element of
5127 // a vector. If Idx != 0, swizzle it into place.
5128 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005129 SmallVector<int, 4> Mask;
5130 Mask.push_back(Idx);
5131 for (unsigned i = 1; i != VecElts; ++i)
5132 Mask.push_back(i);
5133 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00005134 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00005135 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00005136 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005137 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00005138 }
5139 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005140
Chris Lattner19f79692008-03-08 22:59:52 +00005141 // If we have a constant or non-constant insertion into the low element of
5142 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5143 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00005144 // depending on what the source datatype is.
5145 if (Idx == 0) {
5146 if (NumZero == 0) {
5147 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00005148 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
5149 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00005150 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5151 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005152 return getShuffleVectorZeroOrUndef(Item, 0, true,Subtarget->hasXMMInt(),
Eli Friedman10415532009-06-06 06:05:10 +00005153 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00005154 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
5155 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005156 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
5157 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00005158 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
5159 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005160 Subtarget->hasXMMInt(), DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005161 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00005162 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005163 }
Evan Chengf26ffe92008-05-29 08:22:04 +00005164
5165 // Is it a vector logical left shift?
5166 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00005167 X86::isZeroNode(Op.getOperand(0)) &&
5168 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005169 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00005170 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00005171 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005172 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00005173 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005174 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005175
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005176 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00005177 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005178
Chris Lattner19f79692008-03-08 22:59:52 +00005179 // Otherwise, if this is a vector with i32 or f32 elements, and the element
5180 // is a non-constant being inserted into an element other than the low one,
5181 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
5182 // movd/movss) to move this into the low element, then shuffle it into
5183 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005184 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00005185 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00005186
Evan Cheng0db9fe62006-04-25 20:13:52 +00005187 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00005188 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005189 Subtarget->hasXMMInt(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00005190 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005191 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00005192 MaskVec.push_back(i == Idx ? 0 : 1);
5193 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005194 }
5195 }
5196
Chris Lattner67f453a2008-03-09 05:42:06 +00005197 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00005198 if (Values.size() == 1) {
5199 if (EVTBits == 32) {
5200 // Instead of a shuffle like this:
5201 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5202 // Check if it's possible to issue this instead.
5203 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
5204 unsigned Idx = CountTrailingZeros_32(NonZeros);
5205 SDValue Item = Op.getOperand(Idx);
5206 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5207 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5208 }
Dan Gohman475871a2008-07-27 21:46:04 +00005209 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005210 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005211
Dan Gohmana3941172007-07-24 22:55:08 +00005212 // A vector full of immediates; various special cases are already
5213 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005214 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00005215 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00005216
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005217 // For AVX-length vectors, build the individual 128-bit pieces and use
5218 // shuffles to put them in place.
5219 if (VT.getSizeInBits() == 256 && !ISD::isBuildVectorAllZeros(Op.getNode())) {
5220 SmallVector<SDValue, 32> V;
5221 for (unsigned i = 0; i < NumElems; ++i)
5222 V.push_back(Op.getOperand(i));
5223
5224 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
5225
5226 // Build both the lower and upper subvector.
5227 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
5228 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
5229 NumElems/2);
5230
5231 // Recreate the wider vector with the lower and upper part.
Bruno Cardoso Lopes15d03fb2011-07-28 01:26:53 +00005232 SDValue Vec = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), Lower,
5233 DAG.getConstant(0, MVT::i32), DAG, dl);
5234 return Insert128BitVector(Vec, Upper, DAG.getConstant(NumElems/2, MVT::i32),
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005235 DAG, dl);
5236 }
5237
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005238 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005239 if (EVTBits == 64) {
5240 if (NumNonZero == 1) {
5241 // One half is zero or undef.
5242 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005243 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005244 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00005245 return getShuffleVectorZeroOrUndef(V2, Idx, true,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005246 Subtarget->hasXMMInt(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005247 }
Dan Gohman475871a2008-07-27 21:46:04 +00005248 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005249 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005250
5251 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005252 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005253 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00005254 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005255 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005256 }
5257
Bill Wendling826f36f2007-03-28 00:57:11 +00005258 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005259 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00005260 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005261 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005262 }
5263
5264 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00005265 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00005266 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005267 if (NumElems == 4 && NumZero > 0) {
5268 for (unsigned i = 0; i < 4; ++i) {
5269 bool isZero = !(NonZeros & (1 << i));
5270 if (isZero)
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005271 V[i] = getZeroVector(VT, Subtarget->hasXMMInt(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005272 else
Dale Johannesenace16102009-02-03 19:33:06 +00005273 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005274 }
5275
5276 for (unsigned i = 0; i < 2; ++i) {
5277 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5278 default: break;
5279 case 0:
5280 V[i] = V[i*2]; // Must be a zero vector.
5281 break;
5282 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005283 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005284 break;
5285 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005286 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005287 break;
5288 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005289 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005290 break;
5291 }
5292 }
5293
Nate Begeman9008ca62009-04-27 18:41:29 +00005294 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005295 bool Reverse = (NonZeros & 0x3) == 2;
5296 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005297 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005298 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5299 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005300 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
5301 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005302 }
5303
Nate Begemanfdea31a2010-03-24 20:49:50 +00005304 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
5305 // Check for a build vector of consecutive loads.
5306 for (unsigned i = 0; i < NumElems; ++i)
5307 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005308
Nate Begemanfdea31a2010-03-24 20:49:50 +00005309 // Check for elements which are consecutive loads.
5310 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5311 if (LD.getNode())
5312 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005313
5314 // For SSE 4.1, use insertps to put the high elements into the low element.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005315 if (getSubtarget()->hasSSE41() || getSubtarget()->hasAVX()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005316 SDValue Result;
5317 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5318 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5319 else
5320 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005321
Chris Lattner24faf612010-08-28 17:59:08 +00005322 for (unsigned i = 1; i < NumElems; ++i) {
5323 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5324 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005325 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005326 }
5327 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005328 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005329
Chris Lattner6e80e442010-08-28 17:15:43 +00005330 // Otherwise, expand into a number of unpckl*, start by extending each of
5331 // our (non-undef) elements to the full vector width with the element in the
5332 // bottom slot of the vector (which generates no code for SSE).
5333 for (unsigned i = 0; i < NumElems; ++i) {
5334 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5335 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5336 else
5337 V[i] = DAG.getUNDEF(VT);
5338 }
5339
5340 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005341 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5342 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5343 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005344 unsigned EltStride = NumElems >> 1;
5345 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005346 for (unsigned i = 0; i < EltStride; ++i) {
5347 // If V[i+EltStride] is undef and this is the first round of mixing,
5348 // then it is safe to just drop this shuffle: V[i] is already in the
5349 // right place, the one element (since it's the first round) being
5350 // inserted as undef can be dropped. This isn't safe for successive
5351 // rounds because they will permute elements within both vectors.
5352 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5353 EltStride == NumElems/2)
5354 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005355
Chris Lattner6e80e442010-08-28 17:15:43 +00005356 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005357 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005358 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005359 }
5360 return V[0];
5361 }
Dan Gohman475871a2008-07-27 21:46:04 +00005362 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005363}
5364
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005365// LowerMMXCONCAT_VECTORS - We support concatenate two MMX registers and place
5366// them in a MMX register. This is better than doing a stack convert.
5367static SDValue LowerMMXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005368 DebugLoc dl = Op.getDebugLoc();
5369 EVT ResVT = Op.getValueType();
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005370
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005371 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
5372 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
5373 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005374 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005375 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
5376 InVec = Op.getOperand(1);
5377 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5378 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005379 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005380 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
5381 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
5382 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005383 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005384 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
5385 Mask[0] = 0; Mask[1] = 2;
5386 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
5387 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005388 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005389}
5390
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005391// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5392// to create 256-bit vectors from two other 128-bit ones.
5393static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5394 DebugLoc dl = Op.getDebugLoc();
5395 EVT ResVT = Op.getValueType();
5396
5397 assert(ResVT.getSizeInBits() == 256 && "Value type must be 256-bit wide");
5398
5399 SDValue V1 = Op.getOperand(0);
5400 SDValue V2 = Op.getOperand(1);
5401 unsigned NumElems = ResVT.getVectorNumElements();
5402
5403 SDValue V = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, ResVT), V1,
5404 DAG.getConstant(0, MVT::i32), DAG, dl);
5405 return Insert128BitVector(V, V2, DAG.getConstant(NumElems/2, MVT::i32),
5406 DAG, dl);
5407}
5408
5409SDValue
5410X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005411 EVT ResVT = Op.getValueType();
5412
5413 assert(Op.getNumOperands() == 2);
5414 assert((ResVT.getSizeInBits() == 128 || ResVT.getSizeInBits() == 256) &&
5415 "Unsupported CONCAT_VECTORS for value type");
5416
5417 // We support concatenate two MMX registers and place them in a MMX register.
5418 // This is better than doing a stack convert.
5419 if (ResVT.is128BitVector())
5420 return LowerMMXCONCAT_VECTORS(Op, DAG);
5421
5422 // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
5423 // from two other 128-bit ones.
5424 return LowerAVXCONCAT_VECTORS(Op, DAG);
5425}
5426
Nate Begemanb9a47b82009-02-23 08:49:38 +00005427// v8i16 shuffles - Prefer shuffles in the following order:
5428// 1. [all] pshuflw, pshufhw, optional move
5429// 2. [ssse3] 1 x pshufb
5430// 3. [ssse3] 2 x pshufb + 1 x por
5431// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005432SDValue
5433X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
5434 SelectionDAG &DAG) const {
5435 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00005436 SDValue V1 = SVOp->getOperand(0);
5437 SDValue V2 = SVOp->getOperand(1);
5438 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005439 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00005440
Nate Begemanb9a47b82009-02-23 08:49:38 +00005441 // Determine if more than 1 of the words in each of the low and high quadwords
5442 // of the result come from the same quadword of one of the two inputs. Undef
5443 // mask values count as coming from any quadword, for better codegen.
Benjamin Kramer003fad92011-10-15 13:28:31 +00005444 unsigned LoQuad[] = { 0, 0, 0, 0 };
5445 unsigned HiQuad[] = { 0, 0, 0, 0 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005446 BitVector InputQuads(4);
5447 for (unsigned i = 0; i < 8; ++i) {
Benjamin Kramer003fad92011-10-15 13:28:31 +00005448 unsigned *Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00005449 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005450 MaskVals.push_back(EltIdx);
5451 if (EltIdx < 0) {
5452 ++Quad[0];
5453 ++Quad[1];
5454 ++Quad[2];
5455 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00005456 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005457 }
5458 ++Quad[EltIdx / 4];
5459 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00005460 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005461
Nate Begemanb9a47b82009-02-23 08:49:38 +00005462 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005463 unsigned MaxQuad = 1;
5464 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005465 if (LoQuad[i] > MaxQuad) {
5466 BestLoQuad = i;
5467 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005468 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005469 }
5470
Nate Begemanb9a47b82009-02-23 08:49:38 +00005471 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005472 MaxQuad = 1;
5473 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005474 if (HiQuad[i] > MaxQuad) {
5475 BestHiQuad = i;
5476 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005477 }
5478 }
5479
Nate Begemanb9a47b82009-02-23 08:49:38 +00005480 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00005481 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00005482 // single pshufb instruction is necessary. If There are more than 2 input
5483 // quads, disable the next transformation since it does not help SSSE3.
5484 bool V1Used = InputQuads[0] || InputQuads[1];
5485 bool V2Used = InputQuads[2] || InputQuads[3];
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005486 if (Subtarget->hasSSSE3() || Subtarget->hasAVX()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005487 if (InputQuads.count() == 2 && V1Used && V2Used) {
5488 BestLoQuad = InputQuads.find_first();
5489 BestHiQuad = InputQuads.find_next(BestLoQuad);
5490 }
5491 if (InputQuads.count() > 2) {
5492 BestLoQuad = -1;
5493 BestHiQuad = -1;
5494 }
5495 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005496
Nate Begemanb9a47b82009-02-23 08:49:38 +00005497 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5498 // the shuffle mask. If a quad is scored as -1, that means that it contains
5499 // words from all 4 input quadwords.
5500 SDValue NewV;
5501 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005502 SmallVector<int, 8> MaskV;
5503 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
5504 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00005505 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005506 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
5507 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
5508 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005509
Nate Begemanb9a47b82009-02-23 08:49:38 +00005510 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5511 // source words for the shuffle, to aid later transformations.
5512 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00005513 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00005514 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005515 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00005516 if (idx != (int)i)
5517 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005518 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00005519 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005520 AllWordsInNewV = false;
5521 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00005522 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005523
Nate Begemanb9a47b82009-02-23 08:49:38 +00005524 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5525 if (AllWordsInNewV) {
5526 for (int i = 0; i != 8; ++i) {
5527 int idx = MaskVals[i];
5528 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005529 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005530 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005531 if ((idx != i) && idx < 4)
5532 pshufhw = false;
5533 if ((idx != i) && idx > 3)
5534 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00005535 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005536 V1 = NewV;
5537 V2Used = false;
5538 BestLoQuad = 0;
5539 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005540 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005541
Nate Begemanb9a47b82009-02-23 08:49:38 +00005542 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5543 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005544 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005545 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5546 unsigned TargetMask = 0;
5547 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005548 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005549 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
5550 X86::getShufflePSHUFLWImmediate(NewV.getNode());
5551 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005552 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005553 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005554 }
Eric Christopherfd179292009-08-27 18:07:15 +00005555
Nate Begemanb9a47b82009-02-23 08:49:38 +00005556 // If we have SSSE3, and all words of the result are from 1 input vector,
5557 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5558 // is present, fall back to case 4.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005559 if (Subtarget->hasSSSE3() || Subtarget->hasAVX()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005560 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005561
Nate Begemanb9a47b82009-02-23 08:49:38 +00005562 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005563 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005564 // mask, and elements that come from V1 in the V2 mask, so that the two
5565 // results can be OR'd together.
5566 bool TwoInputs = V1Used && V2Used;
5567 for (unsigned i = 0; i != 8; ++i) {
5568 int EltIdx = MaskVals[i] * 2;
5569 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005570 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5571 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005572 continue;
5573 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005574 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
5575 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005576 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005577 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005578 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005579 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005580 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005581 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005582 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005583
Nate Begemanb9a47b82009-02-23 08:49:38 +00005584 // Calculate the shuffle mask for the second input, shuffle it, and
5585 // OR it with the first shuffled input.
5586 pshufbMask.clear();
5587 for (unsigned i = 0; i != 8; ++i) {
5588 int EltIdx = MaskVals[i] * 2;
5589 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005590 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5591 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005592 continue;
5593 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005594 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
5595 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005596 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005597 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005598 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005599 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005600 MVT::v16i8, &pshufbMask[0], 16));
5601 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005602 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005603 }
5604
5605 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5606 // and update MaskVals with new element order.
5607 BitVector InOrder(8);
5608 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005609 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005610 for (int i = 0; i != 4; ++i) {
5611 int idx = MaskVals[i];
5612 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005613 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005614 InOrder.set(i);
5615 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005616 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005617 InOrder.set(i);
5618 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005619 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005620 }
5621 }
5622 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005623 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00005624 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005625 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005626
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005627 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE &&
5628 (Subtarget->hasSSSE3() || Subtarget->hasAVX()))
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005629 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
5630 NewV.getOperand(0),
5631 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
5632 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005633 }
Eric Christopherfd179292009-08-27 18:07:15 +00005634
Nate Begemanb9a47b82009-02-23 08:49:38 +00005635 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5636 // and update MaskVals with the new element order.
5637 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005638 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005639 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005640 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005641 for (unsigned i = 4; i != 8; ++i) {
5642 int idx = MaskVals[i];
5643 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005644 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005645 InOrder.set(i);
5646 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005647 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005648 InOrder.set(i);
5649 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005650 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005651 }
5652 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005653 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005654 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005655
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005656 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE &&
5657 (Subtarget->hasSSSE3() || Subtarget->hasAVX()))
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005658 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
5659 NewV.getOperand(0),
5660 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
5661 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005662 }
Eric Christopherfd179292009-08-27 18:07:15 +00005663
Nate Begemanb9a47b82009-02-23 08:49:38 +00005664 // In case BestHi & BestLo were both -1, which means each quadword has a word
5665 // from each of the four input quadwords, calculate the InOrder bitvector now
5666 // before falling through to the insert/extract cleanup.
5667 if (BestLoQuad == -1 && BestHiQuad == -1) {
5668 NewV = V1;
5669 for (int i = 0; i != 8; ++i)
5670 if (MaskVals[i] < 0 || MaskVals[i] == i)
5671 InOrder.set(i);
5672 }
Eric Christopherfd179292009-08-27 18:07:15 +00005673
Nate Begemanb9a47b82009-02-23 08:49:38 +00005674 // The other elements are put in the right place using pextrw and pinsrw.
5675 for (unsigned i = 0; i != 8; ++i) {
5676 if (InOrder[i])
5677 continue;
5678 int EltIdx = MaskVals[i];
5679 if (EltIdx < 0)
5680 continue;
5681 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00005682 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005683 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00005684 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005685 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005686 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005687 DAG.getIntPtrConstant(i));
5688 }
5689 return NewV;
5690}
5691
5692// v16i8 shuffles - Prefer shuffles in the following order:
5693// 1. [ssse3] 1 x pshufb
5694// 2. [ssse3] 2 x pshufb + 1 x por
5695// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5696static
Nate Begeman9008ca62009-04-27 18:41:29 +00005697SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005698 SelectionDAG &DAG,
5699 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005700 SDValue V1 = SVOp->getOperand(0);
5701 SDValue V2 = SVOp->getOperand(1);
5702 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005703 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00005704 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00005705
Nate Begemanb9a47b82009-02-23 08:49:38 +00005706 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005707 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005708 // present, fall back to case 3.
5709 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
5710 bool V1Only = true;
5711 bool V2Only = true;
5712 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005713 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00005714 if (EltIdx < 0)
5715 continue;
5716 if (EltIdx < 16)
5717 V2Only = false;
5718 else
5719 V1Only = false;
5720 }
Eric Christopherfd179292009-08-27 18:07:15 +00005721
Nate Begemanb9a47b82009-02-23 08:49:38 +00005722 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005723 if (TLI.getSubtarget()->hasSSSE3() || TLI.getSubtarget()->hasAVX()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005724 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005725
Nate Begemanb9a47b82009-02-23 08:49:38 +00005726 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005727 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005728 //
5729 // Otherwise, we have elements from both input vectors, and must zero out
5730 // elements that come from V2 in the first mask, and V1 in the second mask
5731 // so that we can OR them together.
5732 bool TwoInputs = !(V1Only || V2Only);
5733 for (unsigned i = 0; i != 16; ++i) {
5734 int EltIdx = MaskVals[i];
5735 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005736 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005737 continue;
5738 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005739 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005740 }
5741 // If all the elements are from V2, assign it to V1 and return after
5742 // building the first pshufb.
5743 if (V2Only)
5744 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00005745 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005746 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005747 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005748 if (!TwoInputs)
5749 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005750
Nate Begemanb9a47b82009-02-23 08:49:38 +00005751 // Calculate the shuffle mask for the second input, shuffle it, and
5752 // OR it with the first shuffled input.
5753 pshufbMask.clear();
5754 for (unsigned i = 0; i != 16; ++i) {
5755 int EltIdx = MaskVals[i];
5756 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005757 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005758 continue;
5759 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005760 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005761 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005762 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005763 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005764 MVT::v16i8, &pshufbMask[0], 16));
5765 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005766 }
Eric Christopherfd179292009-08-27 18:07:15 +00005767
Nate Begemanb9a47b82009-02-23 08:49:38 +00005768 // No SSSE3 - Calculate in place words and then fix all out of place words
5769 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5770 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005771 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5772 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005773 SDValue NewV = V2Only ? V2 : V1;
5774 for (int i = 0; i != 8; ++i) {
5775 int Elt0 = MaskVals[i*2];
5776 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005777
Nate Begemanb9a47b82009-02-23 08:49:38 +00005778 // This word of the result is all undef, skip it.
5779 if (Elt0 < 0 && Elt1 < 0)
5780 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005781
Nate Begemanb9a47b82009-02-23 08:49:38 +00005782 // This word of the result is already in the correct place, skip it.
5783 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
5784 continue;
5785 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
5786 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005787
Nate Begemanb9a47b82009-02-23 08:49:38 +00005788 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5789 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5790 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005791
5792 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5793 // using a single extract together, load it and store it.
5794 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005795 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005796 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005797 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005798 DAG.getIntPtrConstant(i));
5799 continue;
5800 }
5801
Nate Begemanb9a47b82009-02-23 08:49:38 +00005802 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00005803 // source byte is not also odd, shift the extracted word left 8 bits
5804 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005805 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005806 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005807 DAG.getIntPtrConstant(Elt1 / 2));
5808 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005809 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00005810 DAG.getConstant(8,
5811 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005812 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005813 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
5814 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005815 }
5816 // If Elt0 is defined, extract it from the appropriate source. If the
5817 // source byte is not also even, shift the extracted word right 8 bits. If
5818 // Elt1 was also defined, OR the extracted values together before
5819 // inserting them in the result.
5820 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005821 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005822 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
5823 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005824 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00005825 DAG.getConstant(8,
5826 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005827 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005828 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
5829 DAG.getConstant(0x00FF, MVT::i16));
5830 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00005831 : InsElt0;
5832 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005833 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005834 DAG.getIntPtrConstant(i));
5835 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005836 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005837}
5838
Evan Cheng7a831ce2007-12-15 03:00:47 +00005839/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005840/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00005841/// done when every pair / quad of shuffle mask elements point to elements in
5842/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005843/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00005844static
Nate Begeman9008ca62009-04-27 18:41:29 +00005845SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005846 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00005847 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00005848 SDValue V1 = SVOp->getOperand(0);
5849 SDValue V2 = SVOp->getOperand(1);
5850 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00005851 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005852 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00005853 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005854 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005855 case MVT::v4f32: NewVT = MVT::v2f64; break;
5856 case MVT::v4i32: NewVT = MVT::v2i64; break;
5857 case MVT::v8i16: NewVT = MVT::v4i32; break;
5858 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00005859 }
5860
Nate Begeman9008ca62009-04-27 18:41:29 +00005861 int Scale = NumElems / NewWidth;
5862 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00005863 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005864 int StartIdx = -1;
5865 for (int j = 0; j < Scale; ++j) {
5866 int EltIdx = SVOp->getMaskElt(i+j);
5867 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005868 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00005869 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00005870 StartIdx = EltIdx - (EltIdx % Scale);
5871 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00005872 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005873 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005874 if (StartIdx == -1)
5875 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00005876 else
Nate Begeman9008ca62009-04-27 18:41:29 +00005877 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005878 }
5879
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005880 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
5881 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00005882 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005883}
5884
Evan Chengd880b972008-05-09 21:53:03 +00005885/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005886///
Owen Andersone50ed302009-08-10 22:56:29 +00005887static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00005888 SDValue SrcOp, SelectionDAG &DAG,
5889 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005890 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005891 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00005892 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00005893 LD = dyn_cast<LoadSDNode>(SrcOp);
5894 if (!LD) {
5895 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
5896 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00005897 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00005898 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00005899 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005900 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00005901 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005902 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00005903 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005904 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005905 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
5906 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
5907 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00005908 SrcOp.getOperand(0)
5909 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005910 }
5911 }
5912 }
5913
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005914 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005915 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005916 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00005917 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005918}
5919
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00005920/// areShuffleHalvesWithinDisjointLanes - Check whether each half of a vector
5921/// shuffle node referes to only one lane in the sources.
5922static bool areShuffleHalvesWithinDisjointLanes(ShuffleVectorSDNode *SVOp) {
5923 EVT VT = SVOp->getValueType(0);
5924 int NumElems = VT.getVectorNumElements();
5925 int HalfSize = NumElems/2;
5926 SmallVector<int, 16> M;
5927 SVOp->getMask(M);
5928 bool MatchA = false, MatchB = false;
5929
5930 for (int l = 0; l < NumElems*2; l += HalfSize) {
5931 if (isUndefOrInRange(M, 0, HalfSize, l, l+HalfSize)) {
5932 MatchA = true;
5933 break;
5934 }
5935 }
5936
5937 for (int l = 0; l < NumElems*2; l += HalfSize) {
5938 if (isUndefOrInRange(M, HalfSize, HalfSize, l, l+HalfSize)) {
5939 MatchB = true;
5940 break;
5941 }
5942 }
5943
5944 return MatchA && MatchB;
5945}
5946
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00005947/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
5948/// which could not be matched by any known target speficic shuffle
5949static SDValue
5950LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00005951 if (areShuffleHalvesWithinDisjointLanes(SVOp)) {
5952 // If each half of a vector shuffle node referes to only one lane in the
5953 // source vectors, extract each used 128-bit lane and shuffle them using
5954 // 128-bit shuffles. Then, concatenate the results. Otherwise leave
5955 // the work to the legalizer.
5956 DebugLoc dl = SVOp->getDebugLoc();
5957 EVT VT = SVOp->getValueType(0);
5958 int NumElems = VT.getVectorNumElements();
5959 int HalfSize = NumElems/2;
5960
5961 // Extract the reference for each half
5962 int FstVecExtractIdx = 0, SndVecExtractIdx = 0;
5963 int FstVecOpNum = 0, SndVecOpNum = 0;
5964 for (int i = 0; i < HalfSize; ++i) {
5965 int Elt = SVOp->getMaskElt(i);
5966 if (SVOp->getMaskElt(i) < 0)
5967 continue;
5968 FstVecOpNum = Elt/NumElems;
5969 FstVecExtractIdx = Elt % NumElems < HalfSize ? 0 : HalfSize;
5970 break;
5971 }
5972 for (int i = HalfSize; i < NumElems; ++i) {
5973 int Elt = SVOp->getMaskElt(i);
5974 if (SVOp->getMaskElt(i) < 0)
5975 continue;
5976 SndVecOpNum = Elt/NumElems;
5977 SndVecExtractIdx = Elt % NumElems < HalfSize ? 0 : HalfSize;
5978 break;
5979 }
5980
5981 // Extract the subvectors
5982 SDValue V1 = Extract128BitVector(SVOp->getOperand(FstVecOpNum),
5983 DAG.getConstant(FstVecExtractIdx, MVT::i32), DAG, dl);
5984 SDValue V2 = Extract128BitVector(SVOp->getOperand(SndVecOpNum),
5985 DAG.getConstant(SndVecExtractIdx, MVT::i32), DAG, dl);
5986
5987 // Generate 128-bit shuffles
5988 SmallVector<int, 16> MaskV1, MaskV2;
5989 for (int i = 0; i < HalfSize; ++i) {
5990 int Elt = SVOp->getMaskElt(i);
5991 MaskV1.push_back(Elt < 0 ? Elt : Elt % HalfSize);
5992 }
5993 for (int i = HalfSize; i < NumElems; ++i) {
5994 int Elt = SVOp->getMaskElt(i);
5995 MaskV2.push_back(Elt < 0 ? Elt : Elt % HalfSize);
5996 }
5997
5998 EVT NVT = V1.getValueType();
5999 V1 = DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &MaskV1[0]);
6000 V2 = DAG.getVectorShuffle(NVT, dl, V2, DAG.getUNDEF(NVT), &MaskV2[0]);
6001
6002 // Concatenate the result back
6003 SDValue V = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), V1,
6004 DAG.getConstant(0, MVT::i32), DAG, dl);
6005 return Insert128BitVector(V, V2, DAG.getConstant(NumElems/2, MVT::i32),
6006 DAG, dl);
6007 }
6008
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006009 return SDValue();
6010}
6011
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006012/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
6013/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00006014static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006015LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006016 SDValue V1 = SVOp->getOperand(0);
6017 SDValue V2 = SVOp->getOperand(1);
6018 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006019 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00006020
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006021 assert(VT.getSizeInBits() == 128 && "Unsupported vector size");
6022
Evan Chengace3c172008-07-22 21:13:36 +00006023 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00006024 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00006025 SmallVector<int, 8> Mask1(4U, -1);
6026 SmallVector<int, 8> PermMask;
6027 SVOp->getMask(PermMask);
6028
Evan Chengace3c172008-07-22 21:13:36 +00006029 unsigned NumHi = 0;
6030 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00006031 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006032 int Idx = PermMask[i];
6033 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006034 Locs[i] = std::make_pair(-1, -1);
6035 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006036 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
6037 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006038 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00006039 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006040 NumLo++;
6041 } else {
6042 Locs[i] = std::make_pair(1, NumHi);
6043 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00006044 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006045 NumHi++;
6046 }
6047 }
6048 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006049
Evan Chengace3c172008-07-22 21:13:36 +00006050 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006051 // If no more than two elements come from either vector. This can be
6052 // implemented with two shuffles. First shuffle gather the elements.
6053 // The second shuffle, which takes the first shuffle as both of its
6054 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00006055 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006056
Nate Begeman9008ca62009-04-27 18:41:29 +00006057 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00006058
Evan Chengace3c172008-07-22 21:13:36 +00006059 for (unsigned i = 0; i != 4; ++i) {
6060 if (Locs[i].first == -1)
6061 continue;
6062 else {
6063 unsigned Idx = (i < 2) ? 0 : 4;
6064 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006065 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006066 }
6067 }
6068
Nate Begeman9008ca62009-04-27 18:41:29 +00006069 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006070 } else if (NumLo == 3 || NumHi == 3) {
6071 // Otherwise, we must have three elements from one vector, call it X, and
6072 // one element from the other, call it Y. First, use a shufps to build an
6073 // intermediate vector with the one element from Y and the element from X
6074 // that will be in the same half in the final destination (the indexes don't
6075 // matter). Then, use a shufps to build the final vector, taking the half
6076 // containing the element from Y from the intermediate, and the other half
6077 // from X.
6078 if (NumHi == 3) {
6079 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00006080 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006081 std::swap(V1, V2);
6082 }
6083
6084 // Find the element from V2.
6085 unsigned HiIndex;
6086 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006087 int Val = PermMask[HiIndex];
6088 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006089 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006090 if (Val >= 4)
6091 break;
6092 }
6093
Nate Begeman9008ca62009-04-27 18:41:29 +00006094 Mask1[0] = PermMask[HiIndex];
6095 Mask1[1] = -1;
6096 Mask1[2] = PermMask[HiIndex^1];
6097 Mask1[3] = -1;
6098 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006099
6100 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006101 Mask1[0] = PermMask[0];
6102 Mask1[1] = PermMask[1];
6103 Mask1[2] = HiIndex & 1 ? 6 : 4;
6104 Mask1[3] = HiIndex & 1 ? 4 : 6;
6105 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006106 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006107 Mask1[0] = HiIndex & 1 ? 2 : 0;
6108 Mask1[1] = HiIndex & 1 ? 0 : 2;
6109 Mask1[2] = PermMask[2];
6110 Mask1[3] = PermMask[3];
6111 if (Mask1[2] >= 0)
6112 Mask1[2] += 4;
6113 if (Mask1[3] >= 0)
6114 Mask1[3] += 4;
6115 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006116 }
Evan Chengace3c172008-07-22 21:13:36 +00006117 }
6118
6119 // Break it into (shuffle shuffle_hi, shuffle_lo).
6120 Locs.clear();
David Greenec4db4e52011-02-28 19:06:56 +00006121 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00006122 SmallVector<int,8> LoMask(4U, -1);
6123 SmallVector<int,8> HiMask(4U, -1);
6124
6125 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00006126 unsigned MaskIdx = 0;
6127 unsigned LoIdx = 0;
6128 unsigned HiIdx = 2;
6129 for (unsigned i = 0; i != 4; ++i) {
6130 if (i == 2) {
6131 MaskPtr = &HiMask;
6132 MaskIdx = 1;
6133 LoIdx = 0;
6134 HiIdx = 2;
6135 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006136 int Idx = PermMask[i];
6137 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006138 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00006139 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006140 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00006141 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006142 LoIdx++;
6143 } else {
6144 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00006145 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006146 HiIdx++;
6147 }
6148 }
6149
Nate Begeman9008ca62009-04-27 18:41:29 +00006150 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6151 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
6152 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00006153 for (unsigned i = 0; i != 4; ++i) {
6154 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006155 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00006156 } else {
6157 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006158 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00006159 }
6160 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006161 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006162}
6163
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006164static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006165 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006166 V = V.getOperand(0);
6167 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6168 V = V.getOperand(0);
6169 if (MayFoldLoad(V))
6170 return true;
6171 return false;
6172}
6173
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006174// FIXME: the version above should always be used. Since there's
6175// a bug where several vector shuffles can't be folded because the
6176// DAG is not updated during lowering and a node claims to have two
6177// uses while it only has one, use this version, and let isel match
6178// another instruction if the load really happens to have more than
6179// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00006180// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006181static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006182 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006183 V = V.getOperand(0);
6184 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6185 V = V.getOperand(0);
6186 if (ISD::isNormalLoad(V.getNode()))
6187 return true;
6188 return false;
6189}
6190
6191/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
6192/// a vector extract, and if both can be later optimized into a single load.
6193/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
6194/// here because otherwise a target specific shuffle node is going to be
6195/// emitted for this shuffle, and the optimization not done.
6196/// FIXME: This is probably not the best approach, but fix the problem
6197/// until the right path is decided.
6198static
6199bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
6200 const TargetLowering &TLI) {
6201 EVT VT = V.getValueType();
6202 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
6203
6204 // Be sure that the vector shuffle is present in a pattern like this:
6205 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
6206 if (!V.hasOneUse())
6207 return false;
6208
6209 SDNode *N = *V.getNode()->use_begin();
6210 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
6211 return false;
6212
6213 SDValue EltNo = N->getOperand(1);
6214 if (!isa<ConstantSDNode>(EltNo))
6215 return false;
6216
6217 // If the bit convert changed the number of elements, it is unsafe
6218 // to examine the mask.
6219 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006220 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006221 EVT SrcVT = V.getOperand(0).getValueType();
6222 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
6223 return false;
6224 V = V.getOperand(0);
6225 HasShuffleIntoBitcast = true;
6226 }
6227
6228 // Select the input vector, guarding against out of range extract vector.
6229 unsigned NumElems = VT.getVectorNumElements();
6230 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
6231 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
6232 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
6233
6234 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006235 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006236 V = V.getOperand(0);
6237
6238 if (ISD::isNormalLoad(V.getNode())) {
6239 // Is the original load suitable?
6240 LoadSDNode *LN0 = cast<LoadSDNode>(V);
6241
6242 // FIXME: avoid the multi-use bug that is preventing lots of
6243 // of foldings to be detected, this is still wrong of course, but
6244 // give the temporary desired behavior, and if it happens that
6245 // the load has real more uses, during isel it will not fold, and
6246 // will generate poor code.
6247 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
6248 return false;
6249
6250 if (!HasShuffleIntoBitcast)
6251 return true;
6252
6253 // If there's a bitcast before the shuffle, check if the load type and
6254 // alignment is valid.
6255 unsigned Align = LN0->getAlignment();
6256 unsigned NewAlign =
6257 TLI.getTargetData()->getABITypeAlignment(
6258 VT.getTypeForEVT(*DAG.getContext()));
6259
6260 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
6261 return false;
6262 }
6263
6264 return true;
6265}
6266
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006267static
Evan Cheng835580f2010-10-07 20:50:20 +00006268SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
6269 EVT VT = Op.getValueType();
6270
6271 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006272 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6273 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006274 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6275 V1, DAG));
6276}
6277
6278static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006279SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006280 bool HasXMMInt) {
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006281 SDValue V1 = Op.getOperand(0);
6282 SDValue V2 = Op.getOperand(1);
6283 EVT VT = Op.getValueType();
6284
6285 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6286
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006287 if (HasXMMInt && VT == MVT::v2f64)
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006288 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6289
Evan Cheng0899f5c2011-08-31 02:05:24 +00006290 // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
6291 return DAG.getNode(ISD::BITCAST, dl, VT,
6292 getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
6293 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
6294 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006295}
6296
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006297static
6298SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
6299 SDValue V1 = Op.getOperand(0);
6300 SDValue V2 = Op.getOperand(1);
6301 EVT VT = Op.getValueType();
6302
6303 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6304 "unsupported shuffle type");
6305
6306 if (V2.getOpcode() == ISD::UNDEF)
6307 V2 = V1;
6308
6309 // v4i32 or v4f32
6310 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6311}
6312
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006313static inline unsigned getSHUFPOpcode(EVT VT) {
6314 switch(VT.getSimpleVT().SimpleTy) {
6315 case MVT::v8i32: // Use fp unit for int unpack.
6316 case MVT::v8f32:
6317 case MVT::v4i32: // Use fp unit for int unpack.
6318 case MVT::v4f32: return X86ISD::SHUFPS;
6319 case MVT::v4i64: // Use fp unit for int unpack.
6320 case MVT::v4f64:
6321 case MVT::v2i64: // Use fp unit for int unpack.
6322 case MVT::v2f64: return X86ISD::SHUFPD;
6323 default:
6324 llvm_unreachable("Unknown type for shufp*");
6325 }
6326 return 0;
6327}
6328
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006329static
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006330SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasXMMInt) {
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006331 SDValue V1 = Op.getOperand(0);
6332 SDValue V2 = Op.getOperand(1);
6333 EVT VT = Op.getValueType();
6334 unsigned NumElems = VT.getVectorNumElements();
6335
6336 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6337 // operand of these instructions is only memory, so check if there's a
6338 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6339 // same masks.
6340 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006341
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006342 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006343 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006344 CanFoldLoad = true;
6345
6346 // When V1 is a load, it can be folded later into a store in isel, example:
6347 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6348 // turns into:
6349 // (MOVLPSmr addr:$src1, VR128:$src2)
6350 // So, recognize this potential and also use MOVLPS or MOVLPD
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006351 if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006352 CanFoldLoad = true;
6353
Eric Christopher893a8822011-02-20 05:04:42 +00006354 // Both of them can't be memory operations though.
6355 if (MayFoldVectorLoad(V1) && MayFoldVectorLoad(V2))
6356 CanFoldLoad = false;
Owen Anderson95771af2011-02-25 21:41:48 +00006357
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006358 if (CanFoldLoad) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006359 if (HasXMMInt && NumElems == 2)
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006360 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6361
6362 if (NumElems == 4)
6363 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
6364 }
6365
6366 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6367 // movl and movlp will both match v2i64, but v2i64 is never matched by
6368 // movl earlier because we make it strict to avoid messing with the movlp load
6369 // folding logic (see the code above getMOVLP call). Match it here then,
6370 // this is horrible, but will stay like this until we move all shuffle
6371 // matching to x86 specific nodes. Note that for the 1st condition all
6372 // types are matched with movsd.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006373 if (HasXMMInt) {
Bruno Cardoso Lopes5ca0d142011-09-14 02:36:14 +00006374 // FIXME: isMOVLMask should be checked and matched before getMOVLP,
6375 // as to remove this logic from here, as much as possible
6376 if (NumElems == 2 || !X86::isMOVLMask(SVOp))
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006377 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006378 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006379 }
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006380
6381 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6382
6383 // Invert the operand order and use SHUFPS to match it.
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006384 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V2, V1,
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006385 X86::getShuffleSHUFImmediate(SVOp), DAG);
6386}
6387
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006388static inline unsigned getUNPCKLOpcode(EVT VT) {
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006389 switch(VT.getSimpleVT().SimpleTy) {
6390 case MVT::v4i32: return X86ISD::PUNPCKLDQ;
6391 case MVT::v2i64: return X86ISD::PUNPCKLQDQ;
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00006392 case MVT::v4f32: return X86ISD::UNPCKLPS;
6393 case MVT::v2f64: return X86ISD::UNPCKLPD;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006394 case MVT::v8i32: // Use fp unit for int unpack.
David Greenec4db4e52011-02-28 19:06:56 +00006395 case MVT::v8f32: return X86ISD::VUNPCKLPSY;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006396 case MVT::v4i64: // Use fp unit for int unpack.
David Greenec4db4e52011-02-28 19:06:56 +00006397 case MVT::v4f64: return X86ISD::VUNPCKLPDY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006398 case MVT::v16i8: return X86ISD::PUNPCKLBW;
6399 case MVT::v8i16: return X86ISD::PUNPCKLWD;
6400 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00006401 llvm_unreachable("Unknown type for unpckl");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006402 }
6403 return 0;
6404}
6405
6406static inline unsigned getUNPCKHOpcode(EVT VT) {
6407 switch(VT.getSimpleVT().SimpleTy) {
6408 case MVT::v4i32: return X86ISD::PUNPCKHDQ;
6409 case MVT::v2i64: return X86ISD::PUNPCKHQDQ;
6410 case MVT::v4f32: return X86ISD::UNPCKHPS;
6411 case MVT::v2f64: return X86ISD::UNPCKHPD;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006412 case MVT::v8i32: // Use fp unit for int unpack.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00006413 case MVT::v8f32: return X86ISD::VUNPCKHPSY;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006414 case MVT::v4i64: // Use fp unit for int unpack.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00006415 case MVT::v4f64: return X86ISD::VUNPCKHPDY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006416 case MVT::v16i8: return X86ISD::PUNPCKHBW;
6417 case MVT::v8i16: return X86ISD::PUNPCKHWD;
6418 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00006419 llvm_unreachable("Unknown type for unpckh");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006420 }
6421 return 0;
6422}
6423
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006424static inline unsigned getVPERMILOpcode(EVT VT) {
6425 switch(VT.getSimpleVT().SimpleTy) {
6426 case MVT::v4i32:
6427 case MVT::v4f32: return X86ISD::VPERMILPS;
6428 case MVT::v2i64:
6429 case MVT::v2f64: return X86ISD::VPERMILPD;
6430 case MVT::v8i32:
6431 case MVT::v8f32: return X86ISD::VPERMILPSY;
6432 case MVT::v4i64:
6433 case MVT::v4f64: return X86ISD::VPERMILPDY;
6434 default:
6435 llvm_unreachable("Unknown type for vpermil");
6436 }
6437 return 0;
6438}
6439
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006440/// isVectorBroadcast - Check if the node chain is suitable to be xformed to
6441/// a vbroadcast node. The nodes are suitable whenever we can fold a load coming
6442/// from a 32 or 64 bit scalar. Update Op to the desired load to be folded.
6443static bool isVectorBroadcast(SDValue &Op) {
6444 EVT VT = Op.getValueType();
6445 bool Is256 = VT.getSizeInBits() == 256;
6446
6447 assert((VT.getSizeInBits() == 128 || Is256) &&
6448 "Unsupported type for vbroadcast node");
6449
6450 SDValue V = Op;
6451 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
6452 V = V.getOperand(0);
6453
6454 if (Is256 && !(V.hasOneUse() &&
6455 V.getOpcode() == ISD::INSERT_SUBVECTOR &&
6456 V.getOperand(0).getOpcode() == ISD::UNDEF))
6457 return false;
6458
6459 if (Is256)
6460 V = V.getOperand(1);
Bruno Cardoso Lopesa39ccdb2011-09-01 18:15:06 +00006461
6462 if (!V.hasOneUse())
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006463 return false;
6464
6465 // Check the source scalar_to_vector type. 256-bit broadcasts are
6466 // supported for 32/64-bit sizes, while 128-bit ones are only supported
6467 // for 32-bit scalars.
Bruno Cardoso Lopesa39ccdb2011-09-01 18:15:06 +00006468 if (V.getOpcode() != ISD::SCALAR_TO_VECTOR)
6469 return false;
6470
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006471 unsigned ScalarSize = V.getOperand(0).getValueType().getSizeInBits();
6472 if (ScalarSize != 32 && ScalarSize != 64)
6473 return false;
6474 if (!Is256 && ScalarSize == 64)
6475 return false;
6476
6477 V = V.getOperand(0);
6478 if (!MayFoldLoad(V))
6479 return false;
6480
6481 // Return the load node
6482 Op = V;
6483 return true;
6484}
6485
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006486static
6487SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006488 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006489 const X86Subtarget *Subtarget) {
6490 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6491 EVT VT = Op.getValueType();
6492 DebugLoc dl = Op.getDebugLoc();
6493 SDValue V1 = Op.getOperand(0);
6494 SDValue V2 = Op.getOperand(1);
6495
6496 if (isZeroShuffle(SVOp))
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006497 return getZeroVector(VT, Subtarget->hasXMMInt(), DAG, dl);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006498
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006499 // Handle splat operations
6500 if (SVOp->isSplat()) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006501 unsigned NumElem = VT.getVectorNumElements();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006502 int Size = VT.getSizeInBits();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006503 // Special case, this is the only place now where it's allowed to return
6504 // a vector_shuffle operation without using a target specific node, because
6505 // *hopefully* it will be optimized away by the dag combiner. FIXME: should
6506 // this be moved to DAGCombine instead?
6507 if (NumElem <= 4 && CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006508 return Op;
6509
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006510 // Use vbroadcast whenever the splat comes from a foldable load
6511 if (Subtarget->hasAVX() && isVectorBroadcast(V1))
6512 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, V1);
6513
Bruno Cardoso Lopes6a32adc2011-07-25 23:05:25 +00006514 // Handle splats by matching through known shuffle masks
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006515 if ((Size == 128 && NumElem <= 4) ||
6516 (Size == 256 && NumElem < 8))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006517 return SDValue();
6518
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00006519 // All remaning splats are promoted to target supported vector shuffles.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006520 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006521 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006522
6523 // If the shuffle can be profitably rewritten as a narrower shuffle, then
6524 // do it!
6525 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
6526 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6527 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006528 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006529 } else if ((VT == MVT::v4i32 ||
6530 (VT == MVT::v4f32 && Subtarget->hasXMMInt()))) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006531 // FIXME: Figure out a cleaner way to do this.
6532 // Try to make use of movq to zero out the top part.
6533 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
6534 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6535 if (NewOp.getNode()) {
6536 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
6537 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
6538 DAG, Subtarget, dl);
6539 }
6540 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
6541 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6542 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
6543 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
6544 DAG, Subtarget, dl);
6545 }
6546 }
6547 return SDValue();
6548}
6549
Dan Gohman475871a2008-07-27 21:46:04 +00006550SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006551X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00006552 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00006553 SDValue V1 = Op.getOperand(0);
6554 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006555 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006556 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006557 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006558 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006559 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
6560 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00006561 bool V1IsSplat = false;
6562 bool V2IsSplat = false;
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006563 bool HasXMMInt = Subtarget->hasXMMInt();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006564 MachineFunction &MF = DAG.getMachineFunction();
6565 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006566
Dale Johannesen0488fb62010-09-30 23:57:10 +00006567 // Shuffle operations on MMX not supported.
6568 if (isMMX)
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00006569 return Op;
6570
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006571 // Vector shuffle lowering takes 3 steps:
6572 //
6573 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
6574 // narrowing and commutation of operands should be handled.
6575 // 2) Matching of shuffles with known shuffle masks to x86 target specific
6576 // shuffle nodes.
6577 // 3) Rewriting of unmatched masks into new generic shuffle operations,
6578 // so the shuffle can be broken into other shuffles and the legalizer can
6579 // try the lowering again.
6580 //
6581 // The general ideia is that no vector_shuffle operation should be left to
6582 // be matched during isel, all of them must be converted to a target specific
6583 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00006584
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006585 // Normalize the input vectors. Here splats, zeroed vectors, profitable
6586 // narrowing and commutation of operands should be handled. The actual code
6587 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006588 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006589 if (NewOp.getNode())
6590 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00006591
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006592 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
6593 // unpckh_undef). Only use pshufd if speed is more important than size.
6594 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006595 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006596 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
Rafael Espindola23e31012011-07-22 18:56:05 +00006597 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00006598
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006599 if (X86::isMOVDDUPMask(SVOp) &&
6600 (Subtarget->hasSSE3() || Subtarget->hasAVX()) &&
6601 V2IsUndef && RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00006602 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006603
Dale Johannesen0488fb62010-09-30 23:57:10 +00006604 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006605 return getMOVHighToLow(Op, dl, DAG);
6606
6607 // Use to match splats
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006608 if (HasXMMInt && X86::isUNPCKHMask(SVOp) && V2IsUndef &&
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006609 (VT == MVT::v2f64 || VT == MVT::v2i64))
6610 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
6611
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006612 if (X86::isPSHUFDMask(SVOp)) {
6613 // The actual implementation will match the mask in the if above and then
6614 // during isel it can match several different instructions, not only pshufd
6615 // as its name says, sad but true, emulate the behavior for now...
6616 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
6617 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
6618
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006619 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
6620
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006621 if (HasXMMInt && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006622 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
6623
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006624 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V1,
6625 TargetMask, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006626 }
Eric Christopherfd179292009-08-27 18:07:15 +00006627
Evan Chengf26ffe92008-05-29 08:22:04 +00006628 // Check if this can be converted into a logical shift.
6629 bool isLeft = false;
6630 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00006631 SDValue ShVal;
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006632 bool isShift = getSubtarget()->hasXMMInt() &&
6633 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00006634 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006635 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00006636 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006637 EVT EltVT = VT.getVectorElementType();
6638 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006639 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006640 }
Eric Christopherfd179292009-08-27 18:07:15 +00006641
Nate Begeman9008ca62009-04-27 18:41:29 +00006642 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006643 if (V1IsUndef)
6644 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00006645 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00006646 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00006647 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006648 if (HasXMMInt && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006649 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6650
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006651 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006652 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6653 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00006654 }
Eric Christopherfd179292009-08-27 18:07:15 +00006655
Nate Begeman9008ca62009-04-27 18:41:29 +00006656 // FIXME: fold these into legal mask.
Dale Johannesen0488fb62010-09-30 23:57:10 +00006657 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp))
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006658 return getMOVLowToHigh(Op, dl, DAG, HasXMMInt);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006659
Dale Johannesen0488fb62010-09-30 23:57:10 +00006660 if (X86::isMOVHLPSMask(SVOp))
6661 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006662
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00006663 if (X86::isMOVSHDUPMask(SVOp, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006664 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006665
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00006666 if (X86::isMOVSLDUPMask(SVOp, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006667 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00006668
Dale Johannesen0488fb62010-09-30 23:57:10 +00006669 if (X86::isMOVLPMask(SVOp))
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006670 return getMOVLP(Op, dl, DAG, HasXMMInt);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006671
Nate Begeman9008ca62009-04-27 18:41:29 +00006672 if (ShouldXformToMOVHLPS(SVOp) ||
6673 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
6674 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006675
Evan Chengf26ffe92008-05-29 08:22:04 +00006676 if (isShift) {
6677 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006678 EVT EltVT = VT.getVectorElementType();
6679 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006680 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006681 }
Eric Christopherfd179292009-08-27 18:07:15 +00006682
Evan Cheng9eca5e82006-10-25 21:49:50 +00006683 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00006684 // FIXME: This should also accept a bitcast of a splat? Be careful, not
6685 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00006686 V1IsSplat = isSplatVector(V1.getNode());
6687 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006688
Chris Lattner8a594482007-11-25 00:24:49 +00006689 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00006690 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006691 Op = CommuteVectorShuffle(SVOp, DAG);
6692 SVOp = cast<ShuffleVectorSDNode>(Op);
6693 V1 = SVOp->getOperand(0);
6694 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00006695 std::swap(V1IsSplat, V2IsSplat);
6696 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006697 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00006698 }
6699
Nate Begeman9008ca62009-04-27 18:41:29 +00006700 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
6701 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00006702 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00006703 return V1;
6704 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6705 // the instruction selector will not match, so get a canonical MOVL with
6706 // swapped operands to undo the commute.
6707 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006708 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006709
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006710 if (X86::isUNPCKLMask(SVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006711 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006712
6713 if (X86::isUNPCKHMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006714 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006715
Evan Cheng9bbbb982006-10-25 20:48:19 +00006716 if (V2IsSplat) {
6717 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006718 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00006719 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00006720 SDValue NewMask = NormalizeMask(SVOp, DAG);
6721 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
6722 if (NSVOp != SVOp) {
6723 if (X86::isUNPCKLMask(NSVOp, true)) {
6724 return NewMask;
6725 } else if (X86::isUNPCKHMask(NSVOp, true)) {
6726 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006727 }
6728 }
6729 }
6730
Evan Cheng9eca5e82006-10-25 21:49:50 +00006731 if (Commuted) {
6732 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006733 // FIXME: this seems wrong.
6734 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
6735 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006736
6737 if (X86::isUNPCKLMask(NewSVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006738 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V2, V1, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006739
6740 if (X86::isUNPCKHMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006741 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V2, V1, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006742 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006743
Nate Begeman9008ca62009-04-27 18:41:29 +00006744 // Normalize the node to match x86 shuffle ops if needed
Dale Johannesen0488fb62010-09-30 23:57:10 +00006745 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
Nate Begeman9008ca62009-04-27 18:41:29 +00006746 return CommuteVectorShuffle(SVOp, DAG);
6747
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006748 // The checks below are all present in isShuffleMaskLegal, but they are
6749 // inlined here right now to enable us to directly emit target specific
6750 // nodes, and remove one by one until they don't return Op anymore.
6751 SmallVector<int, 16> M;
6752 SVOp->getMask(M);
6753
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006754 if (isPALIGNRMask(M, VT, Subtarget->hasSSSE3() || Subtarget->hasAVX()))
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006755 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
6756 X86::getShufflePALIGNRImmediate(SVOp),
6757 DAG);
6758
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006759 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6760 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00006761 if (VT == MVT::v2f64)
6762 return getTargetShuffleNode(X86ISD::UNPCKLPD, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006763 if (VT == MVT::v2i64)
6764 return getTargetShuffleNode(X86ISD::PUNPCKLQDQ, dl, VT, V1, V1, DAG);
6765 }
6766
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006767 if (isPSHUFHWMask(M, VT))
6768 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
6769 X86::getShufflePSHUFHWImmediate(SVOp),
6770 DAG);
6771
6772 if (isPSHUFLWMask(M, VT))
6773 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
6774 X86::getShufflePSHUFLWImmediate(SVOp),
6775 DAG);
6776
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006777 if (isSHUFPMask(M, VT))
6778 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6779 X86::getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00006780
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006781 if (X86::isUNPCKL_v_undef_Mask(SVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006782 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006783 if (X86::isUNPCKH_v_undef_Mask(SVOp))
Rafael Espindola23e31012011-07-22 18:56:05 +00006784 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006785
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006786 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006787 // Generate target specific nodes for 128 or 256-bit shuffles only
6788 // supported in the AVX instruction set.
6789 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006790
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006791 // Handle VMOVDDUPY permutations
6792 if (isMOVDDUPYMask(SVOp, Subtarget))
6793 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
6794
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006795 // Handle VPERMILPS* permutations
6796 if (isVPERMILPSMask(M, VT, Subtarget))
6797 return getTargetShuffleNode(getVPERMILOpcode(VT), dl, VT, V1,
6798 getShuffleVPERMILPSImmediate(SVOp), DAG);
6799
6800 // Handle VPERMILPD* permutations
6801 if (isVPERMILPDMask(M, VT, Subtarget))
6802 return getTargetShuffleNode(getVPERMILOpcode(VT), dl, VT, V1,
6803 getShuffleVPERMILPDImmediate(SVOp), DAG);
6804
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00006805 // Handle VPERM2F128 permutations
6806 if (isVPERM2F128Mask(M, VT, Subtarget))
6807 return getTargetShuffleNode(X86ISD::VPERM2F128, dl, VT, V1, V2,
6808 getShuffleVPERM2F128Immediate(SVOp), DAG);
6809
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006810 // Handle VSHUFPSY permutations
6811 if (isVSHUFPSYMask(M, VT, Subtarget))
6812 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6813 getShuffleVSHUFPSYImmediate(SVOp), DAG);
6814
6815 // Handle VSHUFPDY permutations
6816 if (isVSHUFPDYMask(M, VT, Subtarget))
6817 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6818 getShuffleVSHUFPDYImmediate(SVOp), DAG);
6819
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006820 //===--------------------------------------------------------------------===//
6821 // Since no target specific shuffle was selected for this generic one,
6822 // lower it into other known shuffles. FIXME: this isn't true yet, but
6823 // this is the plan.
6824 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00006825
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006826 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
6827 if (VT == MVT::v8i16) {
6828 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
6829 if (NewOp.getNode())
6830 return NewOp;
6831 }
6832
6833 if (VT == MVT::v16i8) {
6834 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
6835 if (NewOp.getNode())
6836 return NewOp;
6837 }
6838
6839 // Handle all 128-bit wide vectors with 4 elements, and match them with
6840 // several different shuffle types.
6841 if (NumElems == 4 && VT.getSizeInBits() == 128)
6842 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
6843
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006844 // Handle general 256-bit shuffles
6845 if (VT.is256BitVector())
6846 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
6847
Dan Gohman475871a2008-07-27 21:46:04 +00006848 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006849}
6850
Dan Gohman475871a2008-07-27 21:46:04 +00006851SDValue
6852X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00006853 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006854 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006855 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006856
6857 if (Op.getOperand(0).getValueType().getSizeInBits() != 128)
6858 return SDValue();
6859
Duncan Sands83ec4b62008-06-06 12:08:01 +00006860 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006861 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006862 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006863 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006864 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006865 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006866 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00006867 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
6868 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
6869 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006870 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6871 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006872 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006873 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00006874 Op.getOperand(0)),
6875 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006876 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006877 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006878 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006879 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006880 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00006881 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00006882 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
6883 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006884 // result has a single use which is a store or a bitcast to i32. And in
6885 // the case of a store, it's not worth it if the index is a constant 0,
6886 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00006887 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00006888 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00006889 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006890 if ((User->getOpcode() != ISD::STORE ||
6891 (isa<ConstantSDNode>(Op.getOperand(1)) &&
6892 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006893 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00006894 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00006895 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00006896 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006897 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00006898 Op.getOperand(0)),
6899 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006900 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Owen Anderson825b72b2009-08-11 20:47:22 +00006901 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00006902 // ExtractPS works with constant index.
6903 if (isa<ConstantSDNode>(Op.getOperand(1)))
6904 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006905 }
Dan Gohman475871a2008-07-27 21:46:04 +00006906 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006907}
6908
6909
Dan Gohman475871a2008-07-27 21:46:04 +00006910SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006911X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
6912 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006913 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00006914 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006915
David Greene74a579d2011-02-10 16:57:36 +00006916 SDValue Vec = Op.getOperand(0);
6917 EVT VecVT = Vec.getValueType();
6918
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006919 // If this is a 256-bit vector result, first extract the 128-bit vector and
6920 // then extract the element from the 128-bit vector.
6921 if (VecVT.getSizeInBits() == 256) {
David Greene74a579d2011-02-10 16:57:36 +00006922 DebugLoc dl = Op.getNode()->getDebugLoc();
6923 unsigned NumElems = VecVT.getVectorNumElements();
6924 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00006925 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
6926
6927 // Get the 128-bit vector.
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006928 bool Upper = IdxVal >= NumElems/2;
6929 Vec = Extract128BitVector(Vec,
6930 DAG.getConstant(Upper ? NumElems/2 : 0, MVT::i32), DAG, dl);
David Greene74a579d2011-02-10 16:57:36 +00006931
David Greene74a579d2011-02-10 16:57:36 +00006932 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006933 Upper ? DAG.getConstant(IdxVal-NumElems/2, MVT::i32) : Idx);
David Greene74a579d2011-02-10 16:57:36 +00006934 }
6935
6936 assert(Vec.getValueSizeInBits() <= 128 && "Unexpected vector length");
6937
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006938 if (Subtarget->hasSSE41() || Subtarget->hasAVX()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006939 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006940 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00006941 return Res;
6942 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00006943
Owen Andersone50ed302009-08-10 22:56:29 +00006944 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006945 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006946 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006947 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00006948 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006949 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006950 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006951 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6952 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006953 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006954 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00006955 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006956 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00006957 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00006958 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006959 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00006960 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006961 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006962 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006963 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006964 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006965 if (Idx == 0)
6966 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00006967
Evan Cheng0db9fe62006-04-25 20:13:52 +00006968 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00006969 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006970 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006971 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006972 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006973 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006974 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00006975 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006976 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
6977 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
6978 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006979 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006980 if (Idx == 0)
6981 return Op;
6982
6983 // UNPCKHPD the element to the lowest double word, then movsd.
6984 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
6985 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00006986 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006987 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006988 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006989 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006990 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006991 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006992 }
6993
Dan Gohman475871a2008-07-27 21:46:04 +00006994 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006995}
6996
Dan Gohman475871a2008-07-27 21:46:04 +00006997SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006998X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
6999 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007000 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007001 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007002 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007003
Dan Gohman475871a2008-07-27 21:46:04 +00007004 SDValue N0 = Op.getOperand(0);
7005 SDValue N1 = Op.getOperand(1);
7006 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00007007
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007008 if (VT.getSizeInBits() == 256)
7009 return SDValue();
7010
Dan Gohman8a55ce42009-09-23 21:02:20 +00007011 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00007012 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007013 unsigned Opc;
7014 if (VT == MVT::v8i16)
7015 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007016 else if (VT == MVT::v16i8)
7017 Opc = X86ISD::PINSRB;
7018 else
7019 Opc = X86ISD::PINSRB;
7020
Nate Begeman14d12ca2008-02-11 04:19:36 +00007021 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
7022 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007023 if (N1.getValueType() != MVT::i32)
7024 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7025 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007026 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00007027 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00007028 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007029 // Bits [7:6] of the constant are the source select. This will always be
7030 // zero here. The DAG Combiner may combine an extract_elt index into these
7031 // bits. For example (insert (extract, 3), 2) could be matched by putting
7032 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00007033 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00007034 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00007035 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00007036 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007037 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00007038 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00007039 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00007040 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00007041 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00007042 // PINSR* works with constant index.
7043 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007044 }
Dan Gohman475871a2008-07-27 21:46:04 +00007045 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007046}
7047
Dan Gohman475871a2008-07-27 21:46:04 +00007048SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007049X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007050 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007051 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007052
David Greene6b381262011-02-09 15:32:06 +00007053 DebugLoc dl = Op.getDebugLoc();
7054 SDValue N0 = Op.getOperand(0);
7055 SDValue N1 = Op.getOperand(1);
7056 SDValue N2 = Op.getOperand(2);
7057
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007058 // If this is a 256-bit vector result, first extract the 128-bit vector,
7059 // insert the element into the extracted half and then place it back.
7060 if (VT.getSizeInBits() == 256) {
David Greene6b381262011-02-09 15:32:06 +00007061 if (!isa<ConstantSDNode>(N2))
7062 return SDValue();
7063
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007064 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00007065 unsigned NumElems = VT.getVectorNumElements();
7066 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007067 bool Upper = IdxVal >= NumElems/2;
7068 SDValue Ins128Idx = DAG.getConstant(Upper ? NumElems/2 : 0, MVT::i32);
7069 SDValue V = Extract128BitVector(N0, Ins128Idx, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007070
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007071 // Insert the element into the desired half.
7072 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V,
7073 N1, Upper ? DAG.getConstant(IdxVal-NumElems/2, MVT::i32) : N2);
David Greene6b381262011-02-09 15:32:06 +00007074
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007075 // Insert the changed part back to the 256-bit vector
7076 return Insert128BitVector(N0, V, Ins128Idx, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007077 }
7078
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007079 if (Subtarget->hasSSE41() || Subtarget->hasAVX())
Nate Begeman14d12ca2008-02-11 04:19:36 +00007080 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
7081
Dan Gohman8a55ce42009-09-23 21:02:20 +00007082 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00007083 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00007084
Dan Gohman8a55ce42009-09-23 21:02:20 +00007085 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00007086 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
7087 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007088 if (N1.getValueType() != MVT::i32)
7089 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7090 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007091 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00007092 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007093 }
Dan Gohman475871a2008-07-27 21:46:04 +00007094 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007095}
7096
Dan Gohman475871a2008-07-27 21:46:04 +00007097SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007098X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007099 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007100 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00007101 EVT OpVT = Op.getValueType();
7102
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007103 // If this is a 256-bit vector result, first insert into a 128-bit
7104 // vector and then insert into the 256-bit vector.
7105 if (OpVT.getSizeInBits() > 128) {
7106 // Insert into a 128-bit vector.
7107 EVT VT128 = EVT::getVectorVT(*Context,
7108 OpVT.getVectorElementType(),
7109 OpVT.getVectorNumElements() / 2);
7110
7111 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
7112
7113 // Insert the 128-bit vector.
7114 return Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, OpVT), Op,
7115 DAG.getConstant(0, MVT::i32),
7116 DAG, dl);
7117 }
7118
Chris Lattnerf172ecd2010-07-04 23:07:25 +00007119 if (Op.getValueType() == MVT::v1i64 &&
7120 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00007121 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00007122
Owen Anderson825b72b2009-08-11 20:47:22 +00007123 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00007124 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
7125 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007126 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00007127 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007128}
7129
David Greene91585092011-01-26 15:38:49 +00007130// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
7131// a simple subregister reference or explicit instructions to grab
7132// upper bits of a vector.
7133SDValue
7134X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7135 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00007136 DebugLoc dl = Op.getNode()->getDebugLoc();
7137 SDValue Vec = Op.getNode()->getOperand(0);
7138 SDValue Idx = Op.getNode()->getOperand(1);
7139
7140 if (Op.getNode()->getValueType(0).getSizeInBits() == 128
7141 && Vec.getNode()->getValueType(0).getSizeInBits() == 256) {
7142 return Extract128BitVector(Vec, Idx, DAG, dl);
7143 }
David Greene91585092011-01-26 15:38:49 +00007144 }
7145 return SDValue();
7146}
7147
David Greenecfe33c42011-01-26 19:13:22 +00007148// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
7149// simple superregister reference or explicit instructions to insert
7150// the upper bits of a vector.
7151SDValue
7152X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7153 if (Subtarget->hasAVX()) {
7154 DebugLoc dl = Op.getNode()->getDebugLoc();
7155 SDValue Vec = Op.getNode()->getOperand(0);
7156 SDValue SubVec = Op.getNode()->getOperand(1);
7157 SDValue Idx = Op.getNode()->getOperand(2);
7158
7159 if (Op.getNode()->getValueType(0).getSizeInBits() == 256
7160 && SubVec.getNode()->getValueType(0).getSizeInBits() == 128) {
David Greenea5f26012011-02-07 19:36:54 +00007161 return Insert128BitVector(Vec, SubVec, Idx, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00007162 }
7163 }
7164 return SDValue();
7165}
7166
Bill Wendling056292f2008-09-16 21:48:12 +00007167// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7168// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7169// one of the above mentioned nodes. It has to be wrapped because otherwise
7170// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7171// be used to form addressing mode. These wrapped nodes will be selected
7172// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00007173SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007174X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007175 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007176
Chris Lattner41621a22009-06-26 19:22:52 +00007177 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7178 // global base reg.
7179 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007180 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007181 CodeModel::Model M = getTargetMachine().getCodeModel();
7182
Chris Lattner4f066492009-07-11 20:29:19 +00007183 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007184 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007185 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007186 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007187 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007188 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007189 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007190
Evan Cheng1606e8e2009-03-13 07:51:59 +00007191 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00007192 CP->getAlignment(),
7193 CP->getOffset(), OpFlag);
7194 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00007195 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007196 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00007197 if (OpFlag) {
7198 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007199 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007200 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007201 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007202 }
7203
7204 return Result;
7205}
7206
Dan Gohmand858e902010-04-17 15:26:15 +00007207SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007208 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007209
Chris Lattner18c59872009-06-27 04:16:01 +00007210 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7211 // global base reg.
7212 unsigned char OpFlag = 0;
7213 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007214 CodeModel::Model M = getTargetMachine().getCodeModel();
7215
Chris Lattner4f066492009-07-11 20:29:19 +00007216 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007217 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007218 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007219 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007220 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007221 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007222 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007223
Chris Lattner18c59872009-06-27 04:16:01 +00007224 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7225 OpFlag);
7226 DebugLoc DL = JT->getDebugLoc();
7227 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007228
Chris Lattner18c59872009-06-27 04:16:01 +00007229 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00007230 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00007231 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7232 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007233 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007234 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007235
Chris Lattner18c59872009-06-27 04:16:01 +00007236 return Result;
7237}
7238
7239SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007240X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007241 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00007242
Chris Lattner18c59872009-06-27 04:16:01 +00007243 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7244 // global base reg.
7245 unsigned char OpFlag = 0;
7246 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007247 CodeModel::Model M = getTargetMachine().getCodeModel();
7248
Chris Lattner4f066492009-07-11 20:29:19 +00007249 if (Subtarget->isPICStyleRIPRel() &&
Eli Friedman586272d2011-08-11 01:48:05 +00007250 (M == CodeModel::Small || M == CodeModel::Kernel)) {
7251 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
7252 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00007253 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00007254 } else if (Subtarget->isPICStyleGOT()) {
7255 OpFlag = X86II::MO_GOT;
7256 } else if (Subtarget->isPICStyleStubPIC()) {
7257 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
7258 } else if (Subtarget->isPICStyleStubNoDynamic()) {
7259 OpFlag = X86II::MO_DARWIN_NONLAZY;
7260 }
Eric Christopherfd179292009-08-27 18:07:15 +00007261
Chris Lattner18c59872009-06-27 04:16:01 +00007262 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00007263
Chris Lattner18c59872009-06-27 04:16:01 +00007264 DebugLoc DL = Op.getDebugLoc();
7265 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007266
7267
Chris Lattner18c59872009-06-27 04:16:01 +00007268 // With PIC, the address is actually $g + Offset.
7269 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007270 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007271 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7272 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007273 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007274 Result);
7275 }
Eric Christopherfd179292009-08-27 18:07:15 +00007276
Eli Friedman586272d2011-08-11 01:48:05 +00007277 // For symbols that require a load from a stub to get the address, emit the
7278 // load.
7279 if (isGlobalStubReference(OpFlag))
7280 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
7281 MachinePointerInfo::getGOT(), false, false, 0);
7282
Chris Lattner18c59872009-06-27 04:16:01 +00007283 return Result;
7284}
7285
Dan Gohman475871a2008-07-27 21:46:04 +00007286SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007287X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007288 // Create the TargetBlockAddressAddress node.
7289 unsigned char OpFlags =
7290 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007291 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007292 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00007293 DebugLoc dl = Op.getDebugLoc();
7294 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
7295 /*isTarget=*/true, OpFlags);
7296
Dan Gohmanf705adb2009-10-30 01:28:02 +00007297 if (Subtarget->isPICStyleRIPRel() &&
7298 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007299 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7300 else
7301 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007302
Dan Gohman29cbade2009-11-20 23:18:13 +00007303 // With PIC, the address is actually $g + Offset.
7304 if (isGlobalRelativeToPICBase(OpFlags)) {
7305 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7306 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7307 Result);
7308 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007309
7310 return Result;
7311}
7312
7313SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00007314X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00007315 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00007316 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007317 // Create the TargetGlobalAddress node, folding in the constant
7318 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007319 unsigned char OpFlags =
7320 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007321 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007322 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007323 if (OpFlags == X86II::MO_NO_FLAG &&
7324 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007325 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007326 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007327 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007328 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007329 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007330 }
Eric Christopherfd179292009-08-27 18:07:15 +00007331
Chris Lattner4f066492009-07-11 20:29:19 +00007332 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007333 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007334 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7335 else
7336 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007337
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007338 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007339 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007340 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7341 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007342 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007343 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007344
Chris Lattner36c25012009-07-10 07:34:39 +00007345 // For globals that require a load from a stub to get the address, emit the
7346 // load.
7347 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007348 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00007349 MachinePointerInfo::getGOT(), false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007350
Dan Gohman6520e202008-10-18 02:06:02 +00007351 // If there was a non-zero offset that we didn't fold, create an explicit
7352 // addition for it.
7353 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007354 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007355 DAG.getConstant(Offset, getPointerTy()));
7356
Evan Cheng0db9fe62006-04-25 20:13:52 +00007357 return Result;
7358}
7359
Evan Chengda43bcf2008-09-24 00:05:32 +00007360SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007361X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007362 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007363 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007364 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007365}
7366
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007367static SDValue
7368GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007369 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00007370 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007371 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007372 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007373 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007374 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007375 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007376 GA->getOffset(),
7377 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007378 if (InFlag) {
7379 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00007380 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007381 } else {
7382 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00007383 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007384 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007385
7386 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007387 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007388
Rafael Espindola15f1b662009-04-24 12:59:40 +00007389 SDValue Flag = Chain.getValue(1);
7390 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007391}
7392
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007393// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007394static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007395LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007396 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007397 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00007398 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
7399 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007400 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007401 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007402 InFlag = Chain.getValue(1);
7403
Chris Lattnerb903bed2009-06-26 21:20:29 +00007404 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007405}
7406
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007407// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007408static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007409LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007410 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007411 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7412 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007413}
7414
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007415// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
7416// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007417static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007418 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00007419 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007420 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00007421
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007422 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
7423 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
7424 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00007425
Michael J. Spencerec38de22010-10-10 22:04:20 +00007426 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007427 DAG.getIntPtrConstant(0),
7428 MachinePointerInfo(Ptr), false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00007429
Chris Lattnerb903bed2009-06-26 21:20:29 +00007430 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007431 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
7432 // initialexec.
7433 unsigned WrapperKind = X86ISD::Wrapper;
7434 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007435 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00007436 } else if (is64Bit) {
7437 assert(model == TLSModel::InitialExec);
7438 OperandFlags = X86II::MO_GOTTPOFF;
7439 WrapperKind = X86ISD::WrapperRIP;
7440 } else {
7441 assert(model == TLSModel::InitialExec);
7442 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00007443 }
Eric Christopherfd179292009-08-27 18:07:15 +00007444
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007445 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
7446 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00007447 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00007448 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007449 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007450 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007451
Rafael Espindola9a580232009-02-27 13:37:18 +00007452 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007453 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00007454 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007455
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007456 // The address of the thread local variable is the add of the thread
7457 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00007458 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007459}
7460
Dan Gohman475871a2008-07-27 21:46:04 +00007461SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007462X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00007463
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007464 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00007465 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00007466
Eric Christopher30ef0e52010-06-03 04:07:48 +00007467 if (Subtarget->isTargetELF()) {
7468 // TODO: implement the "local dynamic" model
7469 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00007470
Eric Christopher30ef0e52010-06-03 04:07:48 +00007471 // If GV is an alias then use the aliasee for determining
7472 // thread-localness.
7473 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
7474 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007475
7476 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00007477 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00007478
Eric Christopher30ef0e52010-06-03 04:07:48 +00007479 switch (model) {
7480 case TLSModel::GeneralDynamic:
7481 case TLSModel::LocalDynamic: // not implemented
7482 if (Subtarget->is64Bit())
7483 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
7484 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00007485
Eric Christopher30ef0e52010-06-03 04:07:48 +00007486 case TLSModel::InitialExec:
7487 case TLSModel::LocalExec:
7488 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
7489 Subtarget->is64Bit());
7490 }
7491 } else if (Subtarget->isTargetDarwin()) {
7492 // Darwin only has one model of TLS. Lower to that.
7493 unsigned char OpFlag = 0;
7494 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7495 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007496
Eric Christopher30ef0e52010-06-03 04:07:48 +00007497 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7498 // global base reg.
7499 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7500 !Subtarget->is64Bit();
7501 if (PIC32)
7502 OpFlag = X86II::MO_TLVP_PIC_BASE;
7503 else
7504 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007505 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007506 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00007507 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007508 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007509 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007510
Eric Christopher30ef0e52010-06-03 04:07:48 +00007511 // With PIC32, the address is actually $g + Offset.
7512 if (PIC32)
7513 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7514 DAG.getNode(X86ISD::GlobalBaseReg,
7515 DebugLoc(), getPointerTy()),
7516 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007517
Eric Christopher30ef0e52010-06-03 04:07:48 +00007518 // Lowering the machine isd will make sure everything is in the right
7519 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007520 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007521 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00007522 SDValue Args[] = { Chain, Offset };
7523 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007524
Eric Christopher30ef0e52010-06-03 04:07:48 +00007525 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7526 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7527 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007528
Eric Christopher30ef0e52010-06-03 04:07:48 +00007529 // And our return value (tls address) is in the standard call return value
7530 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007531 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Evan Chengfd230df2011-10-19 22:22:54 +00007532 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(),
7533 Chain.getValue(1));
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007534 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007535
Eric Christopher30ef0e52010-06-03 04:07:48 +00007536 assert(false &&
7537 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00007538
Torok Edwinc23197a2009-07-14 16:55:14 +00007539 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00007540 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007541}
7542
Evan Cheng0db9fe62006-04-25 20:13:52 +00007543
Nadav Rotem43012222011-05-11 08:12:09 +00007544/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00007545/// take a 2 x i32 value to shift plus a shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +00007546SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00007547 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00007548 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007549 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007550 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007551 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00007552 SDValue ShOpLo = Op.getOperand(0);
7553 SDValue ShOpHi = Op.getOperand(1);
7554 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00007555 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00007556 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00007557 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00007558
Dan Gohman475871a2008-07-27 21:46:04 +00007559 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007560 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007561 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
7562 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007563 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007564 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
7565 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007566 }
Evan Chenge3413162006-01-09 18:33:28 +00007567
Owen Anderson825b72b2009-08-11 20:47:22 +00007568 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
7569 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00007570 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00007571 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00007572
Dan Gohman475871a2008-07-27 21:46:04 +00007573 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00007574 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00007575 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
7576 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00007577
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007578 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007579 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7580 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007581 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007582 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7583 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007584 }
7585
Dan Gohman475871a2008-07-27 21:46:04 +00007586 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00007587 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007588}
Evan Chenga3195e82006-01-12 22:54:21 +00007589
Dan Gohmand858e902010-04-17 15:26:15 +00007590SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
7591 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007592 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00007593
Dale Johannesen0488fb62010-09-30 23:57:10 +00007594 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007595 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007596
Owen Anderson825b72b2009-08-11 20:47:22 +00007597 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00007598 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007599
Eli Friedman36df4992009-05-27 00:47:34 +00007600 // These are really Legal; return the operand so the caller accepts it as
7601 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007602 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00007603 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00007604 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00007605 Subtarget->is64Bit()) {
7606 return Op;
7607 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007608
Bruno Cardoso Lopesa511b8e2011-08-09 17:39:01 +00007609 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007610 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007611 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00007612 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007613 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00007614 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00007615 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007616 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007617 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007618 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
7619}
Evan Cheng0db9fe62006-04-25 20:13:52 +00007620
Owen Andersone50ed302009-08-10 22:56:29 +00007621SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007622 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00007623 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007624 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00007625 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00007626 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00007627 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007628 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007629 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00007630 else
Owen Anderson825b72b2009-08-11 20:47:22 +00007631 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007632
Chris Lattner492a43e2010-09-22 01:28:21 +00007633 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007634
Stuart Hastings84be9582011-06-02 15:57:11 +00007635 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
7636 MachineMemOperand *MMO;
7637 if (FI) {
7638 int SSFI = FI->getIndex();
7639 MMO =
7640 DAG.getMachineFunction()
7641 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7642 MachineMemOperand::MOLoad, ByteSize, ByteSize);
7643 } else {
7644 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
7645 StackSlot = StackSlot.getOperand(1);
7646 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007647 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007648 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
7649 X86ISD::FILD, DL,
7650 Tys, Ops, array_lengthof(Ops),
7651 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007652
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007653 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007654 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00007655 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007656
7657 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
7658 // shouldn't be necessary except that RFP cannot be live across
7659 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007660 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007661 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
7662 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007663 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00007664 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007665 SDValue Ops[] = {
7666 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
7667 };
Chris Lattner492a43e2010-09-22 01:28:21 +00007668 MachineMemOperand *MMO =
7669 DAG.getMachineFunction()
7670 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007671 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007672
Chris Lattner492a43e2010-09-22 01:28:21 +00007673 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
7674 Ops, array_lengthof(Ops),
7675 Op.getValueType(), MMO);
7676 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007677 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007678 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007679 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007680
Evan Cheng0db9fe62006-04-25 20:13:52 +00007681 return Result;
7682}
7683
Bill Wendling8b8a6362009-01-17 03:56:04 +00007684// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007685SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
7686 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00007687 // This algorithm is not obvious. Here it is in C code, more or less:
7688 /*
7689 double uint64_to_double( uint32_t hi, uint32_t lo ) {
7690 static const __m128i exp = { 0x4330000045300000ULL, 0 };
7691 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00007692
Bill Wendling8b8a6362009-01-17 03:56:04 +00007693 // Copy ints to xmm registers.
7694 __m128i xh = _mm_cvtsi32_si128( hi );
7695 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00007696
Bill Wendling8b8a6362009-01-17 03:56:04 +00007697 // Combine into low half of a single xmm register.
7698 __m128i x = _mm_unpacklo_epi32( xh, xl );
7699 __m128d d;
7700 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00007701
Bill Wendling8b8a6362009-01-17 03:56:04 +00007702 // Merge in appropriate exponents to give the integer bits the right
7703 // magnitude.
7704 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00007705
Bill Wendling8b8a6362009-01-17 03:56:04 +00007706 // Subtract away the biases to deal with the IEEE-754 double precision
7707 // implicit 1.
7708 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00007709
Bill Wendling8b8a6362009-01-17 03:56:04 +00007710 // All conversions up to here are exact. The correctly rounded result is
7711 // calculated using the current rounding mode using the following
7712 // horizontal add.
7713 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
7714 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
7715 // store doesn't really need to be here (except
7716 // maybe to zero the other double)
7717 return sd;
7718 }
7719 */
Dale Johannesen040225f2008-10-21 23:07:49 +00007720
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007721 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00007722 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00007723
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007724 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00007725 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00007726 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
7727 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
7728 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
7729 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00007730 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007731 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007732
Bill Wendling8b8a6362009-01-17 03:56:04 +00007733 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00007734 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007735 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00007736 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007737 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00007738 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007739 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007740
Owen Anderson825b72b2009-08-11 20:47:22 +00007741 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7742 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00007743 Op.getOperand(0),
7744 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007745 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7746 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00007747 Op.getOperand(0),
7748 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007749 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
7750 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00007751 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007752 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00007753 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007754 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
Owen Anderson825b72b2009-08-11 20:47:22 +00007755 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00007756 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007757 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00007758 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007759
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007760 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00007761 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00007762 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
7763 DAG.getUNDEF(MVT::v2f64), ShufMask);
7764 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
7765 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007766 DAG.getIntPtrConstant(0));
7767}
7768
Bill Wendling8b8a6362009-01-17 03:56:04 +00007769// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007770SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
7771 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007772 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007773 // FP constant to bias correct the final result.
7774 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00007775 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007776
7777 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00007778 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00007779 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007780
Eli Friedmanf3704762011-08-29 21:15:46 +00007781 // Zero out the upper parts of the register.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00007782 Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget->hasXMMInt(),
7783 DAG);
Eli Friedmanf3704762011-08-29 21:15:46 +00007784
Owen Anderson825b72b2009-08-11 20:47:22 +00007785 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007786 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007787 DAG.getIntPtrConstant(0));
7788
7789 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007790 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007791 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007792 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007793 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007794 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007795 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007796 MVT::v2f64, Bias)));
7797 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007798 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007799 DAG.getIntPtrConstant(0));
7800
7801 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007802 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007803
7804 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00007805 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00007806
Owen Anderson825b72b2009-08-11 20:47:22 +00007807 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007808 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00007809 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007810 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007811 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00007812 }
7813
7814 // Handle final rounding.
7815 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00007816}
7817
Dan Gohmand858e902010-04-17 15:26:15 +00007818SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
7819 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00007820 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007821 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007822
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007823 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00007824 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
7825 // the optimization here.
7826 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00007827 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00007828
Owen Andersone50ed302009-08-10 22:56:29 +00007829 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007830 EVT DstVT = Op.getValueType();
7831 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007832 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007833 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007834 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00007835
7836 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00007837 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007838 if (SrcVT == MVT::i32) {
7839 SDValue WordOff = DAG.getConstant(4, getPointerTy());
7840 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
7841 getPointerTy(), StackSlot, WordOff);
7842 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007843 StackSlot, MachinePointerInfo(),
7844 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007845 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007846 OffsetSlot, MachinePointerInfo(),
7847 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007848 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
7849 return Fild;
7850 }
7851
7852 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
7853 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007854 StackSlot, MachinePointerInfo(),
7855 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007856 // For i64 source, we need to add the appropriate power of 2 if the input
7857 // was negative. This is the same as the optimization in
7858 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
7859 // we must be careful to do the computation in x87 extended precision, not
7860 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00007861 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
7862 MachineMemOperand *MMO =
7863 DAG.getMachineFunction()
7864 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7865 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007866
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007867 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
7868 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007869 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
7870 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007871
7872 APInt FF(32, 0x5F800000ULL);
7873
7874 // Check whether the sign bit is set.
7875 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
7876 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
7877 ISD::SETLT);
7878
7879 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
7880 SDValue FudgePtr = DAG.getConstantPool(
7881 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
7882 getPointerTy());
7883
7884 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
7885 SDValue Zero = DAG.getIntPtrConstant(0);
7886 SDValue Four = DAG.getIntPtrConstant(4);
7887 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
7888 Zero, Four);
7889 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
7890
7891 // Load the value out, extending it from f32 to f80.
7892 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00007893 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00007894 FudgePtr, MachinePointerInfo::getConstantPool(),
7895 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007896 // Extend everything to 80 bits to force it to be done on x87.
7897 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
7898 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007899}
7900
Dan Gohman475871a2008-07-27 21:46:04 +00007901std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00007902FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00007903 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00007904
Owen Andersone50ed302009-08-10 22:56:29 +00007905 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00007906
7907 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007908 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
7909 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00007910 }
7911
Owen Anderson825b72b2009-08-11 20:47:22 +00007912 assert(DstTy.getSimpleVT() <= MVT::i64 &&
7913 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00007914 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00007915
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007916 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007917 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00007918 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007919 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00007920 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00007921 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00007922 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007923 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007924
Evan Cheng87c89352007-10-15 20:11:21 +00007925 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
7926 // stack slot.
7927 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00007928 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00007929 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007930 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00007931
Michael J. Spencerec38de22010-10-10 22:04:20 +00007932
7933
Evan Cheng0db9fe62006-04-25 20:13:52 +00007934 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00007935 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007936 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007937 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
7938 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
7939 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007940 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007941
Dan Gohman475871a2008-07-27 21:46:04 +00007942 SDValue Chain = DAG.getEntryNode();
7943 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00007944 EVT TheVT = Op.getOperand(0).getValueType();
7945 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007946 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00007947 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007948 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007949 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007950 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00007951 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00007952 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00007953 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00007954
Chris Lattner492a43e2010-09-22 01:28:21 +00007955 MachineMemOperand *MMO =
7956 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7957 MachineMemOperand::MOLoad, MemSize, MemSize);
7958 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
7959 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007960 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00007961 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007962 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
7963 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007964
Chris Lattner07290932010-09-22 01:05:16 +00007965 MachineMemOperand *MMO =
7966 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7967 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007968
Evan Cheng0db9fe62006-04-25 20:13:52 +00007969 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00007970 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00007971 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
7972 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00007973
Chris Lattner27a6c732007-11-24 07:07:01 +00007974 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007975}
7976
Dan Gohmand858e902010-04-17 15:26:15 +00007977SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
7978 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00007979 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007980 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007981
Eli Friedman948e95a2009-05-23 09:59:16 +00007982 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00007983 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00007984 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
7985 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00007986
Chris Lattner27a6c732007-11-24 07:07:01 +00007987 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007988 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007989 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00007990}
7991
Dan Gohmand858e902010-04-17 15:26:15 +00007992SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
7993 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00007994 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
7995 SDValue FIST = Vals.first, StackSlot = Vals.second;
7996 assert(FIST.getNode() && "Unexpected failure");
7997
7998 // Load the result.
7999 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008000 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00008001}
8002
Dan Gohmand858e902010-04-17 15:26:15 +00008003SDValue X86TargetLowering::LowerFABS(SDValue Op,
8004 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008005 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008006 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008007 EVT VT = Op.getValueType();
8008 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008009 if (VT.isVector())
8010 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008011 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008012 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008013 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00008014 CV.push_back(C);
8015 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008016 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008017 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00008018 CV.push_back(C);
8019 CV.push_back(C);
8020 CV.push_back(C);
8021 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008022 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008023 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008024 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008025 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008026 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00008027 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008028 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008029}
8030
Dan Gohmand858e902010-04-17 15:26:15 +00008031SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008032 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008033 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008034 EVT VT = Op.getValueType();
8035 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00008036 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00008037 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008038 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008039 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008040 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00008041 CV.push_back(C);
8042 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008043 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008044 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00008045 CV.push_back(C);
8046 CV.push_back(C);
8047 CV.push_back(C);
8048 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008049 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008050 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008051 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008052 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008053 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00008054 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00008055 if (VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008056 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008057 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008058 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00008059 Op.getOperand(0)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008060 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00008061 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00008062 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00008063 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008064}
8065
Dan Gohmand858e902010-04-17 15:26:15 +00008066SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008067 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00008068 SDValue Op0 = Op.getOperand(0);
8069 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008070 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008071 EVT VT = Op.getValueType();
8072 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00008073
8074 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008075 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008076 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008077 SrcVT = VT;
8078 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008079 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008080 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008081 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008082 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008083 }
8084
8085 // At this point the operands and the result should have the same
8086 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00008087
Evan Cheng68c47cb2007-01-05 07:55:56 +00008088 // First get the sign bit of second operand.
8089 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008090 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008091 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
8092 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008093 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008094 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
8095 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8096 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8097 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008098 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008099 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008100 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008101 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008102 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00008103 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008104 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008105
8106 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008107 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008108 // Op0 is MVT::f32, Op1 is MVT::f64.
8109 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
8110 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
8111 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008112 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00008113 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00008114 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008115 }
8116
Evan Cheng73d6cf12007-01-05 21:37:56 +00008117 // Clear first operand sign bit.
8118 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00008119 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008120 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
8121 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008122 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008123 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
8124 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8125 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8126 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008127 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008128 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008129 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008130 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008131 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00008132 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008133 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008134
8135 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00008136 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008137}
8138
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008139SDValue X86TargetLowering::LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) const {
8140 SDValue N0 = Op.getOperand(0);
8141 DebugLoc dl = Op.getDebugLoc();
8142 EVT VT = Op.getValueType();
8143
8144 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
8145 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
8146 DAG.getConstant(1, VT));
8147 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
8148}
8149
Dan Gohman076aee32009-03-04 19:44:21 +00008150/// Emit nodes that will be selected as "test Op0,Op0", or something
8151/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008152SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008153 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008154 DebugLoc dl = Op.getDebugLoc();
8155
Dan Gohman31125812009-03-07 01:58:32 +00008156 // CF and OF aren't always set the way we want. Determine which
8157 // of these we need.
8158 bool NeedCF = false;
8159 bool NeedOF = false;
8160 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008161 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00008162 case X86::COND_A: case X86::COND_AE:
8163 case X86::COND_B: case X86::COND_BE:
8164 NeedCF = true;
8165 break;
8166 case X86::COND_G: case X86::COND_GE:
8167 case X86::COND_L: case X86::COND_LE:
8168 case X86::COND_O: case X86::COND_NO:
8169 NeedOF = true;
8170 break;
Dan Gohman31125812009-03-07 01:58:32 +00008171 }
8172
Dan Gohman076aee32009-03-04 19:44:21 +00008173 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00008174 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
8175 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008176 if (Op.getResNo() != 0 || NeedOF || NeedCF)
8177 // Emit a CMP with 0, which is the TEST pattern.
8178 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8179 DAG.getConstant(0, Op.getValueType()));
8180
8181 unsigned Opcode = 0;
8182 unsigned NumOperands = 0;
8183 switch (Op.getNode()->getOpcode()) {
8184 case ISD::ADD:
8185 // Due to an isel shortcoming, be conservative if this add is likely to be
8186 // selected as part of a load-modify-store instruction. When the root node
8187 // in a match is a store, isel doesn't know how to remap non-chain non-flag
8188 // uses of other nodes in the match, such as the ADD in this case. This
8189 // leads to the ADD being left around and reselected, with the result being
8190 // two adds in the output. Alas, even if none our users are stores, that
8191 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
8192 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
8193 // climbing the DAG back to the root, and it doesn't seem to be worth the
8194 // effort.
8195 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Dan Gohman076aee32009-03-04 19:44:21 +00008196 UE = Op.getNode()->use_end(); UI != UE; ++UI)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008197 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
8198 goto default_case;
8199
8200 if (ConstantSDNode *C =
8201 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
8202 // An add of one will be selected as an INC.
8203 if (C->getAPIntValue() == 1) {
8204 Opcode = X86ISD::INC;
8205 NumOperands = 1;
8206 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00008207 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008208
8209 // An add of negative one (subtract of one) will be selected as a DEC.
8210 if (C->getAPIntValue().isAllOnesValue()) {
8211 Opcode = X86ISD::DEC;
8212 NumOperands = 1;
8213 break;
8214 }
Dan Gohman076aee32009-03-04 19:44:21 +00008215 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008216
8217 // Otherwise use a regular EFLAGS-setting add.
8218 Opcode = X86ISD::ADD;
8219 NumOperands = 2;
8220 break;
8221 case ISD::AND: {
8222 // If the primary and result isn't used, don't bother using X86ISD::AND,
8223 // because a TEST instruction will be better.
8224 bool NonFlagUse = false;
8225 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8226 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
8227 SDNode *User = *UI;
8228 unsigned UOpNo = UI.getOperandNo();
8229 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
8230 // Look pass truncate.
8231 UOpNo = User->use_begin().getOperandNo();
8232 User = *User->use_begin();
8233 }
8234
8235 if (User->getOpcode() != ISD::BRCOND &&
8236 User->getOpcode() != ISD::SETCC &&
8237 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
8238 NonFlagUse = true;
8239 break;
8240 }
Dan Gohman076aee32009-03-04 19:44:21 +00008241 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008242
8243 if (!NonFlagUse)
8244 break;
8245 }
8246 // FALL THROUGH
8247 case ISD::SUB:
8248 case ISD::OR:
8249 case ISD::XOR:
8250 // Due to the ISEL shortcoming noted above, be conservative if this op is
8251 // likely to be selected as part of a load-modify-store instruction.
8252 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8253 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8254 if (UI->getOpcode() == ISD::STORE)
8255 goto default_case;
8256
8257 // Otherwise use a regular EFLAGS-setting instruction.
8258 switch (Op.getNode()->getOpcode()) {
8259 default: llvm_unreachable("unexpected operator!");
8260 case ISD::SUB: Opcode = X86ISD::SUB; break;
8261 case ISD::OR: Opcode = X86ISD::OR; break;
8262 case ISD::XOR: Opcode = X86ISD::XOR; break;
8263 case ISD::AND: Opcode = X86ISD::AND; break;
8264 }
8265
8266 NumOperands = 2;
8267 break;
8268 case X86ISD::ADD:
8269 case X86ISD::SUB:
8270 case X86ISD::INC:
8271 case X86ISD::DEC:
8272 case X86ISD::OR:
8273 case X86ISD::XOR:
8274 case X86ISD::AND:
8275 return SDValue(Op.getNode(), 1);
8276 default:
8277 default_case:
8278 break;
Dan Gohman076aee32009-03-04 19:44:21 +00008279 }
8280
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008281 if (Opcode == 0)
8282 // Emit a CMP with 0, which is the TEST pattern.
8283 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8284 DAG.getConstant(0, Op.getValueType()));
8285
8286 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
8287 SmallVector<SDValue, 4> Ops;
8288 for (unsigned i = 0; i != NumOperands; ++i)
8289 Ops.push_back(Op.getOperand(i));
8290
8291 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
8292 DAG.ReplaceAllUsesWith(Op, New);
8293 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00008294}
8295
8296/// Emit nodes that will be selected as "cmp Op0,Op1", or something
8297/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008298SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008299 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008300 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
8301 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00008302 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00008303
8304 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00008305 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00008306}
8307
Evan Chengd40d03e2010-01-06 19:38:29 +00008308/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
8309/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00008310SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
8311 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008312 SDValue Op0 = And.getOperand(0);
8313 SDValue Op1 = And.getOperand(1);
8314 if (Op0.getOpcode() == ISD::TRUNCATE)
8315 Op0 = Op0.getOperand(0);
8316 if (Op1.getOpcode() == ISD::TRUNCATE)
8317 Op1 = Op1.getOperand(0);
8318
Evan Chengd40d03e2010-01-06 19:38:29 +00008319 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008320 if (Op1.getOpcode() == ISD::SHL)
8321 std::swap(Op0, Op1);
8322 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008323 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
8324 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008325 // If we looked past a truncate, check that it's only truncating away
8326 // known zeros.
8327 unsigned BitWidth = Op0.getValueSizeInBits();
8328 unsigned AndBitWidth = And.getValueSizeInBits();
8329 if (BitWidth > AndBitWidth) {
8330 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
8331 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
8332 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
8333 return SDValue();
8334 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008335 LHS = Op1;
8336 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00008337 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008338 } else if (Op1.getOpcode() == ISD::Constant) {
8339 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
8340 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00008341 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
8342 LHS = AndLHS.getOperand(0);
8343 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008344 }
Evan Chengd40d03e2010-01-06 19:38:29 +00008345 }
Evan Cheng0488db92007-09-25 01:57:46 +00008346
Evan Chengd40d03e2010-01-06 19:38:29 +00008347 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00008348 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00008349 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00008350 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00008351 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00008352 // Also promote i16 to i32 for performance / code size reason.
8353 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00008354 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00008355 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00008356
Evan Chengd40d03e2010-01-06 19:38:29 +00008357 // If the operand types disagree, extend the shift amount to match. Since
8358 // BT ignores high bits (like shifts) we can use anyextend.
8359 if (LHS.getValueType() != RHS.getValueType())
8360 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008361
Evan Chengd40d03e2010-01-06 19:38:29 +00008362 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
8363 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
8364 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8365 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00008366 }
8367
Evan Cheng54de3ea2010-01-05 06:52:31 +00008368 return SDValue();
8369}
8370
Dan Gohmand858e902010-04-17 15:26:15 +00008371SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Duncan Sands28b77e92011-09-06 19:07:46 +00008372
8373 if (Op.getValueType().isVector()) return LowerVSETCC(Op, DAG);
8374
Evan Cheng54de3ea2010-01-05 06:52:31 +00008375 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
8376 SDValue Op0 = Op.getOperand(0);
8377 SDValue Op1 = Op.getOperand(1);
8378 DebugLoc dl = Op.getDebugLoc();
8379 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
8380
8381 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00008382 // Lower (X & (1 << N)) == 0 to BT(X, N).
8383 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
8384 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00008385 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008386 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00008387 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008388 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
8389 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
8390 if (NewSetCC.getNode())
8391 return NewSetCC;
8392 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00008393
Chris Lattner481eebc2010-12-19 21:23:48 +00008394 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
8395 // these.
8396 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00008397 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00008398 cast<ConstantSDNode>(Op1)->isNullValue()) &&
8399 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008400
Chris Lattner481eebc2010-12-19 21:23:48 +00008401 // If the input is a setcc, then reuse the input setcc or use a new one with
8402 // the inverted condition.
8403 if (Op0.getOpcode() == X86ISD::SETCC) {
8404 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
8405 bool Invert = (CC == ISD::SETNE) ^
8406 cast<ConstantSDNode>(Op1)->isNullValue();
8407 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008408
Evan Cheng2c755ba2010-02-27 07:36:59 +00008409 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00008410 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8411 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
8412 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008413 }
8414
Evan Chenge5b51ac2010-04-17 06:13:15 +00008415 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00008416 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008417 if (X86CC == X86::COND_INVALID)
8418 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008419
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008420 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00008421 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008422 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00008423}
8424
Craig Topper89af15e2011-09-18 08:03:58 +00008425// Lower256IntVSETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008426// ones, and then concatenate the result back.
Craig Topper89af15e2011-09-18 08:03:58 +00008427static SDValue Lower256IntVSETCC(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008428 EVT VT = Op.getValueType();
8429
Duncan Sands28b77e92011-09-06 19:07:46 +00008430 assert(VT.getSizeInBits() == 256 && Op.getOpcode() == ISD::SETCC &&
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008431 "Unsupported value type for operation");
8432
8433 int NumElems = VT.getVectorNumElements();
8434 DebugLoc dl = Op.getDebugLoc();
8435 SDValue CC = Op.getOperand(2);
8436 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
8437 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
8438
8439 // Extract the LHS vectors
8440 SDValue LHS = Op.getOperand(0);
8441 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
8442 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
8443
8444 // Extract the RHS vectors
8445 SDValue RHS = Op.getOperand(1);
8446 SDValue RHS1 = Extract128BitVector(RHS, Idx0, DAG, dl);
8447 SDValue RHS2 = Extract128BitVector(RHS, Idx1, DAG, dl);
8448
8449 // Issue the operation on the smaller types and concatenate the result back
8450 MVT EltVT = VT.getVectorElementType().getSimpleVT();
8451 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
8452 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
8453 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
8454 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
8455}
8456
8457
Dan Gohmand858e902010-04-17 15:26:15 +00008458SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008459 SDValue Cond;
8460 SDValue Op0 = Op.getOperand(0);
8461 SDValue Op1 = Op.getOperand(1);
8462 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00008463 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00008464 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
8465 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008466 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00008467
8468 if (isFP) {
8469 unsigned SSECC = 8;
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008470 EVT EltVT = Op0.getValueType().getVectorElementType();
8471 assert(EltVT == MVT::f32 || EltVT == MVT::f64);
8472
8473 unsigned Opc = EltVT == MVT::f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00008474 bool Swap = false;
8475
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008476 // SSE Condition code mapping:
8477 // 0 - EQ
8478 // 1 - LT
8479 // 2 - LE
8480 // 3 - UNORD
8481 // 4 - NEQ
8482 // 5 - NLT
8483 // 6 - NLE
8484 // 7 - ORD
Nate Begeman30a0de92008-07-17 16:51:19 +00008485 switch (SetCCOpcode) {
8486 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008487 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00008488 case ISD::SETEQ: SSECC = 0; break;
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008489 case ISD::SETOGT:
8490 case ISD::SETGT: Swap = true; // Fallthrough
Bruno Cardoso Lopes457d53d2011-09-12 21:24:07 +00008491 case ISD::SETLT:
8492 case ISD::SETOLT: SSECC = 1; break;
8493 case ISD::SETOGE:
8494 case ISD::SETGE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008495 case ISD::SETLE:
8496 case ISD::SETOLE: SSECC = 2; break;
8497 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008498 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00008499 case ISD::SETNE: SSECC = 4; break;
8500 case ISD::SETULE: Swap = true;
8501 case ISD::SETUGE: SSECC = 5; break;
8502 case ISD::SETULT: Swap = true;
8503 case ISD::SETUGT: SSECC = 6; break;
8504 case ISD::SETO: SSECC = 7; break;
8505 }
8506 if (Swap)
8507 std::swap(Op0, Op1);
8508
Nate Begemanfb8ead02008-07-25 19:05:58 +00008509 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00008510 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00008511 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00008512 SDValue UNORD, EQ;
Bruno Cardoso Lopes809f17f2011-09-13 19:33:00 +00008513 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
8514 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00008515 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00008516 }
8517 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00008518 SDValue ORD, NEQ;
Bruno Cardoso Lopes809f17f2011-09-13 19:33:00 +00008519 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
8520 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00008521 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00008522 }
Torok Edwinc23197a2009-07-14 16:55:14 +00008523 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00008524 }
8525 // Handle all other FP comparisons here.
Bruno Cardoso Lopes809f17f2011-09-13 19:33:00 +00008526 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00008527 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008528
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008529 // Break 256-bit integer vector compare into smaller ones.
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008530 if (!isFP && VT.getSizeInBits() == 256)
Craig Topper89af15e2011-09-18 08:03:58 +00008531 return Lower256IntVSETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008532
Nate Begeman30a0de92008-07-17 16:51:19 +00008533 // We are handling one of the integer comparisons here. Since SSE only has
8534 // GT and EQ comparisons for integer, swapping operands and multiple
8535 // operations may be required for some comparisons.
8536 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
8537 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00008538
Owen Anderson825b72b2009-08-11 20:47:22 +00008539 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00008540 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00008541 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00008542 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00008543 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
8544 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008545 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008546
Nate Begeman30a0de92008-07-17 16:51:19 +00008547 switch (SetCCOpcode) {
8548 default: break;
8549 case ISD::SETNE: Invert = true;
8550 case ISD::SETEQ: Opc = EQOpc; break;
8551 case ISD::SETLT: Swap = true;
8552 case ISD::SETGT: Opc = GTOpc; break;
8553 case ISD::SETGE: Swap = true;
8554 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
8555 case ISD::SETULT: Swap = true;
8556 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
8557 case ISD::SETUGE: Swap = true;
8558 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
8559 }
8560 if (Swap)
8561 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008562
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008563 // Check that the operation in question is available (most are plain SSE2,
8564 // but PCMPGTQ and PCMPEQQ have different requirements).
8565 if (Opc == X86ISD::PCMPGTQ && !Subtarget->hasSSE42() && !Subtarget->hasAVX())
8566 return SDValue();
8567 if (Opc == X86ISD::PCMPEQQ && !Subtarget->hasSSE41() && !Subtarget->hasAVX())
8568 return SDValue();
8569
Nate Begeman30a0de92008-07-17 16:51:19 +00008570 // Since SSE has no unsigned integer comparisons, we need to flip the sign
8571 // bits of the inputs before performing those operations.
8572 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00008573 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00008574 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
8575 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00008576 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00008577 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
8578 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00008579 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
8580 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00008581 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008582
Dale Johannesenace16102009-02-03 19:33:06 +00008583 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00008584
8585 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00008586 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00008587 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00008588
Nate Begeman30a0de92008-07-17 16:51:19 +00008589 return Result;
8590}
Evan Cheng0488db92007-09-25 01:57:46 +00008591
Evan Cheng370e5342008-12-03 08:38:43 +00008592// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00008593static bool isX86LogicalCmp(SDValue Op) {
8594 unsigned Opc = Op.getNode()->getOpcode();
8595 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
8596 return true;
8597 if (Op.getResNo() == 1 &&
8598 (Opc == X86ISD::ADD ||
8599 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00008600 Opc == X86ISD::ADC ||
8601 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00008602 Opc == X86ISD::SMUL ||
8603 Opc == X86ISD::UMUL ||
8604 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00008605 Opc == X86ISD::DEC ||
8606 Opc == X86ISD::OR ||
8607 Opc == X86ISD::XOR ||
8608 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00008609 return true;
8610
Chris Lattner9637d5b2010-12-05 07:49:54 +00008611 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
8612 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008613
Dan Gohman076aee32009-03-04 19:44:21 +00008614 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00008615}
8616
Chris Lattnera2b56002010-12-05 01:23:24 +00008617static bool isZero(SDValue V) {
8618 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8619 return C && C->isNullValue();
8620}
8621
Chris Lattner96908b12010-12-05 02:00:51 +00008622static bool isAllOnes(SDValue V) {
8623 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8624 return C && C->isAllOnesValue();
8625}
8626
Dan Gohmand858e902010-04-17 15:26:15 +00008627SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008628 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008629 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00008630 SDValue Op1 = Op.getOperand(1);
8631 SDValue Op2 = Op.getOperand(2);
8632 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008633 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00008634
Dan Gohman1a492952009-10-20 16:22:37 +00008635 if (Cond.getOpcode() == ISD::SETCC) {
8636 SDValue NewCond = LowerSETCC(Cond, DAG);
8637 if (NewCond.getNode())
8638 Cond = NewCond;
8639 }
Evan Cheng734503b2006-09-11 02:19:56 +00008640
Chris Lattnera2b56002010-12-05 01:23:24 +00008641 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008642 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00008643 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008644 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008645 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00008646 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
8647 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008648 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008649
Chris Lattnera2b56002010-12-05 01:23:24 +00008650 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008651
8652 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00008653 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
8654 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00008655
8656 SDValue CmpOp0 = Cmp.getOperand(0);
8657 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
8658 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008659
Chris Lattner96908b12010-12-05 02:00:51 +00008660 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00008661 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8662 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008663
Chris Lattner96908b12010-12-05 02:00:51 +00008664 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
8665 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008666
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008667 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00008668 if (N2C == 0 || !N2C->isNullValue())
8669 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
8670 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008671 }
8672 }
8673
Chris Lattnera2b56002010-12-05 01:23:24 +00008674 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00008675 if (Cond.getOpcode() == ISD::AND &&
8676 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8677 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008678 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008679 Cond = Cond.getOperand(0);
8680 }
8681
Evan Cheng3f41d662007-10-08 22:16:29 +00008682 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8683 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00008684 if (Cond.getOpcode() == X86ISD::SETCC ||
8685 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008686 CC = Cond.getOperand(0);
8687
Dan Gohman475871a2008-07-27 21:46:04 +00008688 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008689 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00008690 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00008691
Evan Cheng3f41d662007-10-08 22:16:29 +00008692 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008693 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00008694 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00008695 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00008696
Chris Lattnerd1980a52009-03-12 06:52:53 +00008697 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
8698 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00008699 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008700 addTest = false;
8701 }
8702 }
8703
8704 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008705 // Look pass the truncate.
8706 if (Cond.getOpcode() == ISD::TRUNCATE)
8707 Cond = Cond.getOperand(0);
8708
8709 // We know the result of AND is compared against zero. Try to match
8710 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00008711 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00008712 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00008713 if (NewSetCC.getNode()) {
8714 CC = NewSetCC.getOperand(0);
8715 Cond = NewSetCC.getOperand(1);
8716 addTest = false;
8717 }
8718 }
8719 }
8720
8721 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008722 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00008723 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00008724 }
8725
Benjamin Kramere915ff32010-12-22 23:09:28 +00008726 // a < b ? -1 : 0 -> RES = ~setcc_carry
8727 // a < b ? 0 : -1 -> RES = setcc_carry
8728 // a >= b ? -1 : 0 -> RES = setcc_carry
8729 // a >= b ? 0 : -1 -> RES = ~setcc_carry
8730 if (Cond.getOpcode() == X86ISD::CMP) {
8731 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
8732
8733 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
8734 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
8735 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8736 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
8737 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
8738 return DAG.getNOT(DL, Res, Res.getValueType());
8739 return Res;
8740 }
8741 }
8742
Evan Cheng0488db92007-09-25 01:57:46 +00008743 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
8744 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008745 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008746 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00008747 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00008748}
8749
Evan Cheng370e5342008-12-03 08:38:43 +00008750// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
8751// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
8752// from the AND / OR.
8753static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
8754 Opc = Op.getOpcode();
8755 if (Opc != ISD::OR && Opc != ISD::AND)
8756 return false;
8757 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8758 Op.getOperand(0).hasOneUse() &&
8759 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
8760 Op.getOperand(1).hasOneUse());
8761}
8762
Evan Cheng961d6d42009-02-02 08:19:07 +00008763// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
8764// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00008765static bool isXor1OfSetCC(SDValue Op) {
8766 if (Op.getOpcode() != ISD::XOR)
8767 return false;
8768 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
8769 if (N1C && N1C->getAPIntValue() == 1) {
8770 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8771 Op.getOperand(0).hasOneUse();
8772 }
8773 return false;
8774}
8775
Dan Gohmand858e902010-04-17 15:26:15 +00008776SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008777 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008778 SDValue Chain = Op.getOperand(0);
8779 SDValue Cond = Op.getOperand(1);
8780 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008781 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008782 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00008783
Dan Gohman1a492952009-10-20 16:22:37 +00008784 if (Cond.getOpcode() == ISD::SETCC) {
8785 SDValue NewCond = LowerSETCC(Cond, DAG);
8786 if (NewCond.getNode())
8787 Cond = NewCond;
8788 }
Chris Lattnere55484e2008-12-25 05:34:37 +00008789#if 0
8790 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00008791 else if (Cond.getOpcode() == X86ISD::ADD ||
8792 Cond.getOpcode() == X86ISD::SUB ||
8793 Cond.getOpcode() == X86ISD::SMUL ||
8794 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00008795 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00008796#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00008797
Evan Chengad9c0a32009-12-15 00:53:42 +00008798 // Look pass (and (setcc_carry (cmp ...)), 1).
8799 if (Cond.getOpcode() == ISD::AND &&
8800 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8801 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008802 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008803 Cond = Cond.getOperand(0);
8804 }
8805
Evan Cheng3f41d662007-10-08 22:16:29 +00008806 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8807 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00008808 if (Cond.getOpcode() == X86ISD::SETCC ||
8809 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008810 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008811
Dan Gohman475871a2008-07-27 21:46:04 +00008812 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008813 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00008814 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00008815 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00008816 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008817 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00008818 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00008819 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00008820 default: break;
8821 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00008822 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00008823 // These can only come from an arithmetic instruction with overflow,
8824 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00008825 Cond = Cond.getNode()->getOperand(1);
8826 addTest = false;
8827 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00008828 }
Evan Cheng0488db92007-09-25 01:57:46 +00008829 }
Evan Cheng370e5342008-12-03 08:38:43 +00008830 } else {
8831 unsigned CondOpc;
8832 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
8833 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00008834 if (CondOpc == ISD::OR) {
8835 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
8836 // two branches instead of an explicit OR instruction with a
8837 // separate test.
8838 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00008839 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00008840 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008841 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00008842 Chain, Dest, CC, Cmp);
8843 CC = Cond.getOperand(1).getOperand(0);
8844 Cond = Cmp;
8845 addTest = false;
8846 }
8847 } else { // ISD::AND
8848 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
8849 // two branches instead of an explicit AND instruction with a
8850 // separate test. However, we only do this if this block doesn't
8851 // have a fall-through edge, because this requires an explicit
8852 // jmp when the condition is false.
8853 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00008854 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00008855 Op.getNode()->hasOneUse()) {
8856 X86::CondCode CCode =
8857 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
8858 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008859 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00008860 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00008861 // Look for an unconditional branch following this conditional branch.
8862 // We need this because we need to reverse the successors in order
8863 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00008864 if (User->getOpcode() == ISD::BR) {
8865 SDValue FalseBB = User->getOperand(1);
8866 SDNode *NewBR =
8867 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00008868 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00008869 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00008870 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00008871
Dale Johannesene4d209d2009-02-03 20:21:25 +00008872 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00008873 Chain, Dest, CC, Cmp);
8874 X86::CondCode CCode =
8875 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
8876 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008877 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00008878 Cond = Cmp;
8879 addTest = false;
8880 }
8881 }
Dan Gohman279c22e2008-10-21 03:29:32 +00008882 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00008883 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
8884 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
8885 // It should be transformed during dag combiner except when the condition
8886 // is set by a arithmetics with overflow node.
8887 X86::CondCode CCode =
8888 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
8889 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008890 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00008891 Cond = Cond.getOperand(0).getOperand(1);
8892 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00008893 }
Evan Cheng0488db92007-09-25 01:57:46 +00008894 }
8895
8896 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008897 // Look pass the truncate.
8898 if (Cond.getOpcode() == ISD::TRUNCATE)
8899 Cond = Cond.getOperand(0);
8900
8901 // We know the result of AND is compared against zero. Try to match
8902 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00008903 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008904 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
8905 if (NewSetCC.getNode()) {
8906 CC = NewSetCC.getOperand(0);
8907 Cond = NewSetCC.getOperand(1);
8908 addTest = false;
8909 }
8910 }
8911 }
8912
8913 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008914 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00008915 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00008916 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00008917 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00008918 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00008919}
8920
Anton Korobeynikove060b532007-04-17 19:34:00 +00008921
8922// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
8923// Calls to _alloca is needed to probe the stack when allocating more than 4k
8924// bytes in one go. Touching the stack at 4K increments is necessary to ensure
8925// that the guard pages used by the OS virtual memory manager are allocated in
8926// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00008927SDValue
8928X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008929 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008930 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
8931 EnableSegmentedStacks) &&
8932 "This should be used only on Windows targets or when segmented stacks "
Rafael Espindola96428ce2011-09-06 18:43:08 +00008933 "are being used");
8934 assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008935 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008936
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008937 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00008938 SDValue Chain = Op.getOperand(0);
8939 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008940 // FIXME: Ensure alignment here
8941
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008942 bool Is64Bit = Subtarget->is64Bit();
8943 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008944
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008945 if (EnableSegmentedStacks) {
8946 MachineFunction &MF = DAG.getMachineFunction();
8947 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008948
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008949 if (Is64Bit) {
8950 // The 64 bit implementation of segmented stacks needs to clobber both r10
Rafael Espindola96428ce2011-09-06 18:43:08 +00008951 // r11. This makes it impossible to use it along with nested parameters.
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008952 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00008953
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008954 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
8955 I != E; I++)
8956 if (I->hasNestAttr())
8957 report_fatal_error("Cannot use segmented stacks with functions that "
8958 "have nested arguments.");
8959 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00008960
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008961 const TargetRegisterClass *AddrRegClass =
8962 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
8963 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
8964 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
8965 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
8966 DAG.getRegister(Vreg, SPTy));
8967 SDValue Ops1[2] = { Value, Chain };
8968 return DAG.getMergeValues(Ops1, 2, dl);
8969 } else {
8970 SDValue Flag;
8971 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008972
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008973 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
8974 Flag = Chain.getValue(1);
8975 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008976
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008977 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
8978 Flag = Chain.getValue(1);
8979
8980 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
8981
8982 SDValue Ops1[2] = { Chain.getValue(0), Chain };
8983 return DAG.getMergeValues(Ops1, 2, dl);
8984 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008985}
8986
Dan Gohmand858e902010-04-17 15:26:15 +00008987SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00008988 MachineFunction &MF = DAG.getMachineFunction();
8989 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
8990
Dan Gohman69de1932008-02-06 22:27:42 +00008991 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00008992 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00008993
Anton Korobeynikove7beda12010-10-03 22:52:07 +00008994 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00008995 // vastart just stores the address of the VarArgsFrameIndex slot into the
8996 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00008997 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
8998 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00008999 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
9000 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009001 }
9002
9003 // __va_list_tag:
9004 // gp_offset (0 - 6 * 8)
9005 // fp_offset (48 - 48 + 8 * 16)
9006 // overflow_arg_area (point to parameters coming in memory).
9007 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00009008 SmallVector<SDValue, 8> MemOps;
9009 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00009010 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009011 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009012 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
9013 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009014 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009015 MemOps.push_back(Store);
9016
9017 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009018 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009019 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009020 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009021 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
9022 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009023 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009024 MemOps.push_back(Store);
9025
9026 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00009027 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009028 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00009029 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9030 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009031 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
9032 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00009033 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009034 MemOps.push_back(Store);
9035
9036 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00009037 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009038 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00009039 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
9040 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009041 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
9042 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009043 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009044 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00009045 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00009046}
9047
Dan Gohmand858e902010-04-17 15:26:15 +00009048SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00009049 assert(Subtarget->is64Bit() &&
9050 "LowerVAARG only handles 64-bit va_arg!");
9051 assert((Subtarget->isTargetLinux() ||
9052 Subtarget->isTargetDarwin()) &&
9053 "Unhandled target in LowerVAARG");
9054 assert(Op.getNode()->getNumOperands() == 4);
9055 SDValue Chain = Op.getOperand(0);
9056 SDValue SrcPtr = Op.getOperand(1);
9057 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
9058 unsigned Align = Op.getConstantOperandVal(3);
9059 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00009060
Dan Gohman320afb82010-10-12 18:00:49 +00009061 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009062 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Dan Gohman320afb82010-10-12 18:00:49 +00009063 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
9064 uint8_t ArgMode;
9065
9066 // Decide which area this value should be read from.
9067 // TODO: Implement the AMD64 ABI in its entirety. This simple
9068 // selection mechanism works only for the basic types.
9069 if (ArgVT == MVT::f80) {
9070 llvm_unreachable("va_arg for f80 not yet implemented");
9071 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
9072 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
9073 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
9074 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
9075 } else {
9076 llvm_unreachable("Unhandled argument type in LowerVAARG");
9077 }
9078
9079 if (ArgMode == 2) {
9080 // Sanity Check: Make sure using fp_offset makes sense.
Michael J. Spencer87b86652010-10-19 07:32:42 +00009081 assert(!UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00009082 !(DAG.getMachineFunction()
9083 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00009084 Subtarget->hasXMM());
Dan Gohman320afb82010-10-12 18:00:49 +00009085 }
9086
9087 // Insert VAARG_64 node into the DAG
9088 // VAARG_64 returns two values: Variable Argument Address, Chain
9089 SmallVector<SDValue, 11> InstOps;
9090 InstOps.push_back(Chain);
9091 InstOps.push_back(SrcPtr);
9092 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
9093 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
9094 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
9095 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
9096 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
9097 VTs, &InstOps[0], InstOps.size(),
9098 MVT::i64,
9099 MachinePointerInfo(SV),
9100 /*Align=*/0,
9101 /*Volatile=*/false,
9102 /*ReadMem=*/true,
9103 /*WriteMem=*/true);
9104 Chain = VAARG.getValue(1);
9105
9106 // Load the next argument and return it
9107 return DAG.getLoad(ArgVT, dl,
9108 Chain,
9109 VAARG,
9110 MachinePointerInfo(),
9111 false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00009112}
9113
Dan Gohmand858e902010-04-17 15:26:15 +00009114SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00009115 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00009116 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00009117 SDValue Chain = Op.getOperand(0);
9118 SDValue DstPtr = Op.getOperand(1);
9119 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00009120 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
9121 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00009122 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00009123
Chris Lattnere72f2022010-09-21 05:40:29 +00009124 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00009125 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00009126 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00009127 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00009128}
9129
Dan Gohman475871a2008-07-27 21:46:04 +00009130SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00009131X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009132 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009133 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00009134 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00009135 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00009136 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00009137 case Intrinsic::x86_sse_comieq_ss:
9138 case Intrinsic::x86_sse_comilt_ss:
9139 case Intrinsic::x86_sse_comile_ss:
9140 case Intrinsic::x86_sse_comigt_ss:
9141 case Intrinsic::x86_sse_comige_ss:
9142 case Intrinsic::x86_sse_comineq_ss:
9143 case Intrinsic::x86_sse_ucomieq_ss:
9144 case Intrinsic::x86_sse_ucomilt_ss:
9145 case Intrinsic::x86_sse_ucomile_ss:
9146 case Intrinsic::x86_sse_ucomigt_ss:
9147 case Intrinsic::x86_sse_ucomige_ss:
9148 case Intrinsic::x86_sse_ucomineq_ss:
9149 case Intrinsic::x86_sse2_comieq_sd:
9150 case Intrinsic::x86_sse2_comilt_sd:
9151 case Intrinsic::x86_sse2_comile_sd:
9152 case Intrinsic::x86_sse2_comigt_sd:
9153 case Intrinsic::x86_sse2_comige_sd:
9154 case Intrinsic::x86_sse2_comineq_sd:
9155 case Intrinsic::x86_sse2_ucomieq_sd:
9156 case Intrinsic::x86_sse2_ucomilt_sd:
9157 case Intrinsic::x86_sse2_ucomile_sd:
9158 case Intrinsic::x86_sse2_ucomigt_sd:
9159 case Intrinsic::x86_sse2_ucomige_sd:
9160 case Intrinsic::x86_sse2_ucomineq_sd: {
9161 unsigned Opc = 0;
9162 ISD::CondCode CC = ISD::SETCC_INVALID;
9163 switch (IntNo) {
9164 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009165 case Intrinsic::x86_sse_comieq_ss:
9166 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009167 Opc = X86ISD::COMI;
9168 CC = ISD::SETEQ;
9169 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009170 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009171 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009172 Opc = X86ISD::COMI;
9173 CC = ISD::SETLT;
9174 break;
9175 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009176 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009177 Opc = X86ISD::COMI;
9178 CC = ISD::SETLE;
9179 break;
9180 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009181 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009182 Opc = X86ISD::COMI;
9183 CC = ISD::SETGT;
9184 break;
9185 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009186 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009187 Opc = X86ISD::COMI;
9188 CC = ISD::SETGE;
9189 break;
9190 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009191 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009192 Opc = X86ISD::COMI;
9193 CC = ISD::SETNE;
9194 break;
9195 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009196 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009197 Opc = X86ISD::UCOMI;
9198 CC = ISD::SETEQ;
9199 break;
9200 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009201 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009202 Opc = X86ISD::UCOMI;
9203 CC = ISD::SETLT;
9204 break;
9205 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009206 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009207 Opc = X86ISD::UCOMI;
9208 CC = ISD::SETLE;
9209 break;
9210 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009211 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009212 Opc = X86ISD::UCOMI;
9213 CC = ISD::SETGT;
9214 break;
9215 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009216 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009217 Opc = X86ISD::UCOMI;
9218 CC = ISD::SETGE;
9219 break;
9220 case Intrinsic::x86_sse_ucomineq_ss:
9221 case Intrinsic::x86_sse2_ucomineq_sd:
9222 Opc = X86ISD::UCOMI;
9223 CC = ISD::SETNE;
9224 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009225 }
Evan Cheng734503b2006-09-11 02:19:56 +00009226
Dan Gohman475871a2008-07-27 21:46:04 +00009227 SDValue LHS = Op.getOperand(1);
9228 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00009229 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00009230 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00009231 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
9232 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9233 DAG.getConstant(X86CC, MVT::i8), Cond);
9234 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00009235 }
Duncan Sands04aa4ae2011-09-23 16:10:22 +00009236 // Arithmetic intrinsics.
9237 case Intrinsic::x86_sse3_hadd_ps:
9238 case Intrinsic::x86_sse3_hadd_pd:
9239 case Intrinsic::x86_avx_hadd_ps_256:
9240 case Intrinsic::x86_avx_hadd_pd_256:
9241 return DAG.getNode(X86ISD::FHADD, dl, Op.getValueType(),
9242 Op.getOperand(1), Op.getOperand(2));
9243 case Intrinsic::x86_sse3_hsub_ps:
9244 case Intrinsic::x86_sse3_hsub_pd:
9245 case Intrinsic::x86_avx_hsub_ps_256:
9246 case Intrinsic::x86_avx_hsub_pd_256:
9247 return DAG.getNode(X86ISD::FHSUB, dl, Op.getValueType(),
9248 Op.getOperand(1), Op.getOperand(2));
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009249 // ptest and testp intrinsics. The intrinsic these come from are designed to
9250 // return an integer value, not just an instruction so lower it to the ptest
9251 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00009252 case Intrinsic::x86_sse41_ptestz:
9253 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009254 case Intrinsic::x86_sse41_ptestnzc:
9255 case Intrinsic::x86_avx_ptestz_256:
9256 case Intrinsic::x86_avx_ptestc_256:
9257 case Intrinsic::x86_avx_ptestnzc_256:
9258 case Intrinsic::x86_avx_vtestz_ps:
9259 case Intrinsic::x86_avx_vtestc_ps:
9260 case Intrinsic::x86_avx_vtestnzc_ps:
9261 case Intrinsic::x86_avx_vtestz_pd:
9262 case Intrinsic::x86_avx_vtestc_pd:
9263 case Intrinsic::x86_avx_vtestnzc_pd:
9264 case Intrinsic::x86_avx_vtestz_ps_256:
9265 case Intrinsic::x86_avx_vtestc_ps_256:
9266 case Intrinsic::x86_avx_vtestnzc_ps_256:
9267 case Intrinsic::x86_avx_vtestz_pd_256:
9268 case Intrinsic::x86_avx_vtestc_pd_256:
9269 case Intrinsic::x86_avx_vtestnzc_pd_256: {
9270 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00009271 unsigned X86CC = 0;
9272 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00009273 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009274 case Intrinsic::x86_avx_vtestz_ps:
9275 case Intrinsic::x86_avx_vtestz_pd:
9276 case Intrinsic::x86_avx_vtestz_ps_256:
9277 case Intrinsic::x86_avx_vtestz_pd_256:
9278 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009279 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009280 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009281 // ZF = 1
9282 X86CC = X86::COND_E;
9283 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009284 case Intrinsic::x86_avx_vtestc_ps:
9285 case Intrinsic::x86_avx_vtestc_pd:
9286 case Intrinsic::x86_avx_vtestc_ps_256:
9287 case Intrinsic::x86_avx_vtestc_pd_256:
9288 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009289 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009290 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009291 // CF = 1
9292 X86CC = X86::COND_B;
9293 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009294 case Intrinsic::x86_avx_vtestnzc_ps:
9295 case Intrinsic::x86_avx_vtestnzc_pd:
9296 case Intrinsic::x86_avx_vtestnzc_ps_256:
9297 case Intrinsic::x86_avx_vtestnzc_pd_256:
9298 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00009299 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009300 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009301 // ZF and CF = 0
9302 X86CC = X86::COND_A;
9303 break;
9304 }
Eric Christopherfd179292009-08-27 18:07:15 +00009305
Eric Christopher71c67532009-07-29 00:28:05 +00009306 SDValue LHS = Op.getOperand(1);
9307 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009308 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
9309 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00009310 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
9311 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
9312 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00009313 }
Evan Cheng5759f972008-05-04 09:15:50 +00009314
9315 // Fix vector shift instructions where the last operand is a non-immediate
9316 // i32 value.
9317 case Intrinsic::x86_sse2_pslli_w:
9318 case Intrinsic::x86_sse2_pslli_d:
9319 case Intrinsic::x86_sse2_pslli_q:
9320 case Intrinsic::x86_sse2_psrli_w:
9321 case Intrinsic::x86_sse2_psrli_d:
9322 case Intrinsic::x86_sse2_psrli_q:
9323 case Intrinsic::x86_sse2_psrai_w:
9324 case Intrinsic::x86_sse2_psrai_d:
9325 case Intrinsic::x86_mmx_pslli_w:
9326 case Intrinsic::x86_mmx_pslli_d:
9327 case Intrinsic::x86_mmx_pslli_q:
9328 case Intrinsic::x86_mmx_psrli_w:
9329 case Intrinsic::x86_mmx_psrli_d:
9330 case Intrinsic::x86_mmx_psrli_q:
9331 case Intrinsic::x86_mmx_psrai_w:
9332 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00009333 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00009334 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00009335 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00009336
9337 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00009338 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00009339 switch (IntNo) {
9340 case Intrinsic::x86_sse2_pslli_w:
9341 NewIntNo = Intrinsic::x86_sse2_psll_w;
9342 break;
9343 case Intrinsic::x86_sse2_pslli_d:
9344 NewIntNo = Intrinsic::x86_sse2_psll_d;
9345 break;
9346 case Intrinsic::x86_sse2_pslli_q:
9347 NewIntNo = Intrinsic::x86_sse2_psll_q;
9348 break;
9349 case Intrinsic::x86_sse2_psrli_w:
9350 NewIntNo = Intrinsic::x86_sse2_psrl_w;
9351 break;
9352 case Intrinsic::x86_sse2_psrli_d:
9353 NewIntNo = Intrinsic::x86_sse2_psrl_d;
9354 break;
9355 case Intrinsic::x86_sse2_psrli_q:
9356 NewIntNo = Intrinsic::x86_sse2_psrl_q;
9357 break;
9358 case Intrinsic::x86_sse2_psrai_w:
9359 NewIntNo = Intrinsic::x86_sse2_psra_w;
9360 break;
9361 case Intrinsic::x86_sse2_psrai_d:
9362 NewIntNo = Intrinsic::x86_sse2_psra_d;
9363 break;
9364 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00009365 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00009366 switch (IntNo) {
9367 case Intrinsic::x86_mmx_pslli_w:
9368 NewIntNo = Intrinsic::x86_mmx_psll_w;
9369 break;
9370 case Intrinsic::x86_mmx_pslli_d:
9371 NewIntNo = Intrinsic::x86_mmx_psll_d;
9372 break;
9373 case Intrinsic::x86_mmx_pslli_q:
9374 NewIntNo = Intrinsic::x86_mmx_psll_q;
9375 break;
9376 case Intrinsic::x86_mmx_psrli_w:
9377 NewIntNo = Intrinsic::x86_mmx_psrl_w;
9378 break;
9379 case Intrinsic::x86_mmx_psrli_d:
9380 NewIntNo = Intrinsic::x86_mmx_psrl_d;
9381 break;
9382 case Intrinsic::x86_mmx_psrli_q:
9383 NewIntNo = Intrinsic::x86_mmx_psrl_q;
9384 break;
9385 case Intrinsic::x86_mmx_psrai_w:
9386 NewIntNo = Intrinsic::x86_mmx_psra_w;
9387 break;
9388 case Intrinsic::x86_mmx_psrai_d:
9389 NewIntNo = Intrinsic::x86_mmx_psra_d;
9390 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00009391 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00009392 }
9393 break;
9394 }
9395 }
Mon P Wangefa42202009-09-03 19:56:25 +00009396
9397 // The vector shift intrinsics with scalars uses 32b shift amounts but
9398 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
9399 // to be zero.
9400 SDValue ShOps[4];
9401 ShOps[0] = ShAmt;
9402 ShOps[1] = DAG.getConstant(0, MVT::i32);
9403 if (ShAmtVT == MVT::v4i32) {
9404 ShOps[2] = DAG.getUNDEF(MVT::i32);
9405 ShOps[3] = DAG.getUNDEF(MVT::i32);
9406 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
9407 } else {
9408 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00009409// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00009410 }
9411
Owen Andersone50ed302009-08-10 22:56:29 +00009412 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009413 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009414 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009415 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00009416 Op.getOperand(1), ShAmt);
9417 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00009418 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00009419}
Evan Cheng72261582005-12-20 06:22:03 +00009420
Dan Gohmand858e902010-04-17 15:26:15 +00009421SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
9422 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00009423 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9424 MFI->setReturnAddressIsTaken(true);
9425
Bill Wendling64e87322009-01-16 19:25:27 +00009426 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009427 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00009428
9429 if (Depth > 0) {
9430 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
9431 SDValue Offset =
9432 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00009433 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009434 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00009435 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009436 FrameAddr, Offset),
Chris Lattner51abfe42010-09-21 06:02:19 +00009437 MachinePointerInfo(), false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00009438 }
9439
9440 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00009441 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00009442 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Chris Lattner51abfe42010-09-21 06:02:19 +00009443 RetAddrFI, MachinePointerInfo(), false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00009444}
9445
Dan Gohmand858e902010-04-17 15:26:15 +00009446SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00009447 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9448 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00009449
Owen Andersone50ed302009-08-10 22:56:29 +00009450 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009451 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00009452 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
9453 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00009454 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00009455 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00009456 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
9457 MachinePointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +00009458 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00009459 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00009460}
9461
Dan Gohman475871a2008-07-27 21:46:04 +00009462SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009463 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00009464 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009465}
9466
Dan Gohmand858e902010-04-17 15:26:15 +00009467SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009468 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00009469 SDValue Chain = Op.getOperand(0);
9470 SDValue Offset = Op.getOperand(1);
9471 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009472 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009473
Dan Gohmand8816272010-08-11 18:14:00 +00009474 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
9475 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
9476 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009477 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009478
Dan Gohmand8816272010-08-11 18:14:00 +00009479 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
9480 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009481 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009482 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
9483 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00009484 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009485 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009486
Dale Johannesene4d209d2009-02-03 20:21:25 +00009487 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00009488 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009489 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009490}
9491
Duncan Sands4a544a72011-09-06 13:37:06 +00009492SDValue X86TargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op,
9493 SelectionDAG &DAG) const {
9494 return Op.getOperand(0);
9495}
9496
9497SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
9498 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00009499 SDValue Root = Op.getOperand(0);
9500 SDValue Trmp = Op.getOperand(1); // trampoline
9501 SDValue FPtr = Op.getOperand(2); // nested function
9502 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009503 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009504
Dan Gohman69de1932008-02-06 22:27:42 +00009505 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009506
9507 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00009508 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00009509
9510 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00009511 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
9512 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00009513
Evan Cheng0e6a0522011-07-18 20:57:22 +00009514 const unsigned char N86R10 = X86_MC::getX86RegNum(X86::R10);
9515 const unsigned char N86R11 = X86_MC::getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00009516
9517 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
9518
9519 // Load the pointer to the nested function into R11.
9520 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00009521 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00009522 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009523 Addr, MachinePointerInfo(TrmpAddr),
9524 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009525
Owen Anderson825b72b2009-08-11 20:47:22 +00009526 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9527 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009528 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
9529 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00009530 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00009531
9532 // Load the 'nest' parameter value into R10.
9533 // R10 is specified in X86CallingConv.td
9534 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00009535 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9536 DAG.getConstant(10, MVT::i64));
9537 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009538 Addr, MachinePointerInfo(TrmpAddr, 10),
9539 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009540
Owen Anderson825b72b2009-08-11 20:47:22 +00009541 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9542 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009543 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
9544 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00009545 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00009546
9547 // Jump to the nested function.
9548 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00009549 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9550 DAG.getConstant(20, MVT::i64));
9551 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009552 Addr, MachinePointerInfo(TrmpAddr, 20),
9553 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009554
9555 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00009556 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9557 DAG.getConstant(22, MVT::i64));
9558 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00009559 MachinePointerInfo(TrmpAddr, 22),
9560 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009561
Duncan Sands4a544a72011-09-06 13:37:06 +00009562 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009563 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00009564 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00009565 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00009566 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00009567 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009568
9569 switch (CC) {
9570 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00009571 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00009572 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00009573 case CallingConv::X86_StdCall: {
9574 // Pass 'nest' parameter in ECX.
9575 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00009576 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009577
9578 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009579 FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00009580 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009581
Chris Lattner58d74912008-03-12 17:45:29 +00009582 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00009583 unsigned InRegCount = 0;
9584 unsigned Idx = 1;
9585
9586 for (FunctionType::param_iterator I = FTy->param_begin(),
9587 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00009588 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00009589 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00009590 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009591
9592 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00009593 report_fatal_error("Nest register in use - reduce number of inreg"
9594 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00009595 }
9596 }
9597 break;
9598 }
9599 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00009600 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00009601 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00009602 // Pass 'nest' parameter in EAX.
9603 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00009604 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009605 break;
9606 }
9607
Dan Gohman475871a2008-07-27 21:46:04 +00009608 SDValue OutChains[4];
9609 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009610
Owen Anderson825b72b2009-08-11 20:47:22 +00009611 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9612 DAG.getConstant(10, MVT::i32));
9613 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009614
Chris Lattnera62fe662010-02-05 19:20:30 +00009615 // This is storing the opcode for MOV32ri.
9616 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Evan Cheng0e6a0522011-07-18 20:57:22 +00009617 const unsigned char N86Reg = X86_MC::getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009618 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00009619 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009620 Trmp, MachinePointerInfo(TrmpAddr),
9621 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009622
Owen Anderson825b72b2009-08-11 20:47:22 +00009623 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9624 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009625 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
9626 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00009627 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009628
Chris Lattnera62fe662010-02-05 19:20:30 +00009629 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00009630 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9631 DAG.getConstant(5, MVT::i32));
9632 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00009633 MachinePointerInfo(TrmpAddr, 5),
9634 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009635
Owen Anderson825b72b2009-08-11 20:47:22 +00009636 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9637 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009638 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
9639 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00009640 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009641
Duncan Sands4a544a72011-09-06 13:37:06 +00009642 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009643 }
9644}
9645
Dan Gohmand858e902010-04-17 15:26:15 +00009646SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
9647 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009648 /*
9649 The rounding mode is in bits 11:10 of FPSR, and has the following
9650 settings:
9651 00 Round to nearest
9652 01 Round to -inf
9653 10 Round to +inf
9654 11 Round to 0
9655
9656 FLT_ROUNDS, on the other hand, expects the following:
9657 -1 Undefined
9658 0 Round to 0
9659 1 Round to nearest
9660 2 Round to +inf
9661 3 Round to -inf
9662
9663 To perform the conversion, we do:
9664 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
9665 */
9666
9667 MachineFunction &MF = DAG.getMachineFunction();
9668 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00009669 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009670 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00009671 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00009672 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009673
9674 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00009675 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00009676 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009677
Michael J. Spencerec38de22010-10-10 22:04:20 +00009678
Chris Lattner2156b792010-09-22 01:11:26 +00009679 MachineMemOperand *MMO =
9680 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
9681 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009682
Chris Lattner2156b792010-09-22 01:11:26 +00009683 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
9684 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
9685 DAG.getVTList(MVT::Other),
9686 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009687
9688 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00009689 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Chris Lattner51abfe42010-09-21 06:02:19 +00009690 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009691
9692 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00009693 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00009694 DAG.getNode(ISD::SRL, DL, MVT::i16,
9695 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00009696 CWD, DAG.getConstant(0x800, MVT::i16)),
9697 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00009698 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00009699 DAG.getNode(ISD::SRL, DL, MVT::i16,
9700 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00009701 CWD, DAG.getConstant(0x400, MVT::i16)),
9702 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009703
Dan Gohman475871a2008-07-27 21:46:04 +00009704 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00009705 DAG.getNode(ISD::AND, DL, MVT::i16,
9706 DAG.getNode(ISD::ADD, DL, MVT::i16,
9707 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00009708 DAG.getConstant(1, MVT::i16)),
9709 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009710
9711
Duncan Sands83ec4b62008-06-06 12:08:01 +00009712 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00009713 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009714}
9715
Dan Gohmand858e902010-04-17 15:26:15 +00009716SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009717 EVT VT = Op.getValueType();
9718 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009719 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009720 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00009721
9722 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009723 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00009724 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00009725 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00009726 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009727 }
Evan Cheng18efe262007-12-14 02:13:44 +00009728
Evan Cheng152804e2007-12-14 08:30:15 +00009729 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00009730 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009731 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00009732
9733 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00009734 SDValue Ops[] = {
9735 Op,
9736 DAG.getConstant(NumBits+NumBits-1, OpVT),
9737 DAG.getConstant(X86::COND_E, MVT::i8),
9738 Op.getValue(1)
9739 };
9740 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00009741
9742 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00009743 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00009744
Owen Anderson825b72b2009-08-11 20:47:22 +00009745 if (VT == MVT::i8)
9746 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009747 return Op;
9748}
9749
Dan Gohmand858e902010-04-17 15:26:15 +00009750SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009751 EVT VT = Op.getValueType();
9752 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009753 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009754 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00009755
9756 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009757 if (VT == MVT::i8) {
9758 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00009759 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009760 }
Evan Cheng152804e2007-12-14 08:30:15 +00009761
9762 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00009763 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009764 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00009765
9766 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00009767 SDValue Ops[] = {
9768 Op,
9769 DAG.getConstant(NumBits, OpVT),
9770 DAG.getConstant(X86::COND_E, MVT::i8),
9771 Op.getValue(1)
9772 };
9773 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00009774
Owen Anderson825b72b2009-08-11 20:47:22 +00009775 if (VT == MVT::i8)
9776 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009777 return Op;
9778}
9779
Craig Topper13894fa2011-08-24 06:14:18 +00009780// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
9781// ones, and then concatenate the result back.
9782static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00009783 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +00009784
9785 assert(VT.getSizeInBits() == 256 && VT.isInteger() &&
9786 "Unsupported value type for operation");
9787
9788 int NumElems = VT.getVectorNumElements();
9789 DebugLoc dl = Op.getDebugLoc();
9790 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
9791 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
9792
9793 // Extract the LHS vectors
9794 SDValue LHS = Op.getOperand(0);
9795 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
9796 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
9797
9798 // Extract the RHS vectors
9799 SDValue RHS = Op.getOperand(1);
9800 SDValue RHS1 = Extract128BitVector(RHS, Idx0, DAG, dl);
9801 SDValue RHS2 = Extract128BitVector(RHS, Idx1, DAG, dl);
9802
9803 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9804 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
9805
9806 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
9807 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
9808 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
9809}
9810
9811SDValue X86TargetLowering::LowerADD(SDValue Op, SelectionDAG &DAG) const {
9812 assert(Op.getValueType().getSizeInBits() == 256 &&
9813 Op.getValueType().isInteger() &&
9814 "Only handle AVX 256-bit vector integer operation");
9815 return Lower256IntArith(Op, DAG);
9816}
9817
9818SDValue X86TargetLowering::LowerSUB(SDValue Op, SelectionDAG &DAG) const {
9819 assert(Op.getValueType().getSizeInBits() == 256 &&
9820 Op.getValueType().isInteger() &&
9821 "Only handle AVX 256-bit vector integer operation");
9822 return Lower256IntArith(Op, DAG);
9823}
9824
9825SDValue X86TargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
9826 EVT VT = Op.getValueType();
9827
9828 // Decompose 256-bit ops into smaller 128-bit ops.
9829 if (VT.getSizeInBits() == 256)
9830 return Lower256IntArith(Op, DAG);
9831
Owen Anderson825b72b2009-08-11 20:47:22 +00009832 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009833 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00009834
Mon P Wangaf9b9522008-12-18 21:42:19 +00009835 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
9836 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
9837 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
9838 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
9839 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
9840 //
9841 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
9842 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
9843 // return AloBlo + AloBhi + AhiBlo;
9844
9845 SDValue A = Op.getOperand(0);
9846 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009847
Dale Johannesene4d209d2009-02-03 20:21:25 +00009848 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009849 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
9850 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009851 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009852 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
9853 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009854 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009855 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00009856 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009857 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009858 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00009859 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009860 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009861 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00009862 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009863 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009864 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
9865 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009866 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009867 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
9868 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009869 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
9870 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00009871 return Res;
9872}
9873
Nadav Rotem43012222011-05-11 08:12:09 +00009874SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
9875
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009876 EVT VT = Op.getValueType();
9877 DebugLoc dl = Op.getDebugLoc();
9878 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +00009879 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009880 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009881
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00009882 if (!Subtarget->hasXMMInt())
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00009883 return SDValue();
9884
9885 // Decompose 256-bit shifts into smaller 128-bit shifts.
9886 if (VT.getSizeInBits() == 256) {
9887 int NumElems = VT.getVectorNumElements();
9888 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9889 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
9890
9891 // Extract the two vectors
9892 SDValue V1 = Extract128BitVector(R, DAG.getConstant(0, MVT::i32), DAG, dl);
9893 SDValue V2 = Extract128BitVector(R, DAG.getConstant(NumElems/2, MVT::i32),
9894 DAG, dl);
9895
9896 // Recreate the shift amount vectors
Bruno Cardoso Lopes0dd80b02011-08-17 22:12:20 +00009897 SDValue Amt1, Amt2;
9898 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
9899 // Constant shift amount
9900 SmallVector<SDValue, 4> Amt1Csts;
9901 SmallVector<SDValue, 4> Amt2Csts;
9902 for (int i = 0; i < NumElems/2; ++i)
9903 Amt1Csts.push_back(Amt->getOperand(i));
9904 for (int i = NumElems/2; i < NumElems; ++i)
9905 Amt2Csts.push_back(Amt->getOperand(i));
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00009906
Bruno Cardoso Lopes0dd80b02011-08-17 22:12:20 +00009907 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
9908 &Amt1Csts[0], NumElems/2);
9909 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
9910 &Amt2Csts[0], NumElems/2);
9911 } else {
9912 // Variable shift amount
9913 Amt1 = Extract128BitVector(Amt, DAG.getConstant(0, MVT::i32), DAG, dl);
9914 Amt2 = Extract128BitVector(Amt, DAG.getConstant(NumElems/2, MVT::i32),
9915 DAG, dl);
9916 }
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00009917
9918 // Issue new vector shifts for the smaller types
9919 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
9920 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
9921
9922 // Concatenate the result back
9923 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
9924 }
Nate Begeman51409212010-07-28 00:21:48 +00009925
Nadav Rotem43012222011-05-11 08:12:09 +00009926 // Optimize shl/srl/sra with constant shift amount.
9927 if (isSplatVector(Amt.getNode())) {
9928 SDValue SclrAmt = Amt->getOperand(0);
9929 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
9930 uint64_t ShiftAmt = C->getZExtValue();
9931
9932 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SHL)
9933 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9934 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
9935 R, DAG.getConstant(ShiftAmt, MVT::i32));
9936
9937 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SHL)
9938 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9939 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
9940 R, DAG.getConstant(ShiftAmt, MVT::i32));
9941
9942 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SHL)
9943 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9944 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
9945 R, DAG.getConstant(ShiftAmt, MVT::i32));
9946
9947 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SRL)
9948 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9949 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
9950 R, DAG.getConstant(ShiftAmt, MVT::i32));
9951
9952 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRL)
9953 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9954 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
9955 R, DAG.getConstant(ShiftAmt, MVT::i32));
9956
9957 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRL)
9958 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9959 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
9960 R, DAG.getConstant(ShiftAmt, MVT::i32));
9961
9962 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRA)
9963 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9964 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
9965 R, DAG.getConstant(ShiftAmt, MVT::i32));
9966
9967 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRA)
9968 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9969 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
9970 R, DAG.getConstant(ShiftAmt, MVT::i32));
9971 }
9972 }
9973
9974 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +00009975 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +00009976 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9977 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
9978 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
9979
9980 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009981
Nate Begeman51409212010-07-28 00:21:48 +00009982 std::vector<Constant*> CV(4, CI);
9983 Constant *C = ConstantVector::get(CV);
9984 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
9985 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00009986 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00009987 false, false, 16);
9988
9989 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009990 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +00009991 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
9992 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
9993 }
Nadav Rotem43012222011-05-11 08:12:09 +00009994 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +00009995 // a = a << 5;
9996 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9997 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
9998 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
9999
10000 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
10001 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
10002
10003 std::vector<Constant*> CVM1(16, CM1);
10004 std::vector<Constant*> CVM2(16, CM2);
10005 Constant *C = ConstantVector::get(CVM1);
10006 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10007 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010008 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +000010009 false, false, 16);
10010
10011 // r = pblendv(r, psllw(r & (char16)15, 4), a);
10012 M = DAG.getNode(ISD::AND, dl, VT, R, M);
10013 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10014 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
10015 DAG.getConstant(4, MVT::i32));
Nadav Rotemfbad25e2011-09-11 15:02:23 +000010016 R = DAG.getNode(ISD::VSELECT, dl, VT, Op, R, M);
Nate Begeman51409212010-07-28 00:21:48 +000010017 // a += a
10018 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010019
Nate Begeman51409212010-07-28 00:21:48 +000010020 C = ConstantVector::get(CVM2);
10021 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10022 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010023 MachinePointerInfo::getConstantPool(),
Chris Lattner51abfe42010-09-21 06:02:19 +000010024 false, false, 16);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010025
Nate Begeman51409212010-07-28 00:21:48 +000010026 // r = pblendv(r, psllw(r & (char16)63, 2), a);
10027 M = DAG.getNode(ISD::AND, dl, VT, R, M);
10028 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10029 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
10030 DAG.getConstant(2, MVT::i32));
Nadav Rotemfbad25e2011-09-11 15:02:23 +000010031 R = DAG.getNode(ISD::VSELECT, dl, VT, Op, R, M);
Nate Begeman51409212010-07-28 00:21:48 +000010032 // a += a
10033 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010034
Nate Begeman51409212010-07-28 00:21:48 +000010035 // return pblendv(r, r+r, a);
Nadav Rotemfbad25e2011-09-11 15:02:23 +000010036 R = DAG.getNode(ISD::VSELECT, dl, VT, Op,
10037 R, DAG.getNode(ISD::ADD, dl, VT, R, R));
Nate Begeman51409212010-07-28 00:21:48 +000010038 return R;
10039 }
10040 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010041}
Mon P Wangaf9b9522008-12-18 21:42:19 +000010042
Dan Gohmand858e902010-04-17 15:26:15 +000010043SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +000010044 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
10045 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +000010046 // looks for this combo and may remove the "setcc" instruction if the "setcc"
10047 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +000010048 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +000010049 SDValue LHS = N->getOperand(0);
10050 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +000010051 unsigned BaseOp = 0;
10052 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010053 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +000010054 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010055 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +000010056 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +000010057 // A subtract of one will be selected as a INC. Note that INC doesn't
10058 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010059 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10060 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010061 BaseOp = X86ISD::INC;
10062 Cond = X86::COND_O;
10063 break;
10064 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010065 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +000010066 Cond = X86::COND_O;
10067 break;
10068 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010069 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +000010070 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010071 break;
10072 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +000010073 // A subtract of one will be selected as a DEC. Note that DEC doesn't
10074 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010075 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10076 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010077 BaseOp = X86ISD::DEC;
10078 Cond = X86::COND_O;
10079 break;
10080 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010081 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +000010082 Cond = X86::COND_O;
10083 break;
10084 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010085 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +000010086 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010087 break;
10088 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000010089 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000010090 Cond = X86::COND_O;
10091 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010092 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
10093 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
10094 MVT::i32);
10095 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010096
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010097 SDValue SetCC =
10098 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10099 DAG.getConstant(X86::COND_O, MVT::i32),
10100 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010101
Dan Gohman6e5fda22011-07-22 18:45:15 +000010102 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010103 }
Bill Wendling74c37652008-12-09 22:08:41 +000010104 }
Bill Wendling3fafd932008-11-26 22:37:40 +000010105
Bill Wendling61edeb52008-12-02 01:06:39 +000010106 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010107 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010108 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000010109
Bill Wendling61edeb52008-12-02 01:06:39 +000010110 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010111 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
10112 DAG.getConstant(Cond, MVT::i32),
10113 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000010114
Dan Gohman6e5fda22011-07-22 18:45:15 +000010115 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000010116}
10117
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010118SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op, SelectionDAG &DAG) const{
10119 DebugLoc dl = Op.getDebugLoc();
10120 SDNode* Node = Op.getNode();
10121 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
10122 EVT VT = Node->getValueType(0);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010123 if (Subtarget->hasXMMInt() && VT.isVector()) {
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010124 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
10125 ExtraVT.getScalarType().getSizeInBits();
10126 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
10127
10128 unsigned SHLIntrinsicsID = 0;
10129 unsigned SRAIntrinsicsID = 0;
10130 switch (VT.getSimpleVT().SimpleTy) {
10131 default:
10132 return SDValue();
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010133 case MVT::v4i32: {
10134 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_d;
10135 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_d;
10136 break;
10137 }
10138 case MVT::v8i16: {
10139 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_w;
10140 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_w;
10141 break;
10142 }
10143 }
10144
10145 SDValue Tmp1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10146 DAG.getConstant(SHLIntrinsicsID, MVT::i32),
10147 Node->getOperand(0), ShAmt);
10148
Nadav Rotema7934dd2011-10-10 19:31:45 +000010149 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10150 DAG.getConstant(SRAIntrinsicsID, MVT::i32),
10151 Tmp1, ShAmt);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010152 }
10153
10154 return SDValue();
10155}
10156
10157
Eric Christopher9a9d2752010-07-22 02:48:34 +000010158SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
10159 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010160
Eric Christopher77ed1352011-07-08 00:04:56 +000010161 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
10162 // There isn't any reason to disable it if the target processor supports it.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010163 if (!Subtarget->hasXMMInt() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +000010164 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +000010165 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +000010166 SDValue Ops[] = {
10167 DAG.getRegister(X86::ESP, MVT::i32), // Base
10168 DAG.getTargetConstant(1, MVT::i8), // Scale
10169 DAG.getRegister(0, MVT::i32), // Index
10170 DAG.getTargetConstant(0, MVT::i32), // Disp
10171 DAG.getRegister(0, MVT::i32), // Segment.
10172 Zero,
10173 Chain
10174 };
Michael J. Spencerec38de22010-10-10 22:04:20 +000010175 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +000010176 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
10177 array_lengthof(Ops));
10178 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +000010179 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010180
Eric Christopher9a9d2752010-07-22 02:48:34 +000010181 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +000010182 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +000010183 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010184
Chris Lattner132929a2010-08-14 17:26:09 +000010185 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10186 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
10187 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
10188 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010189
Chris Lattner132929a2010-08-14 17:26:09 +000010190 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
10191 if (!Op1 && !Op2 && !Op3 && Op4)
10192 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010193
Chris Lattner132929a2010-08-14 17:26:09 +000010194 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
10195 if (Op1 && !Op2 && !Op3 && !Op4)
10196 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010197
10198 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +000010199 // (MFENCE)>;
10200 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +000010201}
10202
Eli Friedman14648462011-07-27 22:21:52 +000010203SDValue X86TargetLowering::LowerATOMIC_FENCE(SDValue Op,
10204 SelectionDAG &DAG) const {
10205 DebugLoc dl = Op.getDebugLoc();
10206 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
10207 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
10208 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
10209 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
10210
10211 // The only fence that needs an instruction is a sequentially-consistent
10212 // cross-thread fence.
10213 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
10214 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
10215 // no-sse2). There isn't any reason to disable it if the target processor
10216 // supports it.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010217 if (Subtarget->hasXMMInt() || Subtarget->is64Bit())
Eli Friedman14648462011-07-27 22:21:52 +000010218 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
10219
10220 SDValue Chain = Op.getOperand(0);
10221 SDValue Zero = DAG.getConstant(0, MVT::i32);
10222 SDValue Ops[] = {
10223 DAG.getRegister(X86::ESP, MVT::i32), // Base
10224 DAG.getTargetConstant(1, MVT::i8), // Scale
10225 DAG.getRegister(0, MVT::i32), // Index
10226 DAG.getTargetConstant(0, MVT::i32), // Disp
10227 DAG.getRegister(0, MVT::i32), // Segment.
10228 Zero,
10229 Chain
10230 };
10231 SDNode *Res =
10232 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
10233 array_lengthof(Ops));
10234 return SDValue(Res, 0);
10235 }
10236
10237 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
10238 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
10239}
10240
10241
Dan Gohmand858e902010-04-17 15:26:15 +000010242SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010243 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010244 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000010245 unsigned Reg = 0;
10246 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000010247 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000010248 default:
10249 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000010250 case MVT::i8: Reg = X86::AL; size = 1; break;
10251 case MVT::i16: Reg = X86::AX; size = 2; break;
10252 case MVT::i32: Reg = X86::EAX; size = 4; break;
10253 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000010254 assert(Subtarget->is64Bit() && "Node not type legal!");
10255 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000010256 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000010257 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010258 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000010259 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000010260 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000010261 Op.getOperand(1),
10262 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000010263 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000010264 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010265 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010266 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
10267 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
10268 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000010269 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010270 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000010271 return cpOut;
10272}
10273
Duncan Sands1607f052008-12-01 11:39:25 +000010274SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010275 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +000010276 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010277 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000010278 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010279 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +000010280 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010281 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
10282 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000010283 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000010284 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
10285 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000010286 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000010287 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000010288 rdx.getValue(1)
10289 };
Dale Johannesene4d209d2009-02-03 20:21:25 +000010290 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010291}
10292
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010293SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +000010294 SelectionDAG &DAG) const {
10295 EVT SrcVT = Op.getOperand(0).getValueType();
10296 EVT DstVT = Op.getValueType();
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010297 assert(Subtarget->is64Bit() && !Subtarget->hasXMMInt() &&
Chris Lattner2a786eb2010-12-19 20:19:20 +000010298 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000010299 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000010300 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010301 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000010302 // i64 <=> MMX conversions are Legal.
10303 if (SrcVT==MVT::i64 && DstVT.isVector())
10304 return Op;
10305 if (DstVT==MVT::i64 && SrcVT.isVector())
10306 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000010307 // MMX <=> MMX conversions are Legal.
10308 if (SrcVT.isVector() && DstVT.isVector())
10309 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000010310 // All other conversions need to be expanded.
10311 return SDValue();
10312}
Chris Lattner5b856542010-12-20 00:59:46 +000010313
Dan Gohmand858e902010-04-17 15:26:15 +000010314SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000010315 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +000010316 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000010317 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010318 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000010319 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010320 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010321 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000010322 Node->getOperand(0),
10323 Node->getOperand(1), negOp,
10324 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000010325 cast<AtomicSDNode>(Node)->getAlignment(),
10326 cast<AtomicSDNode>(Node)->getOrdering(),
10327 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000010328}
10329
Eli Friedman327236c2011-08-24 20:50:09 +000010330static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
10331 SDNode *Node = Op.getNode();
10332 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010333 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000010334
10335 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010336 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
10337 // FIXME: On 32-bit, store -> fist or movq would be more efficient
10338 // (The only way to get a 16-byte store is cmpxchg16b)
10339 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
10340 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
10341 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000010342 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
10343 cast<AtomicSDNode>(Node)->getMemoryVT(),
10344 Node->getOperand(0),
10345 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010346 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000010347 cast<AtomicSDNode>(Node)->getOrdering(),
10348 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000010349 return Swap.getValue(1);
10350 }
10351 // Other atomic stores have a simple pattern.
10352 return Op;
10353}
10354
Chris Lattner5b856542010-12-20 00:59:46 +000010355static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
10356 EVT VT = Op.getNode()->getValueType(0);
10357
10358 // Let legalize expand this if it isn't a legal type yet.
10359 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
10360 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010361
Chris Lattner5b856542010-12-20 00:59:46 +000010362 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010363
Chris Lattner5b856542010-12-20 00:59:46 +000010364 unsigned Opc;
10365 bool ExtraOp = false;
10366 switch (Op.getOpcode()) {
10367 default: assert(0 && "Invalid code");
10368 case ISD::ADDC: Opc = X86ISD::ADD; break;
10369 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
10370 case ISD::SUBC: Opc = X86ISD::SUB; break;
10371 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
10372 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010373
Chris Lattner5b856542010-12-20 00:59:46 +000010374 if (!ExtraOp)
10375 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
10376 Op.getOperand(1));
10377 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
10378 Op.getOperand(1), Op.getOperand(2));
10379}
10380
Evan Cheng0db9fe62006-04-25 20:13:52 +000010381/// LowerOperation - Provide custom lowering hooks for some operations.
10382///
Dan Gohmand858e902010-04-17 15:26:15 +000010383SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000010384 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010385 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010386 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Eric Christopher9a9d2752010-07-22 02:48:34 +000010387 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Eli Friedman14648462011-07-27 22:21:52 +000010388 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010389 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
10390 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000010391 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010392 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000010393 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010394 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
10395 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
10396 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +000010397 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +000010398 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010399 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
10400 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
10401 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000010402 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000010403 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000010404 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010405 case ISD::SHL_PARTS:
10406 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000010407 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010408 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000010409 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010410 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000010411 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010412 case ISD::FABS: return LowerFABS(Op, DAG);
10413 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000010414 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000010415 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000010416 case ISD::SETCC: return LowerSETCC(Op, DAG);
10417 case ISD::SELECT: return LowerSELECT(Op, DAG);
10418 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010419 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010420 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000010421 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +000010422 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010423 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000010424 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
10425 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010426 case ISD::FRAME_TO_ARGS_OFFSET:
10427 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010428 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010429 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +000010430 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
10431 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000010432 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000010433 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
10434 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000010435 case ISD::MUL: return LowerMUL(Op, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000010436 case ISD::SRA:
10437 case ISD::SRL:
10438 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000010439 case ISD::SADDO:
10440 case ISD::UADDO:
10441 case ISD::SSUBO:
10442 case ISD::USUBO:
10443 case ISD::SMULO:
10444 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +000010445 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010446 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000010447 case ISD::ADDC:
10448 case ISD::ADDE:
10449 case ISD::SUBC:
10450 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000010451 case ISD::ADD: return LowerADD(Op, DAG);
10452 case ISD::SUB: return LowerSUB(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010453 }
Chris Lattner27a6c732007-11-24 07:07:01 +000010454}
10455
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010456static void ReplaceATOMIC_LOAD(SDNode *Node,
10457 SmallVectorImpl<SDValue> &Results,
10458 SelectionDAG &DAG) {
10459 DebugLoc dl = Node->getDebugLoc();
10460 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
10461
10462 // Convert wide load -> cmpxchg8b/cmpxchg16b
10463 // FIXME: On 32-bit, load -> fild or movq would be more efficient
10464 // (The only way to get a 16-byte load is cmpxchg16b)
10465 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000010466 SDValue Zero = DAG.getConstant(0, VT);
10467 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010468 Node->getOperand(0),
10469 Node->getOperand(1), Zero, Zero,
10470 cast<AtomicSDNode>(Node)->getMemOperand(),
10471 cast<AtomicSDNode>(Node)->getOrdering(),
10472 cast<AtomicSDNode>(Node)->getSynchScope());
10473 Results.push_back(Swap.getValue(0));
10474 Results.push_back(Swap.getValue(1));
10475}
10476
Duncan Sands1607f052008-12-01 11:39:25 +000010477void X86TargetLowering::
10478ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000010479 SelectionDAG &DAG, unsigned NewOp) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010480 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +000010481 assert (Node->getValueType(0) == MVT::i64 &&
10482 "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000010483
10484 SDValue Chain = Node->getOperand(0);
10485 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010486 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010487 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000010488 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010489 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000010490 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000010491 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000010492 SDValue Result =
10493 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
10494 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000010495 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000010496 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010497 Results.push_back(Result.getValue(2));
10498}
10499
Duncan Sands126d9072008-07-04 11:47:58 +000010500/// ReplaceNodeResults - Replace a node with an illegal result type
10501/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000010502void X86TargetLowering::ReplaceNodeResults(SDNode *N,
10503 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000010504 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010505 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +000010506 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000010507 default:
Duncan Sands1607f052008-12-01 11:39:25 +000010508 assert(false && "Do not know how to custom type legalize this operation!");
10509 return;
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010510 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000010511 case ISD::ADDC:
10512 case ISD::ADDE:
10513 case ISD::SUBC:
10514 case ISD::SUBE:
10515 // We don't want to expand or promote these.
10516 return;
Duncan Sands1607f052008-12-01 11:39:25 +000010517 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +000010518 std::pair<SDValue,SDValue> Vals =
10519 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +000010520 SDValue FIST = Vals.first, StackSlot = Vals.second;
10521 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000010522 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000010523 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +000010524 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
10525 MachinePointerInfo(), false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +000010526 }
10527 return;
10528 }
10529 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010530 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000010531 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010532 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010533 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000010534 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000010535 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010536 eax.getValue(2));
10537 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
10538 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +000010539 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010540 Results.push_back(edx.getValue(1));
10541 return;
10542 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010543 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000010544 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000010545 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000010546 bool Regs64bit = T == MVT::i128;
10547 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000010548 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000010549 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
10550 DAG.getConstant(0, HalfT));
10551 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
10552 DAG.getConstant(1, HalfT));
10553 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
10554 Regs64bit ? X86::RAX : X86::EAX,
10555 cpInL, SDValue());
10556 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
10557 Regs64bit ? X86::RDX : X86::EDX,
10558 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000010559 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000010560 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
10561 DAG.getConstant(0, HalfT));
10562 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
10563 DAG.getConstant(1, HalfT));
10564 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
10565 Regs64bit ? X86::RBX : X86::EBX,
10566 swapInL, cpInH.getValue(1));
10567 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
10568 Regs64bit ? X86::RCX : X86::ECX,
10569 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000010570 SDValue Ops[] = { swapInH.getValue(0),
10571 N->getOperand(1),
10572 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010573 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000010574 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000010575 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
10576 X86ISD::LCMPXCHG8_DAG;
10577 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000010578 Ops, 3, T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000010579 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
10580 Regs64bit ? X86::RAX : X86::EAX,
10581 HalfT, Result.getValue(1));
10582 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
10583 Regs64bit ? X86::RDX : X86::EDX,
10584 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000010585 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000010586 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010587 Results.push_back(cpOutH.getValue(1));
10588 return;
10589 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010590 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +000010591 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
10592 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010593 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +000010594 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
10595 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010596 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +000010597 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
10598 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010599 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +000010600 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
10601 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010602 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +000010603 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
10604 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010605 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +000010606 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
10607 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010608 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +000010609 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
10610 return;
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010611 case ISD::ATOMIC_LOAD:
10612 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000010613 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000010614}
10615
Evan Cheng72261582005-12-20 06:22:03 +000010616const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
10617 switch (Opcode) {
10618 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000010619 case X86ISD::BSF: return "X86ISD::BSF";
10620 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000010621 case X86ISD::SHLD: return "X86ISD::SHLD";
10622 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000010623 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000010624 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000010625 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000010626 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000010627 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000010628 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000010629 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
10630 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
10631 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000010632 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000010633 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000010634 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000010635 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000010636 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000010637 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000010638 case X86ISD::COMI: return "X86ISD::COMI";
10639 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000010640 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000010641 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000010642 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
10643 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000010644 case X86ISD::CMOV: return "X86ISD::CMOV";
10645 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000010646 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000010647 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
10648 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000010649 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000010650 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000010651 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000010652 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000010653 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000010654 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
10655 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000010656 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000010657 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000010658 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Nate Begemanb65c1752010-12-17 22:55:37 +000010659 case X86ISD::PSIGNB: return "X86ISD::PSIGNB";
10660 case X86ISD::PSIGNW: return "X86ISD::PSIGNW";
10661 case X86ISD::PSIGND: return "X86ISD::PSIGND";
Evan Cheng8ca29322006-11-10 21:43:37 +000010662 case X86ISD::FMAX: return "X86ISD::FMAX";
10663 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +000010664 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
10665 case X86ISD::FRCP: return "X86ISD::FRCP";
Duncan Sands17470be2011-09-22 20:15:48 +000010666 case X86ISD::FHADD: return "X86ISD::FHADD";
10667 case X86ISD::FHSUB: return "X86ISD::FHSUB";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000010668 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000010669 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010670 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000010671 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010672 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +000010673 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
10674 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010675 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
10676 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
10677 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
10678 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
10679 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
10680 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000010681 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
10682 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +000010683 case X86ISD::VSHL: return "X86ISD::VSHL";
10684 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +000010685 case X86ISD::CMPPD: return "X86ISD::CMPPD";
10686 case X86ISD::CMPPS: return "X86ISD::CMPPS";
10687 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
10688 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
10689 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
10690 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
10691 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
10692 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
10693 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
10694 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010695 case X86ISD::ADD: return "X86ISD::ADD";
10696 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000010697 case X86ISD::ADC: return "X86ISD::ADC";
10698 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000010699 case X86ISD::SMUL: return "X86ISD::SMUL";
10700 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000010701 case X86ISD::INC: return "X86ISD::INC";
10702 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000010703 case X86ISD::OR: return "X86ISD::OR";
10704 case X86ISD::XOR: return "X86ISD::XOR";
10705 case X86ISD::AND: return "X86ISD::AND";
Craig Topper54a11172011-10-14 07:06:56 +000010706 case X86ISD::ANDN: return "X86ISD::ANDN";
Evan Cheng73f24c92009-03-30 21:36:47 +000010707 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000010708 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010709 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010710 case X86ISD::PALIGN: return "X86ISD::PALIGN";
10711 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
10712 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
10713 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
10714 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
10715 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
10716 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
10717 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
10718 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010719 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000010720 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010721 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000010722 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
10723 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010724 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
10725 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
10726 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
10727 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
10728 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
10729 case X86ISD::MOVSD: return "X86ISD::MOVSD";
10730 case X86ISD::MOVSS: return "X86ISD::MOVSS";
10731 case X86ISD::UNPCKLPS: return "X86ISD::UNPCKLPS";
10732 case X86ISD::UNPCKLPD: return "X86ISD::UNPCKLPD";
David Greenefbf05d32011-02-22 23:31:46 +000010733 case X86ISD::VUNPCKLPDY: return "X86ISD::VUNPCKLPDY";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010734 case X86ISD::UNPCKHPS: return "X86ISD::UNPCKHPS";
10735 case X86ISD::UNPCKHPD: return "X86ISD::UNPCKHPD";
10736 case X86ISD::PUNPCKLBW: return "X86ISD::PUNPCKLBW";
10737 case X86ISD::PUNPCKLWD: return "X86ISD::PUNPCKLWD";
10738 case X86ISD::PUNPCKLDQ: return "X86ISD::PUNPCKLDQ";
10739 case X86ISD::PUNPCKLQDQ: return "X86ISD::PUNPCKLQDQ";
10740 case X86ISD::PUNPCKHBW: return "X86ISD::PUNPCKHBW";
10741 case X86ISD::PUNPCKHWD: return "X86ISD::PUNPCKHWD";
10742 case X86ISD::PUNPCKHDQ: return "X86ISD::PUNPCKHDQ";
10743 case X86ISD::PUNPCKHQDQ: return "X86ISD::PUNPCKHQDQ";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000010744 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +000010745 case X86ISD::VPERMILPS: return "X86ISD::VPERMILPS";
10746 case X86ISD::VPERMILPSY: return "X86ISD::VPERMILPSY";
10747 case X86ISD::VPERMILPD: return "X86ISD::VPERMILPD";
10748 case X86ISD::VPERMILPDY: return "X86ISD::VPERMILPDY";
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +000010749 case X86ISD::VPERM2F128: return "X86ISD::VPERM2F128";
Dan Gohmand6708ea2009-08-15 01:38:56 +000010750 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000010751 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010752 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000010753 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000010754 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +000010755 }
10756}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010757
Chris Lattnerc9addb72007-03-30 23:15:24 +000010758// isLegalAddressingMode - Return true if the addressing mode represented
10759// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000010760bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010761 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000010762 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010763 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000010764 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000010765
Chris Lattnerc9addb72007-03-30 23:15:24 +000010766 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010767 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000010768 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000010769
Chris Lattnerc9addb72007-03-30 23:15:24 +000010770 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000010771 unsigned GVFlags =
10772 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010773
Chris Lattnerdfed4132009-07-10 07:38:24 +000010774 // If a reference to this global requires an extra load, we can't fold it.
10775 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000010776 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010777
Chris Lattnerdfed4132009-07-10 07:38:24 +000010778 // If BaseGV requires a register for the PIC base, we cannot also have a
10779 // BaseReg specified.
10780 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000010781 return false;
Evan Cheng52787842007-08-01 23:46:47 +000010782
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010783 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000010784 if ((M != CodeModel::Small || R != Reloc::Static) &&
10785 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010786 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000010787 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010788
Chris Lattnerc9addb72007-03-30 23:15:24 +000010789 switch (AM.Scale) {
10790 case 0:
10791 case 1:
10792 case 2:
10793 case 4:
10794 case 8:
10795 // These scales always work.
10796 break;
10797 case 3:
10798 case 5:
10799 case 9:
10800 // These scales are formed with basereg+scalereg. Only accept if there is
10801 // no basereg yet.
10802 if (AM.HasBaseReg)
10803 return false;
10804 break;
10805 default: // Other stuff never works.
10806 return false;
10807 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010808
Chris Lattnerc9addb72007-03-30 23:15:24 +000010809 return true;
10810}
10811
10812
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010813bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000010814 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000010815 return false;
Evan Chenge127a732007-10-29 07:57:50 +000010816 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
10817 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000010818 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +000010819 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000010820 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +000010821}
10822
Owen Andersone50ed302009-08-10 22:56:29 +000010823bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000010824 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000010825 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000010826 unsigned NumBits1 = VT1.getSizeInBits();
10827 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000010828 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +000010829 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000010830 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000010831}
Evan Cheng2bd122c2007-10-26 01:56:11 +000010832
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010833bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000010834 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000010835 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000010836}
10837
Owen Andersone50ed302009-08-10 22:56:29 +000010838bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000010839 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000010840 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000010841}
10842
Owen Andersone50ed302009-08-10 22:56:29 +000010843bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000010844 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000010845 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000010846}
10847
Evan Cheng60c07e12006-07-05 22:17:51 +000010848/// isShuffleMaskLegal - Targets can use this to indicate that they only
10849/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
10850/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
10851/// are assumed to be legal.
10852bool
Eric Christopherfd179292009-08-27 18:07:15 +000010853X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000010854 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000010855 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000010856 if (VT.getSizeInBits() == 64)
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010857 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3() || Subtarget->hasAVX());
Nate Begeman9008ca62009-04-27 18:41:29 +000010858
Nate Begemana09008b2009-10-19 02:17:23 +000010859 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000010860 return (VT.getVectorNumElements() == 2 ||
10861 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
10862 isMOVLMask(M, VT) ||
10863 isSHUFPMask(M, VT) ||
10864 isPSHUFDMask(M, VT) ||
10865 isPSHUFHWMask(M, VT) ||
10866 isPSHUFLWMask(M, VT) ||
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010867 isPALIGNRMask(M, VT, Subtarget->hasSSSE3() || Subtarget->hasAVX()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000010868 isUNPCKLMask(M, VT) ||
10869 isUNPCKHMask(M, VT) ||
10870 isUNPCKL_v_undef_Mask(M, VT) ||
10871 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +000010872}
10873
Dan Gohman7d8143f2008-04-09 20:09:42 +000010874bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000010875X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000010876 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000010877 unsigned NumElts = VT.getVectorNumElements();
10878 // FIXME: This collection of masks seems suspect.
10879 if (NumElts == 2)
10880 return true;
10881 if (NumElts == 4 && VT.getSizeInBits() == 128) {
10882 return (isMOVLMask(Mask, VT) ||
10883 isCommutedMOVLMask(Mask, VT, true) ||
10884 isSHUFPMask(Mask, VT) ||
10885 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +000010886 }
10887 return false;
10888}
10889
10890//===----------------------------------------------------------------------===//
10891// X86 Scheduler Hooks
10892//===----------------------------------------------------------------------===//
10893
Mon P Wang63307c32008-05-05 19:05:59 +000010894// private utility function
10895MachineBasicBlock *
10896X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
10897 MachineBasicBlock *MBB,
10898 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010899 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010900 unsigned LoadOpc,
10901 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010902 unsigned notOpc,
10903 unsigned EAXreg,
10904 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000010905 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000010906 // For the atomic bitwise operator, we generate
10907 // thisMBB:
10908 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000010909 // ld t1 = [bitinstr.addr]
10910 // op t2 = t1, [bitinstr.val]
10911 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000010912 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
10913 // bz newMBB
10914 // fallthrough -->nextMBB
10915 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10916 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000010917 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000010918 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000010919
Mon P Wang63307c32008-05-05 19:05:59 +000010920 /// First build the CFG
10921 MachineFunction *F = MBB->getParent();
10922 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000010923 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
10924 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
10925 F->insert(MBBIter, newMBB);
10926 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010927
Dan Gohman14152b42010-07-06 20:24:04 +000010928 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
10929 nextMBB->splice(nextMBB->begin(), thisMBB,
10930 llvm::next(MachineBasicBlock::iterator(bInstr)),
10931 thisMBB->end());
10932 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010933
Mon P Wang63307c32008-05-05 19:05:59 +000010934 // Update thisMBB to fall through to newMBB
10935 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010936
Mon P Wang63307c32008-05-05 19:05:59 +000010937 // newMBB jumps to itself and fall through to nextMBB
10938 newMBB->addSuccessor(nextMBB);
10939 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010940
Mon P Wang63307c32008-05-05 19:05:59 +000010941 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010942 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000010943 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +000010944 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000010945 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010946 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000010947 int numArgs = bInstr->getNumOperands() - 1;
10948 for (int i=0; i < numArgs; ++i)
10949 argOpers[i] = &bInstr->getOperand(i+1);
10950
10951 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010952 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000010953 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000010954
Dale Johannesen140be2d2008-08-19 18:47:28 +000010955 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010956 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000010957 for (int i=0; i <= lastAddrIndx; ++i)
10958 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010959
Dale Johannesen140be2d2008-08-19 18:47:28 +000010960 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010961 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010962 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010963 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010964 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010965 tt = t1;
10966
Dale Johannesen140be2d2008-08-19 18:47:28 +000010967 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +000010968 assert((argOpers[valArgIndx]->isReg() ||
10969 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000010970 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +000010971 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000010972 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000010973 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000010974 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010975 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +000010976 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010977
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010978 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +000010979 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +000010980
Dale Johannesene4d209d2009-02-03 20:21:25 +000010981 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +000010982 for (int i=0; i <= lastAddrIndx; ++i)
10983 (*MIB).addOperand(*argOpers[i]);
10984 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +000010985 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000010986 (*MIB).setMemRefs(bInstr->memoperands_begin(),
10987 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +000010988
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010989 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +000010990 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +000010991
Mon P Wang63307c32008-05-05 19:05:59 +000010992 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000010993 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000010994
Dan Gohman14152b42010-07-06 20:24:04 +000010995 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000010996 return nextMBB;
10997}
10998
Dale Johannesen1b54c7f2008-10-03 19:41:08 +000010999// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +000011000MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011001X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
11002 MachineBasicBlock *MBB,
11003 unsigned regOpcL,
11004 unsigned regOpcH,
11005 unsigned immOpcL,
11006 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011007 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011008 // For the atomic bitwise operator, we generate
11009 // thisMBB (instructions are in pairs, except cmpxchg8b)
11010 // ld t1,t2 = [bitinstr.addr]
11011 // newMBB:
11012 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
11013 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +000011014 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011015 // mov ECX, EBX <- t5, t6
11016 // mov EAX, EDX <- t1, t2
11017 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
11018 // mov t3, t4 <- EAX, EDX
11019 // bz newMBB
11020 // result in out1, out2
11021 // fallthrough -->nextMBB
11022
11023 const TargetRegisterClass *RC = X86::GR32RegisterClass;
11024 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011025 const unsigned NotOpc = X86::NOT32r;
11026 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11027 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11028 MachineFunction::iterator MBBIter = MBB;
11029 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011030
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011031 /// First build the CFG
11032 MachineFunction *F = MBB->getParent();
11033 MachineBasicBlock *thisMBB = MBB;
11034 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11035 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11036 F->insert(MBBIter, newMBB);
11037 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011038
Dan Gohman14152b42010-07-06 20:24:04 +000011039 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11040 nextMBB->splice(nextMBB->begin(), thisMBB,
11041 llvm::next(MachineBasicBlock::iterator(bInstr)),
11042 thisMBB->end());
11043 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011044
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011045 // Update thisMBB to fall through to newMBB
11046 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011047
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011048 // newMBB jumps to itself and fall through to nextMBB
11049 newMBB->addSuccessor(nextMBB);
11050 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011051
Dale Johannesene4d209d2009-02-03 20:21:25 +000011052 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011053 // Insert instructions into newMBB based on incoming instruction
11054 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011055 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011056 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011057 MachineOperand& dest1Oper = bInstr->getOperand(0);
11058 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011059 MachineOperand* argOpers[2 + X86::AddrNumOperands];
11060 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011061 argOpers[i] = &bInstr->getOperand(i+2);
11062
Dan Gohman71ea4e52010-05-14 21:01:44 +000011063 // We use some of the operands multiple times, so conservatively just
11064 // clear any kill flags that might be present.
11065 if (argOpers[i]->isReg() && argOpers[i]->isUse())
11066 argOpers[i]->setIsKill(false);
11067 }
11068
Evan Chengad5b52f2010-01-08 19:14:57 +000011069 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011070 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +000011071
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011072 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011073 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011074 for (int i=0; i <= lastAddrIndx; ++i)
11075 (*MIB).addOperand(*argOpers[i]);
11076 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011077 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +000011078 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +000011079 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011080 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +000011081 MachineOperand newOp3 = *(argOpers[3]);
11082 if (newOp3.isImm())
11083 newOp3.setImm(newOp3.getImm()+4);
11084 else
11085 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011086 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +000011087 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011088
11089 // t3/4 are defined later, at the bottom of the loop
11090 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
11091 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011092 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011093 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011094 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011095 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
11096
Evan Cheng306b4ca2010-01-08 23:41:50 +000011097 // The subsequent operations should be using the destination registers of
11098 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +000011099 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +000011100 t1 = F->getRegInfo().createVirtualRegister(RC);
11101 t2 = F->getRegInfo().createVirtualRegister(RC);
11102 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
11103 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011104 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +000011105 t1 = dest1Oper.getReg();
11106 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011107 }
11108
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011109 int valArgIndx = lastAddrIndx + 1;
11110 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +000011111 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011112 "invalid operand");
11113 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
11114 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011115 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011116 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011117 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011118 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +000011119 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011120 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011121 (*MIB).addOperand(*argOpers[valArgIndx]);
11122 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011123 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011124 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011125 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011126 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011127 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011128 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011129 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +000011130 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011131 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011132 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011133
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011134 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011135 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011136 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011137 MIB.addReg(t2);
11138
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011139 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011140 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011141 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011142 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +000011143
Dale Johannesene4d209d2009-02-03 20:21:25 +000011144 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011145 for (int i=0; i <= lastAddrIndx; ++i)
11146 (*MIB).addOperand(*argOpers[i]);
11147
11148 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011149 (*MIB).setMemRefs(bInstr->memoperands_begin(),
11150 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011151
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011152 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011153 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011154 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011155 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000011156
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011157 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011158 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011159
Dan Gohman14152b42010-07-06 20:24:04 +000011160 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011161 return nextMBB;
11162}
11163
11164// private utility function
11165MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +000011166X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
11167 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011168 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000011169 // For the atomic min/max operator, we generate
11170 // thisMBB:
11171 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000011172 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +000011173 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +000011174 // cmp t1, t2
11175 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +000011176 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000011177 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
11178 // bz newMBB
11179 // fallthrough -->nextMBB
11180 //
11181 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11182 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011183 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000011184 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011185
Mon P Wang63307c32008-05-05 19:05:59 +000011186 /// First build the CFG
11187 MachineFunction *F = MBB->getParent();
11188 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011189 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11190 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11191 F->insert(MBBIter, newMBB);
11192 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011193
Dan Gohman14152b42010-07-06 20:24:04 +000011194 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11195 nextMBB->splice(nextMBB->begin(), thisMBB,
11196 llvm::next(MachineBasicBlock::iterator(mInstr)),
11197 thisMBB->end());
11198 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011199
Mon P Wang63307c32008-05-05 19:05:59 +000011200 // Update thisMBB to fall through to newMBB
11201 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011202
Mon P Wang63307c32008-05-05 19:05:59 +000011203 // newMBB jumps to newMBB and fall through to nextMBB
11204 newMBB->addSuccessor(nextMBB);
11205 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011206
Dale Johannesene4d209d2009-02-03 20:21:25 +000011207 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000011208 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011209 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011210 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +000011211 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011212 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000011213 int numArgs = mInstr->getNumOperands() - 1;
11214 for (int i=0; i < numArgs; ++i)
11215 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +000011216
Mon P Wang63307c32008-05-05 19:05:59 +000011217 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011218 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011219 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000011220
Mon P Wangab3e7472008-05-05 22:56:23 +000011221 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011222 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011223 for (int i=0; i <= lastAddrIndx; ++i)
11224 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +000011225
Mon P Wang63307c32008-05-05 19:05:59 +000011226 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +000011227 assert((argOpers[valArgIndx]->isReg() ||
11228 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000011229 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +000011230
11231 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +000011232 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011233 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +000011234 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011235 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000011236 (*MIB).addOperand(*argOpers[valArgIndx]);
11237
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011238 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +000011239 MIB.addReg(t1);
11240
Dale Johannesene4d209d2009-02-03 20:21:25 +000011241 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +000011242 MIB.addReg(t1);
11243 MIB.addReg(t2);
11244
11245 // Generate movc
11246 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011247 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +000011248 MIB.addReg(t2);
11249 MIB.addReg(t1);
11250
11251 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +000011252 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +000011253 for (int i=0; i <= lastAddrIndx; ++i)
11254 (*MIB).addOperand(*argOpers[i]);
11255 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +000011256 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011257 (*MIB).setMemRefs(mInstr->memoperands_begin(),
11258 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +000011259
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011260 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +000011261 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +000011262
Mon P Wang63307c32008-05-05 19:05:59 +000011263 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011264 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000011265
Dan Gohman14152b42010-07-06 20:24:04 +000011266 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000011267 return nextMBB;
11268}
11269
Eric Christopherf83a5de2009-08-27 18:08:16 +000011270// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011271// or XMM0_V32I8 in AVX all of this code can be replaced with that
11272// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +000011273MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +000011274X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +000011275 unsigned numArgs, bool memArg) const {
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011276 assert((Subtarget->hasSSE42() || Subtarget->hasAVX()) &&
11277 "Target must have SSE4.2 or AVX features enabled");
11278
Eric Christopherb120ab42009-08-18 22:50:32 +000011279 DebugLoc dl = MI->getDebugLoc();
11280 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +000011281 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011282 if (!Subtarget->hasAVX()) {
11283 if (memArg)
11284 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
11285 else
11286 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
11287 } else {
11288 if (memArg)
11289 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
11290 else
11291 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
11292 }
Eric Christopherb120ab42009-08-18 22:50:32 +000011293
Eric Christopher41c902f2010-11-30 08:20:21 +000011294 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +000011295 for (unsigned i = 0; i < numArgs; ++i) {
11296 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +000011297 if (!(Op.isReg() && Op.isImplicit()))
11298 MIB.addOperand(Op);
11299 }
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011300 BuildMI(*BB, MI, dl,
11301 TII->get(Subtarget->hasAVX() ? X86::VMOVAPSrr : X86::MOVAPSrr),
11302 MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000011303 .addReg(X86::XMM0);
11304
Dan Gohman14152b42010-07-06 20:24:04 +000011305 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000011306 return BB;
11307}
11308
11309MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +000011310X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000011311 DebugLoc dl = MI->getDebugLoc();
11312 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011313
Eric Christopher228232b2010-11-30 07:20:12 +000011314 // Address into RAX/EAX, other two args into ECX, EDX.
11315 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
11316 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
11317 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
11318 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000011319 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011320
Eric Christopher228232b2010-11-30 07:20:12 +000011321 unsigned ValOps = X86::AddrNumOperands;
11322 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
11323 .addReg(MI->getOperand(ValOps).getReg());
11324 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
11325 .addReg(MI->getOperand(ValOps+1).getReg());
11326
11327 // The instruction doesn't actually take any operands though.
11328 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011329
Eric Christopher228232b2010-11-30 07:20:12 +000011330 MI->eraseFromParent(); // The pseudo is gone now.
11331 return BB;
11332}
11333
11334MachineBasicBlock *
11335X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000011336 DebugLoc dl = MI->getDebugLoc();
11337 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011338
Eric Christopher228232b2010-11-30 07:20:12 +000011339 // First arg in ECX, the second in EAX.
11340 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
11341 .addReg(MI->getOperand(0).getReg());
11342 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
11343 .addReg(MI->getOperand(1).getReg());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011344
Eric Christopher228232b2010-11-30 07:20:12 +000011345 // The instruction doesn't actually take any operands though.
11346 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011347
Eric Christopher228232b2010-11-30 07:20:12 +000011348 MI->eraseFromParent(); // The pseudo is gone now.
11349 return BB;
11350}
11351
11352MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000011353X86TargetLowering::EmitVAARG64WithCustomInserter(
11354 MachineInstr *MI,
11355 MachineBasicBlock *MBB) const {
11356 // Emit va_arg instruction on X86-64.
11357
11358 // Operands to this pseudo-instruction:
11359 // 0 ) Output : destination address (reg)
11360 // 1-5) Input : va_list address (addr, i64mem)
11361 // 6 ) ArgSize : Size (in bytes) of vararg type
11362 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
11363 // 8 ) Align : Alignment of type
11364 // 9 ) EFLAGS (implicit-def)
11365
11366 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
11367 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
11368
11369 unsigned DestReg = MI->getOperand(0).getReg();
11370 MachineOperand &Base = MI->getOperand(1);
11371 MachineOperand &Scale = MI->getOperand(2);
11372 MachineOperand &Index = MI->getOperand(3);
11373 MachineOperand &Disp = MI->getOperand(4);
11374 MachineOperand &Segment = MI->getOperand(5);
11375 unsigned ArgSize = MI->getOperand(6).getImm();
11376 unsigned ArgMode = MI->getOperand(7).getImm();
11377 unsigned Align = MI->getOperand(8).getImm();
11378
11379 // Memory Reference
11380 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
11381 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
11382 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
11383
11384 // Machine Information
11385 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11386 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
11387 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
11388 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
11389 DebugLoc DL = MI->getDebugLoc();
11390
11391 // struct va_list {
11392 // i32 gp_offset
11393 // i32 fp_offset
11394 // i64 overflow_area (address)
11395 // i64 reg_save_area (address)
11396 // }
11397 // sizeof(va_list) = 24
11398 // alignment(va_list) = 8
11399
11400 unsigned TotalNumIntRegs = 6;
11401 unsigned TotalNumXMMRegs = 8;
11402 bool UseGPOffset = (ArgMode == 1);
11403 bool UseFPOffset = (ArgMode == 2);
11404 unsigned MaxOffset = TotalNumIntRegs * 8 +
11405 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
11406
11407 /* Align ArgSize to a multiple of 8 */
11408 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
11409 bool NeedsAlign = (Align > 8);
11410
11411 MachineBasicBlock *thisMBB = MBB;
11412 MachineBasicBlock *overflowMBB;
11413 MachineBasicBlock *offsetMBB;
11414 MachineBasicBlock *endMBB;
11415
11416 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
11417 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
11418 unsigned OffsetReg = 0;
11419
11420 if (!UseGPOffset && !UseFPOffset) {
11421 // If we only pull from the overflow region, we don't create a branch.
11422 // We don't need to alter control flow.
11423 OffsetDestReg = 0; // unused
11424 OverflowDestReg = DestReg;
11425
11426 offsetMBB = NULL;
11427 overflowMBB = thisMBB;
11428 endMBB = thisMBB;
11429 } else {
11430 // First emit code to check if gp_offset (or fp_offset) is below the bound.
11431 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
11432 // If not, pull from overflow_area. (branch to overflowMBB)
11433 //
11434 // thisMBB
11435 // | .
11436 // | .
11437 // offsetMBB overflowMBB
11438 // | .
11439 // | .
11440 // endMBB
11441
11442 // Registers for the PHI in endMBB
11443 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
11444 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
11445
11446 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11447 MachineFunction *MF = MBB->getParent();
11448 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11449 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11450 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11451
11452 MachineFunction::iterator MBBIter = MBB;
11453 ++MBBIter;
11454
11455 // Insert the new basic blocks
11456 MF->insert(MBBIter, offsetMBB);
11457 MF->insert(MBBIter, overflowMBB);
11458 MF->insert(MBBIter, endMBB);
11459
11460 // Transfer the remainder of MBB and its successor edges to endMBB.
11461 endMBB->splice(endMBB->begin(), thisMBB,
11462 llvm::next(MachineBasicBlock::iterator(MI)),
11463 thisMBB->end());
11464 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
11465
11466 // Make offsetMBB and overflowMBB successors of thisMBB
11467 thisMBB->addSuccessor(offsetMBB);
11468 thisMBB->addSuccessor(overflowMBB);
11469
11470 // endMBB is a successor of both offsetMBB and overflowMBB
11471 offsetMBB->addSuccessor(endMBB);
11472 overflowMBB->addSuccessor(endMBB);
11473
11474 // Load the offset value into a register
11475 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
11476 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
11477 .addOperand(Base)
11478 .addOperand(Scale)
11479 .addOperand(Index)
11480 .addDisp(Disp, UseFPOffset ? 4 : 0)
11481 .addOperand(Segment)
11482 .setMemRefs(MMOBegin, MMOEnd);
11483
11484 // Check if there is enough room left to pull this argument.
11485 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
11486 .addReg(OffsetReg)
11487 .addImm(MaxOffset + 8 - ArgSizeA8);
11488
11489 // Branch to "overflowMBB" if offset >= max
11490 // Fall through to "offsetMBB" otherwise
11491 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
11492 .addMBB(overflowMBB);
11493 }
11494
11495 // In offsetMBB, emit code to use the reg_save_area.
11496 if (offsetMBB) {
11497 assert(OffsetReg != 0);
11498
11499 // Read the reg_save_area address.
11500 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
11501 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
11502 .addOperand(Base)
11503 .addOperand(Scale)
11504 .addOperand(Index)
11505 .addDisp(Disp, 16)
11506 .addOperand(Segment)
11507 .setMemRefs(MMOBegin, MMOEnd);
11508
11509 // Zero-extend the offset
11510 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
11511 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
11512 .addImm(0)
11513 .addReg(OffsetReg)
11514 .addImm(X86::sub_32bit);
11515
11516 // Add the offset to the reg_save_area to get the final address.
11517 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
11518 .addReg(OffsetReg64)
11519 .addReg(RegSaveReg);
11520
11521 // Compute the offset for the next argument
11522 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
11523 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
11524 .addReg(OffsetReg)
11525 .addImm(UseFPOffset ? 16 : 8);
11526
11527 // Store it back into the va_list.
11528 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
11529 .addOperand(Base)
11530 .addOperand(Scale)
11531 .addOperand(Index)
11532 .addDisp(Disp, UseFPOffset ? 4 : 0)
11533 .addOperand(Segment)
11534 .addReg(NextOffsetReg)
11535 .setMemRefs(MMOBegin, MMOEnd);
11536
11537 // Jump to endMBB
11538 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
11539 .addMBB(endMBB);
11540 }
11541
11542 //
11543 // Emit code to use overflow area
11544 //
11545
11546 // Load the overflow_area address into a register.
11547 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
11548 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
11549 .addOperand(Base)
11550 .addOperand(Scale)
11551 .addOperand(Index)
11552 .addDisp(Disp, 8)
11553 .addOperand(Segment)
11554 .setMemRefs(MMOBegin, MMOEnd);
11555
11556 // If we need to align it, do so. Otherwise, just copy the address
11557 // to OverflowDestReg.
11558 if (NeedsAlign) {
11559 // Align the overflow address
11560 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
11561 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
11562
11563 // aligned_addr = (addr + (align-1)) & ~(align-1)
11564 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
11565 .addReg(OverflowAddrReg)
11566 .addImm(Align-1);
11567
11568 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
11569 .addReg(TmpReg)
11570 .addImm(~(uint64_t)(Align-1));
11571 } else {
11572 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
11573 .addReg(OverflowAddrReg);
11574 }
11575
11576 // Compute the next overflow address after this argument.
11577 // (the overflow address should be kept 8-byte aligned)
11578 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
11579 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
11580 .addReg(OverflowDestReg)
11581 .addImm(ArgSizeA8);
11582
11583 // Store the new overflow address.
11584 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
11585 .addOperand(Base)
11586 .addOperand(Scale)
11587 .addOperand(Index)
11588 .addDisp(Disp, 8)
11589 .addOperand(Segment)
11590 .addReg(NextAddrReg)
11591 .setMemRefs(MMOBegin, MMOEnd);
11592
11593 // If we branched, emit the PHI to the front of endMBB.
11594 if (offsetMBB) {
11595 BuildMI(*endMBB, endMBB->begin(), DL,
11596 TII->get(X86::PHI), DestReg)
11597 .addReg(OffsetDestReg).addMBB(offsetMBB)
11598 .addReg(OverflowDestReg).addMBB(overflowMBB);
11599 }
11600
11601 // Erase the pseudo instruction
11602 MI->eraseFromParent();
11603
11604 return endMBB;
11605}
11606
11607MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000011608X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
11609 MachineInstr *MI,
11610 MachineBasicBlock *MBB) const {
11611 // Emit code to save XMM registers to the stack. The ABI says that the
11612 // number of registers to save is given in %al, so it's theoretically
11613 // possible to do an indirect jump trick to avoid saving all of them,
11614 // however this code takes a simpler approach and just executes all
11615 // of the stores if %al is non-zero. It's less code, and it's probably
11616 // easier on the hardware branch predictor, and stores aren't all that
11617 // expensive anyway.
11618
11619 // Create the new basic blocks. One block contains all the XMM stores,
11620 // and one block is the final destination regardless of whether any
11621 // stores were performed.
11622 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11623 MachineFunction *F = MBB->getParent();
11624 MachineFunction::iterator MBBIter = MBB;
11625 ++MBBIter;
11626 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
11627 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
11628 F->insert(MBBIter, XMMSaveMBB);
11629 F->insert(MBBIter, EndMBB);
11630
Dan Gohman14152b42010-07-06 20:24:04 +000011631 // Transfer the remainder of MBB and its successor edges to EndMBB.
11632 EndMBB->splice(EndMBB->begin(), MBB,
11633 llvm::next(MachineBasicBlock::iterator(MI)),
11634 MBB->end());
11635 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
11636
Dan Gohmand6708ea2009-08-15 01:38:56 +000011637 // The original block will now fall through to the XMM save block.
11638 MBB->addSuccessor(XMMSaveMBB);
11639 // The XMMSaveMBB will fall through to the end block.
11640 XMMSaveMBB->addSuccessor(EndMBB);
11641
11642 // Now add the instructions.
11643 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11644 DebugLoc DL = MI->getDebugLoc();
11645
11646 unsigned CountReg = MI->getOperand(0).getReg();
11647 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
11648 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
11649
11650 if (!Subtarget->isTargetWin64()) {
11651 // If %al is 0, branch around the XMM save block.
11652 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011653 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000011654 MBB->addSuccessor(EndMBB);
11655 }
11656
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011657 unsigned MOVOpc = Subtarget->hasAVX() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000011658 // In the XMM save block, save all the XMM argument registers.
11659 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
11660 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000011661 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000011662 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000011663 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000011664 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000011665 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011666 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000011667 .addFrameIndex(RegSaveFrameIndex)
11668 .addImm(/*Scale=*/1)
11669 .addReg(/*IndexReg=*/0)
11670 .addImm(/*Disp=*/Offset)
11671 .addReg(/*Segment=*/0)
11672 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000011673 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000011674 }
11675
Dan Gohman14152b42010-07-06 20:24:04 +000011676 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000011677
11678 return EndMBB;
11679}
Mon P Wang63307c32008-05-05 19:05:59 +000011680
Evan Cheng60c07e12006-07-05 22:17:51 +000011681MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000011682X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000011683 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000011684 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11685 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000011686
Chris Lattner52600972009-09-02 05:57:00 +000011687 // To "insert" a SELECT_CC instruction, we actually have to insert the
11688 // diamond control-flow pattern. The incoming instruction knows the
11689 // destination vreg to set, the condition code register to branch on, the
11690 // true/false values to select between, and a branch opcode to use.
11691 const BasicBlock *LLVM_BB = BB->getBasicBlock();
11692 MachineFunction::iterator It = BB;
11693 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000011694
Chris Lattner52600972009-09-02 05:57:00 +000011695 // thisMBB:
11696 // ...
11697 // TrueVal = ...
11698 // cmpTY ccX, r1, r2
11699 // bCC copy1MBB
11700 // fallthrough --> copy0MBB
11701 MachineBasicBlock *thisMBB = BB;
11702 MachineFunction *F = BB->getParent();
11703 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
11704 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000011705 F->insert(It, copy0MBB);
11706 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000011707
Bill Wendling730c07e2010-06-25 20:48:10 +000011708 // If the EFLAGS register isn't dead in the terminator, then claim that it's
11709 // live into the sink and copy blocks.
Jakob Stoklund Olesen4a1b9d82011-09-02 23:52:49 +000011710 if (!MI->killsRegister(X86::EFLAGS)) {
11711 copy0MBB->addLiveIn(X86::EFLAGS);
11712 sinkMBB->addLiveIn(X86::EFLAGS);
Bill Wendling730c07e2010-06-25 20:48:10 +000011713 }
11714
Dan Gohman14152b42010-07-06 20:24:04 +000011715 // Transfer the remainder of BB and its successor edges to sinkMBB.
11716 sinkMBB->splice(sinkMBB->begin(), BB,
11717 llvm::next(MachineBasicBlock::iterator(MI)),
11718 BB->end());
11719 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
11720
11721 // Add the true and fallthrough blocks as its successors.
11722 BB->addSuccessor(copy0MBB);
11723 BB->addSuccessor(sinkMBB);
11724
11725 // Create the conditional branch instruction.
11726 unsigned Opc =
11727 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
11728 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
11729
Chris Lattner52600972009-09-02 05:57:00 +000011730 // copy0MBB:
11731 // %FalseValue = ...
11732 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000011733 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000011734
Chris Lattner52600972009-09-02 05:57:00 +000011735 // sinkMBB:
11736 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
11737 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000011738 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
11739 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000011740 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
11741 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
11742
Dan Gohman14152b42010-07-06 20:24:04 +000011743 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000011744 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000011745}
11746
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011747MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000011748X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
11749 bool Is64Bit) const {
11750 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11751 DebugLoc DL = MI->getDebugLoc();
11752 MachineFunction *MF = BB->getParent();
11753 const BasicBlock *LLVM_BB = BB->getBasicBlock();
11754
11755 assert(EnableSegmentedStacks);
11756
11757 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
11758 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
11759
11760 // BB:
11761 // ... [Till the alloca]
11762 // If stacklet is not large enough, jump to mallocMBB
11763 //
11764 // bumpMBB:
11765 // Allocate by subtracting from RSP
11766 // Jump to continueMBB
11767 //
11768 // mallocMBB:
11769 // Allocate by call to runtime
11770 //
11771 // continueMBB:
11772 // ...
11773 // [rest of original BB]
11774 //
11775
11776 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11777 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11778 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11779
11780 MachineRegisterInfo &MRI = MF->getRegInfo();
11781 const TargetRegisterClass *AddrRegClass =
11782 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
11783
11784 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
11785 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
11786 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola66bf7432011-10-26 21:16:41 +000011787 SPLimitVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola151ab3e2011-08-30 19:47:04 +000011788 sizeVReg = MI->getOperand(1).getReg(),
11789 physSPReg = Is64Bit ? X86::RSP : X86::ESP;
11790
11791 MachineFunction::iterator MBBIter = BB;
11792 ++MBBIter;
11793
11794 MF->insert(MBBIter, bumpMBB);
11795 MF->insert(MBBIter, mallocMBB);
11796 MF->insert(MBBIter, continueMBB);
11797
11798 continueMBB->splice(continueMBB->begin(), BB, llvm::next
11799 (MachineBasicBlock::iterator(MI)), BB->end());
11800 continueMBB->transferSuccessorsAndUpdatePHIs(BB);
11801
11802 // Add code to the main basic block to check if the stack limit has been hit,
11803 // and if so, jump to mallocMBB otherwise to bumpMBB.
11804 BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
Rafael Espindola66bf7432011-10-26 21:16:41 +000011805 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), SPLimitVReg)
Rafael Espindola151ab3e2011-08-30 19:47:04 +000011806 .addReg(tmpSPVReg).addReg(sizeVReg);
11807 BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
11808 .addReg(0).addImm(0).addReg(0).addImm(TlsOffset).addReg(TlsReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000011809 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000011810 BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
11811
11812 // bumpMBB simply decreases the stack pointer, since we know the current
11813 // stacklet has enough space.
11814 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000011815 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000011816 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000011817 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000011818 BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
11819
11820 // Calls into a routine in libgcc to allocate more space from the heap.
11821 if (Is64Bit) {
11822 BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
11823 .addReg(sizeVReg);
11824 BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
11825 .addExternalSymbol("__morestack_allocate_stack_space").addReg(X86::RDI);
11826 } else {
11827 BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
11828 .addImm(12);
11829 BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
11830 BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
11831 .addExternalSymbol("__morestack_allocate_stack_space");
11832 }
11833
11834 if (!Is64Bit)
11835 BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
11836 .addImm(16);
11837
11838 BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
11839 .addReg(Is64Bit ? X86::RAX : X86::EAX);
11840 BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
11841
11842 // Set up the CFG correctly.
11843 BB->addSuccessor(bumpMBB);
11844 BB->addSuccessor(mallocMBB);
11845 mallocMBB->addSuccessor(continueMBB);
11846 bumpMBB->addSuccessor(continueMBB);
11847
11848 // Take care of the PHI nodes.
11849 BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
11850 MI->getOperand(0).getReg())
11851 .addReg(mallocPtrVReg).addMBB(mallocMBB)
11852 .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
11853
11854 // Delete the original pseudo instruction.
11855 MI->eraseFromParent();
11856
11857 // And we're done.
11858 return continueMBB;
11859}
11860
11861MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000011862X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000011863 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011864 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11865 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011866
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000011867 assert(!Subtarget->isTargetEnvMacho());
11868
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011869 // The lowering is pretty easy: we're just emitting the call to _alloca. The
11870 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011871
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000011872 if (Subtarget->isTargetWin64()) {
11873 if (Subtarget->isTargetCygMing()) {
11874 // ___chkstk(Mingw64):
11875 // Clobbers R10, R11, RAX and EFLAGS.
11876 // Updates RSP.
11877 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
11878 .addExternalSymbol("___chkstk")
11879 .addReg(X86::RAX, RegState::Implicit)
11880 .addReg(X86::RSP, RegState::Implicit)
11881 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
11882 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
11883 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
11884 } else {
11885 // __chkstk(MSVCRT): does not update stack pointer.
11886 // Clobbers R10, R11 and EFLAGS.
11887 // FIXME: RAX(allocated size) might be reused and not killed.
11888 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
11889 .addExternalSymbol("__chkstk")
11890 .addReg(X86::RAX, RegState::Implicit)
11891 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
11892 // RAX has the offset to subtracted from RSP.
11893 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
11894 .addReg(X86::RSP)
11895 .addReg(X86::RAX);
11896 }
11897 } else {
11898 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000011899 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
11900
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000011901 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
11902 .addExternalSymbol(StackProbeSymbol)
11903 .addReg(X86::EAX, RegState::Implicit)
11904 .addReg(X86::ESP, RegState::Implicit)
11905 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
11906 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
11907 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
11908 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011909
Dan Gohman14152b42010-07-06 20:24:04 +000011910 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011911 return BB;
11912}
Chris Lattner52600972009-09-02 05:57:00 +000011913
11914MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000011915X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
11916 MachineBasicBlock *BB) const {
11917 // This is pretty easy. We're taking the value that we received from
11918 // our load from the relocation, sticking it in either RDI (x86-64)
11919 // or EAX and doing an indirect call. The return value will then
11920 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000011921 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000011922 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000011923 DebugLoc DL = MI->getDebugLoc();
11924 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000011925
11926 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000011927 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000011928
Eric Christopher30ef0e52010-06-03 04:07:48 +000011929 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000011930 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
11931 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000011932 .addReg(X86::RIP)
11933 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000011934 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000011935 MI->getOperand(3).getTargetFlags())
11936 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000011937 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000011938 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +000011939 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000011940 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
11941 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000011942 .addReg(0)
11943 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000011944 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000011945 MI->getOperand(3).getTargetFlags())
11946 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000011947 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000011948 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000011949 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000011950 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
11951 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000011952 .addReg(TII->getGlobalBaseReg(F))
11953 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000011954 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000011955 MI->getOperand(3).getTargetFlags())
11956 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000011957 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000011958 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000011959 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000011960
Dan Gohman14152b42010-07-06 20:24:04 +000011961 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000011962 return BB;
11963}
11964
11965MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000011966X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000011967 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000011968 switch (MI->getOpcode()) {
Richard Trieu23946fc2011-09-21 03:09:09 +000011969 default: assert(0 && "Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000011970 case X86::TAILJMPd64:
11971 case X86::TAILJMPr64:
11972 case X86::TAILJMPm64:
Richard Trieu23946fc2011-09-21 03:09:09 +000011973 assert(0 && "TAILJMP64 would not be touched here.");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000011974 case X86::TCRETURNdi64:
11975 case X86::TCRETURNri64:
11976 case X86::TCRETURNmi64:
11977 // Defs of TCRETURNxx64 has Win64's callee-saved registers, as subset.
11978 // On AMD64, additional defs should be added before register allocation.
11979 if (!Subtarget->isTargetWin64()) {
11980 MI->addRegisterDefined(X86::RSI);
11981 MI->addRegisterDefined(X86::RDI);
11982 MI->addRegisterDefined(X86::XMM6);
11983 MI->addRegisterDefined(X86::XMM7);
11984 MI->addRegisterDefined(X86::XMM8);
11985 MI->addRegisterDefined(X86::XMM9);
11986 MI->addRegisterDefined(X86::XMM10);
11987 MI->addRegisterDefined(X86::XMM11);
11988 MI->addRegisterDefined(X86::XMM12);
11989 MI->addRegisterDefined(X86::XMM13);
11990 MI->addRegisterDefined(X86::XMM14);
11991 MI->addRegisterDefined(X86::XMM15);
11992 }
11993 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000011994 case X86::WIN_ALLOCA:
11995 return EmitLoweredWinAlloca(MI, BB);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000011996 case X86::SEG_ALLOCA_32:
11997 return EmitLoweredSegAlloca(MI, BB, false);
11998 case X86::SEG_ALLOCA_64:
11999 return EmitLoweredSegAlloca(MI, BB, true);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012000 case X86::TLSCall_32:
12001 case X86::TLSCall_64:
12002 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000012003 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000012004 case X86::CMOV_FR32:
12005 case X86::CMOV_FR64:
12006 case X86::CMOV_V4F32:
12007 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000012008 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000012009 case X86::CMOV_V8F32:
12010 case X86::CMOV_V4F64:
12011 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000012012 case X86::CMOV_GR16:
12013 case X86::CMOV_GR32:
12014 case X86::CMOV_RFP32:
12015 case X86::CMOV_RFP64:
12016 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012017 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000012018
Dale Johannesen849f2142007-07-03 00:53:03 +000012019 case X86::FP32_TO_INT16_IN_MEM:
12020 case X86::FP32_TO_INT32_IN_MEM:
12021 case X86::FP32_TO_INT64_IN_MEM:
12022 case X86::FP64_TO_INT16_IN_MEM:
12023 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000012024 case X86::FP64_TO_INT64_IN_MEM:
12025 case X86::FP80_TO_INT16_IN_MEM:
12026 case X86::FP80_TO_INT32_IN_MEM:
12027 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000012028 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12029 DebugLoc DL = MI->getDebugLoc();
12030
Evan Cheng60c07e12006-07-05 22:17:51 +000012031 // Change the floating point control register to use "round towards zero"
12032 // mode when truncating to an integer value.
12033 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000012034 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000012035 addFrameReference(BuildMI(*BB, MI, DL,
12036 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012037
12038 // Load the old value of the high byte of the control word...
12039 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000012040 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000012041 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000012042 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012043
12044 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000012045 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012046 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000012047
12048 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000012049 addFrameReference(BuildMI(*BB, MI, DL,
12050 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012051
12052 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000012053 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012054 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000012055
12056 // Get the X86 opcode to use.
12057 unsigned Opc;
12058 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000012059 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000012060 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
12061 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
12062 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
12063 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
12064 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
12065 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000012066 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
12067 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
12068 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000012069 }
12070
12071 X86AddressMode AM;
12072 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000012073 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012074 AM.BaseType = X86AddressMode::RegBase;
12075 AM.Base.Reg = Op.getReg();
12076 } else {
12077 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000012078 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000012079 }
12080 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000012081 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012082 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012083 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000012084 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012085 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012086 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000012087 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012088 AM.GV = Op.getGlobal();
12089 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000012090 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012091 }
Dan Gohman14152b42010-07-06 20:24:04 +000012092 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000012093 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000012094
12095 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000012096 addFrameReference(BuildMI(*BB, MI, DL,
12097 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012098
Dan Gohman14152b42010-07-06 20:24:04 +000012099 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000012100 return BB;
12101 }
Eric Christopherb120ab42009-08-18 22:50:32 +000012102 // String/text processing lowering.
12103 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012104 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000012105 return EmitPCMP(MI, BB, 3, false /* in-mem */);
12106 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012107 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000012108 return EmitPCMP(MI, BB, 3, true /* in-mem */);
12109 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012110 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000012111 return EmitPCMP(MI, BB, 5, false /* in mem */);
12112 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012113 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000012114 return EmitPCMP(MI, BB, 5, true /* in mem */);
12115
Eric Christopher228232b2010-11-30 07:20:12 +000012116 // Thread synchronization.
12117 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012118 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000012119 case X86::MWAIT:
12120 return EmitMwait(MI, BB);
12121
Eric Christopherb120ab42009-08-18 22:50:32 +000012122 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000012123 case X86::ATOMAND32:
12124 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012125 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012126 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012127 X86::NOT32r, X86::EAX,
12128 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000012129 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000012130 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
12131 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012132 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012133 X86::NOT32r, X86::EAX,
12134 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000012135 case X86::ATOMXOR32:
12136 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012137 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012138 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012139 X86::NOT32r, X86::EAX,
12140 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000012141 case X86::ATOMNAND32:
12142 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012143 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012144 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012145 X86::NOT32r, X86::EAX,
12146 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000012147 case X86::ATOMMIN32:
12148 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
12149 case X86::ATOMMAX32:
12150 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
12151 case X86::ATOMUMIN32:
12152 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
12153 case X86::ATOMUMAX32:
12154 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000012155
12156 case X86::ATOMAND16:
12157 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
12158 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012159 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012160 X86::NOT16r, X86::AX,
12161 X86::GR16RegisterClass);
12162 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000012163 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012164 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012165 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012166 X86::NOT16r, X86::AX,
12167 X86::GR16RegisterClass);
12168 case X86::ATOMXOR16:
12169 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
12170 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012171 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012172 X86::NOT16r, X86::AX,
12173 X86::GR16RegisterClass);
12174 case X86::ATOMNAND16:
12175 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
12176 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012177 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012178 X86::NOT16r, X86::AX,
12179 X86::GR16RegisterClass, true);
12180 case X86::ATOMMIN16:
12181 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
12182 case X86::ATOMMAX16:
12183 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
12184 case X86::ATOMUMIN16:
12185 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
12186 case X86::ATOMUMAX16:
12187 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
12188
12189 case X86::ATOMAND8:
12190 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
12191 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012192 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012193 X86::NOT8r, X86::AL,
12194 X86::GR8RegisterClass);
12195 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000012196 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012197 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012198 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012199 X86::NOT8r, X86::AL,
12200 X86::GR8RegisterClass);
12201 case X86::ATOMXOR8:
12202 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
12203 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012204 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012205 X86::NOT8r, X86::AL,
12206 X86::GR8RegisterClass);
12207 case X86::ATOMNAND8:
12208 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
12209 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012210 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012211 X86::NOT8r, X86::AL,
12212 X86::GR8RegisterClass, true);
12213 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012214 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000012215 case X86::ATOMAND64:
12216 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012217 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012218 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012219 X86::NOT64r, X86::RAX,
12220 X86::GR64RegisterClass);
12221 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000012222 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
12223 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012224 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012225 X86::NOT64r, X86::RAX,
12226 X86::GR64RegisterClass);
12227 case X86::ATOMXOR64:
12228 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012229 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012230 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012231 X86::NOT64r, X86::RAX,
12232 X86::GR64RegisterClass);
12233 case X86::ATOMNAND64:
12234 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
12235 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012236 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012237 X86::NOT64r, X86::RAX,
12238 X86::GR64RegisterClass, true);
12239 case X86::ATOMMIN64:
12240 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
12241 case X86::ATOMMAX64:
12242 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
12243 case X86::ATOMUMIN64:
12244 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
12245 case X86::ATOMUMAX64:
12246 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012247
12248 // This group does 64-bit operations on a 32-bit host.
12249 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012250 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012251 X86::AND32rr, X86::AND32rr,
12252 X86::AND32ri, X86::AND32ri,
12253 false);
12254 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012255 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012256 X86::OR32rr, X86::OR32rr,
12257 X86::OR32ri, X86::OR32ri,
12258 false);
12259 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012260 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012261 X86::XOR32rr, X86::XOR32rr,
12262 X86::XOR32ri, X86::XOR32ri,
12263 false);
12264 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012265 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012266 X86::AND32rr, X86::AND32rr,
12267 X86::AND32ri, X86::AND32ri,
12268 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012269 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012270 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012271 X86::ADD32rr, X86::ADC32rr,
12272 X86::ADD32ri, X86::ADC32ri,
12273 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012274 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012275 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012276 X86::SUB32rr, X86::SBB32rr,
12277 X86::SUB32ri, X86::SBB32ri,
12278 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000012279 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012280 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000012281 X86::MOV32rr, X86::MOV32rr,
12282 X86::MOV32ri, X86::MOV32ri,
12283 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012284 case X86::VASTART_SAVE_XMM_REGS:
12285 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000012286
12287 case X86::VAARG_64:
12288 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000012289 }
12290}
12291
12292//===----------------------------------------------------------------------===//
12293// X86 Optimization Hooks
12294//===----------------------------------------------------------------------===//
12295
Dan Gohman475871a2008-07-27 21:46:04 +000012296void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000012297 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000012298 APInt &KnownZero,
12299 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000012300 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000012301 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012302 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000012303 assert((Opc >= ISD::BUILTIN_OP_END ||
12304 Opc == ISD::INTRINSIC_WO_CHAIN ||
12305 Opc == ISD::INTRINSIC_W_CHAIN ||
12306 Opc == ISD::INTRINSIC_VOID) &&
12307 "Should use MaskedValueIsZero if you don't know whether Op"
12308 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012309
Dan Gohmanf4f92f52008-02-13 23:07:24 +000012310 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012311 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000012312 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012313 case X86ISD::ADD:
12314 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000012315 case X86ISD::ADC:
12316 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012317 case X86ISD::SMUL:
12318 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000012319 case X86ISD::INC:
12320 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000012321 case X86ISD::OR:
12322 case X86ISD::XOR:
12323 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012324 // These nodes' second result is a boolean.
12325 if (Op.getResNo() == 0)
12326 break;
12327 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012328 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000012329 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
12330 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000012331 break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000012332 case ISD::INTRINSIC_WO_CHAIN: {
12333 unsigned IntId = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
12334 unsigned NumLoBits = 0;
12335 switch (IntId) {
12336 default: break;
12337 case Intrinsic::x86_sse_movmsk_ps:
12338 case Intrinsic::x86_avx_movmsk_ps_256:
12339 case Intrinsic::x86_sse2_movmsk_pd:
12340 case Intrinsic::x86_avx_movmsk_pd_256:
12341 case Intrinsic::x86_mmx_pmovmskb:
12342 case Intrinsic::x86_sse2_pmovmskb_128: {
12343 // High bits of movmskp{s|d}, pmovmskb are known zero.
12344 switch (IntId) {
12345 case Intrinsic::x86_sse_movmsk_ps: NumLoBits = 4; break;
12346 case Intrinsic::x86_avx_movmsk_ps_256: NumLoBits = 8; break;
12347 case Intrinsic::x86_sse2_movmsk_pd: NumLoBits = 2; break;
12348 case Intrinsic::x86_avx_movmsk_pd_256: NumLoBits = 4; break;
12349 case Intrinsic::x86_mmx_pmovmskb: NumLoBits = 8; break;
12350 case Intrinsic::x86_sse2_pmovmskb_128: NumLoBits = 16; break;
12351 }
12352 KnownZero = APInt::getHighBitsSet(Mask.getBitWidth(),
12353 Mask.getBitWidth() - NumLoBits);
12354 break;
12355 }
12356 }
12357 break;
12358 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012359 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012360}
Chris Lattner259e97c2006-01-31 19:43:35 +000012361
Owen Andersonbc146b02010-09-21 20:42:50 +000012362unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
12363 unsigned Depth) const {
12364 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
12365 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
12366 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000012367
Owen Andersonbc146b02010-09-21 20:42:50 +000012368 // Fallback case.
12369 return 1;
12370}
12371
Evan Cheng206ee9d2006-07-07 08:33:52 +000012372/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000012373/// node is a GlobalAddress + offset.
12374bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000012375 const GlobalValue* &GA,
12376 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000012377 if (N->getOpcode() == X86ISD::Wrapper) {
12378 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000012379 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000012380 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000012381 return true;
12382 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000012383 }
Evan Chengad4196b2008-05-12 19:56:52 +000012384 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000012385}
12386
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012387/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
12388/// same as extracting the high 128-bit part of 256-bit vector and then
12389/// inserting the result into the low part of a new 256-bit vector
12390static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
12391 EVT VT = SVOp->getValueType(0);
12392 int NumElems = VT.getVectorNumElements();
12393
12394 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
12395 for (int i = 0, j = NumElems/2; i < NumElems/2; ++i, ++j)
12396 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
12397 SVOp->getMaskElt(j) >= 0)
12398 return false;
12399
12400 return true;
12401}
12402
12403/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
12404/// same as extracting the low 128-bit part of 256-bit vector and then
12405/// inserting the result into the high part of a new 256-bit vector
12406static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
12407 EVT VT = SVOp->getValueType(0);
12408 int NumElems = VT.getVectorNumElements();
12409
12410 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
12411 for (int i = NumElems/2, j = 0; i < NumElems; ++i, ++j)
12412 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
12413 SVOp->getMaskElt(j) >= 0)
12414 return false;
12415
12416 return true;
12417}
12418
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012419/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
12420static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
12421 TargetLowering::DAGCombinerInfo &DCI) {
12422 DebugLoc dl = N->getDebugLoc();
12423 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
12424 SDValue V1 = SVOp->getOperand(0);
12425 SDValue V2 = SVOp->getOperand(1);
12426 EVT VT = SVOp->getValueType(0);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012427 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012428
12429 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
12430 V2.getOpcode() == ISD::CONCAT_VECTORS) {
12431 //
12432 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000012433 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012434 // V UNDEF BUILD_VECTOR UNDEF
12435 // \ / \ /
12436 // CONCAT_VECTOR CONCAT_VECTOR
12437 // \ /
12438 // \ /
12439 // RESULT: V + zero extended
12440 //
12441 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
12442 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
12443 V1.getOperand(1).getOpcode() != ISD::UNDEF)
12444 return SDValue();
12445
12446 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
12447 return SDValue();
12448
12449 // To match the shuffle mask, the first half of the mask should
12450 // be exactly the first vector, and all the rest a splat with the
12451 // first element of the second one.
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012452 for (int i = 0; i < NumElems/2; ++i)
12453 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
12454 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
12455 return SDValue();
12456
12457 // Emit a zeroed vector and insert the desired subvector on its
12458 // first half.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000012459 SDValue Zeros = getZeroVector(VT, true /* HasXMMInt */, DAG, dl);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012460 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0),
12461 DAG.getConstant(0, MVT::i32), DAG, dl);
12462 return DCI.CombineTo(N, InsV);
12463 }
12464
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012465 //===--------------------------------------------------------------------===//
12466 // Combine some shuffles into subvector extracts and inserts:
12467 //
12468
12469 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
12470 if (isShuffleHigh128VectorInsertLow(SVOp)) {
12471 SDValue V = Extract128BitVector(V1, DAG.getConstant(NumElems/2, MVT::i32),
12472 DAG, dl);
12473 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT),
12474 V, DAG.getConstant(0, MVT::i32), DAG, dl);
12475 return DCI.CombineTo(N, InsV);
12476 }
12477
12478 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
12479 if (isShuffleLow128VectorInsertHigh(SVOp)) {
12480 SDValue V = Extract128BitVector(V1, DAG.getConstant(0, MVT::i32), DAG, dl);
12481 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT),
12482 V, DAG.getConstant(NumElems/2, MVT::i32), DAG, dl);
12483 return DCI.CombineTo(N, InsV);
12484 }
12485
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012486 return SDValue();
12487}
12488
12489/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000012490static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000012491 TargetLowering::DAGCombinerInfo &DCI,
12492 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000012493 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000012494 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000012495
Mon P Wanga0fd0d52010-12-19 23:55:53 +000012496 // Don't create instructions with illegal types after legalize types has run.
12497 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12498 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
12499 return SDValue();
12500
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000012501 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
12502 if (Subtarget->hasAVX() && VT.getSizeInBits() == 256 &&
12503 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012504 return PerformShuffleCombine256(N, DAG, DCI);
12505
12506 // Only handle 128 wide vector from here on.
12507 if (VT.getSizeInBits() != 128)
12508 return SDValue();
12509
12510 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
12511 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
12512 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000012513 SmallVector<SDValue, 16> Elts;
12514 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012515 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000012516
Nate Begemanfdea31a2010-03-24 20:49:50 +000012517 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000012518}
Evan Chengd880b972008-05-09 21:53:03 +000012519
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000012520/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
12521/// generation and convert it from being a bunch of shuffles and extracts
12522/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012523static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
12524 const TargetLowering &TLI) {
12525 SDValue InputVector = N->getOperand(0);
12526
12527 // Only operate on vectors of 4 elements, where the alternative shuffling
12528 // gets to be more expensive.
12529 if (InputVector.getValueType() != MVT::v4i32)
12530 return SDValue();
12531
12532 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
12533 // single use which is a sign-extend or zero-extend, and all elements are
12534 // used.
12535 SmallVector<SDNode *, 4> Uses;
12536 unsigned ExtractedElements = 0;
12537 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
12538 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
12539 if (UI.getUse().getResNo() != InputVector.getResNo())
12540 return SDValue();
12541
12542 SDNode *Extract = *UI;
12543 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
12544 return SDValue();
12545
12546 if (Extract->getValueType(0) != MVT::i32)
12547 return SDValue();
12548 if (!Extract->hasOneUse())
12549 return SDValue();
12550 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
12551 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
12552 return SDValue();
12553 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
12554 return SDValue();
12555
12556 // Record which element was extracted.
12557 ExtractedElements |=
12558 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
12559
12560 Uses.push_back(Extract);
12561 }
12562
12563 // If not all the elements were used, this may not be worthwhile.
12564 if (ExtractedElements != 15)
12565 return SDValue();
12566
12567 // Ok, we've now decided to do the transformation.
12568 DebugLoc dl = InputVector.getDebugLoc();
12569
12570 // Store the value to a temporary stack slot.
12571 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000012572 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
12573 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012574
12575 // Replace each use (extract) with a load of the appropriate element.
12576 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
12577 UE = Uses.end(); UI != UE; ++UI) {
12578 SDNode *Extract = *UI;
12579
Nadav Rotem86694292011-05-17 08:31:57 +000012580 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012581 SDValue Idx = Extract->getOperand(1);
12582 unsigned EltSize =
12583 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
12584 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
12585 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
12586
Nadav Rotem86694292011-05-17 08:31:57 +000012587 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000012588 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012589
12590 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000012591 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000012592 ScalarAddr, MachinePointerInfo(),
12593 false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012594
12595 // Replace the exact with the load.
12596 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
12597 }
12598
12599 // The replacement was made in place; don't return anything.
12600 return SDValue();
12601}
12602
Duncan Sands6bcd2192011-09-17 16:49:39 +000012603/// PerformSELECTCombine - Do target-specific dag combines on SELECT and VSELECT
12604/// nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000012605static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000012606 const X86Subtarget *Subtarget) {
12607 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000012608 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000012609 // Get the LHS/RHS of the select.
12610 SDValue LHS = N->getOperand(1);
12611 SDValue RHS = N->getOperand(2);
Bruno Cardoso Lopes149f29f2011-09-20 22:34:45 +000012612 EVT VT = LHS.getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +000012613
Dan Gohman670e5392009-09-21 18:03:22 +000012614 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000012615 // instructions match the semantics of the common C idiom x<y?x:y but not
12616 // x<=y?x:y, because of how they handle negative zero (which can be
12617 // ignored in unsafe-math mode).
Benjamin Kramer2c2ccbf2011-09-22 03:27:22 +000012618 if (Cond.getOpcode() == ISD::SETCC && VT.isFloatingPoint() &&
12619 VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT) &&
12620 (Subtarget->hasXMMInt() ||
12621 (Subtarget->hasSSE1() && VT.getScalarType() == MVT::f32))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000012622 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012623
Chris Lattner47b4ce82009-03-11 05:48:52 +000012624 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000012625 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000012626 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
12627 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000012628 switch (CC) {
12629 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000012630 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000012631 // Converting this to a min would handle NaNs incorrectly, and swapping
12632 // the operands would cause it to handle comparisons between positive
12633 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000012634 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000012635 if (!UnsafeFPMath &&
12636 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
12637 break;
12638 std::swap(LHS, RHS);
12639 }
Dan Gohman670e5392009-09-21 18:03:22 +000012640 Opcode = X86ISD::FMIN;
12641 break;
12642 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000012643 // Converting this to a min would handle comparisons between positive
12644 // and negative zero incorrectly.
12645 if (!UnsafeFPMath &&
12646 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
12647 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012648 Opcode = X86ISD::FMIN;
12649 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000012650 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000012651 // Converting this to a min would handle both negative zeros and NaNs
12652 // incorrectly, but we can swap the operands to fix both.
12653 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012654 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012655 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000012656 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012657 Opcode = X86ISD::FMIN;
12658 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012659
Dan Gohman670e5392009-09-21 18:03:22 +000012660 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012661 // Converting this to a max would handle comparisons between positive
12662 // and negative zero incorrectly.
12663 if (!UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000012664 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012665 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012666 Opcode = X86ISD::FMAX;
12667 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000012668 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000012669 // Converting this to a max would handle NaNs incorrectly, and swapping
12670 // the operands would cause it to handle comparisons between positive
12671 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000012672 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000012673 if (!UnsafeFPMath &&
12674 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
12675 break;
12676 std::swap(LHS, RHS);
12677 }
Dan Gohman670e5392009-09-21 18:03:22 +000012678 Opcode = X86ISD::FMAX;
12679 break;
12680 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012681 // Converting this to a max would handle both negative zeros and NaNs
12682 // incorrectly, but we can swap the operands to fix both.
12683 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012684 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012685 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012686 case ISD::SETGE:
12687 Opcode = X86ISD::FMAX;
12688 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000012689 }
Dan Gohman670e5392009-09-21 18:03:22 +000012690 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000012691 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
12692 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000012693 switch (CC) {
12694 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000012695 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012696 // Converting this to a min would handle comparisons between positive
12697 // and negative zero incorrectly, and swapping the operands would
12698 // cause it to handle NaNs incorrectly.
12699 if (!UnsafeFPMath &&
12700 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000012701 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012702 break;
12703 std::swap(LHS, RHS);
12704 }
Dan Gohman670e5392009-09-21 18:03:22 +000012705 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000012706 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012707 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000012708 // Converting this to a min would handle NaNs incorrectly.
12709 if (!UnsafeFPMath &&
12710 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
12711 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012712 Opcode = X86ISD::FMIN;
12713 break;
12714 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012715 // Converting this to a min would handle both negative zeros and NaNs
12716 // incorrectly, but we can swap the operands to fix both.
12717 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012718 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012719 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012720 case ISD::SETGE:
12721 Opcode = X86ISD::FMIN;
12722 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012723
Dan Gohman670e5392009-09-21 18:03:22 +000012724 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000012725 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000012726 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012727 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012728 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000012729 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012730 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000012731 // Converting this to a max would handle comparisons between positive
12732 // and negative zero incorrectly, and swapping the operands would
12733 // cause it to handle NaNs incorrectly.
12734 if (!UnsafeFPMath &&
12735 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000012736 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012737 break;
12738 std::swap(LHS, RHS);
12739 }
Dan Gohman670e5392009-09-21 18:03:22 +000012740 Opcode = X86ISD::FMAX;
12741 break;
12742 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000012743 // Converting this to a max would handle both negative zeros and NaNs
12744 // incorrectly, but we can swap the operands to fix both.
12745 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012746 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012747 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000012748 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012749 Opcode = X86ISD::FMAX;
12750 break;
12751 }
Chris Lattner83e6c992006-10-04 06:57:07 +000012752 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012753
Chris Lattner47b4ce82009-03-11 05:48:52 +000012754 if (Opcode)
12755 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000012756 }
Eric Christopherfd179292009-08-27 18:07:15 +000012757
Chris Lattnerd1980a52009-03-12 06:52:53 +000012758 // If this is a select between two integer constants, try to do some
12759 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000012760 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
12761 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000012762 // Don't do this for crazy integer types.
12763 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
12764 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000012765 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000012766 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000012767
Chris Lattnercee56e72009-03-13 05:53:31 +000012768 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000012769 // Efficiently invertible.
12770 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
12771 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
12772 isa<ConstantSDNode>(Cond.getOperand(1))))) {
12773 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000012774 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000012775 }
Eric Christopherfd179292009-08-27 18:07:15 +000012776
Chris Lattnerd1980a52009-03-12 06:52:53 +000012777 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000012778 if (FalseC->getAPIntValue() == 0 &&
12779 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000012780 if (NeedsCondInvert) // Invert the condition if needed.
12781 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
12782 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000012783
Chris Lattnerd1980a52009-03-12 06:52:53 +000012784 // Zero extend the condition if needed.
12785 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000012786
Chris Lattnercee56e72009-03-13 05:53:31 +000012787 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000012788 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000012789 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000012790 }
Eric Christopherfd179292009-08-27 18:07:15 +000012791
Chris Lattner97a29a52009-03-13 05:22:11 +000012792 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000012793 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000012794 if (NeedsCondInvert) // Invert the condition if needed.
12795 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
12796 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000012797
Chris Lattner97a29a52009-03-13 05:22:11 +000012798 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000012799 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
12800 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000012801 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000012802 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000012803 }
Eric Christopherfd179292009-08-27 18:07:15 +000012804
Chris Lattnercee56e72009-03-13 05:53:31 +000012805 // Optimize cases that will turn into an LEA instruction. This requires
12806 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000012807 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000012808 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000012809 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000012810
Chris Lattnercee56e72009-03-13 05:53:31 +000012811 bool isFastMultiplier = false;
12812 if (Diff < 10) {
12813 switch ((unsigned char)Diff) {
12814 default: break;
12815 case 1: // result = add base, cond
12816 case 2: // result = lea base( , cond*2)
12817 case 3: // result = lea base(cond, cond*2)
12818 case 4: // result = lea base( , cond*4)
12819 case 5: // result = lea base(cond, cond*4)
12820 case 8: // result = lea base( , cond*8)
12821 case 9: // result = lea base(cond, cond*8)
12822 isFastMultiplier = true;
12823 break;
12824 }
12825 }
Eric Christopherfd179292009-08-27 18:07:15 +000012826
Chris Lattnercee56e72009-03-13 05:53:31 +000012827 if (isFastMultiplier) {
12828 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
12829 if (NeedsCondInvert) // Invert the condition if needed.
12830 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
12831 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000012832
Chris Lattnercee56e72009-03-13 05:53:31 +000012833 // Zero extend the condition if needed.
12834 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
12835 Cond);
12836 // Scale the condition by the difference.
12837 if (Diff != 1)
12838 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
12839 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000012840
Chris Lattnercee56e72009-03-13 05:53:31 +000012841 // Add the base if non-zero.
12842 if (FalseC->getAPIntValue() != 0)
12843 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
12844 SDValue(FalseC, 0));
12845 return Cond;
12846 }
Eric Christopherfd179292009-08-27 18:07:15 +000012847 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000012848 }
12849 }
Eric Christopherfd179292009-08-27 18:07:15 +000012850
Dan Gohman475871a2008-07-27 21:46:04 +000012851 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000012852}
12853
Chris Lattnerd1980a52009-03-12 06:52:53 +000012854/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
12855static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
12856 TargetLowering::DAGCombinerInfo &DCI) {
12857 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000012858
Chris Lattnerd1980a52009-03-12 06:52:53 +000012859 // If the flag operand isn't dead, don't touch this CMOV.
12860 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
12861 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000012862
Evan Chengb5a55d92011-05-24 01:48:22 +000012863 SDValue FalseOp = N->getOperand(0);
12864 SDValue TrueOp = N->getOperand(1);
12865 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
12866 SDValue Cond = N->getOperand(3);
12867 if (CC == X86::COND_E || CC == X86::COND_NE) {
12868 switch (Cond.getOpcode()) {
12869 default: break;
12870 case X86ISD::BSR:
12871 case X86ISD::BSF:
12872 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
12873 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
12874 return (CC == X86::COND_E) ? FalseOp : TrueOp;
12875 }
12876 }
12877
Chris Lattnerd1980a52009-03-12 06:52:53 +000012878 // If this is a select between two integer constants, try to do some
12879 // optimizations. Note that the operands are ordered the opposite of SELECT
12880 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000012881 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
12882 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000012883 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
12884 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000012885 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
12886 CC = X86::GetOppositeBranchCondition(CC);
12887 std::swap(TrueC, FalseC);
12888 }
Eric Christopherfd179292009-08-27 18:07:15 +000012889
Chris Lattnerd1980a52009-03-12 06:52:53 +000012890 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000012891 // This is efficient for any integer data type (including i8/i16) and
12892 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000012893 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012894 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
12895 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000012896
Chris Lattnerd1980a52009-03-12 06:52:53 +000012897 // Zero extend the condition if needed.
12898 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000012899
Chris Lattnerd1980a52009-03-12 06:52:53 +000012900 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
12901 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000012902 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000012903 if (N->getNumValues() == 2) // Dead flag value?
12904 return DCI.CombineTo(N, Cond, SDValue());
12905 return Cond;
12906 }
Eric Christopherfd179292009-08-27 18:07:15 +000012907
Chris Lattnercee56e72009-03-13 05:53:31 +000012908 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
12909 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000012910 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012911 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
12912 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000012913
Chris Lattner97a29a52009-03-13 05:22:11 +000012914 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000012915 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
12916 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000012917 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
12918 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000012919
Chris Lattner97a29a52009-03-13 05:22:11 +000012920 if (N->getNumValues() == 2) // Dead flag value?
12921 return DCI.CombineTo(N, Cond, SDValue());
12922 return Cond;
12923 }
Eric Christopherfd179292009-08-27 18:07:15 +000012924
Chris Lattnercee56e72009-03-13 05:53:31 +000012925 // Optimize cases that will turn into an LEA instruction. This requires
12926 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000012927 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000012928 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000012929 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000012930
Chris Lattnercee56e72009-03-13 05:53:31 +000012931 bool isFastMultiplier = false;
12932 if (Diff < 10) {
12933 switch ((unsigned char)Diff) {
12934 default: break;
12935 case 1: // result = add base, cond
12936 case 2: // result = lea base( , cond*2)
12937 case 3: // result = lea base(cond, cond*2)
12938 case 4: // result = lea base( , cond*4)
12939 case 5: // result = lea base(cond, cond*4)
12940 case 8: // result = lea base( , cond*8)
12941 case 9: // result = lea base(cond, cond*8)
12942 isFastMultiplier = true;
12943 break;
12944 }
12945 }
Eric Christopherfd179292009-08-27 18:07:15 +000012946
Chris Lattnercee56e72009-03-13 05:53:31 +000012947 if (isFastMultiplier) {
12948 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000012949 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
12950 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000012951 // Zero extend the condition if needed.
12952 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
12953 Cond);
12954 // Scale the condition by the difference.
12955 if (Diff != 1)
12956 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
12957 DAG.getConstant(Diff, Cond.getValueType()));
12958
12959 // Add the base if non-zero.
12960 if (FalseC->getAPIntValue() != 0)
12961 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
12962 SDValue(FalseC, 0));
12963 if (N->getNumValues() == 2) // Dead flag value?
12964 return DCI.CombineTo(N, Cond, SDValue());
12965 return Cond;
12966 }
Eric Christopherfd179292009-08-27 18:07:15 +000012967 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000012968 }
12969 }
12970 return SDValue();
12971}
12972
12973
Evan Cheng0b0cd912009-03-28 05:57:29 +000012974/// PerformMulCombine - Optimize a single multiply with constant into two
12975/// in order to implement it with two cheaper instructions, e.g.
12976/// LEA + SHL, LEA + LEA.
12977static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
12978 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000012979 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
12980 return SDValue();
12981
Owen Andersone50ed302009-08-10 22:56:29 +000012982 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012983 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000012984 return SDValue();
12985
12986 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
12987 if (!C)
12988 return SDValue();
12989 uint64_t MulAmt = C->getZExtValue();
12990 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
12991 return SDValue();
12992
12993 uint64_t MulAmt1 = 0;
12994 uint64_t MulAmt2 = 0;
12995 if ((MulAmt % 9) == 0) {
12996 MulAmt1 = 9;
12997 MulAmt2 = MulAmt / 9;
12998 } else if ((MulAmt % 5) == 0) {
12999 MulAmt1 = 5;
13000 MulAmt2 = MulAmt / 5;
13001 } else if ((MulAmt % 3) == 0) {
13002 MulAmt1 = 3;
13003 MulAmt2 = MulAmt / 3;
13004 }
13005 if (MulAmt2 &&
13006 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
13007 DebugLoc DL = N->getDebugLoc();
13008
13009 if (isPowerOf2_64(MulAmt2) &&
13010 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
13011 // If second multiplifer is pow2, issue it first. We want the multiply by
13012 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
13013 // is an add.
13014 std::swap(MulAmt1, MulAmt2);
13015
13016 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000013017 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000013018 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000013019 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000013020 else
Evan Cheng73f24c92009-03-30 21:36:47 +000013021 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000013022 DAG.getConstant(MulAmt1, VT));
13023
Eric Christopherfd179292009-08-27 18:07:15 +000013024 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000013025 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000013026 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000013027 else
Evan Cheng73f24c92009-03-30 21:36:47 +000013028 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000013029 DAG.getConstant(MulAmt2, VT));
13030
13031 // Do not add new nodes to DAG combiner worklist.
13032 DCI.CombineTo(N, NewMul, false);
13033 }
13034 return SDValue();
13035}
13036
Evan Chengad9c0a32009-12-15 00:53:42 +000013037static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
13038 SDValue N0 = N->getOperand(0);
13039 SDValue N1 = N->getOperand(1);
13040 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
13041 EVT VT = N0.getValueType();
13042
13043 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
13044 // since the result of setcc_c is all zero's or all ones.
13045 if (N1C && N0.getOpcode() == ISD::AND &&
13046 N0.getOperand(1).getOpcode() == ISD::Constant) {
13047 SDValue N00 = N0.getOperand(0);
13048 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
13049 ((N00.getOpcode() == ISD::ANY_EXTEND ||
13050 N00.getOpcode() == ISD::ZERO_EXTEND) &&
13051 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
13052 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
13053 APInt ShAmt = N1C->getAPIntValue();
13054 Mask = Mask.shl(ShAmt);
13055 if (Mask != 0)
13056 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
13057 N00, DAG.getConstant(Mask, VT));
13058 }
13059 }
13060
13061 return SDValue();
13062}
Evan Cheng0b0cd912009-03-28 05:57:29 +000013063
Nate Begeman740ab032009-01-26 00:52:55 +000013064/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
13065/// when possible.
13066static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
13067 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000013068 EVT VT = N->getValueType(0);
13069 if (!VT.isVector() && VT.isInteger() &&
13070 N->getOpcode() == ISD::SHL)
13071 return PerformSHLCombine(N, DAG);
13072
Nate Begeman740ab032009-01-26 00:52:55 +000013073 // On X86 with SSE2 support, we can transform this to a vector shift if
13074 // all elements are shifted by the same amount. We can't do this in legalize
13075 // because the a constant vector is typically transformed to a constant pool
13076 // so we have no knowledge of the shift amount.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013077 if (!Subtarget->hasXMMInt())
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013078 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000013079
Owen Anderson825b72b2009-08-11 20:47:22 +000013080 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013081 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000013082
Mon P Wang3becd092009-01-28 08:12:05 +000013083 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000013084 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000013085 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000013086 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000013087 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
13088 unsigned NumElts = VT.getVectorNumElements();
13089 unsigned i = 0;
13090 for (; i != NumElts; ++i) {
13091 SDValue Arg = ShAmtOp.getOperand(i);
13092 if (Arg.getOpcode() == ISD::UNDEF) continue;
13093 BaseShAmt = Arg;
13094 break;
13095 }
13096 for (; i != NumElts; ++i) {
13097 SDValue Arg = ShAmtOp.getOperand(i);
13098 if (Arg.getOpcode() == ISD::UNDEF) continue;
13099 if (Arg != BaseShAmt) {
13100 return SDValue();
13101 }
13102 }
13103 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000013104 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000013105 SDValue InVec = ShAmtOp.getOperand(0);
13106 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
13107 unsigned NumElts = InVec.getValueType().getVectorNumElements();
13108 unsigned i = 0;
13109 for (; i != NumElts; ++i) {
13110 SDValue Arg = InVec.getOperand(i);
13111 if (Arg.getOpcode() == ISD::UNDEF) continue;
13112 BaseShAmt = Arg;
13113 break;
13114 }
13115 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
13116 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000013117 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000013118 if (C->getZExtValue() == SplatIdx)
13119 BaseShAmt = InVec.getOperand(1);
13120 }
13121 }
13122 if (BaseShAmt.getNode() == 0)
13123 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
13124 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000013125 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013126 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000013127
Mon P Wangefa42202009-09-03 19:56:25 +000013128 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000013129 if (EltVT.bitsGT(MVT::i32))
13130 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
13131 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000013132 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000013133
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013134 // The shift amount is identical so we can do a vector shift.
13135 SDValue ValOp = N->getOperand(0);
13136 switch (N->getOpcode()) {
13137 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000013138 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013139 break;
13140 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000013141 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013142 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013143 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013144 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013145 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013146 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013147 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013148 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013149 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013150 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013151 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013152 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013153 break;
13154 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000013155 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013156 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013157 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013158 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013159 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013160 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013161 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013162 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013163 break;
13164 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000013165 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013166 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013167 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013168 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013169 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013170 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013171 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013172 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013173 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013174 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013175 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013176 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013177 break;
Nate Begeman740ab032009-01-26 00:52:55 +000013178 }
13179 return SDValue();
13180}
13181
Nate Begemanb65c1752010-12-17 22:55:37 +000013182
Stuart Hastings865f0932011-06-03 23:53:54 +000013183// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
13184// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
13185// and friends. Likewise for OR -> CMPNEQSS.
13186static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
13187 TargetLowering::DAGCombinerInfo &DCI,
13188 const X86Subtarget *Subtarget) {
13189 unsigned opcode;
13190
13191 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
13192 // we're requiring SSE2 for both.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013193 if (Subtarget->hasXMMInt() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
Stuart Hastings865f0932011-06-03 23:53:54 +000013194 SDValue N0 = N->getOperand(0);
13195 SDValue N1 = N->getOperand(1);
13196 SDValue CMP0 = N0->getOperand(1);
13197 SDValue CMP1 = N1->getOperand(1);
13198 DebugLoc DL = N->getDebugLoc();
13199
13200 // The SETCCs should both refer to the same CMP.
13201 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
13202 return SDValue();
13203
13204 SDValue CMP00 = CMP0->getOperand(0);
13205 SDValue CMP01 = CMP0->getOperand(1);
13206 EVT VT = CMP00.getValueType();
13207
13208 if (VT == MVT::f32 || VT == MVT::f64) {
13209 bool ExpectingFlags = false;
13210 // Check for any users that want flags:
13211 for (SDNode::use_iterator UI = N->use_begin(),
13212 UE = N->use_end();
13213 !ExpectingFlags && UI != UE; ++UI)
13214 switch (UI->getOpcode()) {
13215 default:
13216 case ISD::BR_CC:
13217 case ISD::BRCOND:
13218 case ISD::SELECT:
13219 ExpectingFlags = true;
13220 break;
13221 case ISD::CopyToReg:
13222 case ISD::SIGN_EXTEND:
13223 case ISD::ZERO_EXTEND:
13224 case ISD::ANY_EXTEND:
13225 break;
13226 }
13227
13228 if (!ExpectingFlags) {
13229 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
13230 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
13231
13232 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
13233 X86::CondCode tmp = cc0;
13234 cc0 = cc1;
13235 cc1 = tmp;
13236 }
13237
13238 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
13239 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
13240 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
13241 X86ISD::NodeType NTOperator = is64BitFP ?
13242 X86ISD::FSETCCsd : X86ISD::FSETCCss;
13243 // FIXME: need symbolic constants for these magic numbers.
13244 // See X86ATTInstPrinter.cpp:printSSECC().
13245 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
13246 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
13247 DAG.getConstant(x86cc, MVT::i8));
13248 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
13249 OnesOrZeroesF);
13250 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
13251 DAG.getConstant(1, MVT::i32));
13252 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
13253 return OneBitOfTruth;
13254 }
13255 }
13256 }
13257 }
13258 return SDValue();
13259}
13260
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013261/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
13262/// so it can be folded inside ANDNP.
13263static bool CanFoldXORWithAllOnes(const SDNode *N) {
13264 EVT VT = N->getValueType(0);
13265
13266 // Match direct AllOnes for 128 and 256-bit vectors
13267 if (ISD::isBuildVectorAllOnes(N))
13268 return true;
13269
13270 // Look through a bit convert.
13271 if (N->getOpcode() == ISD::BITCAST)
13272 N = N->getOperand(0).getNode();
13273
13274 // Sometimes the operand may come from a insert_subvector building a 256-bit
13275 // allones vector
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013276 if (VT.getSizeInBits() == 256 &&
Bill Wendling456a9252011-08-04 00:32:58 +000013277 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
13278 SDValue V1 = N->getOperand(0);
13279 SDValue V2 = N->getOperand(1);
13280
13281 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
13282 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
13283 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
13284 ISD::isBuildVectorAllOnes(V2.getNode()))
13285 return true;
13286 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013287
13288 return false;
13289}
13290
Nate Begemanb65c1752010-12-17 22:55:37 +000013291static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
13292 TargetLowering::DAGCombinerInfo &DCI,
13293 const X86Subtarget *Subtarget) {
13294 if (DCI.isBeforeLegalizeOps())
13295 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013296
Stuart Hastings865f0932011-06-03 23:53:54 +000013297 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
13298 if (R.getNode())
13299 return R;
13300
Craig Topper54a11172011-10-14 07:06:56 +000013301 EVT VT = N->getValueType(0);
13302
Craig Topperb4c94572011-10-21 06:55:01 +000013303 // Create ANDN, BLSI, and BLSR instructions
13304 // BLSI is X & (-X)
13305 // BLSR is X & (X-1)
Craig Topper54a11172011-10-14 07:06:56 +000013306 if (Subtarget->hasBMI() && (VT == MVT::i32 || VT == MVT::i64)) {
13307 SDValue N0 = N->getOperand(0);
13308 SDValue N1 = N->getOperand(1);
13309 DebugLoc DL = N->getDebugLoc();
13310
13311 // Check LHS for not
13312 if (N0.getOpcode() == ISD::XOR && isAllOnes(N0.getOperand(1)))
13313 return DAG.getNode(X86ISD::ANDN, DL, VT, N0.getOperand(0), N1);
13314 // Check RHS for not
13315 if (N1.getOpcode() == ISD::XOR && isAllOnes(N1.getOperand(1)))
13316 return DAG.getNode(X86ISD::ANDN, DL, VT, N1.getOperand(0), N0);
13317
Craig Topperb4c94572011-10-21 06:55:01 +000013318 // Check LHS for neg
13319 if (N0.getOpcode() == ISD::SUB && N0.getOperand(1) == N1 &&
13320 isZero(N0.getOperand(0)))
13321 return DAG.getNode(X86ISD::BLSI, DL, VT, N1);
13322
13323 // Check RHS for neg
13324 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1) == N0 &&
13325 isZero(N1.getOperand(0)))
13326 return DAG.getNode(X86ISD::BLSI, DL, VT, N0);
13327
13328 // Check LHS for X-1
13329 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
13330 isAllOnes(N0.getOperand(1)))
13331 return DAG.getNode(X86ISD::BLSR, DL, VT, N1);
13332
13333 // Check RHS for X-1
13334 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
13335 isAllOnes(N1.getOperand(1)))
13336 return DAG.getNode(X86ISD::BLSR, DL, VT, N0);
13337
Craig Topper54a11172011-10-14 07:06:56 +000013338 return SDValue();
13339 }
13340
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000013341 // Want to form ANDNP nodes:
13342 // 1) In the hopes of then easily combining them with OR and AND nodes
13343 // to form PBLEND/PSIGN.
13344 // 2) To match ANDN packed intrinsics
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000013345 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000013346 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013347
Nate Begemanb65c1752010-12-17 22:55:37 +000013348 SDValue N0 = N->getOperand(0);
13349 SDValue N1 = N->getOperand(1);
13350 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013351
Nate Begemanb65c1752010-12-17 22:55:37 +000013352 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013353 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013354 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
13355 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013356 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000013357
13358 // Check RHS for vnot
13359 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013360 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
13361 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013362 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013363
Nate Begemanb65c1752010-12-17 22:55:37 +000013364 return SDValue();
13365}
13366
Evan Cheng760d1942010-01-04 21:22:48 +000013367static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000013368 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000013369 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000013370 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000013371 return SDValue();
13372
Stuart Hastings865f0932011-06-03 23:53:54 +000013373 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
13374 if (R.getNode())
13375 return R;
13376
Evan Cheng760d1942010-01-04 21:22:48 +000013377 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000013378 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64 && VT != MVT::v2i64)
Evan Cheng760d1942010-01-04 21:22:48 +000013379 return SDValue();
13380
Evan Cheng760d1942010-01-04 21:22:48 +000013381 SDValue N0 = N->getOperand(0);
13382 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013383
Nate Begemanb65c1752010-12-17 22:55:37 +000013384 // look for psign/blend
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013385 if (Subtarget->hasSSSE3() || Subtarget->hasAVX()) {
Nate Begemanb65c1752010-12-17 22:55:37 +000013386 if (VT == MVT::v2i64) {
13387 // Canonicalize pandn to RHS
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013388 if (N0.getOpcode() == X86ISD::ANDNP)
Nate Begemanb65c1752010-12-17 22:55:37 +000013389 std::swap(N0, N1);
13390 // or (and (m, x), (pandn m, y))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013391 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
Nate Begemanb65c1752010-12-17 22:55:37 +000013392 SDValue Mask = N1.getOperand(0);
13393 SDValue X = N1.getOperand(1);
13394 SDValue Y;
13395 if (N0.getOperand(0) == Mask)
13396 Y = N0.getOperand(1);
13397 if (N0.getOperand(1) == Mask)
13398 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013399
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013400 // Check to see if the mask appeared in both the AND and ANDNP and
Nate Begemanb65c1752010-12-17 22:55:37 +000013401 if (!Y.getNode())
13402 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013403
Nate Begemanb65c1752010-12-17 22:55:37 +000013404 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
13405 if (Mask.getOpcode() != ISD::BITCAST ||
13406 X.getOpcode() != ISD::BITCAST ||
13407 Y.getOpcode() != ISD::BITCAST)
13408 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013409
Nate Begemanb65c1752010-12-17 22:55:37 +000013410 // Look through mask bitcast.
13411 Mask = Mask.getOperand(0);
13412 EVT MaskVT = Mask.getValueType();
13413
13414 // Validate that the Mask operand is a vector sra node. The sra node
13415 // will be an intrinsic.
13416 if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
13417 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013418
Nate Begemanb65c1752010-12-17 22:55:37 +000013419 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
13420 // there is no psrai.b
13421 switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
13422 case Intrinsic::x86_sse2_psrai_w:
13423 case Intrinsic::x86_sse2_psrai_d:
13424 break;
13425 default: return SDValue();
13426 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013427
Nate Begemanb65c1752010-12-17 22:55:37 +000013428 // Check that the SRA is all signbits.
13429 SDValue SraC = Mask.getOperand(2);
13430 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
13431 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
13432 if ((SraAmt + 1) != EltBits)
13433 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013434
Nate Begemanb65c1752010-12-17 22:55:37 +000013435 DebugLoc DL = N->getDebugLoc();
13436
13437 // Now we know we at least have a plendvb with the mask val. See if
13438 // we can form a psignb/w/d.
13439 // psign = x.type == y.type == mask.type && y = sub(0, x);
13440 X = X.getOperand(0);
13441 Y = Y.getOperand(0);
13442 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
13443 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
13444 X.getValueType() == MaskVT && X.getValueType() == Y.getValueType()){
13445 unsigned Opc = 0;
13446 switch (EltBits) {
13447 case 8: Opc = X86ISD::PSIGNB; break;
13448 case 16: Opc = X86ISD::PSIGNW; break;
13449 case 32: Opc = X86ISD::PSIGND; break;
13450 default: break;
13451 }
13452 if (Opc) {
13453 SDValue Sign = DAG.getNode(Opc, DL, MaskVT, X, Mask.getOperand(1));
13454 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Sign);
13455 }
13456 }
13457 // PBLENDVB only available on SSE 4.1
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013458 if (!(Subtarget->hasSSE41() || Subtarget->hasAVX()))
Nate Begemanb65c1752010-12-17 22:55:37 +000013459 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013460
Nate Begemanb65c1752010-12-17 22:55:37 +000013461 X = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, X);
13462 Y = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Y);
13463 Mask = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Mask);
Nadav Rotemfbad25e2011-09-11 15:02:23 +000013464 Mask = DAG.getNode(ISD::VSELECT, DL, MVT::v16i8, Mask, X, Y);
Nate Begemanb65c1752010-12-17 22:55:37 +000013465 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Mask);
13466 }
13467 }
13468 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013469
Nate Begemanb65c1752010-12-17 22:55:37 +000013470 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000013471 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
13472 std::swap(N0, N1);
13473 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
13474 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000013475 if (!N0.hasOneUse() || !N1.hasOneUse())
13476 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000013477
13478 SDValue ShAmt0 = N0.getOperand(1);
13479 if (ShAmt0.getValueType() != MVT::i8)
13480 return SDValue();
13481 SDValue ShAmt1 = N1.getOperand(1);
13482 if (ShAmt1.getValueType() != MVT::i8)
13483 return SDValue();
13484 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
13485 ShAmt0 = ShAmt0.getOperand(0);
13486 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
13487 ShAmt1 = ShAmt1.getOperand(0);
13488
13489 DebugLoc DL = N->getDebugLoc();
13490 unsigned Opc = X86ISD::SHLD;
13491 SDValue Op0 = N0.getOperand(0);
13492 SDValue Op1 = N1.getOperand(0);
13493 if (ShAmt0.getOpcode() == ISD::SUB) {
13494 Opc = X86ISD::SHRD;
13495 std::swap(Op0, Op1);
13496 std::swap(ShAmt0, ShAmt1);
13497 }
13498
Evan Cheng8b1190a2010-04-28 01:18:01 +000013499 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000013500 if (ShAmt1.getOpcode() == ISD::SUB) {
13501 SDValue Sum = ShAmt1.getOperand(0);
13502 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000013503 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
13504 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
13505 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
13506 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000013507 return DAG.getNode(Opc, DL, VT,
13508 Op0, Op1,
13509 DAG.getNode(ISD::TRUNCATE, DL,
13510 MVT::i8, ShAmt0));
13511 }
13512 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
13513 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
13514 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000013515 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000013516 return DAG.getNode(Opc, DL, VT,
13517 N0.getOperand(0), N1.getOperand(0),
13518 DAG.getNode(ISD::TRUNCATE, DL,
13519 MVT::i8, ShAmt0));
13520 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013521
Evan Cheng760d1942010-01-04 21:22:48 +000013522 return SDValue();
13523}
13524
Craig Topperb4c94572011-10-21 06:55:01 +000013525static SDValue PerformXorCombine(SDNode *N, SelectionDAG &DAG,
13526 TargetLowering::DAGCombinerInfo &DCI,
13527 const X86Subtarget *Subtarget) {
13528 if (DCI.isBeforeLegalizeOps())
13529 return SDValue();
13530
13531 EVT VT = N->getValueType(0);
13532
13533 if (VT != MVT::i32 && VT != MVT::i64)
13534 return SDValue();
13535
13536 // Create BLSMSK instructions by finding X ^ (X-1)
13537 SDValue N0 = N->getOperand(0);
13538 SDValue N1 = N->getOperand(1);
13539 DebugLoc DL = N->getDebugLoc();
13540
13541 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
13542 isAllOnes(N0.getOperand(1)))
13543 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N1);
13544
13545 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
13546 isAllOnes(N1.getOperand(1)))
13547 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N0);
13548
13549 return SDValue();
13550}
13551
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013552/// PerformLOADCombine - Do target-specific dag combines on LOAD nodes.
13553static SDValue PerformLOADCombine(SDNode *N, SelectionDAG &DAG,
13554 const X86Subtarget *Subtarget) {
13555 LoadSDNode *Ld = cast<LoadSDNode>(N);
13556 EVT RegVT = Ld->getValueType(0);
13557 EVT MemVT = Ld->getMemoryVT();
13558 DebugLoc dl = Ld->getDebugLoc();
13559 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13560
13561 ISD::LoadExtType Ext = Ld->getExtensionType();
13562
Nadav Rotemca6f2962011-09-18 19:00:23 +000013563 // If this is a vector EXT Load then attempt to optimize it using a
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013564 // shuffle. We need SSE4 for the shuffles.
13565 // TODO: It is possible to support ZExt by zeroing the undef values
13566 // during the shuffle phase or after the shuffle.
13567 if (RegVT.isVector() && Ext == ISD::EXTLOAD && Subtarget->hasSSE41()) {
13568 assert(MemVT != RegVT && "Cannot extend to the same type");
13569 assert(MemVT.isVector() && "Must load a vector from memory");
13570
13571 unsigned NumElems = RegVT.getVectorNumElements();
13572 unsigned RegSz = RegVT.getSizeInBits();
13573 unsigned MemSz = MemVT.getSizeInBits();
13574 assert(RegSz > MemSz && "Register size must be greater than the mem size");
Nadav Rotemca6f2962011-09-18 19:00:23 +000013575 // All sizes must be a power of two
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013576 if (!isPowerOf2_32(RegSz * MemSz * NumElems)) return SDValue();
13577
13578 // Attempt to load the original value using a single load op.
13579 // Find a scalar type which is equal to the loaded word size.
13580 MVT SclrLoadTy = MVT::i8;
13581 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
13582 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
13583 MVT Tp = (MVT::SimpleValueType)tp;
13584 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() == MemSz) {
13585 SclrLoadTy = Tp;
13586 break;
13587 }
13588 }
13589
13590 // Proceed if a load word is found.
13591 if (SclrLoadTy.getSizeInBits() != MemSz) return SDValue();
13592
13593 EVT LoadUnitVecVT = EVT::getVectorVT(*DAG.getContext(), SclrLoadTy,
13594 RegSz/SclrLoadTy.getSizeInBits());
13595
13596 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
13597 RegSz/MemVT.getScalarType().getSizeInBits());
13598 // Can't shuffle using an illegal type.
13599 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
13600
13601 // Perform a single load.
13602 SDValue ScalarLoad = DAG.getLoad(SclrLoadTy, dl, Ld->getChain(),
13603 Ld->getBasePtr(),
13604 Ld->getPointerInfo(), Ld->isVolatile(),
13605 Ld->isNonTemporal(), Ld->getAlignment());
13606
13607 // Insert the word loaded into a vector.
13608 SDValue ScalarInVector = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
13609 LoadUnitVecVT, ScalarLoad);
13610
13611 // Bitcast the loaded value to a vector of the original element type, in
13612 // the size of the target vector type.
13613 SDValue SlicedVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, ScalarInVector);
13614 unsigned SizeRatio = RegSz/MemSz;
13615
13616 // Redistribute the loaded elements into the different locations.
13617 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
13618 for (unsigned i = 0; i < NumElems; i++) ShuffleVec[i*SizeRatio] = i;
13619
13620 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
13621 DAG.getUNDEF(SlicedVec.getValueType()),
13622 ShuffleVec.data());
13623
13624 // Bitcast to the requested type.
13625 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
13626 // Replace the original load with the new sequence
13627 // and return the new chain.
13628 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Shuff);
13629 return SDValue(ScalarLoad.getNode(), 1);
13630 }
13631
13632 return SDValue();
13633}
13634
Chris Lattner149a4e52008-02-22 02:09:43 +000013635/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000013636static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000013637 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000013638 StoreSDNode *St = cast<StoreSDNode>(N);
13639 EVT VT = St->getValue().getValueType();
13640 EVT StVT = St->getMemoryVT();
13641 DebugLoc dl = St->getDebugLoc();
Nadav Rotem5e742a32011-08-11 16:41:21 +000013642 SDValue StoredVal = St->getOperand(1);
13643 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13644
13645 // If we are saving a concatination of two XMM registers, perform two stores.
Nadav Rotem6236f7f2011-08-11 17:05:47 +000013646 // This is better in Sandy Bridge cause one 256-bit mem op is done via two
13647 // 128-bit ones. If in the future the cost becomes only one memory access the
13648 // first version would be better.
Nadav Rotem5e742a32011-08-11 16:41:21 +000013649 if (VT.getSizeInBits() == 256 &&
13650 StoredVal.getNode()->getOpcode() == ISD::CONCAT_VECTORS &&
13651 StoredVal.getNumOperands() == 2) {
13652
13653 SDValue Value0 = StoredVal.getOperand(0);
13654 SDValue Value1 = StoredVal.getOperand(1);
13655
13656 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
13657 SDValue Ptr0 = St->getBasePtr();
13658 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
13659
13660 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
13661 St->getPointerInfo(), St->isVolatile(),
13662 St->isNonTemporal(), St->getAlignment());
13663 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
13664 St->getPointerInfo(), St->isVolatile(),
13665 St->isNonTemporal(), St->getAlignment());
13666 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
13667 }
Nadav Rotem614061b2011-08-10 19:30:14 +000013668
13669 // Optimize trunc store (of multiple scalars) to shuffle and store.
13670 // First, pack all of the elements in one place. Next, store to memory
13671 // in fewer chunks.
13672 if (St->isTruncatingStore() && VT.isVector()) {
13673 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13674 unsigned NumElems = VT.getVectorNumElements();
13675 assert(StVT != VT && "Cannot truncate to the same type");
13676 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
13677 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
13678
13679 // From, To sizes and ElemCount must be pow of two
13680 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000013681 // We are going to use the original vector elt for storing.
Nadav Rotem64ac73b2011-09-21 17:14:40 +000013682 // Accumulated smaller vector elements must be a multiple of the store size.
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000013683 if (0 != (NumElems * FromSz) % ToSz) return SDValue();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013684
Nadav Rotem614061b2011-08-10 19:30:14 +000013685 unsigned SizeRatio = FromSz / ToSz;
13686
13687 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
13688
13689 // Create a type on which we perform the shuffle
13690 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
13691 StVT.getScalarType(), NumElems*SizeRatio);
13692
13693 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
13694
13695 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
13696 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
13697 for (unsigned i = 0; i < NumElems; i++ ) ShuffleVec[i] = i * SizeRatio;
13698
13699 // Can't shuffle using an illegal type
13700 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
13701
13702 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
13703 DAG.getUNDEF(WideVec.getValueType()),
13704 ShuffleVec.data());
13705 // At this point all of the data is stored at the bottom of the
13706 // register. We now need to save it to mem.
13707
13708 // Find the largest store unit
13709 MVT StoreType = MVT::i8;
13710 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
13711 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
13712 MVT Tp = (MVT::SimpleValueType)tp;
13713 if (TLI.isTypeLegal(Tp) && StoreType.getSizeInBits() < NumElems * ToSz)
13714 StoreType = Tp;
13715 }
13716
13717 // Bitcast the original vector into a vector of store-size units
13718 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
13719 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
13720 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
13721 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
13722 SmallVector<SDValue, 8> Chains;
13723 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
13724 TLI.getPointerTy());
13725 SDValue Ptr = St->getBasePtr();
13726
13727 // Perform one or more big stores into memory.
13728 for (unsigned i = 0; i < (ToSz*NumElems)/StoreType.getSizeInBits() ; i++) {
13729 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
13730 StoreType, ShuffWide,
13731 DAG.getIntPtrConstant(i));
13732 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
13733 St->getPointerInfo(), St->isVolatile(),
13734 St->isNonTemporal(), St->getAlignment());
13735 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
13736 Chains.push_back(Ch);
13737 }
13738
13739 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
13740 Chains.size());
13741 }
13742
13743
Chris Lattner149a4e52008-02-22 02:09:43 +000013744 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
13745 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000013746 // A preferable solution to the general problem is to figure out the right
13747 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000013748
13749 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000013750 if (VT.getSizeInBits() != 64)
13751 return SDValue();
13752
Devang Patel578efa92009-06-05 21:57:13 +000013753 const Function *F = DAG.getMachineFunction().getFunction();
13754 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +000013755 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013756 && Subtarget->hasXMMInt();
Evan Cheng536e6672009-03-12 05:59:15 +000013757 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000013758 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000013759 isa<LoadSDNode>(St->getValue()) &&
13760 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
13761 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000013762 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000013763 LoadSDNode *Ld = 0;
13764 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000013765 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000013766 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000013767 // Must be a store of a load. We currently handle two cases: the load
13768 // is a direct child, and it's under an intervening TokenFactor. It is
13769 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000013770 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000013771 Ld = cast<LoadSDNode>(St->getChain());
13772 else if (St->getValue().hasOneUse() &&
13773 ChainVal->getOpcode() == ISD::TokenFactor) {
13774 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000013775 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000013776 TokenFactorIndex = i;
13777 Ld = cast<LoadSDNode>(St->getValue());
13778 } else
13779 Ops.push_back(ChainVal->getOperand(i));
13780 }
13781 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000013782
Evan Cheng536e6672009-03-12 05:59:15 +000013783 if (!Ld || !ISD::isNormalLoad(Ld))
13784 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000013785
Evan Cheng536e6672009-03-12 05:59:15 +000013786 // If this is not the MMX case, i.e. we are just turning i64 load/store
13787 // into f64 load/store, avoid the transformation if there are multiple
13788 // uses of the loaded value.
13789 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
13790 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000013791
Evan Cheng536e6672009-03-12 05:59:15 +000013792 DebugLoc LdDL = Ld->getDebugLoc();
13793 DebugLoc StDL = N->getDebugLoc();
13794 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
13795 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
13796 // pair instead.
13797 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000013798 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000013799 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
13800 Ld->getPointerInfo(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000013801 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000013802 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000013803 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000013804 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000013805 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000013806 Ops.size());
13807 }
Evan Cheng536e6672009-03-12 05:59:15 +000013808 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000013809 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000013810 St->isVolatile(), St->isNonTemporal(),
13811 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000013812 }
Evan Cheng536e6672009-03-12 05:59:15 +000013813
13814 // Otherwise, lower to two pairs of 32-bit loads / stores.
13815 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000013816 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
13817 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000013818
Owen Anderson825b72b2009-08-11 20:47:22 +000013819 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000013820 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000013821 Ld->isVolatile(), Ld->isNonTemporal(),
13822 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000013823 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000013824 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000013825 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000013826 MinAlign(Ld->getAlignment(), 4));
13827
13828 SDValue NewChain = LoLd.getValue(1);
13829 if (TokenFactorIndex != -1) {
13830 Ops.push_back(LoLd);
13831 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000013832 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000013833 Ops.size());
13834 }
13835
13836 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000013837 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
13838 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000013839
13840 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000013841 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000013842 St->isVolatile(), St->isNonTemporal(),
13843 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000013844 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000013845 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000013846 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000013847 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000013848 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000013849 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000013850 }
Dan Gohman475871a2008-07-27 21:46:04 +000013851 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000013852}
13853
Duncan Sands17470be2011-09-22 20:15:48 +000013854/// isHorizontalBinOp - Return 'true' if this vector operation is "horizontal"
13855/// and return the operands for the horizontal operation in LHS and RHS. A
13856/// horizontal operation performs the binary operation on successive elements
13857/// of its first operand, then on successive elements of its second operand,
13858/// returning the resulting values in a vector. For example, if
13859/// A = < float a0, float a1, float a2, float a3 >
13860/// and
13861/// B = < float b0, float b1, float b2, float b3 >
13862/// then the result of doing a horizontal operation on A and B is
13863/// A horizontal-op B = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >.
13864/// In short, LHS and RHS are inspected to see if LHS op RHS is of the form
13865/// A horizontal-op B, for some already available A and B, and if so then LHS is
13866/// set to A, RHS to B, and the routine returns 'true'.
13867/// Note that the binary operation should have the property that if one of the
13868/// operands is UNDEF then the result is UNDEF.
13869static bool isHorizontalBinOp(SDValue &LHS, SDValue &RHS, bool isCommutative) {
13870 // Look for the following pattern: if
13871 // A = < float a0, float a1, float a2, float a3 >
13872 // B = < float b0, float b1, float b2, float b3 >
13873 // and
13874 // LHS = VECTOR_SHUFFLE A, B, <0, 2, 4, 6>
13875 // RHS = VECTOR_SHUFFLE A, B, <1, 3, 5, 7>
13876 // then LHS op RHS = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >
13877 // which is A horizontal-op B.
13878
13879 // At least one of the operands should be a vector shuffle.
13880 if (LHS.getOpcode() != ISD::VECTOR_SHUFFLE &&
13881 RHS.getOpcode() != ISD::VECTOR_SHUFFLE)
13882 return false;
13883
13884 EVT VT = LHS.getValueType();
13885 unsigned N = VT.getVectorNumElements();
13886
13887 // View LHS in the form
13888 // LHS = VECTOR_SHUFFLE A, B, LMask
13889 // If LHS is not a shuffle then pretend it is the shuffle
13890 // LHS = VECTOR_SHUFFLE LHS, undef, <0, 1, ..., N-1>
13891 // NOTE: in what follows a default initialized SDValue represents an UNDEF of
13892 // type VT.
13893 SDValue A, B;
13894 SmallVector<int, 8> LMask(N);
13895 if (LHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
13896 if (LHS.getOperand(0).getOpcode() != ISD::UNDEF)
13897 A = LHS.getOperand(0);
13898 if (LHS.getOperand(1).getOpcode() != ISD::UNDEF)
13899 B = LHS.getOperand(1);
13900 cast<ShuffleVectorSDNode>(LHS.getNode())->getMask(LMask);
13901 } else {
13902 if (LHS.getOpcode() != ISD::UNDEF)
13903 A = LHS;
13904 for (unsigned i = 0; i != N; ++i)
13905 LMask[i] = i;
13906 }
13907
13908 // Likewise, view RHS in the form
13909 // RHS = VECTOR_SHUFFLE C, D, RMask
13910 SDValue C, D;
13911 SmallVector<int, 8> RMask(N);
13912 if (RHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
13913 if (RHS.getOperand(0).getOpcode() != ISD::UNDEF)
13914 C = RHS.getOperand(0);
13915 if (RHS.getOperand(1).getOpcode() != ISD::UNDEF)
13916 D = RHS.getOperand(1);
13917 cast<ShuffleVectorSDNode>(RHS.getNode())->getMask(RMask);
13918 } else {
13919 if (RHS.getOpcode() != ISD::UNDEF)
13920 C = RHS;
13921 for (unsigned i = 0; i != N; ++i)
13922 RMask[i] = i;
13923 }
13924
13925 // Check that the shuffles are both shuffling the same vectors.
13926 if (!(A == C && B == D) && !(A == D && B == C))
13927 return false;
13928
13929 // If everything is UNDEF then bail out: it would be better to fold to UNDEF.
13930 if (!A.getNode() && !B.getNode())
13931 return false;
13932
13933 // If A and B occur in reverse order in RHS, then "swap" them (which means
13934 // rewriting the mask).
13935 if (A != C)
13936 for (unsigned i = 0; i != N; ++i) {
13937 unsigned Idx = RMask[i];
13938 if (Idx < N)
13939 RMask[i] += N;
13940 else if (Idx < 2*N)
13941 RMask[i] -= N;
13942 }
13943
13944 // At this point LHS and RHS are equivalent to
13945 // LHS = VECTOR_SHUFFLE A, B, LMask
13946 // RHS = VECTOR_SHUFFLE A, B, RMask
13947 // Check that the masks correspond to performing a horizontal operation.
13948 for (unsigned i = 0; i != N; ++i) {
13949 unsigned LIdx = LMask[i], RIdx = RMask[i];
13950
13951 // Ignore any UNDEF components.
13952 if (LIdx >= 2*N || RIdx >= 2*N || (!A.getNode() && (LIdx < N || RIdx < N))
13953 || (!B.getNode() && (LIdx >= N || RIdx >= N)))
13954 continue;
13955
13956 // Check that successive elements are being operated on. If not, this is
13957 // not a horizontal operation.
13958 if (!(LIdx == 2*i && RIdx == 2*i + 1) &&
13959 !(isCommutative && LIdx == 2*i + 1 && RIdx == 2*i))
13960 return false;
13961 }
13962
13963 LHS = A.getNode() ? A : B; // If A is 'UNDEF', use B for it.
13964 RHS = B.getNode() ? B : A; // If B is 'UNDEF', use A for it.
13965 return true;
13966}
13967
13968/// PerformFADDCombine - Do target-specific dag combines on floating point adds.
13969static SDValue PerformFADDCombine(SDNode *N, SelectionDAG &DAG,
13970 const X86Subtarget *Subtarget) {
13971 EVT VT = N->getValueType(0);
13972 SDValue LHS = N->getOperand(0);
13973 SDValue RHS = N->getOperand(1);
13974
13975 // Try to synthesize horizontal adds from adds of shuffles.
13976 if ((Subtarget->hasSSE3() || Subtarget->hasAVX()) &&
13977 (VT == MVT::v4f32 || VT == MVT::v2f64) &&
13978 isHorizontalBinOp(LHS, RHS, true))
13979 return DAG.getNode(X86ISD::FHADD, N->getDebugLoc(), VT, LHS, RHS);
13980 return SDValue();
13981}
13982
13983/// PerformFSUBCombine - Do target-specific dag combines on floating point subs.
13984static SDValue PerformFSUBCombine(SDNode *N, SelectionDAG &DAG,
13985 const X86Subtarget *Subtarget) {
13986 EVT VT = N->getValueType(0);
13987 SDValue LHS = N->getOperand(0);
13988 SDValue RHS = N->getOperand(1);
13989
13990 // Try to synthesize horizontal subs from subs of shuffles.
13991 if ((Subtarget->hasSSE3() || Subtarget->hasAVX()) &&
13992 (VT == MVT::v4f32 || VT == MVT::v2f64) &&
13993 isHorizontalBinOp(LHS, RHS, false))
13994 return DAG.getNode(X86ISD::FHSUB, N->getDebugLoc(), VT, LHS, RHS);
13995 return SDValue();
13996}
13997
Chris Lattner6cf73262008-01-25 06:14:17 +000013998/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
13999/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000014000static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000014001 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
14002 // F[X]OR(0.0, x) -> x
14003 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000014004 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
14005 if (C->getValueAPF().isPosZero())
14006 return N->getOperand(1);
14007 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
14008 if (C->getValueAPF().isPosZero())
14009 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000014010 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000014011}
14012
14013/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000014014static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000014015 // FAND(0.0, x) -> 0.0
14016 // FAND(x, 0.0) -> 0.0
14017 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
14018 if (C->getValueAPF().isPosZero())
14019 return N->getOperand(0);
14020 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
14021 if (C->getValueAPF().isPosZero())
14022 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000014023 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000014024}
14025
Dan Gohmane5af2d32009-01-29 01:59:02 +000014026static SDValue PerformBTCombine(SDNode *N,
14027 SelectionDAG &DAG,
14028 TargetLowering::DAGCombinerInfo &DCI) {
14029 // BT ignores high bits in the bit index operand.
14030 SDValue Op1 = N->getOperand(1);
14031 if (Op1.hasOneUse()) {
14032 unsigned BitWidth = Op1.getValueSizeInBits();
14033 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
14034 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014035 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
14036 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000014037 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000014038 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
14039 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
14040 DCI.CommitTargetLoweringOpt(TLO);
14041 }
14042 return SDValue();
14043}
Chris Lattner83e6c992006-10-04 06:57:07 +000014044
Eli Friedman7a5e5552009-06-07 06:52:44 +000014045static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
14046 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000014047 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000014048 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000014049 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000014050 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000014051 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000014052 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000014053 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000014054 }
14055 return SDValue();
14056}
14057
Evan Cheng2e489c42009-12-16 00:53:11 +000014058static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
14059 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
14060 // (and (i32 x86isd::setcc_carry), 1)
14061 // This eliminates the zext. This transformation is necessary because
14062 // ISD::SETCC is always legalized to i8.
14063 DebugLoc dl = N->getDebugLoc();
14064 SDValue N0 = N->getOperand(0);
14065 EVT VT = N->getValueType(0);
14066 if (N0.getOpcode() == ISD::AND &&
14067 N0.hasOneUse() &&
14068 N0.getOperand(0).hasOneUse()) {
14069 SDValue N00 = N0.getOperand(0);
14070 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
14071 return SDValue();
14072 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
14073 if (!C || C->getZExtValue() != 1)
14074 return SDValue();
14075 return DAG.getNode(ISD::AND, dl, VT,
14076 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
14077 N00.getOperand(0), N00.getOperand(1)),
14078 DAG.getConstant(1, VT));
14079 }
14080
14081 return SDValue();
14082}
14083
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014084// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
14085static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
14086 unsigned X86CC = N->getConstantOperandVal(0);
14087 SDValue EFLAG = N->getOperand(1);
14088 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014089
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014090 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
14091 // a zext and produces an all-ones bit which is more useful than 0/1 in some
14092 // cases.
14093 if (X86CC == X86::COND_B)
14094 return DAG.getNode(ISD::AND, DL, MVT::i8,
14095 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
14096 DAG.getConstant(X86CC, MVT::i8), EFLAG),
14097 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014098
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014099 return SDValue();
14100}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014101
Benjamin Kramer1396c402011-06-18 11:09:41 +000014102static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
14103 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000014104 SDValue Op0 = N->getOperand(0);
14105 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
14106 // a 32-bit target where SSE doesn't support i64->FP operations.
14107 if (Op0.getOpcode() == ISD::LOAD) {
14108 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
14109 EVT VT = Ld->getValueType(0);
14110 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
14111 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
14112 !XTLI->getSubtarget()->is64Bit() &&
14113 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000014114 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
14115 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000014116 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
14117 return FILDChain;
14118 }
14119 }
14120 return SDValue();
14121}
14122
Chris Lattner23a01992010-12-20 01:37:09 +000014123// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
14124static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
14125 X86TargetLowering::DAGCombinerInfo &DCI) {
14126 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
14127 // the result is either zero or one (depending on the input carry bit).
14128 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
14129 if (X86::isZeroNode(N->getOperand(0)) &&
14130 X86::isZeroNode(N->getOperand(1)) &&
14131 // We don't have a good way to replace an EFLAGS use, so only do this when
14132 // dead right now.
14133 SDValue(N, 1).use_empty()) {
14134 DebugLoc DL = N->getDebugLoc();
14135 EVT VT = N->getValueType(0);
14136 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
14137 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
14138 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
14139 DAG.getConstant(X86::COND_B,MVT::i8),
14140 N->getOperand(2)),
14141 DAG.getConstant(1, VT));
14142 return DCI.CombineTo(N, Res1, CarryOut);
14143 }
14144
14145 return SDValue();
14146}
14147
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014148// fold (add Y, (sete X, 0)) -> adc 0, Y
14149// (add Y, (setne X, 0)) -> sbb -1, Y
14150// (sub (sete X, 0), Y) -> sbb 0, Y
14151// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014152static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014153 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014154
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014155 // Look through ZExts.
14156 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
14157 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
14158 return SDValue();
14159
14160 SDValue SetCC = Ext.getOperand(0);
14161 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
14162 return SDValue();
14163
14164 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
14165 if (CC != X86::COND_E && CC != X86::COND_NE)
14166 return SDValue();
14167
14168 SDValue Cmp = SetCC.getOperand(1);
14169 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000014170 !X86::isZeroNode(Cmp.getOperand(1)) ||
14171 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014172 return SDValue();
14173
14174 SDValue CmpOp0 = Cmp.getOperand(0);
14175 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
14176 DAG.getConstant(1, CmpOp0.getValueType()));
14177
14178 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
14179 if (CC == X86::COND_NE)
14180 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
14181 DL, OtherVal.getValueType(), OtherVal,
14182 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
14183 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
14184 DL, OtherVal.getValueType(), OtherVal,
14185 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
14186}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014187
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014188static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG) {
14189 SDValue Op0 = N->getOperand(0);
14190 SDValue Op1 = N->getOperand(1);
14191
14192 // X86 can't encode an immediate LHS of a sub. See if we can push the
14193 // negation into a preceding instruction.
14194 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014195 // If the RHS of the sub is a XOR with one use and a constant, invert the
14196 // immediate. Then add one to the LHS of the sub so we can turn
14197 // X-Y -> X+~Y+1, saving one register.
14198 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
14199 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000014200 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014201 EVT VT = Op0.getValueType();
14202 SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
14203 Op1.getOperand(0),
14204 DAG.getConstant(~XorC, VT));
14205 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000014206 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014207 }
14208 }
14209
14210 return OptimizeConditionalInDecrement(N, DAG);
14211}
14212
Dan Gohman475871a2008-07-27 21:46:04 +000014213SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000014214 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000014215 SelectionDAG &DAG = DCI.DAG;
14216 switch (N->getOpcode()) {
14217 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014218 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014219 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Duncan Sands6bcd2192011-09-17 16:49:39 +000014220 case ISD::VSELECT:
Chris Lattneraf723b92008-01-25 05:46:26 +000014221 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000014222 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014223 case ISD::ADD: return OptimizeConditionalInDecrement(N, DAG);
14224 case ISD::SUB: return PerformSubCombine(N, DAG);
Chris Lattner23a01992010-12-20 01:37:09 +000014225 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000014226 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000014227 case ISD::SHL:
14228 case ISD::SRA:
14229 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000014230 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000014231 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Craig Topperb4c94572011-10-21 06:55:01 +000014232 case ISD::XOR: return PerformXorCombine(N, DAG, DCI, Subtarget);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014233 case ISD::LOAD: return PerformLOADCombine(N, DAG, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000014234 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000014235 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Duncan Sands17470be2011-09-22 20:15:48 +000014236 case ISD::FADD: return PerformFADDCombine(N, DAG, Subtarget);
14237 case ISD::FSUB: return PerformFSUBCombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000014238 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000014239 case X86ISD::FOR: return PerformFORCombine(N, DAG);
14240 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000014241 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000014242 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000014243 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014244 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014245 case X86ISD::SHUFPS: // Handle all target specific shuffles
14246 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000014247 case X86ISD::PALIGN:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014248 case X86ISD::PUNPCKHBW:
14249 case X86ISD::PUNPCKHWD:
14250 case X86ISD::PUNPCKHDQ:
14251 case X86ISD::PUNPCKHQDQ:
14252 case X86ISD::UNPCKHPS:
14253 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +000014254 case X86ISD::VUNPCKHPSY:
14255 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014256 case X86ISD::PUNPCKLBW:
14257 case X86ISD::PUNPCKLWD:
14258 case X86ISD::PUNPCKLDQ:
14259 case X86ISD::PUNPCKLQDQ:
14260 case X86ISD::UNPCKLPS:
14261 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +000014262 case X86ISD::VUNPCKLPSY:
14263 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014264 case X86ISD::MOVHLPS:
14265 case X86ISD::MOVLHPS:
14266 case X86ISD::PSHUFD:
14267 case X86ISD::PSHUFHW:
14268 case X86ISD::PSHUFLW:
14269 case X86ISD::MOVSS:
14270 case X86ISD::MOVSD:
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +000014271 case X86ISD::VPERMILPS:
14272 case X86ISD::VPERMILPSY:
14273 case X86ISD::VPERMILPD:
14274 case X86ISD::VPERMILPDY:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +000014275 case X86ISD::VPERM2F128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014276 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000014277 }
14278
Dan Gohman475871a2008-07-27 21:46:04 +000014279 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000014280}
14281
Evan Chenge5b51ac2010-04-17 06:13:15 +000014282/// isTypeDesirableForOp - Return true if the target has native support for
14283/// the specified value type and it is 'desirable' to use the type for the
14284/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
14285/// instruction encodings are longer and some i16 instructions are slow.
14286bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
14287 if (!isTypeLegal(VT))
14288 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014289 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000014290 return true;
14291
14292 switch (Opc) {
14293 default:
14294 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000014295 case ISD::LOAD:
14296 case ISD::SIGN_EXTEND:
14297 case ISD::ZERO_EXTEND:
14298 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000014299 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000014300 case ISD::SRL:
14301 case ISD::SUB:
14302 case ISD::ADD:
14303 case ISD::MUL:
14304 case ISD::AND:
14305 case ISD::OR:
14306 case ISD::XOR:
14307 return false;
14308 }
14309}
14310
14311/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000014312/// beneficial for dag combiner to promote the specified node. If true, it
14313/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000014314bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000014315 EVT VT = Op.getValueType();
14316 if (VT != MVT::i16)
14317 return false;
14318
Evan Cheng4c26e932010-04-19 19:29:22 +000014319 bool Promote = false;
14320 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014321 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000014322 default: break;
14323 case ISD::LOAD: {
14324 LoadSDNode *LD = cast<LoadSDNode>(Op);
14325 // If the non-extending load has a single use and it's not live out, then it
14326 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014327 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
14328 Op.hasOneUse()*/) {
14329 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
14330 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
14331 // The only case where we'd want to promote LOAD (rather then it being
14332 // promoted as an operand is when it's only use is liveout.
14333 if (UI->getOpcode() != ISD::CopyToReg)
14334 return false;
14335 }
14336 }
Evan Cheng4c26e932010-04-19 19:29:22 +000014337 Promote = true;
14338 break;
14339 }
14340 case ISD::SIGN_EXTEND:
14341 case ISD::ZERO_EXTEND:
14342 case ISD::ANY_EXTEND:
14343 Promote = true;
14344 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014345 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014346 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000014347 SDValue N0 = Op.getOperand(0);
14348 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000014349 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000014350 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000014351 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014352 break;
14353 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000014354 case ISD::ADD:
14355 case ISD::MUL:
14356 case ISD::AND:
14357 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000014358 case ISD::XOR:
14359 Commute = true;
14360 // fallthrough
14361 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000014362 SDValue N0 = Op.getOperand(0);
14363 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000014364 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014365 return false;
14366 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000014367 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014368 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000014369 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014370 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000014371 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014372 }
14373 }
14374
14375 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000014376 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014377}
14378
Evan Cheng60c07e12006-07-05 22:17:51 +000014379//===----------------------------------------------------------------------===//
14380// X86 Inline Assembly Support
14381//===----------------------------------------------------------------------===//
14382
Chris Lattnerb8105652009-07-20 17:51:36 +000014383bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
14384 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000014385
14386 std::string AsmStr = IA->getAsmString();
14387
14388 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000014389 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000014390 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000014391
14392 switch (AsmPieces.size()) {
14393 default: return false;
14394 case 1:
14395 AsmStr = AsmPieces[0];
14396 AsmPieces.clear();
14397 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
14398
Chris Lattner7a2bdde2011-04-15 05:18:47 +000014399 // FIXME: this should verify that we are targeting a 486 or better. If not,
Evan Cheng55d42002011-01-08 01:24:27 +000014400 // we will turn this bswap into something that will be lowered to logical ops
14401 // instead of emitting the bswap asm. For now, we don't support 486 or lower
14402 // so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000014403 // bswap $0
14404 if (AsmPieces.size() == 2 &&
14405 (AsmPieces[0] == "bswap" ||
14406 AsmPieces[0] == "bswapq" ||
14407 AsmPieces[0] == "bswapl") &&
14408 (AsmPieces[1] == "$0" ||
14409 AsmPieces[1] == "${0:q}")) {
14410 // No need to check constraints, nothing other than the equivalent of
14411 // "=r,0" would be valid here.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014412 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014413 if (!Ty || Ty->getBitWidth() % 16 != 0)
14414 return false;
14415 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000014416 }
14417 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000014418 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000014419 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000014420 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000014421 AsmPieces[1] == "$$8," &&
14422 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000014423 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
14424 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014425 const std::string &ConstraintsStr = IA->getConstraintString();
14426 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000014427 std::sort(AsmPieces.begin(), AsmPieces.end());
14428 if (AsmPieces.size() == 4 &&
14429 AsmPieces[0] == "~{cc}" &&
14430 AsmPieces[1] == "~{dirflag}" &&
14431 AsmPieces[2] == "~{flags}" &&
14432 AsmPieces[3] == "~{fpsr}") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014433 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014434 if (!Ty || Ty->getBitWidth() % 16 != 0)
14435 return false;
14436 return IntrinsicLowering::LowerToByteSwap(CI);
Dan Gohman0ef701e2010-03-04 19:58:08 +000014437 }
Chris Lattnerb8105652009-07-20 17:51:36 +000014438 }
14439 break;
14440 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000014441 if (CI->getType()->isIntegerTy(32) &&
14442 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
14443 SmallVector<StringRef, 4> Words;
14444 SplitString(AsmPieces[0], Words, " \t,");
14445 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
14446 Words[2] == "${0:w}") {
14447 Words.clear();
14448 SplitString(AsmPieces[1], Words, " \t,");
14449 if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
14450 Words[2] == "$0") {
14451 Words.clear();
14452 SplitString(AsmPieces[2], Words, " \t,");
14453 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
14454 Words[2] == "${0:w}") {
14455 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014456 const std::string &ConstraintsStr = IA->getConstraintString();
14457 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Peter Collingbourne948cf022010-11-13 19:54:30 +000014458 std::sort(AsmPieces.begin(), AsmPieces.end());
14459 if (AsmPieces.size() == 4 &&
14460 AsmPieces[0] == "~{cc}" &&
14461 AsmPieces[1] == "~{dirflag}" &&
14462 AsmPieces[2] == "~{flags}" &&
14463 AsmPieces[3] == "~{fpsr}") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014464 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014465 if (!Ty || Ty->getBitWidth() % 16 != 0)
14466 return false;
14467 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000014468 }
14469 }
14470 }
14471 }
14472 }
Evan Cheng55d42002011-01-08 01:24:27 +000014473
14474 if (CI->getType()->isIntegerTy(64)) {
14475 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
14476 if (Constraints.size() >= 2 &&
14477 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
14478 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
14479 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
14480 SmallVector<StringRef, 4> Words;
14481 SplitString(AsmPieces[0], Words, " \t");
14482 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
Chris Lattnerb8105652009-07-20 17:51:36 +000014483 Words.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014484 SplitString(AsmPieces[1], Words, " \t");
14485 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
14486 Words.clear();
14487 SplitString(AsmPieces[2], Words, " \t,");
14488 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
14489 Words[2] == "%edx") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014490 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014491 if (!Ty || Ty->getBitWidth() % 16 != 0)
14492 return false;
14493 return IntrinsicLowering::LowerToByteSwap(CI);
14494 }
Chris Lattnerb8105652009-07-20 17:51:36 +000014495 }
14496 }
14497 }
14498 }
14499 break;
14500 }
14501 return false;
14502}
14503
14504
14505
Chris Lattnerf4dff842006-07-11 02:54:03 +000014506/// getConstraintType - Given a constraint letter, return the type of
14507/// constraint it is for this target.
14508X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000014509X86TargetLowering::getConstraintType(const std::string &Constraint) const {
14510 if (Constraint.size() == 1) {
14511 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000014512 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000014513 case 'q':
14514 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000014515 case 'f':
14516 case 't':
14517 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000014518 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000014519 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000014520 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000014521 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000014522 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000014523 case 'a':
14524 case 'b':
14525 case 'c':
14526 case 'd':
14527 case 'S':
14528 case 'D':
14529 case 'A':
14530 return C_Register;
14531 case 'I':
14532 case 'J':
14533 case 'K':
14534 case 'L':
14535 case 'M':
14536 case 'N':
14537 case 'G':
14538 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000014539 case 'e':
14540 case 'Z':
14541 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000014542 default:
14543 break;
14544 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000014545 }
Chris Lattner4234f572007-03-25 02:14:49 +000014546 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000014547}
14548
John Thompson44ab89e2010-10-29 17:29:13 +000014549/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000014550/// This object must already have been set up with the operand type
14551/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000014552TargetLowering::ConstraintWeight
14553 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000014554 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000014555 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014556 Value *CallOperandVal = info.CallOperandVal;
14557 // If we don't have a value, we can't do a match,
14558 // but allow it at the lowest weight.
14559 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000014560 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014561 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000014562 // Look at the constraint type.
14563 switch (*constraint) {
14564 default:
John Thompson44ab89e2010-10-29 17:29:13 +000014565 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
14566 case 'R':
14567 case 'q':
14568 case 'Q':
14569 case 'a':
14570 case 'b':
14571 case 'c':
14572 case 'd':
14573 case 'S':
14574 case 'D':
14575 case 'A':
14576 if (CallOperandVal->getType()->isIntegerTy())
14577 weight = CW_SpecificReg;
14578 break;
14579 case 'f':
14580 case 't':
14581 case 'u':
14582 if (type->isFloatingPointTy())
14583 weight = CW_SpecificReg;
14584 break;
14585 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000014586 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000014587 weight = CW_SpecificReg;
14588 break;
14589 case 'x':
14590 case 'Y':
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014591 if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
John Thompson44ab89e2010-10-29 17:29:13 +000014592 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014593 break;
14594 case 'I':
14595 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
14596 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000014597 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014598 }
14599 break;
John Thompson44ab89e2010-10-29 17:29:13 +000014600 case 'J':
14601 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14602 if (C->getZExtValue() <= 63)
14603 weight = CW_Constant;
14604 }
14605 break;
14606 case 'K':
14607 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14608 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
14609 weight = CW_Constant;
14610 }
14611 break;
14612 case 'L':
14613 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14614 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
14615 weight = CW_Constant;
14616 }
14617 break;
14618 case 'M':
14619 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14620 if (C->getZExtValue() <= 3)
14621 weight = CW_Constant;
14622 }
14623 break;
14624 case 'N':
14625 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14626 if (C->getZExtValue() <= 0xff)
14627 weight = CW_Constant;
14628 }
14629 break;
14630 case 'G':
14631 case 'C':
14632 if (dyn_cast<ConstantFP>(CallOperandVal)) {
14633 weight = CW_Constant;
14634 }
14635 break;
14636 case 'e':
14637 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14638 if ((C->getSExtValue() >= -0x80000000LL) &&
14639 (C->getSExtValue() <= 0x7fffffffLL))
14640 weight = CW_Constant;
14641 }
14642 break;
14643 case 'Z':
14644 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14645 if (C->getZExtValue() <= 0xffffffff)
14646 weight = CW_Constant;
14647 }
14648 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014649 }
14650 return weight;
14651}
14652
Dale Johannesenba2a0b92008-01-29 02:21:21 +000014653/// LowerXConstraint - try to replace an X constraint, which matches anything,
14654/// with another that has more specific requirements based on the type of the
14655/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000014656const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000014657LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000014658 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
14659 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000014660 if (ConstraintVT.isFloatingPoint()) {
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014661 if (Subtarget->hasXMMInt())
Chris Lattner5e764232008-04-26 23:02:14 +000014662 return "Y";
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014663 if (Subtarget->hasXMM())
Chris Lattner5e764232008-04-26 23:02:14 +000014664 return "x";
14665 }
Scott Michelfdc40a02009-02-17 22:15:04 +000014666
Chris Lattner5e764232008-04-26 23:02:14 +000014667 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000014668}
14669
Chris Lattner48884cd2007-08-25 00:47:38 +000014670/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
14671/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000014672void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000014673 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000014674 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000014675 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000014676 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000014677
Eric Christopher100c8332011-06-02 23:16:42 +000014678 // Only support length 1 constraints for now.
14679 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000014680
Eric Christopher100c8332011-06-02 23:16:42 +000014681 char ConstraintLetter = Constraint[0];
14682 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000014683 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000014684 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000014685 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000014686 if (C->getZExtValue() <= 31) {
14687 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000014688 break;
14689 }
Devang Patel84f7fd22007-03-17 00:13:28 +000014690 }
Chris Lattner48884cd2007-08-25 00:47:38 +000014691 return;
Evan Cheng364091e2008-09-22 23:57:37 +000014692 case 'J':
14693 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000014694 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000014695 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
14696 break;
14697 }
14698 }
14699 return;
14700 case 'K':
14701 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000014702 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000014703 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
14704 break;
14705 }
14706 }
14707 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000014708 case 'N':
14709 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000014710 if (C->getZExtValue() <= 255) {
14711 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000014712 break;
14713 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000014714 }
Chris Lattner48884cd2007-08-25 00:47:38 +000014715 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000014716 case 'e': {
14717 // 32-bit signed value
14718 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000014719 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
14720 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000014721 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000014722 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000014723 break;
14724 }
14725 // FIXME gcc accepts some relocatable values here too, but only in certain
14726 // memory models; it's complicated.
14727 }
14728 return;
14729 }
14730 case 'Z': {
14731 // 32-bit unsigned value
14732 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000014733 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
14734 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000014735 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
14736 break;
14737 }
14738 }
14739 // FIXME gcc accepts some relocatable values here too, but only in certain
14740 // memory models; it's complicated.
14741 return;
14742 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000014743 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000014744 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000014745 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000014746 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000014747 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000014748 break;
14749 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014750
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000014751 // In any sort of PIC mode addresses need to be computed at runtime by
14752 // adding in a register or some sort of table lookup. These can't
14753 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000014754 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000014755 return;
14756
Chris Lattnerdc43a882007-05-03 16:52:29 +000014757 // If we are in non-pic codegen mode, we allow the address of a global (with
14758 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000014759 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000014760 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000014761
Chris Lattner49921962009-05-08 18:23:14 +000014762 // Match either (GA), (GA+C), (GA+C1+C2), etc.
14763 while (1) {
14764 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
14765 Offset += GA->getOffset();
14766 break;
14767 } else if (Op.getOpcode() == ISD::ADD) {
14768 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
14769 Offset += C->getZExtValue();
14770 Op = Op.getOperand(0);
14771 continue;
14772 }
14773 } else if (Op.getOpcode() == ISD::SUB) {
14774 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
14775 Offset += -C->getZExtValue();
14776 Op = Op.getOperand(0);
14777 continue;
14778 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000014779 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000014780
Chris Lattner49921962009-05-08 18:23:14 +000014781 // Otherwise, this isn't something we can handle, reject it.
14782 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000014783 }
Eric Christopherfd179292009-08-27 18:07:15 +000014784
Dan Gohman46510a72010-04-15 01:51:59 +000014785 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000014786 // If we require an extra load to get this address, as in PIC mode, we
14787 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000014788 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
14789 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000014790 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000014791
Devang Patel0d881da2010-07-06 22:08:15 +000014792 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
14793 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000014794 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000014795 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000014796 }
Scott Michelfdc40a02009-02-17 22:15:04 +000014797
Gabor Greifba36cb52008-08-28 21:40:38 +000014798 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000014799 Ops.push_back(Result);
14800 return;
14801 }
Dale Johannesen1784d162010-06-25 21:55:36 +000014802 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000014803}
14804
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014805std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000014806X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000014807 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000014808 // First, see if this is a constraint that directly corresponds to an LLVM
14809 // register class.
14810 if (Constraint.size() == 1) {
14811 // GCC Constraint Letters
14812 switch (Constraint[0]) {
14813 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000014814 // TODO: Slight differences here in allocation order and leaving
14815 // RIP in the class. Do they matter any more here than they do
14816 // in the normal allocation?
14817 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
14818 if (Subtarget->is64Bit()) {
Nick Lewycky9bf45d02011-07-08 00:19:27 +000014819 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000014820 return std::make_pair(0U, X86::GR32RegisterClass);
14821 else if (VT == MVT::i16)
14822 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000014823 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000014824 return std::make_pair(0U, X86::GR8RegisterClass);
Nick Lewycky9bf45d02011-07-08 00:19:27 +000014825 else if (VT == MVT::i64 || VT == MVT::f64)
Eric Christopherd176af82011-06-29 17:23:50 +000014826 return std::make_pair(0U, X86::GR64RegisterClass);
14827 break;
14828 }
14829 // 32-bit fallthrough
14830 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000014831 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000014832 return std::make_pair(0U, X86::GR32_ABCDRegisterClass);
14833 else if (VT == MVT::i16)
14834 return std::make_pair(0U, X86::GR16_ABCDRegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000014835 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000014836 return std::make_pair(0U, X86::GR8_ABCD_LRegisterClass);
14837 else if (VT == MVT::i64)
14838 return std::make_pair(0U, X86::GR64_ABCDRegisterClass);
14839 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000014840 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000014841 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000014842 if (VT == MVT::i8 || VT == MVT::i1)
Chris Lattner0f65cad2007-04-09 05:49:22 +000014843 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000014844 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000014845 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000014846 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000014847 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000014848 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000014849 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000014850 if (VT == MVT::i8 || VT == MVT::i1)
Dale Johannesen5f3663e2009-10-07 22:47:20 +000014851 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
14852 if (VT == MVT::i16)
14853 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
14854 if (VT == MVT::i32 || !Subtarget->is64Bit())
14855 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
14856 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000014857 case 'f': // FP Stack registers.
14858 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
14859 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000014860 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000014861 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000014862 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000014863 return std::make_pair(0U, X86::RFP64RegisterClass);
14864 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000014865 case 'y': // MMX_REGS if MMX allowed.
14866 if (!Subtarget->hasMMX()) break;
14867 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000014868 case 'Y': // SSE_REGS if SSE2 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014869 if (!Subtarget->hasXMMInt()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000014870 // FALL THROUGH.
14871 case 'x': // SSE_REGS if SSE1 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014872 if (!Subtarget->hasXMM()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000014873
Owen Anderson825b72b2009-08-11 20:47:22 +000014874 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000014875 default: break;
14876 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000014877 case MVT::f32:
14878 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000014879 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000014880 case MVT::f64:
14881 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000014882 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000014883 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000014884 case MVT::v16i8:
14885 case MVT::v8i16:
14886 case MVT::v4i32:
14887 case MVT::v2i64:
14888 case MVT::v4f32:
14889 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000014890 return std::make_pair(0U, X86::VR128RegisterClass);
14891 }
Chris Lattnerad043e82007-04-09 05:11:28 +000014892 break;
14893 }
14894 }
Scott Michelfdc40a02009-02-17 22:15:04 +000014895
Chris Lattnerf76d1802006-07-31 23:26:50 +000014896 // Use the default implementation in TargetLowering to convert the register
14897 // constraint into a member of a register class.
14898 std::pair<unsigned, const TargetRegisterClass*> Res;
14899 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000014900
14901 // Not found as a standard register?
14902 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000014903 // Map st(0) -> st(7) -> ST0
14904 if (Constraint.size() == 7 && Constraint[0] == '{' &&
14905 tolower(Constraint[1]) == 's' &&
14906 tolower(Constraint[2]) == 't' &&
14907 Constraint[3] == '(' &&
14908 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
14909 Constraint[5] == ')' &&
14910 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000014911
Chris Lattner56d77c72009-09-13 22:41:48 +000014912 Res.first = X86::ST0+Constraint[4]-'0';
14913 Res.second = X86::RFP80RegisterClass;
14914 return Res;
14915 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000014916
Chris Lattner56d77c72009-09-13 22:41:48 +000014917 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000014918 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000014919 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000014920 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000014921 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000014922 }
Chris Lattner56d77c72009-09-13 22:41:48 +000014923
14924 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000014925 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000014926 Res.first = X86::EFLAGS;
14927 Res.second = X86::CCRRegisterClass;
14928 return Res;
14929 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000014930
Dale Johannesen330169f2008-11-13 21:52:36 +000014931 // 'A' means EAX + EDX.
14932 if (Constraint == "A") {
14933 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000014934 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000014935 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000014936 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000014937 return Res;
14938 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014939
Chris Lattnerf76d1802006-07-31 23:26:50 +000014940 // Otherwise, check to see if this is a register class of the wrong value
14941 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
14942 // turn into {ax},{dx}.
14943 if (Res.second->hasType(VT))
14944 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014945
Chris Lattnerf76d1802006-07-31 23:26:50 +000014946 // All of the single-register GCC register classes map their values onto
14947 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
14948 // really want an 8-bit or 32-bit register, map to the appropriate register
14949 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000014950 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000014951 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000014952 unsigned DestReg = 0;
14953 switch (Res.first) {
14954 default: break;
14955 case X86::AX: DestReg = X86::AL; break;
14956 case X86::DX: DestReg = X86::DL; break;
14957 case X86::CX: DestReg = X86::CL; break;
14958 case X86::BX: DestReg = X86::BL; break;
14959 }
14960 if (DestReg) {
14961 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000014962 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000014963 }
Owen Anderson825b72b2009-08-11 20:47:22 +000014964 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000014965 unsigned DestReg = 0;
14966 switch (Res.first) {
14967 default: break;
14968 case X86::AX: DestReg = X86::EAX; break;
14969 case X86::DX: DestReg = X86::EDX; break;
14970 case X86::CX: DestReg = X86::ECX; break;
14971 case X86::BX: DestReg = X86::EBX; break;
14972 case X86::SI: DestReg = X86::ESI; break;
14973 case X86::DI: DestReg = X86::EDI; break;
14974 case X86::BP: DestReg = X86::EBP; break;
14975 case X86::SP: DestReg = X86::ESP; break;
14976 }
14977 if (DestReg) {
14978 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000014979 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000014980 }
Owen Anderson825b72b2009-08-11 20:47:22 +000014981 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000014982 unsigned DestReg = 0;
14983 switch (Res.first) {
14984 default: break;
14985 case X86::AX: DestReg = X86::RAX; break;
14986 case X86::DX: DestReg = X86::RDX; break;
14987 case X86::CX: DestReg = X86::RCX; break;
14988 case X86::BX: DestReg = X86::RBX; break;
14989 case X86::SI: DestReg = X86::RSI; break;
14990 case X86::DI: DestReg = X86::RDI; break;
14991 case X86::BP: DestReg = X86::RBP; break;
14992 case X86::SP: DestReg = X86::RSP; break;
14993 }
14994 if (DestReg) {
14995 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000014996 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000014997 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000014998 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000014999 } else if (Res.second == X86::FR32RegisterClass ||
15000 Res.second == X86::FR64RegisterClass ||
15001 Res.second == X86::VR128RegisterClass) {
15002 // Handle references to XMM physical registers that got mapped into the
15003 // wrong class. This can happen with constraints like {xmm0} where the
15004 // target independent register mapper will just pick the first match it can
15005 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000015006 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000015007 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000015008 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000015009 Res.second = X86::FR64RegisterClass;
15010 else if (X86::VR128RegisterClass->hasType(VT))
15011 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000015012 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015013
Chris Lattnerf76d1802006-07-31 23:26:50 +000015014 return Res;
15015}