blob: f1b160cdfae756248c1c6a1569e8a98e1be57fde [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);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001158
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001159 computeRegisterProperties();
1160
Evan Cheng05219282011-01-06 06:52:41 +00001161 // On Darwin, -Os means optimize for size without hurting performance,
1162 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001163 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001164 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001165 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001166 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1167 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1168 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengfb8075d2008-02-28 00:43:03 +00001169 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001170 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001171
1172 setPrefFunctionAlignment(4);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001173}
1174
Scott Michel5b8f82e2008-03-10 15:42:14 +00001175
Duncan Sands28b77e92011-09-06 19:07:46 +00001176EVT X86TargetLowering::getSetCCResultType(EVT VT) const {
1177 if (!VT.isVector()) return MVT::i8;
1178 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +00001179}
1180
1181
Evan Cheng29286502008-01-23 23:17:41 +00001182/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1183/// the desired ByVal argument alignment.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001184static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
Evan Cheng29286502008-01-23 23:17:41 +00001185 if (MaxAlign == 16)
1186 return;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001187 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001188 if (VTy->getBitWidth() == 128)
1189 MaxAlign = 16;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001190 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001191 unsigned EltAlign = 0;
1192 getMaxByValAlign(ATy->getElementType(), EltAlign);
1193 if (EltAlign > MaxAlign)
1194 MaxAlign = EltAlign;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001195 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001196 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1197 unsigned EltAlign = 0;
1198 getMaxByValAlign(STy->getElementType(i), EltAlign);
1199 if (EltAlign > MaxAlign)
1200 MaxAlign = EltAlign;
1201 if (MaxAlign == 16)
1202 break;
1203 }
1204 }
1205 return;
1206}
1207
1208/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1209/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001210/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1211/// are at 4-byte boundaries.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001212unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001213 if (Subtarget->is64Bit()) {
1214 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001215 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001216 if (TyAlign > 8)
1217 return TyAlign;
1218 return 8;
1219 }
1220
Evan Cheng29286502008-01-23 23:17:41 +00001221 unsigned Align = 4;
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001222 if (Subtarget->hasXMM())
Dale Johannesen0c191872008-02-08 19:48:20 +00001223 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001224 return Align;
1225}
Chris Lattner2b02a442007-02-25 08:29:00 +00001226
Evan Chengf0df0312008-05-15 08:39:06 +00001227/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001228/// and store operations as a result of memset, memcpy, and memmove
1229/// lowering. If DstAlign is zero that means it's safe to destination
1230/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1231/// means there isn't a need to check it against alignment requirement,
1232/// probably because the source does not need to be loaded. If
1233/// 'NonScalarIntSafe' is true, that means it's safe to return a
1234/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1235/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1236/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001237/// It returns EVT::Other if the type should be determined using generic
1238/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001239EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001240X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1241 unsigned DstAlign, unsigned SrcAlign,
Evan Chengf28f8bc2010-04-02 19:36:14 +00001242 bool NonScalarIntSafe,
Evan Chengc3b0c342010-04-08 07:37:57 +00001243 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001244 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001245 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1246 // linux. This is because the stack realignment code can't handle certain
1247 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001248 const Function *F = MF.getFunction();
Evan Chenga5e13622011-01-07 19:35:30 +00001249 if (NonScalarIntSafe &&
1250 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001251 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001252 (Subtarget->isUnalignedMemAccessFast() ||
1253 ((DstAlign == 0 || DstAlign >= 16) &&
1254 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001255 Subtarget->getStackAlignment() >= 16) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00001256 if (Subtarget->hasAVX() &&
1257 Subtarget->getStackAlignment() >= 32)
1258 return MVT::v8f32;
1259 if (Subtarget->hasXMMInt())
Evan Cheng255f20f2010-04-01 06:04:33 +00001260 return MVT::v4i32;
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00001261 if (Subtarget->hasXMM())
Evan Cheng255f20f2010-04-01 06:04:33 +00001262 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001263 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001264 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001265 Subtarget->getStackAlignment() >= 8 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001266 Subtarget->hasXMMInt()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001267 // Do not use f64 to lower memcpy if source is string constant. It's
1268 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001269 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001270 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001271 }
Evan Chengf0df0312008-05-15 08:39:06 +00001272 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001273 return MVT::i64;
1274 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001275}
1276
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001277/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1278/// current function. The returned value is a member of the
1279/// MachineJumpTableInfo::JTEntryKind enum.
1280unsigned X86TargetLowering::getJumpTableEncoding() const {
1281 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1282 // symbol.
1283 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1284 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001285 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001286
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001287 // Otherwise, use the normal jump table encoding heuristics.
1288 return TargetLowering::getJumpTableEncoding();
1289}
1290
Chris Lattnerc64daab2010-01-26 05:02:42 +00001291const MCExpr *
1292X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1293 const MachineBasicBlock *MBB,
1294 unsigned uid,MCContext &Ctx) const{
1295 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1296 Subtarget->isPICStyleGOT());
1297 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1298 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001299 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1300 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001301}
1302
Evan Chengcc415862007-11-09 01:32:10 +00001303/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1304/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001305SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001306 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001307 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001308 // This doesn't have DebugLoc associated with it, but is not really the
1309 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001310 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001311 return Table;
1312}
1313
Chris Lattner589c6f62010-01-26 06:28:43 +00001314/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1315/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1316/// MCExpr.
1317const MCExpr *X86TargetLowering::
1318getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1319 MCContext &Ctx) const {
1320 // X86-64 uses RIP relative addressing based on the jump table label.
1321 if (Subtarget->isPICStyleRIPRel())
1322 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1323
1324 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001325 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001326}
1327
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001328// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001329std::pair<const TargetRegisterClass*, uint8_t>
1330X86TargetLowering::findRepresentativeClass(EVT VT) const{
1331 const TargetRegisterClass *RRC = 0;
1332 uint8_t Cost = 1;
1333 switch (VT.getSimpleVT().SimpleTy) {
1334 default:
1335 return TargetLowering::findRepresentativeClass(VT);
1336 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
1337 RRC = (Subtarget->is64Bit()
1338 ? X86::GR64RegisterClass : X86::GR32RegisterClass);
1339 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001340 case MVT::x86mmx:
Evan Chengdee81012010-07-26 21:50:05 +00001341 RRC = X86::VR64RegisterClass;
1342 break;
1343 case MVT::f32: case MVT::f64:
1344 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1345 case MVT::v4f32: case MVT::v2f64:
1346 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1347 case MVT::v4f64:
1348 RRC = X86::VR128RegisterClass;
1349 break;
1350 }
1351 return std::make_pair(RRC, Cost);
1352}
1353
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001354bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1355 unsigned &Offset) const {
1356 if (!Subtarget->isTargetLinux())
1357 return false;
1358
1359 if (Subtarget->is64Bit()) {
1360 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1361 Offset = 0x28;
1362 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1363 AddressSpace = 256;
1364 else
1365 AddressSpace = 257;
1366 } else {
1367 // %gs:0x14 on i386
1368 Offset = 0x14;
1369 AddressSpace = 256;
1370 }
1371 return true;
1372}
1373
1374
Chris Lattner2b02a442007-02-25 08:29:00 +00001375//===----------------------------------------------------------------------===//
1376// Return Value Calling Convention Implementation
1377//===----------------------------------------------------------------------===//
1378
Chris Lattner59ed56b2007-02-28 04:55:35 +00001379#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001380
Michael J. Spencerec38de22010-10-10 22:04:20 +00001381bool
Eric Christopher471e4222011-06-08 23:55:35 +00001382X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
1383 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001384 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001385 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001386 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001387 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001388 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001389 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001390}
1391
Dan Gohman98ca4f22009-08-05 01:29:28 +00001392SDValue
1393X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001394 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001395 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001396 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001397 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001398 MachineFunction &MF = DAG.getMachineFunction();
1399 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001400
Chris Lattner9774c912007-02-27 05:28:59 +00001401 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001402 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001403 RVLocs, *DAG.getContext());
1404 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001405
Evan Chengdcea1632010-02-04 02:40:39 +00001406 // Add the regs to the liveout set for the function.
1407 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1408 for (unsigned i = 0; i != RVLocs.size(); ++i)
1409 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1410 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001411
Dan Gohman475871a2008-07-27 21:46:04 +00001412 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001413
Dan Gohman475871a2008-07-27 21:46:04 +00001414 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001415 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1416 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001417 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1418 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001419
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001420 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001421 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1422 CCValAssign &VA = RVLocs[i];
1423 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001424 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001425 EVT ValVT = ValToCopy.getValueType();
1426
Dale Johannesenc4510512010-09-24 19:05:48 +00001427 // If this is x86-64, and we disabled SSE, we can't return FP values,
1428 // or SSE or MMX vectors.
1429 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1430 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001431 (Subtarget->is64Bit() && !Subtarget->hasXMM())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001432 report_fatal_error("SSE register return with SSE disabled");
1433 }
1434 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1435 // llvm-gcc has never done it right and no one has noticed, so this
1436 // should be OK for now.
1437 if (ValVT == MVT::f64 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001438 (Subtarget->is64Bit() && !Subtarget->hasXMMInt()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001439 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001440
Chris Lattner447ff682008-03-11 03:23:40 +00001441 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1442 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001443 if (VA.getLocReg() == X86::ST0 ||
1444 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001445 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1446 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001447 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001448 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001449 RetOps.push_back(ValToCopy);
1450 // Don't emit a copytoreg.
1451 continue;
1452 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001453
Evan Cheng242b38b2009-02-23 09:03:22 +00001454 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1455 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001456 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001457 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001458 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001459 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001460 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1461 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001462 // If we don't have SSE2 available, convert to v4f32 so the generated
1463 // register is legal.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00001464 if (!Subtarget->hasXMMInt())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001465 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001466 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001467 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001468 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001469
Dale Johannesendd64c412009-02-04 00:33:20 +00001470 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001471 Flag = Chain.getValue(1);
1472 }
Dan Gohman61a92132008-04-21 23:59:07 +00001473
1474 // The x86-64 ABI for returning structs by value requires that we copy
1475 // the sret argument into %rax for the return. We saved the argument into
1476 // a virtual register in the entry block, so now we copy the value out
1477 // and into %rax.
1478 if (Subtarget->is64Bit() &&
1479 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1480 MachineFunction &MF = DAG.getMachineFunction();
1481 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1482 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001483 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001484 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001485 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001486
Dale Johannesendd64c412009-02-04 00:33:20 +00001487 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001488 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001489
1490 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001491 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001492 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001493
Chris Lattner447ff682008-03-11 03:23:40 +00001494 RetOps[0] = Chain; // Update chain.
1495
1496 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001497 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001498 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001499
1500 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001501 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001502}
1503
Evan Cheng3d2125c2010-11-30 23:55:39 +00001504bool X86TargetLowering::isUsedByReturnOnly(SDNode *N) const {
1505 if (N->getNumValues() != 1)
1506 return false;
1507 if (!N->hasNUsesOfValue(1, 0))
1508 return false;
1509
1510 SDNode *Copy = *N->use_begin();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001511 if (Copy->getOpcode() != ISD::CopyToReg &&
1512 Copy->getOpcode() != ISD::FP_EXTEND)
Evan Cheng3d2125c2010-11-30 23:55:39 +00001513 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001514
1515 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001516 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001517 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001518 if (UI->getOpcode() != X86ISD::RET_FLAG)
1519 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001520 HasRet = true;
1521 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001522
Evan Cheng1bf891a2010-12-01 22:59:46 +00001523 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001524}
1525
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001526EVT
1527X86TargetLowering::getTypeForExtArgOrReturn(LLVMContext &Context, EVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001528 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001529 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001530 // TODO: Is this also valid on 32-bit?
1531 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001532 ReturnMVT = MVT::i8;
1533 else
1534 ReturnMVT = MVT::i32;
1535
1536 EVT MinVT = getRegisterType(Context, ReturnMVT);
1537 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001538}
1539
Dan Gohman98ca4f22009-08-05 01:29:28 +00001540/// LowerCallResult - Lower the result values of a call into the
1541/// appropriate copies out of appropriate physical registers.
1542///
1543SDValue
1544X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001545 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001546 const SmallVectorImpl<ISD::InputArg> &Ins,
1547 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001548 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001549
Chris Lattnere32bbf62007-02-28 07:09:55 +00001550 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001551 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001552 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001553 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1554 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001555 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001556
Chris Lattner3085e152007-02-25 08:59:22 +00001557 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001558 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001559 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001560 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001561
Torok Edwin3f142c32009-02-01 18:15:56 +00001562 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001563 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001564 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasXMM())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001565 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001566 }
1567
Evan Cheng79fb3b42009-02-20 20:43:02 +00001568 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001569
1570 // If this is a call to a function that returns an fp value on the floating
1571 // point stack, we must guarantee the the value is popped from the stack, so
1572 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001573 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001574 // instead.
1575 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1576 // If we prefer to use the value in xmm registers, copy it out as f80 and
1577 // use a truncate to move it from fp stack reg to xmm reg.
1578 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001579 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001580 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1581 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001582 Val = Chain.getValue(0);
1583
1584 // Round the f80 to the right size, which also moves it to the appropriate
1585 // xmm register.
1586 if (CopyVT != VA.getValVT())
1587 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1588 // This truncation won't change the value.
1589 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001590 } else {
1591 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1592 CopyVT, InFlag).getValue(1);
1593 Val = Chain.getValue(0);
1594 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001595 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001596 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001597 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001598
Dan Gohman98ca4f22009-08-05 01:29:28 +00001599 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001600}
1601
1602
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001603//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001604// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001605//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001606// StdCall calling convention seems to be standard for many Windows' API
1607// routines and around. It differs from C calling convention just a little:
1608// callee should clean up the stack, not caller. Symbols should be also
1609// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001610// For info on fast calling convention see Fast Calling Convention (tail call)
1611// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001612
Dan Gohman98ca4f22009-08-05 01:29:28 +00001613/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001614/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001615static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1616 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001617 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001618
Dan Gohman98ca4f22009-08-05 01:29:28 +00001619 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001620}
1621
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001622/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001623/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001624static bool
1625ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1626 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001627 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001628
Dan Gohman98ca4f22009-08-05 01:29:28 +00001629 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001630}
1631
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001632/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1633/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001634/// the specific parameter attribute. The copy will be passed as a byval
1635/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001636static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001637CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001638 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1639 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001640 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001641
Dale Johannesendd64c412009-02-04 00:33:20 +00001642 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001643 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001644 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001645}
1646
Chris Lattner29689432010-03-11 00:22:57 +00001647/// IsTailCallConvention - Return true if the calling convention is one that
1648/// supports tail call optimization.
1649static bool IsTailCallConvention(CallingConv::ID CC) {
1650 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1651}
1652
Evan Cheng485fafc2011-03-21 01:19:09 +00001653bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1654 if (!CI->isTailCall())
1655 return false;
1656
1657 CallSite CS(CI);
1658 CallingConv::ID CalleeCC = CS.getCallingConv();
1659 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1660 return false;
1661
1662 return true;
1663}
1664
Evan Cheng0c439eb2010-01-27 00:07:07 +00001665/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1666/// a tailcall target by changing its ABI.
1667static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001668 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001669}
1670
Dan Gohman98ca4f22009-08-05 01:29:28 +00001671SDValue
1672X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001673 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001674 const SmallVectorImpl<ISD::InputArg> &Ins,
1675 DebugLoc dl, SelectionDAG &DAG,
1676 const CCValAssign &VA,
1677 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001678 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001679 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001680 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001681 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001682 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001683 EVT ValVT;
1684
1685 // If value is passed by pointer we have address passed instead of the value
1686 // itself.
1687 if (VA.getLocInfo() == CCValAssign::Indirect)
1688 ValVT = VA.getLocVT();
1689 else
1690 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001691
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001692 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001693 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001694 // In case of tail call optimization mark all arguments mutable. Since they
1695 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001696 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001697 unsigned Bytes = Flags.getByValSize();
1698 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1699 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001700 return DAG.getFrameIndex(FI, getPointerTy());
1701 } else {
1702 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001703 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001704 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1705 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001706 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00001707 false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001708 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001709}
1710
Dan Gohman475871a2008-07-27 21:46:04 +00001711SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001712X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001713 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001714 bool isVarArg,
1715 const SmallVectorImpl<ISD::InputArg> &Ins,
1716 DebugLoc dl,
1717 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001718 SmallVectorImpl<SDValue> &InVals)
1719 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001720 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001721 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001722
Gordon Henriksen86737662008-01-05 16:56:59 +00001723 const Function* Fn = MF.getFunction();
1724 if (Fn->hasExternalLinkage() &&
1725 Subtarget->isTargetCygMing() &&
1726 Fn->getName() == "main")
1727 FuncInfo->setForceFramePointer(true);
1728
Evan Cheng1bc78042006-04-26 01:20:17 +00001729 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001730 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001731 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001732
Chris Lattner29689432010-03-11 00:22:57 +00001733 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1734 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001735
Chris Lattner638402b2007-02-28 07:00:42 +00001736 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001737 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001738 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001739 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001740
1741 // Allocate shadow area for Win64
1742 if (IsWin64) {
1743 CCInfo.AllocateStack(32, 8);
1744 }
1745
Duncan Sands45907662010-10-31 13:21:44 +00001746 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001747
Chris Lattnerf39f7712007-02-28 05:46:49 +00001748 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001749 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001750 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1751 CCValAssign &VA = ArgLocs[i];
1752 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1753 // places.
1754 assert(VA.getValNo() != LastVal &&
1755 "Don't support value assigned to multiple locs yet");
Duncan Sands17001ce2011-10-18 12:44:00 +00001756 (void)LastVal;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001757 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001758
Chris Lattnerf39f7712007-02-28 05:46:49 +00001759 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001760 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001761 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001762 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001763 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001764 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001765 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001766 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001767 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001768 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001769 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001770 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1771 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001772 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001773 RC = X86::VR128RegisterClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001774 else if (RegVT == MVT::x86mmx)
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001775 RC = X86::VR64RegisterClass;
1776 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001777 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001778
Devang Patel68e6bee2011-02-21 23:21:26 +00001779 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001780 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001781
Chris Lattnerf39f7712007-02-28 05:46:49 +00001782 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1783 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1784 // right size.
1785 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001786 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001787 DAG.getValueType(VA.getValVT()));
1788 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001789 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001790 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001791 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001792 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001793
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001794 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001795 // Handle MMX values passed in XMM regs.
1796 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001797 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1798 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001799 } else
1800 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001801 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001802 } else {
1803 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001804 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001805 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001806
1807 // If value is passed via pointer - do a load.
1808 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001809 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
1810 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001811
Dan Gohman98ca4f22009-08-05 01:29:28 +00001812 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001813 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001814
Dan Gohman61a92132008-04-21 23:59:07 +00001815 // The x86-64 ABI for returning structs by value requires that we copy
1816 // the sret argument into %rax for the return. Save the argument into
1817 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001818 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001819 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1820 unsigned Reg = FuncInfo->getSRetReturnReg();
1821 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001822 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001823 FuncInfo->setSRetReturnReg(Reg);
1824 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001825 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001826 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001827 }
1828
Chris Lattnerf39f7712007-02-28 05:46:49 +00001829 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001830 // Align stack specially for tail calls.
1831 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001832 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001833
Evan Cheng1bc78042006-04-26 01:20:17 +00001834 // If the function takes variable number of arguments, make a frame index for
1835 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001836 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001837 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1838 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001839 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001840 }
1841 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001842 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1843
1844 // FIXME: We should really autogenerate these arrays
1845 static const unsigned GPR64ArgRegsWin64[] = {
1846 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001847 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001848 static const unsigned GPR64ArgRegs64Bit[] = {
1849 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1850 };
1851 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001852 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1853 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1854 };
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001855 const unsigned *GPR64ArgRegs;
1856 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001857
1858 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001859 // The XMM registers which might contain var arg parameters are shadowed
1860 // in their paired GPR. So we only need to save the GPR to their home
1861 // slots.
1862 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001863 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001864 } else {
1865 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1866 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001867
1868 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit, TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001869 }
1870 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1871 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001872
Devang Patel578efa92009-06-05 21:57:13 +00001873 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001874 assert(!(NumXMMRegs && !Subtarget->hasXMM()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001875 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001876 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001877 "SSE register cannot be used when SSE is disabled!");
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001878 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasXMM())
Torok Edwin3f142c32009-02-01 18:15:56 +00001879 // Kernel mode asks for SSE to be disabled, so don't push them
1880 // on the stack.
1881 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001882
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001883 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001884 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001885 // Get to the caller-allocated home save location. Add 8 to account
1886 // for the return address.
1887 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001888 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001889 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001890 // Fixup to set vararg frame on shadow area (4 x i64).
1891 if (NumIntRegs < 4)
1892 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001893 } else {
1894 // For X86-64, if there are vararg parameters that are passed via
1895 // registers, then we must store them to their spots on the stack so they
1896 // may be loaded by deferencing the result of va_next.
1897 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1898 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1899 FuncInfo->setRegSaveFrameIndex(
1900 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00001901 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001902 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001903
Gordon Henriksen86737662008-01-05 16:56:59 +00001904 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001905 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001906 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1907 getPointerTy());
1908 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001909 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001910 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1911 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001912 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001913 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001914 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001915 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001916 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001917 MachinePointerInfo::getFixedStack(
1918 FuncInfo->getRegSaveFrameIndex(), Offset),
1919 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001920 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001921 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001922 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001923
Dan Gohmanface41a2009-08-16 21:24:25 +00001924 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1925 // Now store the XMM (fp + vector) parameter registers.
1926 SmallVector<SDValue, 11> SaveXMMOps;
1927 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001928
Devang Patel68e6bee2011-02-21 23:21:26 +00001929 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001930 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1931 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001932
Dan Gohman1e93df62010-04-17 14:41:14 +00001933 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1934 FuncInfo->getRegSaveFrameIndex()));
1935 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1936 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001937
Dan Gohmanface41a2009-08-16 21:24:25 +00001938 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001939 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001940 X86::VR128RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001941 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1942 SaveXMMOps.push_back(Val);
1943 }
1944 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1945 MVT::Other,
1946 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001947 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001948
1949 if (!MemOps.empty())
1950 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1951 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001952 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001953 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001954
Gordon Henriksen86737662008-01-05 16:56:59 +00001955 // Some CCs need callee pop.
Evan Chengef41ff62011-06-23 17:54:54 +00001956 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg, GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001957 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001958 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001959 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001960 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001961 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001962 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001963 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001964
Gordon Henriksen86737662008-01-05 16:56:59 +00001965 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001966 // RegSaveFrameIndex is X86-64 only.
1967 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001968 if (CallConv == CallingConv::X86_FastCall ||
1969 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001970 // fastcc functions can't have varargs.
1971 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001972 }
Evan Cheng25caf632006-05-23 21:06:34 +00001973
Rafael Espindola76927d752011-08-30 19:39:58 +00001974 FuncInfo->setArgumentStackSize(StackSize);
1975
Dan Gohman98ca4f22009-08-05 01:29:28 +00001976 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001977}
1978
Dan Gohman475871a2008-07-27 21:46:04 +00001979SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001980X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1981 SDValue StackPtr, SDValue Arg,
1982 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001983 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001984 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001985 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001986 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001987 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001988 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00001989 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001990
1991 return DAG.getStore(Chain, dl, Arg, PtrOff,
1992 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00001993 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001994}
1995
Bill Wendling64e87322009-01-16 19:25:27 +00001996/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001997/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001998SDValue
1999X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00002000 SDValue &OutRetAddr, SDValue Chain,
2001 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00002002 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002003 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00002004 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002005 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00002006
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002007 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00002008 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
2009 false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00002010 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002011}
2012
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002013/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002014/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00002015static SDValue
2016EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00002017 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00002018 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002019 // Store the return address to the appropriate stack slot.
2020 if (!FPDiff) return Chain;
2021 // Calculate the new stack slot for the return address.
2022 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00002023 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00002024 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00002025 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00002026 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002027 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00002028 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00002029 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002030 return Chain;
2031}
2032
Dan Gohman98ca4f22009-08-05 01:29:28 +00002033SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00002034X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002035 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00002036 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002037 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002038 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002039 const SmallVectorImpl<ISD::InputArg> &Ins,
2040 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002041 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002042 MachineFunction &MF = DAG.getMachineFunction();
2043 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002044 bool IsWin64 = Subtarget->isTargetWin64();
Dan Gohman98ca4f22009-08-05 01:29:28 +00002045 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00002046 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002047
Evan Cheng5f941932010-02-05 02:21:12 +00002048 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002049 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00002050 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
2051 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00002052 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00002053
2054 // Sibcalls are automatically detected tailcalls which do not require
2055 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00002056 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002057 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002058
2059 if (isTailCall)
2060 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002061 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002062
Chris Lattner29689432010-03-11 00:22:57 +00002063 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
2064 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002065
Chris Lattner638402b2007-02-28 07:00:42 +00002066 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002067 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002068 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002069 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002070
2071 // Allocate shadow area for Win64
2072 if (IsWin64) {
2073 CCInfo.AllocateStack(32, 8);
2074 }
2075
Duncan Sands45907662010-10-31 13:21:44 +00002076 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002077
Chris Lattner423c5f42007-02-28 05:31:48 +00002078 // Get a count of how many bytes are to be pushed on the stack.
2079 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002080 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002081 // This is a sibcall. The memory operands are available in caller's
2082 // own caller's stack.
2083 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00002084 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002085 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002086
Gordon Henriksen86737662008-01-05 16:56:59 +00002087 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002088 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002089 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00002090 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00002091 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2092 FPDiff = NumBytesCallerPushed - NumBytes;
2093
2094 // Set the delta of movement of the returnaddr stackslot.
2095 // But only set if delta is greater than previous delta.
2096 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2097 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2098 }
2099
Evan Chengf22f9b32010-02-06 03:28:46 +00002100 if (!IsSibcall)
2101 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002102
Dan Gohman475871a2008-07-27 21:46:04 +00002103 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002104 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002105 if (isTailCall && FPDiff)
2106 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2107 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002108
Dan Gohman475871a2008-07-27 21:46:04 +00002109 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2110 SmallVector<SDValue, 8> MemOpChains;
2111 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002112
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002113 // Walk the register/memloc assignments, inserting copies/loads. In the case
2114 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002115 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2116 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002117 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002118 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002119 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002120 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002121
Chris Lattner423c5f42007-02-28 05:31:48 +00002122 // Promote the value if needed.
2123 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002124 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002125 case CCValAssign::Full: break;
2126 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002127 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002128 break;
2129 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002130 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002131 break;
2132 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002133 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
2134 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002135 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002136 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2137 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002138 } else
2139 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2140 break;
2141 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002142 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002143 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002144 case CCValAssign::Indirect: {
2145 // Store the argument.
2146 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002147 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002148 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002149 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002150 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002151 Arg = SpillSlot;
2152 break;
2153 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002154 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002155
Chris Lattner423c5f42007-02-28 05:31:48 +00002156 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002157 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2158 if (isVarArg && IsWin64) {
2159 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2160 // shadow reg if callee is a varargs function.
2161 unsigned ShadowReg = 0;
2162 switch (VA.getLocReg()) {
2163 case X86::XMM0: ShadowReg = X86::RCX; break;
2164 case X86::XMM1: ShadowReg = X86::RDX; break;
2165 case X86::XMM2: ShadowReg = X86::R8; break;
2166 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002167 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002168 if (ShadowReg)
2169 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002170 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002171 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002172 assert(VA.isMemLoc());
2173 if (StackPtr.getNode() == 0)
2174 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2175 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2176 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002177 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002178 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002179
Evan Cheng32fe1032006-05-25 00:59:30 +00002180 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002181 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002182 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002183
Evan Cheng347d5f72006-04-28 21:29:37 +00002184 // Build a sequence of copy-to-reg nodes chained together with token chain
2185 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002186 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002187 // Tail call byval lowering might overwrite argument registers so in case of
2188 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002189 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002190 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002191 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002192 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002193 InFlag = Chain.getValue(1);
2194 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002195
Chris Lattner88e1fd52009-07-09 04:24:46 +00002196 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002197 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2198 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002199 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002200 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2201 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00002202 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002203 InFlag);
2204 InFlag = Chain.getValue(1);
2205 } else {
2206 // If we are tail calling and generating PIC/GOT style code load the
2207 // address of the callee into ECX. The value in ecx is used as target of
2208 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2209 // for tail calls on PIC/GOT architectures. Normally we would just put the
2210 // address of GOT into ebx and then call target@PLT. But for tail calls
2211 // ebx would be restored (since ebx is callee saved) before jumping to the
2212 // target@PLT.
2213
2214 // Note: The actual moving to ECX is done further down.
2215 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2216 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2217 !G->getGlobal()->hasProtectedVisibility())
2218 Callee = LowerGlobalAddress(Callee, DAG);
2219 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002220 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002221 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002222 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002223
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002224 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002225 // From AMD64 ABI document:
2226 // For calls that may call functions that use varargs or stdargs
2227 // (prototype-less calls or calls to functions containing ellipsis (...) in
2228 // the declaration) %al is used as hidden argument to specify the number
2229 // of SSE registers used. The contents of %al do not need to match exactly
2230 // the number of registers, but must be an ubound on the number of SSE
2231 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002232
Gordon Henriksen86737662008-01-05 16:56:59 +00002233 // Count the number of XMM registers allocated.
2234 static const unsigned XMMArgRegs[] = {
2235 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2236 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2237 };
2238 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00002239 assert((Subtarget->hasXMM() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002240 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002241
Dale Johannesendd64c412009-02-04 00:33:20 +00002242 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002243 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002244 InFlag = Chain.getValue(1);
2245 }
2246
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002247
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002248 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002249 if (isTailCall) {
2250 // Force all the incoming stack arguments to be loaded from the stack
2251 // before any new outgoing arguments are stored to the stack, because the
2252 // outgoing stack slots may alias the incoming argument stack slots, and
2253 // the alias isn't otherwise explicit. This is slightly more conservative
2254 // than necessary, because it means that each store effectively depends
2255 // on every argument instead of just those arguments it would clobber.
2256 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2257
Dan Gohman475871a2008-07-27 21:46:04 +00002258 SmallVector<SDValue, 8> MemOpChains2;
2259 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002260 int FI = 0;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002261 // Do not flag preceding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002262 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002263 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002264 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2265 CCValAssign &VA = ArgLocs[i];
2266 if (VA.isRegLoc())
2267 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002268 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002269 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002270 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002271 // Create frame index.
2272 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002273 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002274 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002275 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002276
Duncan Sands276dcbd2008-03-21 09:14:45 +00002277 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002278 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002279 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002280 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002281 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002282 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002283 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002284
Dan Gohman98ca4f22009-08-05 01:29:28 +00002285 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2286 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002287 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002288 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002289 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002290 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002291 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002292 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002293 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002294 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002295 }
2296 }
2297
2298 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002299 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002300 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002301
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002302 // Copy arguments to their registers.
2303 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002304 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002305 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002306 InFlag = Chain.getValue(1);
2307 }
Dan Gohman475871a2008-07-27 21:46:04 +00002308 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002309
Gordon Henriksen86737662008-01-05 16:56:59 +00002310 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002311 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002312 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002313 }
2314
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002315 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2316 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2317 // In the 64-bit large code model, we have to make all calls
2318 // through a register, since the call instruction's 32-bit
2319 // pc-relative offset may not be large enough to hold the whole
2320 // address.
2321 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002322 // If the callee is a GlobalAddress node (quite common, every direct call
2323 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2324 // it.
2325
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002326 // We should use extra load for direct calls to dllimported functions in
2327 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002328 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002329 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002330 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002331 bool ExtraLoad = false;
2332 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002333
Chris Lattner48a7d022009-07-09 05:02:21 +00002334 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2335 // external symbols most go through the PLT in PIC mode. If the symbol
2336 // has hidden or protected visibility, or if it is static or local, then
2337 // we don't need to use the PLT - we can directly call it.
2338 if (Subtarget->isTargetELF() &&
2339 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002340 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002341 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002342 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002343 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002344 (!Subtarget->getTargetTriple().isMacOSX() ||
2345 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002346 // PC-relative references to external symbols should go through $stub,
2347 // unless we're building with the leopard linker or later, which
2348 // automatically synthesizes these stubs.
2349 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002350 } else if (Subtarget->isPICStyleRIPRel() &&
2351 isa<Function>(GV) &&
2352 cast<Function>(GV)->hasFnAttr(Attribute::NonLazyBind)) {
2353 // If the function is marked as non-lazy, generate an indirect call
2354 // which loads from the GOT directly. This avoids runtime overhead
2355 // at the cost of eager binding (and one extra byte of encoding).
2356 OpFlags = X86II::MO_GOTPCREL;
2357 WrapperKind = X86ISD::WrapperRIP;
2358 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002359 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002360
Devang Patel0d881da2010-07-06 22:08:15 +00002361 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002362 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002363
2364 // Add a wrapper if needed.
2365 if (WrapperKind != ISD::DELETED_NODE)
2366 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2367 // Add extra indirection if needed.
2368 if (ExtraLoad)
2369 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2370 MachinePointerInfo::getGOT(),
2371 false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002372 }
Bill Wendling056292f2008-09-16 21:48:12 +00002373 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002374 unsigned char OpFlags = 0;
2375
Evan Cheng1bf891a2010-12-01 22:59:46 +00002376 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2377 // external symbols should go through the PLT.
2378 if (Subtarget->isTargetELF() &&
2379 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2380 OpFlags = X86II::MO_PLT;
2381 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002382 (!Subtarget->getTargetTriple().isMacOSX() ||
2383 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002384 // PC-relative references to external symbols should go through $stub,
2385 // unless we're building with the leopard linker or later, which
2386 // automatically synthesizes these stubs.
2387 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002388 }
Eric Christopherfd179292009-08-27 18:07:15 +00002389
Chris Lattner48a7d022009-07-09 05:02:21 +00002390 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2391 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002392 }
2393
Chris Lattnerd96d0722007-02-25 06:40:16 +00002394 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002395 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002396 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002397
Evan Chengf22f9b32010-02-06 03:28:46 +00002398 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002399 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2400 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002401 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002402 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002403
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002404 Ops.push_back(Chain);
2405 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002406
Dan Gohman98ca4f22009-08-05 01:29:28 +00002407 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002408 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002409
Gordon Henriksen86737662008-01-05 16:56:59 +00002410 // Add argument registers to the end of the list so that they are known live
2411 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002412 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2413 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2414 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002415
Evan Cheng586ccac2008-03-18 23:36:35 +00002416 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002417 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002418 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2419
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002420 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002421 if (Is64Bit && isVarArg && !IsWin64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002422 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002423
Gabor Greifba36cb52008-08-28 21:40:38 +00002424 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002425 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002426
Dan Gohman98ca4f22009-08-05 01:29:28 +00002427 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002428 // We used to do:
2429 //// If this is the first return lowered for this function, add the regs
2430 //// to the liveout set for the function.
2431 // This isn't right, although it's probably harmless on x86; liveouts
2432 // should be computed from returns not tail calls. Consider a void
2433 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002434 return DAG.getNode(X86ISD::TC_RETURN, dl,
2435 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002436 }
2437
Dale Johannesenace16102009-02-03 19:33:06 +00002438 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002439 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002440
Chris Lattner2d297092006-05-23 18:50:38 +00002441 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002442 unsigned NumBytesForCalleeToPush;
Evan Chengef41ff62011-06-23 17:54:54 +00002443 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg, GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002444 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002445 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002446 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002447 // pops the hidden struct pointer, so we have to push it back.
2448 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002449 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002450 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002451 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002452
Gordon Henriksenae636f82008-01-03 16:47:34 +00002453 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002454 if (!IsSibcall) {
2455 Chain = DAG.getCALLSEQ_END(Chain,
2456 DAG.getIntPtrConstant(NumBytes, true),
2457 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2458 true),
2459 InFlag);
2460 InFlag = Chain.getValue(1);
2461 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002462
Chris Lattner3085e152007-02-25 08:59:22 +00002463 // Handle result values, copying them out of physregs into vregs that we
2464 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002465 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2466 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002467}
2468
Evan Cheng25ab6902006-09-08 06:48:29 +00002469
2470//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002471// Fast Calling Convention (tail call) implementation
2472//===----------------------------------------------------------------------===//
2473
2474// Like std call, callee cleans arguments, convention except that ECX is
2475// reserved for storing the tail called function address. Only 2 registers are
2476// free for argument passing (inreg). Tail call optimization is performed
2477// provided:
2478// * tailcallopt is enabled
2479// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002480// On X86_64 architecture with GOT-style position independent code only local
2481// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002482// To keep the stack aligned according to platform abi the function
2483// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2484// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002485// If a tail called function callee has more arguments than the caller the
2486// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002487// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002488// original REtADDR, but before the saved framepointer or the spilled registers
2489// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2490// stack layout:
2491// arg1
2492// arg2
2493// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002494// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002495// move area ]
2496// (possible EBP)
2497// ESI
2498// EDI
2499// local1 ..
2500
2501/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2502/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002503unsigned
2504X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2505 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002506 MachineFunction &MF = DAG.getMachineFunction();
2507 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002508 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002509 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002510 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002511 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002512 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002513 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2514 // Number smaller than 12 so just add the difference.
2515 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2516 } else {
2517 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002518 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002519 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002520 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002521 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002522}
2523
Evan Cheng5f941932010-02-05 02:21:12 +00002524/// MatchingStackOffset - Return true if the given stack call argument is
2525/// already available in the same position (relatively) of the caller's
2526/// incoming argument stack.
2527static
2528bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2529 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2530 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002531 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2532 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002533 if (Arg.getOpcode() == ISD::CopyFromReg) {
2534 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002535 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002536 return false;
2537 MachineInstr *Def = MRI->getVRegDef(VR);
2538 if (!Def)
2539 return false;
2540 if (!Flags.isByVal()) {
2541 if (!TII->isLoadFromStackSlot(Def, FI))
2542 return false;
2543 } else {
2544 unsigned Opcode = Def->getOpcode();
2545 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2546 Def->getOperand(1).isFI()) {
2547 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002548 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002549 } else
2550 return false;
2551 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002552 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2553 if (Flags.isByVal())
2554 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002555 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002556 // define @foo(%struct.X* %A) {
2557 // tail call @bar(%struct.X* byval %A)
2558 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002559 return false;
2560 SDValue Ptr = Ld->getBasePtr();
2561 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2562 if (!FINode)
2563 return false;
2564 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002565 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002566 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002567 FI = FINode->getIndex();
2568 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002569 } else
2570 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002571
Evan Cheng4cae1332010-03-05 08:38:04 +00002572 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002573 if (!MFI->isFixedObjectIndex(FI))
2574 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002575 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002576}
2577
Dan Gohman98ca4f22009-08-05 01:29:28 +00002578/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2579/// for tail call optimization. Targets which want to do tail call
2580/// optimization should implement this function.
2581bool
2582X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002583 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002584 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002585 bool isCalleeStructRet,
2586 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002587 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002588 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002589 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002590 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002591 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002592 CalleeCC != CallingConv::C)
2593 return false;
2594
Evan Cheng7096ae42010-01-29 06:45:59 +00002595 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002596 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002597 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002598 CallingConv::ID CallerCC = CallerF->getCallingConv();
2599 bool CCMatch = CallerCC == CalleeCC;
2600
Dan Gohman1797ed52010-02-08 20:27:50 +00002601 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002602 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002603 return true;
2604 return false;
2605 }
2606
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002607 // Look for obvious safe cases to perform tail call optimization that do not
2608 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002609
Evan Cheng2c12cb42010-03-26 16:26:03 +00002610 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2611 // emit a special epilogue.
2612 if (RegInfo->needsStackRealignment(MF))
2613 return false;
2614
Evan Chenga375d472010-03-15 18:54:48 +00002615 // Also avoid sibcall optimization if either caller or callee uses struct
2616 // return semantics.
2617 if (isCalleeStructRet || isCallerStructRet)
2618 return false;
2619
Chad Rosier2416da32011-06-24 21:15:36 +00002620 // An stdcall caller is expected to clean up its arguments; the callee
2621 // isn't going to do that.
2622 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2623 return false;
2624
Chad Rosier871f6642011-05-18 19:59:50 +00002625 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002626 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002627 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002628
2629 // Optimizing for varargs on Win64 is unlikely to be safe without
2630 // additional testing.
2631 if (Subtarget->isTargetWin64())
2632 return false;
2633
Chad Rosier871f6642011-05-18 19:59:50 +00002634 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002635 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2636 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002637
Chad Rosier871f6642011-05-18 19:59:50 +00002638 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2639 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2640 if (!ArgLocs[i].isRegLoc())
2641 return false;
2642 }
2643
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002644 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2645 // Therefore if it's not used by the call it is not safe to optimize this into
2646 // a sibcall.
2647 bool Unused = false;
2648 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2649 if (!Ins[i].Used) {
2650 Unused = true;
2651 break;
2652 }
2653 }
2654 if (Unused) {
2655 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002656 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
2657 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002658 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002659 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002660 CCValAssign &VA = RVLocs[i];
2661 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2662 return false;
2663 }
2664 }
2665
Evan Cheng13617962010-04-30 01:12:32 +00002666 // If the calling conventions do not match, then we'd better make sure the
2667 // results are returned in the same way as what the caller expects.
2668 if (!CCMatch) {
2669 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002670 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
2671 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002672 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2673
2674 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002675 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
2676 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002677 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2678
2679 if (RVLocs1.size() != RVLocs2.size())
2680 return false;
2681 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2682 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2683 return false;
2684 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2685 return false;
2686 if (RVLocs1[i].isRegLoc()) {
2687 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2688 return false;
2689 } else {
2690 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2691 return false;
2692 }
2693 }
2694 }
2695
Evan Chenga6bff982010-01-30 01:22:00 +00002696 // If the callee takes no arguments then go on to check the results of the
2697 // call.
2698 if (!Outs.empty()) {
2699 // Check if stack adjustment is needed. For now, do not do this if any
2700 // argument is passed on the stack.
2701 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002702 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2703 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002704
2705 // Allocate shadow area for Win64
2706 if (Subtarget->isTargetWin64()) {
2707 CCInfo.AllocateStack(32, 8);
2708 }
2709
Duncan Sands45907662010-10-31 13:21:44 +00002710 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002711 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002712 MachineFunction &MF = DAG.getMachineFunction();
2713 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2714 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002715
2716 // Check if the arguments are already laid out in the right way as
2717 // the caller's fixed stack objects.
2718 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002719 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2720 const X86InstrInfo *TII =
2721 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002722 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2723 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002724 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002725 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002726 if (VA.getLocInfo() == CCValAssign::Indirect)
2727 return false;
2728 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002729 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2730 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002731 return false;
2732 }
2733 }
2734 }
Evan Cheng9c044672010-05-29 01:35:22 +00002735
2736 // If the tailcall address may be in a register, then make sure it's
2737 // possible to register allocate for it. In 32-bit, the call address can
2738 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002739 // callee-saved registers are restored. These happen to be the same
2740 // registers used to pass 'inreg' arguments so watch out for those.
2741 if (!Subtarget->is64Bit() &&
2742 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002743 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002744 unsigned NumInRegs = 0;
2745 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2746 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002747 if (!VA.isRegLoc())
2748 continue;
2749 unsigned Reg = VA.getLocReg();
2750 switch (Reg) {
2751 default: break;
2752 case X86::EAX: case X86::EDX: case X86::ECX:
2753 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002754 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002755 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002756 }
2757 }
2758 }
Evan Chenga6bff982010-01-30 01:22:00 +00002759 }
Evan Chengb1712452010-01-27 06:25:16 +00002760
Evan Cheng86809cc2010-02-03 03:28:02 +00002761 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002762}
2763
Dan Gohman3df24e62008-09-03 23:12:08 +00002764FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002765X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2766 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002767}
2768
2769
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002770//===----------------------------------------------------------------------===//
2771// Other Lowering Hooks
2772//===----------------------------------------------------------------------===//
2773
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002774static bool MayFoldLoad(SDValue Op) {
2775 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2776}
2777
2778static bool MayFoldIntoStore(SDValue Op) {
2779 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2780}
2781
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002782static bool isTargetShuffle(unsigned Opcode) {
2783 switch(Opcode) {
2784 default: return false;
2785 case X86ISD::PSHUFD:
2786 case X86ISD::PSHUFHW:
2787 case X86ISD::PSHUFLW:
2788 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002789 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002790 case X86ISD::SHUFPS:
2791 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002792 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002793 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002794 case X86ISD::MOVLPS:
2795 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002796 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002797 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002798 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002799 case X86ISD::MOVSS:
2800 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002801 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002802 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002803 case X86ISD::VUNPCKLPSY:
2804 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002805 case X86ISD::PUNPCKLWD:
2806 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002807 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002808 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002809 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002810 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00002811 case X86ISD::VUNPCKHPSY:
2812 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002813 case X86ISD::PUNPCKHWD:
2814 case X86ISD::PUNPCKHBW:
2815 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002816 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00002817 case X86ISD::VPERMILPS:
2818 case X86ISD::VPERMILPSY:
2819 case X86ISD::VPERMILPD:
2820 case X86ISD::VPERMILPDY:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00002821 case X86ISD::VPERM2F128:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002822 return true;
2823 }
2824 return false;
2825}
2826
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002827static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002828 SDValue V1, SelectionDAG &DAG) {
2829 switch(Opc) {
2830 default: llvm_unreachable("Unknown x86 shuffle node");
2831 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002832 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002833 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002834 return DAG.getNode(Opc, dl, VT, V1);
2835 }
2836
2837 return SDValue();
2838}
2839
2840static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002841 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002842 switch(Opc) {
2843 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002844 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002845 case X86ISD::PSHUFHW:
2846 case X86ISD::PSHUFLW:
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00002847 case X86ISD::VPERMILPS:
2848 case X86ISD::VPERMILPSY:
2849 case X86ISD::VPERMILPD:
2850 case X86ISD::VPERMILPDY:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002851 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2852 }
2853
2854 return SDValue();
2855}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002856
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002857static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2858 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2859 switch(Opc) {
2860 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002861 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002862 case X86ISD::SHUFPD:
2863 case X86ISD::SHUFPS:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00002864 case X86ISD::VPERM2F128:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002865 return DAG.getNode(Opc, dl, VT, V1, V2,
2866 DAG.getConstant(TargetMask, MVT::i8));
2867 }
2868 return SDValue();
2869}
2870
2871static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2872 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2873 switch(Opc) {
2874 default: llvm_unreachable("Unknown x86 shuffle node");
2875 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002876 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002877 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002878 case X86ISD::MOVLPS:
2879 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002880 case X86ISD::MOVSS:
2881 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002882 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002883 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002884 case X86ISD::VUNPCKLPSY:
2885 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002886 case X86ISD::PUNPCKLWD:
2887 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002888 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002889 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002890 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002891 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00002892 case X86ISD::VUNPCKHPSY:
2893 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002894 case X86ISD::PUNPCKHWD:
2895 case X86ISD::PUNPCKHBW:
2896 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002897 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002898 return DAG.getNode(Opc, dl, VT, V1, V2);
2899 }
2900 return SDValue();
2901}
2902
Dan Gohmand858e902010-04-17 15:26:15 +00002903SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002904 MachineFunction &MF = DAG.getMachineFunction();
2905 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2906 int ReturnAddrIndex = FuncInfo->getRAIndex();
2907
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002908 if (ReturnAddrIndex == 0) {
2909 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002910 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002911 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002912 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002913 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002914 }
2915
Evan Cheng25ab6902006-09-08 06:48:29 +00002916 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002917}
2918
2919
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002920bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2921 bool hasSymbolicDisplacement) {
2922 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002923 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002924 return false;
2925
2926 // If we don't have a symbolic displacement - we don't have any extra
2927 // restrictions.
2928 if (!hasSymbolicDisplacement)
2929 return true;
2930
2931 // FIXME: Some tweaks might be needed for medium code model.
2932 if (M != CodeModel::Small && M != CodeModel::Kernel)
2933 return false;
2934
2935 // For small code model we assume that latest object is 16MB before end of 31
2936 // bits boundary. We may also accept pretty large negative constants knowing
2937 // that all objects are in the positive half of address space.
2938 if (M == CodeModel::Small && Offset < 16*1024*1024)
2939 return true;
2940
2941 // For kernel code model we know that all object resist in the negative half
2942 // of 32bits address space. We may not accept negative offsets, since they may
2943 // be just off and we may accept pretty large positive ones.
2944 if (M == CodeModel::Kernel && Offset > 0)
2945 return true;
2946
2947 return false;
2948}
2949
Evan Chengef41ff62011-06-23 17:54:54 +00002950/// isCalleePop - Determines whether the callee is required to pop its
2951/// own arguments. Callee pop is necessary to support tail calls.
2952bool X86::isCalleePop(CallingConv::ID CallingConv,
2953 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
2954 if (IsVarArg)
2955 return false;
2956
2957 switch (CallingConv) {
2958 default:
2959 return false;
2960 case CallingConv::X86_StdCall:
2961 return !is64Bit;
2962 case CallingConv::X86_FastCall:
2963 return !is64Bit;
2964 case CallingConv::X86_ThisCall:
2965 return !is64Bit;
2966 case CallingConv::Fast:
2967 return TailCallOpt;
2968 case CallingConv::GHC:
2969 return TailCallOpt;
2970 }
2971}
2972
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002973/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2974/// specific condition code, returning the condition code and the LHS/RHS of the
2975/// comparison to make.
2976static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2977 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002978 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002979 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2980 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2981 // X > -1 -> X == 0, jump !sign.
2982 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002983 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002984 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2985 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002986 return X86::COND_S;
Andrew Trickf6c39412011-03-23 23:11:02 +00002987 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002988 // X < 1 -> X <= 0
2989 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002990 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002991 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002992 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002993
Evan Chengd9558e02006-01-06 00:43:03 +00002994 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002995 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002996 case ISD::SETEQ: return X86::COND_E;
2997 case ISD::SETGT: return X86::COND_G;
2998 case ISD::SETGE: return X86::COND_GE;
2999 case ISD::SETLT: return X86::COND_L;
3000 case ISD::SETLE: return X86::COND_LE;
3001 case ISD::SETNE: return X86::COND_NE;
3002 case ISD::SETULT: return X86::COND_B;
3003 case ISD::SETUGT: return X86::COND_A;
3004 case ISD::SETULE: return X86::COND_BE;
3005 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00003006 }
Chris Lattner4c78e022008-12-23 23:42:27 +00003007 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003008
Chris Lattner4c78e022008-12-23 23:42:27 +00003009 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00003010
Chris Lattner4c78e022008-12-23 23:42:27 +00003011 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00003012 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
3013 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00003014 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
3015 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00003016 }
3017
Chris Lattner4c78e022008-12-23 23:42:27 +00003018 switch (SetCCOpcode) {
3019 default: break;
3020 case ISD::SETOLT:
3021 case ISD::SETOLE:
3022 case ISD::SETUGT:
3023 case ISD::SETUGE:
3024 std::swap(LHS, RHS);
3025 break;
3026 }
3027
3028 // On a floating point condition, the flags are set as follows:
3029 // ZF PF CF op
3030 // 0 | 0 | 0 | X > Y
3031 // 0 | 0 | 1 | X < Y
3032 // 1 | 0 | 0 | X == Y
3033 // 1 | 1 | 1 | unordered
3034 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003035 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00003036 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003037 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00003038 case ISD::SETOLT: // flipped
3039 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003040 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00003041 case ISD::SETOLE: // flipped
3042 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003043 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003044 case ISD::SETUGT: // flipped
3045 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003046 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00003047 case ISD::SETUGE: // flipped
3048 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003049 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003050 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003051 case ISD::SETNE: return X86::COND_NE;
3052 case ISD::SETUO: return X86::COND_P;
3053 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003054 case ISD::SETOEQ:
3055 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003056 }
Evan Chengd9558e02006-01-06 00:43:03 +00003057}
3058
Evan Cheng4a460802006-01-11 00:33:36 +00003059/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3060/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003061/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003062static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003063 switch (X86CC) {
3064 default:
3065 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003066 case X86::COND_B:
3067 case X86::COND_BE:
3068 case X86::COND_E:
3069 case X86::COND_P:
3070 case X86::COND_A:
3071 case X86::COND_AE:
3072 case X86::COND_NE:
3073 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003074 return true;
3075 }
3076}
3077
Evan Chengeb2f9692009-10-27 19:56:55 +00003078/// isFPImmLegal - Returns true if the target can instruction select the
3079/// specified FP immediate natively. If false, the legalizer will
3080/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003081bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003082 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3083 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3084 return true;
3085 }
3086 return false;
3087}
3088
Nate Begeman9008ca62009-04-27 18:41:29 +00003089/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3090/// the specified range (L, H].
3091static bool isUndefOrInRange(int Val, int Low, int Hi) {
3092 return (Val < 0) || (Val >= Low && Val < Hi);
3093}
3094
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00003095/// isUndefOrInRange - Return true if every element in Mask, begining
3096/// from position Pos and ending in Pos+Size, falls within the specified
3097/// range (L, L+Pos]. or is undef.
3098static bool isUndefOrInRange(const SmallVectorImpl<int> &Mask,
3099 int Pos, int Size, int Low, int Hi) {
3100 for (int i = Pos, e = Pos+Size; i != e; ++i)
3101 if (!isUndefOrInRange(Mask[i], Low, Hi))
3102 return false;
3103 return true;
3104}
3105
Nate Begeman9008ca62009-04-27 18:41:29 +00003106/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3107/// specified value.
3108static bool isUndefOrEqual(int Val, int CmpVal) {
3109 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003110 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003111 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00003112}
3113
Bruno Cardoso Lopes4002d7e2011-08-12 21:54:42 +00003114/// isSequentialOrUndefInRange - Return true if every element in Mask, begining
3115/// from position Pos and ending in Pos+Size, falls within the specified
3116/// sequential range (L, L+Pos]. or is undef.
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003117static bool isSequentialOrUndefInRange(const SmallVectorImpl<int> &Mask,
3118 int Pos, int Size, int Low) {
3119 for (int i = Pos, e = Pos+Size; i != e; ++i, ++Low)
3120 if (!isUndefOrEqual(Mask[i], Low))
3121 return false;
3122 return true;
3123}
3124
Nate Begeman9008ca62009-04-27 18:41:29 +00003125/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3126/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3127/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00003128static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003129 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003130 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003131 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003132 return (Mask[0] < 2 && Mask[1] < 2);
3133 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003134}
3135
Nate Begeman9008ca62009-04-27 18:41:29 +00003136bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003137 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003138 N->getMask(M);
3139 return ::isPSHUFDMask(M, N->getValueType(0));
3140}
Evan Cheng0188ecb2006-03-22 18:59:22 +00003141
Nate Begeman9008ca62009-04-27 18:41:29 +00003142/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3143/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00003144static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003145 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00003146 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003147
Nate Begeman9008ca62009-04-27 18:41:29 +00003148 // Lower quadword copied in order or undef.
3149 for (int i = 0; i != 4; ++i)
3150 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00003151 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003152
Evan Cheng506d3df2006-03-29 23:07:14 +00003153 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003154 for (int i = 4; i != 8; ++i)
3155 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00003156 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003157
Evan Cheng506d3df2006-03-29 23:07:14 +00003158 return true;
3159}
3160
Nate Begeman9008ca62009-04-27 18:41:29 +00003161bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003162 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003163 N->getMask(M);
3164 return ::isPSHUFHWMask(M, N->getValueType(0));
3165}
Evan Cheng506d3df2006-03-29 23:07:14 +00003166
Nate Begeman9008ca62009-04-27 18:41:29 +00003167/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3168/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00003169static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003170 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00003171 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003172
Rafael Espindola15684b22009-04-24 12:40:33 +00003173 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00003174 for (int i = 4; i != 8; ++i)
3175 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00003176 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003177
Rafael Espindola15684b22009-04-24 12:40:33 +00003178 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003179 for (int i = 0; i != 4; ++i)
3180 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00003181 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003182
Rafael Espindola15684b22009-04-24 12:40:33 +00003183 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003184}
3185
Nate Begeman9008ca62009-04-27 18:41:29 +00003186bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003187 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003188 N->getMask(M);
3189 return ::isPSHUFLWMask(M, N->getValueType(0));
3190}
3191
Nate Begemana09008b2009-10-19 02:17:23 +00003192/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3193/// is suitable for input to PALIGNR.
3194static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00003195 bool hasSSSE3OrAVX) {
Nate Begemana09008b2009-10-19 02:17:23 +00003196 int i, e = VT.getVectorNumElements();
Bruno Cardoso Lopes9065d4b2011-07-29 01:30:59 +00003197 if (VT.getSizeInBits() != 128 && VT.getSizeInBits() != 64)
3198 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003199
Nate Begemana09008b2009-10-19 02:17:23 +00003200 // Do not handle v2i64 / v2f64 shuffles with palignr.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00003201 if (e < 4 || !hasSSSE3OrAVX)
Nate Begemana09008b2009-10-19 02:17:23 +00003202 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003203
Nate Begemana09008b2009-10-19 02:17:23 +00003204 for (i = 0; i != e; ++i)
3205 if (Mask[i] >= 0)
3206 break;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003207
Nate Begemana09008b2009-10-19 02:17:23 +00003208 // All undef, not a palignr.
3209 if (i == e)
3210 return false;
3211
Eli Friedman63f8dde2011-07-25 21:36:45 +00003212 // Make sure we're shifting in the right direction.
3213 if (Mask[i] <= i)
3214 return false;
Nate Begemana09008b2009-10-19 02:17:23 +00003215
3216 int s = Mask[i] - i;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003217
Nate Begemana09008b2009-10-19 02:17:23 +00003218 // Check the rest of the elements to see if they are consecutive.
3219 for (++i; i != e; ++i) {
3220 int m = Mask[i];
Eli Friedman63f8dde2011-07-25 21:36:45 +00003221 if (m >= 0 && m != s+i)
Nate Begemana09008b2009-10-19 02:17:23 +00003222 return false;
3223 }
3224 return true;
3225}
3226
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003227/// isVSHUFPSYMask - Return true if the specified VECTOR_SHUFFLE operand
3228/// specifies a shuffle of elements that is suitable for input to 256-bit
3229/// VSHUFPSY.
3230static bool isVSHUFPSYMask(const SmallVectorImpl<int> &Mask, EVT VT,
3231 const X86Subtarget *Subtarget) {
3232 int NumElems = VT.getVectorNumElements();
3233
3234 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256)
3235 return false;
3236
3237 if (NumElems != 8)
3238 return false;
3239
3240 // VSHUFPSY divides the resulting vector into 4 chunks.
3241 // The sources are also splitted into 4 chunks, and each destination
3242 // chunk must come from a different source chunk.
3243 //
3244 // SRC1 => X7 X6 X5 X4 X3 X2 X1 X0
3245 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3246 //
3247 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3248 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3249 //
3250 int QuarterSize = NumElems/4;
3251 int HalfSize = QuarterSize*2;
3252 for (int i = 0; i < QuarterSize; ++i)
3253 if (!isUndefOrInRange(Mask[i], 0, HalfSize))
3254 return false;
3255 for (int i = QuarterSize; i < QuarterSize*2; ++i)
3256 if (!isUndefOrInRange(Mask[i], NumElems, NumElems+HalfSize))
3257 return false;
3258
3259 // The mask of the second half must be the same as the first but with
3260 // the appropriate offsets. This works in the same way as VPERMILPS
3261 // works with masks.
3262 for (int i = QuarterSize*2; i < QuarterSize*3; ++i) {
3263 if (!isUndefOrInRange(Mask[i], HalfSize, NumElems))
3264 return false;
3265 int FstHalfIdx = i-HalfSize;
3266 if (Mask[FstHalfIdx] < 0)
3267 continue;
3268 if (!isUndefOrEqual(Mask[i], Mask[FstHalfIdx]+HalfSize))
3269 return false;
3270 }
3271 for (int i = QuarterSize*3; i < NumElems; ++i) {
3272 if (!isUndefOrInRange(Mask[i], NumElems+HalfSize, NumElems*2))
3273 return false;
3274 int FstHalfIdx = i-HalfSize;
3275 if (Mask[FstHalfIdx] < 0)
3276 continue;
3277 if (!isUndefOrEqual(Mask[i], Mask[FstHalfIdx]+HalfSize))
3278 return false;
3279
3280 }
3281
3282 return true;
3283}
3284
3285/// getShuffleVSHUFPSYImmediate - Return the appropriate immediate to shuffle
3286/// the specified VECTOR_MASK mask with VSHUFPSY instruction.
3287static unsigned getShuffleVSHUFPSYImmediate(SDNode *N) {
3288 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3289 EVT VT = SVOp->getValueType(0);
3290 int NumElems = VT.getVectorNumElements();
3291
3292 assert(NumElems == 8 && VT.getSizeInBits() == 256 &&
3293 "Only supports v8i32 and v8f32 types");
3294
3295 int HalfSize = NumElems/2;
3296 unsigned Mask = 0;
3297 for (int i = 0; i != NumElems ; ++i) {
3298 if (SVOp->getMaskElt(i) < 0)
3299 continue;
3300 // The mask of the first half must be equal to the second one.
3301 unsigned Shamt = (i%HalfSize)*2;
3302 unsigned Elt = SVOp->getMaskElt(i) % HalfSize;
3303 Mask |= Elt << Shamt;
3304 }
3305
3306 return Mask;
3307}
3308
3309/// isVSHUFPDYMask - Return true if the specified VECTOR_SHUFFLE operand
3310/// specifies a shuffle of elements that is suitable for input to 256-bit
3311/// VSHUFPDY. This shuffle doesn't have the same restriction as the PS
3312/// version and the mask of the second half isn't binded with the first
3313/// one.
3314static bool isVSHUFPDYMask(const SmallVectorImpl<int> &Mask, EVT VT,
3315 const X86Subtarget *Subtarget) {
3316 int NumElems = VT.getVectorNumElements();
3317
3318 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256)
3319 return false;
3320
3321 if (NumElems != 4)
3322 return false;
3323
3324 // VSHUFPSY divides the resulting vector into 4 chunks.
3325 // The sources are also splitted into 4 chunks, and each destination
3326 // chunk must come from a different source chunk.
3327 //
3328 // SRC1 => X3 X2 X1 X0
3329 // SRC2 => Y3 Y2 Y1 Y0
3330 //
3331 // DST => Y2..Y3, X2..X3, Y1..Y0, X1..X0
3332 //
3333 int QuarterSize = NumElems/4;
3334 int HalfSize = QuarterSize*2;
3335 for (int i = 0; i < QuarterSize; ++i)
3336 if (!isUndefOrInRange(Mask[i], 0, HalfSize))
3337 return false;
3338 for (int i = QuarterSize; i < QuarterSize*2; ++i)
3339 if (!isUndefOrInRange(Mask[i], NumElems, NumElems+HalfSize))
3340 return false;
3341 for (int i = QuarterSize*2; i < QuarterSize*3; ++i)
3342 if (!isUndefOrInRange(Mask[i], HalfSize, NumElems))
3343 return false;
3344 for (int i = QuarterSize*3; i < NumElems; ++i)
3345 if (!isUndefOrInRange(Mask[i], NumElems+HalfSize, NumElems*2))
3346 return false;
3347
3348 return true;
3349}
3350
3351/// getShuffleVSHUFPDYImmediate - Return the appropriate immediate to shuffle
3352/// the specified VECTOR_MASK mask with VSHUFPDY instruction.
3353static unsigned getShuffleVSHUFPDYImmediate(SDNode *N) {
3354 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3355 EVT VT = SVOp->getValueType(0);
3356 int NumElems = VT.getVectorNumElements();
3357
3358 assert(NumElems == 4 && VT.getSizeInBits() == 256 &&
3359 "Only supports v4i64 and v4f64 types");
3360
3361 int HalfSize = NumElems/2;
3362 unsigned Mask = 0;
3363 for (int i = 0; i != NumElems ; ++i) {
3364 if (SVOp->getMaskElt(i) < 0)
3365 continue;
3366 int Elt = SVOp->getMaskElt(i) % HalfSize;
3367 Mask |= Elt << i;
3368 }
3369
3370 return Mask;
3371}
3372
Evan Cheng14aed5e2006-03-24 01:18:28 +00003373/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopesaf002d82011-08-24 23:17:55 +00003374/// specifies a shuffle of elements that is suitable for input to 128-bit
3375/// SHUFPS and SHUFPD.
Owen Andersone50ed302009-08-10 22:56:29 +00003376static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003377 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesaf002d82011-08-24 23:17:55 +00003378
3379 if (VT.getSizeInBits() != 128)
3380 return false;
3381
Nate Begeman9008ca62009-04-27 18:41:29 +00003382 if (NumElems != 2 && NumElems != 4)
3383 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003384
Nate Begeman9008ca62009-04-27 18:41:29 +00003385 int Half = NumElems / 2;
3386 for (int i = 0; i < Half; ++i)
3387 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003388 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003389 for (int i = Half; i < NumElems; ++i)
3390 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003391 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003392
Evan Cheng14aed5e2006-03-24 01:18:28 +00003393 return true;
3394}
3395
Nate Begeman9008ca62009-04-27 18:41:29 +00003396bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
3397 SmallVector<int, 8> M;
3398 N->getMask(M);
3399 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003400}
3401
Evan Cheng213d2cf2007-05-17 18:45:50 +00003402/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00003403/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
3404/// half elements to come from vector 1 (which would equal the dest.) and
3405/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00003406static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003407 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003408
3409 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00003410 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003411
Nate Begeman9008ca62009-04-27 18:41:29 +00003412 int Half = NumElems / 2;
3413 for (int i = 0; i < Half; ++i)
3414 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003415 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003416 for (int i = Half; i < NumElems; ++i)
3417 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003418 return false;
3419 return true;
3420}
3421
Nate Begeman9008ca62009-04-27 18:41:29 +00003422static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
3423 SmallVector<int, 8> M;
3424 N->getMask(M);
3425 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003426}
3427
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003428/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3429/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00003430bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003431 EVT VT = N->getValueType(0);
3432 unsigned NumElems = VT.getVectorNumElements();
3433
3434 if (VT.getSizeInBits() != 128)
3435 return false;
3436
3437 if (NumElems != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003438 return false;
3439
Evan Cheng2064a2b2006-03-28 06:50:32 +00003440 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00003441 return isUndefOrEqual(N->getMaskElt(0), 6) &&
3442 isUndefOrEqual(N->getMaskElt(1), 7) &&
3443 isUndefOrEqual(N->getMaskElt(2), 2) &&
3444 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003445}
3446
Nate Begeman0b10b912009-11-07 23:17:15 +00003447/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3448/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3449/// <2, 3, 2, 3>
3450bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003451 EVT VT = N->getValueType(0);
3452 unsigned NumElems = VT.getVectorNumElements();
3453
3454 if (VT.getSizeInBits() != 128)
3455 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003456
Nate Begeman0b10b912009-11-07 23:17:15 +00003457 if (NumElems != 4)
3458 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003459
Nate Begeman0b10b912009-11-07 23:17:15 +00003460 return isUndefOrEqual(N->getMaskElt(0), 2) &&
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003461 isUndefOrEqual(N->getMaskElt(1), 3) &&
3462 isUndefOrEqual(N->getMaskElt(2), 2) &&
3463 isUndefOrEqual(N->getMaskElt(3), 3);
Nate Begeman0b10b912009-11-07 23:17:15 +00003464}
3465
Evan Cheng5ced1d82006-04-06 23:23:56 +00003466/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3467/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003468bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3469 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003470
Evan Cheng5ced1d82006-04-06 23:23:56 +00003471 if (NumElems != 2 && NumElems != 4)
3472 return false;
3473
Evan Chengc5cdff22006-04-07 21:53:05 +00003474 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003475 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003476 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003477
Evan Chengc5cdff22006-04-07 21:53:05 +00003478 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003479 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003480 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003481
3482 return true;
3483}
3484
Nate Begeman0b10b912009-11-07 23:17:15 +00003485/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3486/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3487bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003488 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003489
David Greenea20244d2011-03-02 17:23:43 +00003490 if ((NumElems != 2 && NumElems != 4)
3491 || N->getValueType(0).getSizeInBits() > 128)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003492 return false;
3493
Evan Chengc5cdff22006-04-07 21:53:05 +00003494 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003495 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003496 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003497
Nate Begeman9008ca62009-04-27 18:41:29 +00003498 for (unsigned i = 0; i < NumElems/2; ++i)
3499 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003500 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003501
3502 return true;
3503}
3504
Evan Cheng0038e592006-03-28 00:39:58 +00003505/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3506/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003507static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003508 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003509 int NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003510
3511 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3512 "Unsupported vector type for unpckh");
3513
3514 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8)
Evan Cheng0038e592006-03-28 00:39:58 +00003515 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003516
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003517 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3518 // independently on 128-bit lanes.
3519 unsigned NumLanes = VT.getSizeInBits()/128;
3520 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003521
3522 unsigned Start = 0;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003523 unsigned End = NumLaneElts;
3524 for (unsigned s = 0; s < NumLanes; ++s) {
3525 for (unsigned i = Start, j = s * NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003526 i != End;
3527 i += 2, ++j) {
3528 int BitI = Mask[i];
3529 int BitI1 = Mask[i+1];
3530 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003531 return false;
David Greenea20244d2011-03-02 17:23:43 +00003532 if (V2IsSplat) {
3533 if (!isUndefOrEqual(BitI1, NumElts))
3534 return false;
3535 } else {
3536 if (!isUndefOrEqual(BitI1, j + NumElts))
3537 return false;
3538 }
Evan Cheng39623da2006-04-20 08:58:49 +00003539 }
David Greenea20244d2011-03-02 17:23:43 +00003540 // Process the next 128 bits.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003541 Start += NumLaneElts;
3542 End += NumLaneElts;
Evan Cheng0038e592006-03-28 00:39:58 +00003543 }
David Greenea20244d2011-03-02 17:23:43 +00003544
Evan Cheng0038e592006-03-28 00:39:58 +00003545 return true;
3546}
3547
Nate Begeman9008ca62009-04-27 18:41:29 +00003548bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3549 SmallVector<int, 8> M;
3550 N->getMask(M);
3551 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003552}
3553
Evan Cheng4fcb9222006-03-28 02:43:26 +00003554/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3555/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003556static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003557 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003558 int NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003559
3560 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3561 "Unsupported vector type for unpckh");
3562
3563 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8)
Evan Cheng4fcb9222006-03-28 02:43:26 +00003564 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003565
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003566 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3567 // independently on 128-bit lanes.
3568 unsigned NumLanes = VT.getSizeInBits()/128;
3569 unsigned NumLaneElts = NumElts/NumLanes;
3570
3571 unsigned Start = 0;
3572 unsigned End = NumLaneElts;
3573 for (unsigned l = 0; l != NumLanes; ++l) {
3574 for (unsigned i = Start, j = (l*NumLaneElts)+NumLaneElts/2;
3575 i != End; i += 2, ++j) {
3576 int BitI = Mask[i];
3577 int BitI1 = Mask[i+1];
3578 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003579 return false;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003580 if (V2IsSplat) {
3581 if (isUndefOrEqual(BitI1, NumElts))
3582 return false;
3583 } else {
3584 if (!isUndefOrEqual(BitI1, j+NumElts))
3585 return false;
3586 }
Evan Cheng39623da2006-04-20 08:58:49 +00003587 }
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003588 // Process the next 128 bits.
3589 Start += NumLaneElts;
3590 End += NumLaneElts;
Evan Cheng4fcb9222006-03-28 02:43:26 +00003591 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003592 return true;
3593}
3594
Nate Begeman9008ca62009-04-27 18:41:29 +00003595bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3596 SmallVector<int, 8> M;
3597 N->getMask(M);
3598 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003599}
3600
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003601/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3602/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3603/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003604static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003605 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003606 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003607 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003608
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003609 // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern
3610 // FIXME: Need a better way to get rid of this, there's no latency difference
3611 // between UNPCKLPD and MOVDDUP, the later should always be checked first and
3612 // the former later. We should also remove the "_undef" special mask.
3613 if (NumElems == 4 && VT.getSizeInBits() == 256)
3614 return false;
3615
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003616 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3617 // independently on 128-bit lanes.
3618 unsigned NumLanes = VT.getSizeInBits() / 128;
3619 unsigned NumLaneElts = NumElems / NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003620
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003621 for (unsigned s = 0; s < NumLanes; ++s) {
3622 for (unsigned i = s * NumLaneElts, j = s * NumLaneElts;
3623 i != NumLaneElts * (s + 1);
David Greenea20244d2011-03-02 17:23:43 +00003624 i += 2, ++j) {
3625 int BitI = Mask[i];
3626 int BitI1 = Mask[i+1];
3627
3628 if (!isUndefOrEqual(BitI, j))
3629 return false;
3630 if (!isUndefOrEqual(BitI1, j))
3631 return false;
3632 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003633 }
David Greenea20244d2011-03-02 17:23:43 +00003634
Rafael Espindola15684b22009-04-24 12:40:33 +00003635 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003636}
3637
Nate Begeman9008ca62009-04-27 18:41:29 +00003638bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3639 SmallVector<int, 8> M;
3640 N->getMask(M);
3641 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3642}
3643
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003644/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3645/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3646/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003647static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003648 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003649 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3650 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003651
Nate Begeman9008ca62009-04-27 18:41:29 +00003652 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3653 int BitI = Mask[i];
3654 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003655 if (!isUndefOrEqual(BitI, j))
3656 return false;
3657 if (!isUndefOrEqual(BitI1, j))
3658 return false;
3659 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003660 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003661}
3662
Nate Begeman9008ca62009-04-27 18:41:29 +00003663bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3664 SmallVector<int, 8> M;
3665 N->getMask(M);
3666 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3667}
3668
Evan Cheng017dcc62006-04-21 01:05:10 +00003669/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3670/// specifies a shuffle of elements that is suitable for input to MOVSS,
3671/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003672static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003673 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003674 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003675
3676 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003677
Nate Begeman9008ca62009-04-27 18:41:29 +00003678 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003679 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003680
Nate Begeman9008ca62009-04-27 18:41:29 +00003681 for (int i = 1; i < NumElts; ++i)
3682 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003683 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003684
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003685 return true;
3686}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003687
Nate Begeman9008ca62009-04-27 18:41:29 +00003688bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3689 SmallVector<int, 8> M;
3690 N->getMask(M);
3691 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003692}
3693
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003694/// isVPERM2F128Mask - Match 256-bit shuffles where the elements are considered
3695/// as permutations between 128-bit chunks or halves. As an example: this
3696/// shuffle bellow:
3697/// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
3698/// The first half comes from the second half of V1 and the second half from the
3699/// the second half of V2.
3700static bool isVPERM2F128Mask(const SmallVectorImpl<int> &Mask, EVT VT,
3701 const X86Subtarget *Subtarget) {
3702 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256)
3703 return false;
3704
3705 // The shuffle result is divided into half A and half B. In total the two
3706 // sources have 4 halves, namely: C, D, E, F. The final values of A and
3707 // B must come from C, D, E or F.
3708 int HalfSize = VT.getVectorNumElements()/2;
3709 bool MatchA = false, MatchB = false;
3710
3711 // Check if A comes from one of C, D, E, F.
3712 for (int Half = 0; Half < 4; ++Half) {
3713 if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
3714 MatchA = true;
3715 break;
3716 }
3717 }
3718
3719 // Check if B comes from one of C, D, E, F.
3720 for (int Half = 0; Half < 4; ++Half) {
3721 if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) {
3722 MatchB = true;
3723 break;
3724 }
3725 }
3726
3727 return MatchA && MatchB;
3728}
3729
3730/// getShuffleVPERM2F128Immediate - Return the appropriate immediate to shuffle
3731/// the specified VECTOR_MASK mask with VPERM2F128 instructions.
3732static unsigned getShuffleVPERM2F128Immediate(SDNode *N) {
3733 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3734 EVT VT = SVOp->getValueType(0);
3735
3736 int HalfSize = VT.getVectorNumElements()/2;
3737
3738 int FstHalf = 0, SndHalf = 0;
3739 for (int i = 0; i < HalfSize; ++i) {
3740 if (SVOp->getMaskElt(i) > 0) {
3741 FstHalf = SVOp->getMaskElt(i)/HalfSize;
3742 break;
3743 }
3744 }
3745 for (int i = HalfSize; i < HalfSize*2; ++i) {
3746 if (SVOp->getMaskElt(i) > 0) {
3747 SndHalf = SVOp->getMaskElt(i)/HalfSize;
3748 break;
3749 }
3750 }
3751
3752 return (FstHalf | (SndHalf << 4));
3753}
3754
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003755/// isVPERMILPDMask - Return true if the specified VECTOR_SHUFFLE operand
3756/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
3757/// Note that VPERMIL mask matching is different depending whether theunderlying
3758/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3759/// to the same elements of the low, but to the higher half of the source.
3760/// In VPERMILPD the two lanes could be shuffled independently of each other
3761/// with the same restriction that lanes can't be crossed.
3762static bool isVPERMILPDMask(const SmallVectorImpl<int> &Mask, EVT VT,
3763 const X86Subtarget *Subtarget) {
3764 int NumElts = VT.getVectorNumElements();
3765 int NumLanes = VT.getSizeInBits()/128;
3766
3767 if (!Subtarget->hasAVX())
3768 return false;
3769
Eli Friedmandca62d52011-10-10 22:28:47 +00003770 // Only match 256-bit with 64-bit types
3771 if (VT.getSizeInBits() != 256 || NumElts != 4)
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003772 return false;
3773
3774 // The mask on the high lane is independent of the low. Both can match
3775 // any element in inside its own lane, but can't cross.
3776 int LaneSize = NumElts/NumLanes;
3777 for (int l = 0; l < NumLanes; ++l)
3778 for (int i = l*LaneSize; i < LaneSize*(l+1); ++i) {
3779 int LaneStart = l*LaneSize;
3780 if (!isUndefOrInRange(Mask[i], LaneStart, LaneStart+LaneSize))
3781 return false;
3782 }
3783
3784 return true;
3785}
3786
3787/// isVPERMILPSMask - Return true if the specified VECTOR_SHUFFLE operand
3788/// specifies a shuffle of elements that is suitable for input to VPERMILPS*.
3789/// Note that VPERMIL mask matching is different depending whether theunderlying
3790/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3791/// to the same elements of the low, but to the higher half of the source.
3792/// In VPERMILPD the two lanes could be shuffled independently of each other
3793/// with the same restriction that lanes can't be crossed.
3794static bool isVPERMILPSMask(const SmallVectorImpl<int> &Mask, EVT VT,
3795 const X86Subtarget *Subtarget) {
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003796 unsigned NumElts = VT.getVectorNumElements();
3797 unsigned NumLanes = VT.getSizeInBits()/128;
3798
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003799 if (!Subtarget->hasAVX())
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003800 return false;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003801
Eli Friedmandca62d52011-10-10 22:28:47 +00003802 // Only match 256-bit with 32-bit types
3803 if (VT.getSizeInBits() != 256 || NumElts != 8)
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003804 return false;
3805
3806 // The mask on the high lane should be the same as the low. Actually,
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003807 // they can differ if any of the corresponding index in a lane is undef
3808 // and the other stays in range.
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003809 int LaneSize = NumElts/NumLanes;
3810 for (int i = 0; i < LaneSize; ++i) {
3811 int HighElt = i+LaneSize;
Bruno Cardoso Lopes155a92a2011-08-10 01:54:17 +00003812 bool HighValid = isUndefOrInRange(Mask[HighElt], LaneSize, NumElts);
3813 bool LowValid = isUndefOrInRange(Mask[i], 0, LaneSize);
3814
3815 if (!HighValid || !LowValid)
3816 return false;
3817 if (Mask[i] < 0 || Mask[HighElt] < 0)
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003818 continue;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003819 if (Mask[HighElt]-Mask[i] != LaneSize)
3820 return false;
3821 }
3822
3823 return true;
3824}
3825
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003826/// getShuffleVPERMILPSImmediate - Return the appropriate immediate to shuffle
3827/// the specified VECTOR_MASK mask with VPERMILPS* instructions.
3828static unsigned getShuffleVPERMILPSImmediate(SDNode *N) {
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003829 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3830 EVT VT = SVOp->getValueType(0);
3831
3832 int NumElts = VT.getVectorNumElements();
3833 int NumLanes = VT.getSizeInBits()/128;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003834 int LaneSize = NumElts/NumLanes;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003835
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003836 // Although the mask is equal for both lanes do it twice to get the cases
3837 // where a mask will match because the same mask element is undef on the
3838 // first half but valid on the second. This would get pathological cases
3839 // such as: shuffle <u, 0, 1, 2, 4, 4, 5, 6>, which is completely valid.
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003840 unsigned Mask = 0;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003841 for (int l = 0; l < NumLanes; ++l) {
3842 for (int i = 0; i < LaneSize; ++i) {
3843 int MaskElt = SVOp->getMaskElt(i+(l*LaneSize));
3844 if (MaskElt < 0)
3845 continue;
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003846 if (MaskElt >= LaneSize)
3847 MaskElt -= LaneSize;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003848 Mask |= MaskElt << (i*2);
3849 }
Bruno Cardoso Lopes377baa52011-07-29 01:31:04 +00003850 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003851
3852 return Mask;
3853}
3854
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003855/// getShuffleVPERMILPDImmediate - Return the appropriate immediate to shuffle
3856/// the specified VECTOR_MASK mask with VPERMILPD* instructions.
3857static unsigned getShuffleVPERMILPDImmediate(SDNode *N) {
3858 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3859 EVT VT = SVOp->getValueType(0);
3860
3861 int NumElts = VT.getVectorNumElements();
3862 int NumLanes = VT.getSizeInBits()/128;
3863
3864 unsigned Mask = 0;
3865 int LaneSize = NumElts/NumLanes;
3866 for (int l = 0; l < NumLanes; ++l)
Bruno Cardoso Lopes377baa52011-07-29 01:31:04 +00003867 for (int i = l*LaneSize; i < LaneSize*(l+1); ++i) {
3868 int MaskElt = SVOp->getMaskElt(i);
3869 if (MaskElt < 0)
3870 continue;
3871 Mask |= (MaskElt-l*LaneSize) << i;
3872 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003873
3874 return Mask;
3875}
3876
Evan Cheng017dcc62006-04-21 01:05:10 +00003877/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3878/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003879/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003880static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003881 bool V2IsSplat = false, bool V2IsUndef = false) {
3882 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003883 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003884 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003885
Nate Begeman9008ca62009-04-27 18:41:29 +00003886 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003887 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003888
Nate Begeman9008ca62009-04-27 18:41:29 +00003889 for (int i = 1; i < NumOps; ++i)
3890 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3891 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3892 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003893 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003894
Evan Cheng39623da2006-04-20 08:58:49 +00003895 return true;
3896}
3897
Nate Begeman9008ca62009-04-27 18:41:29 +00003898static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003899 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003900 SmallVector<int, 8> M;
3901 N->getMask(M);
3902 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003903}
3904
Evan Chengd9539472006-04-14 21:59:03 +00003905/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3906/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003907/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
3908bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N,
3909 const X86Subtarget *Subtarget) {
3910 if (!Subtarget->hasSSE3() && !Subtarget->hasAVX())
Evan Chengd9539472006-04-14 21:59:03 +00003911 return false;
3912
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003913 // The second vector must be undef
3914 if (N->getOperand(1).getOpcode() != ISD::UNDEF)
3915 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003916
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003917 EVT VT = N->getValueType(0);
3918 unsigned NumElems = VT.getVectorNumElements();
3919
3920 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3921 (VT.getSizeInBits() == 256 && NumElems != 8))
3922 return false;
3923
3924 // "i+1" is the value the indexed mask element must have
3925 for (unsigned i = 0; i < NumElems; i += 2)
3926 if (!isUndefOrEqual(N->getMaskElt(i), i+1) ||
3927 !isUndefOrEqual(N->getMaskElt(i+1), i+1))
Nate Begeman9008ca62009-04-27 18:41:29 +00003928 return false;
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003929
3930 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003931}
3932
3933/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3934/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003935/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
3936bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N,
3937 const X86Subtarget *Subtarget) {
3938 if (!Subtarget->hasSSE3() && !Subtarget->hasAVX())
Evan Chengd9539472006-04-14 21:59:03 +00003939 return false;
3940
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003941 // The second vector must be undef
3942 if (N->getOperand(1).getOpcode() != ISD::UNDEF)
3943 return false;
3944
3945 EVT VT = N->getValueType(0);
3946 unsigned NumElems = VT.getVectorNumElements();
3947
3948 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3949 (VT.getSizeInBits() == 256 && NumElems != 8))
3950 return false;
3951
3952 // "i" is the value the indexed mask element must have
3953 for (unsigned i = 0; i < NumElems; i += 2)
3954 if (!isUndefOrEqual(N->getMaskElt(i), i) ||
3955 !isUndefOrEqual(N->getMaskElt(i+1), i))
Nate Begeman9008ca62009-04-27 18:41:29 +00003956 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003957
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003958 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003959}
3960
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003961/// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand
3962/// specifies a shuffle of elements that is suitable for input to 256-bit
3963/// version of MOVDDUP.
3964static bool isMOVDDUPYMask(ShuffleVectorSDNode *N,
3965 const X86Subtarget *Subtarget) {
3966 EVT VT = N->getValueType(0);
3967 int NumElts = VT.getVectorNumElements();
3968 bool V2IsUndef = N->getOperand(1).getOpcode() == ISD::UNDEF;
3969
3970 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256 ||
3971 !V2IsUndef || NumElts != 4)
3972 return false;
3973
3974 for (int i = 0; i != NumElts/2; ++i)
3975 if (!isUndefOrEqual(N->getMaskElt(i), 0))
3976 return false;
3977 for (int i = NumElts/2; i != NumElts; ++i)
3978 if (!isUndefOrEqual(N->getMaskElt(i), NumElts/2))
3979 return false;
3980 return true;
3981}
3982
Evan Cheng0b457f02008-09-25 20:50:48 +00003983/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003984/// specifies a shuffle of elements that is suitable for input to 128-bit
3985/// version of MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003986bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003987 EVT VT = N->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00003988
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003989 if (VT.getSizeInBits() != 128)
3990 return false;
3991
3992 int e = VT.getVectorNumElements() / 2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003993 for (int i = 0; i < e; ++i)
3994 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003995 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003996 for (int i = 0; i < e; ++i)
3997 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003998 return false;
3999 return true;
4000}
4001
David Greenec38a03e2011-02-03 15:50:00 +00004002/// isVEXTRACTF128Index - Return true if the specified
4003/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
4004/// suitable for input to VEXTRACTF128.
4005bool X86::isVEXTRACTF128Index(SDNode *N) {
4006 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4007 return false;
4008
4009 // The index should be aligned on a 128-bit boundary.
4010 uint64_t Index =
4011 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4012
4013 unsigned VL = N->getValueType(0).getVectorNumElements();
4014 unsigned VBits = N->getValueType(0).getSizeInBits();
4015 unsigned ElSize = VBits / VL;
4016 bool Result = (Index * ElSize) % 128 == 0;
4017
4018 return Result;
4019}
4020
David Greeneccacdc12011-02-04 16:08:29 +00004021/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
4022/// operand specifies a subvector insert that is suitable for input to
4023/// VINSERTF128.
4024bool X86::isVINSERTF128Index(SDNode *N) {
4025 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4026 return false;
4027
4028 // The index should be aligned on a 128-bit boundary.
4029 uint64_t Index =
4030 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
4031
4032 unsigned VL = N->getValueType(0).getVectorNumElements();
4033 unsigned VBits = N->getValueType(0).getSizeInBits();
4034 unsigned ElSize = VBits / VL;
4035 bool Result = (Index * ElSize) % 128 == 0;
4036
4037 return Result;
4038}
4039
Evan Cheng63d33002006-03-22 08:01:21 +00004040/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004041/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00004042unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004043 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
4044 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
4045
Evan Chengb9df0ca2006-03-22 02:53:00 +00004046 unsigned Shift = (NumOperands == 4) ? 2 : 1;
4047 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00004048 for (int i = 0; i < NumOperands; ++i) {
4049 int Val = SVOp->getMaskElt(NumOperands-i-1);
4050 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00004051 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00004052 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00004053 if (i != NumOperands - 1)
4054 Mask <<= Shift;
4055 }
Evan Cheng63d33002006-03-22 08:01:21 +00004056 return Mask;
4057}
4058
Evan Cheng506d3df2006-03-29 23:07:14 +00004059/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004060/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00004061unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004062 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00004063 unsigned Mask = 0;
4064 // 8 nodes, but we only care about the last 4.
4065 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004066 int Val = SVOp->getMaskElt(i);
4067 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00004068 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00004069 if (i != 4)
4070 Mask <<= 2;
4071 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004072 return Mask;
4073}
4074
4075/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004076/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00004077unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004078 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00004079 unsigned Mask = 0;
4080 // 8 nodes, but we only care about the first 4.
4081 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004082 int Val = SVOp->getMaskElt(i);
4083 if (Val >= 0)
4084 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00004085 if (i != 0)
4086 Mask <<= 2;
4087 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004088 return Mask;
4089}
4090
Nate Begemana09008b2009-10-19 02:17:23 +00004091/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
4092/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
4093unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
4094 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
4095 EVT VVT = N->getValueType(0);
4096 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
4097 int Val = 0;
4098
4099 unsigned i, e;
4100 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
4101 Val = SVOp->getMaskElt(i);
4102 if (Val >= 0)
4103 break;
4104 }
Eli Friedman63f8dde2011-07-25 21:36:45 +00004105 assert(Val - i > 0 && "PALIGNR imm should be positive");
Nate Begemana09008b2009-10-19 02:17:23 +00004106 return (Val - i) * EltSize;
4107}
4108
David Greenec38a03e2011-02-03 15:50:00 +00004109/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
4110/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
4111/// instructions.
4112unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
4113 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4114 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
4115
4116 uint64_t Index =
4117 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4118
4119 EVT VecVT = N->getOperand(0).getValueType();
4120 EVT ElVT = VecVT.getVectorElementType();
4121
4122 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004123 return Index / NumElemsPerChunk;
4124}
4125
David Greeneccacdc12011-02-04 16:08:29 +00004126/// getInsertVINSERTF128Immediate - Return the appropriate immediate
4127/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
4128/// instructions.
4129unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
4130 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4131 llvm_unreachable("Illegal insert subvector for VINSERTF128");
4132
4133 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00004134 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00004135
4136 EVT VecVT = N->getValueType(0);
4137 EVT ElVT = VecVT.getVectorElementType();
4138
4139 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004140 return Index / NumElemsPerChunk;
4141}
4142
Evan Cheng37b73872009-07-30 08:33:02 +00004143/// isZeroNode - Returns true if Elt is a constant zero or a floating point
4144/// constant +0.0.
4145bool X86::isZeroNode(SDValue Elt) {
4146 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00004147 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00004148 (isa<ConstantFPSDNode>(Elt) &&
4149 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
4150}
4151
Nate Begeman9008ca62009-04-27 18:41:29 +00004152/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
4153/// their permute mask.
4154static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
4155 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004156 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004157 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004158 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00004159
Nate Begeman5a5ca152009-04-29 05:20:52 +00004160 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004161 int idx = SVOp->getMaskElt(i);
4162 if (idx < 0)
4163 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004164 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00004165 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004166 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004167 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004168 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004169 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
4170 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004171}
4172
Evan Cheng779ccea2007-12-07 21:30:01 +00004173/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
4174/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00004175static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00004176 unsigned NumElems = VT.getVectorNumElements();
4177 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004178 int idx = Mask[i];
4179 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004180 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004181 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00004182 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004183 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004184 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004185 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004186}
4187
Evan Cheng533a0aa2006-04-19 20:35:22 +00004188/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
4189/// match movhlps. The lower half elements should come from upper half of
4190/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004191/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00004192static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004193 EVT VT = Op->getValueType(0);
4194 if (VT.getSizeInBits() != 128)
4195 return false;
4196 if (VT.getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00004197 return false;
4198 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004199 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004200 return false;
4201 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004202 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004203 return false;
4204 return true;
4205}
4206
Evan Cheng5ced1d82006-04-06 23:23:56 +00004207/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00004208/// is promoted to a vector. It also returns the LoadSDNode by reference if
4209/// required.
4210static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00004211 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
4212 return false;
4213 N = N->getOperand(0).getNode();
4214 if (!ISD::isNON_EXTLoad(N))
4215 return false;
4216 if (LD)
4217 *LD = cast<LoadSDNode>(N);
4218 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004219}
4220
Evan Cheng533a0aa2006-04-19 20:35:22 +00004221/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4222/// match movlp{s|d}. The lower half elements should come from lower half of
4223/// V1 (and in order), and the upper half elements should come from the upper
4224/// half of V2 (and in order). And since V1 will become the source of the
4225/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004226static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
4227 ShuffleVectorSDNode *Op) {
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004228 EVT VT = Op->getValueType(0);
4229 if (VT.getSizeInBits() != 128)
4230 return false;
4231
Evan Cheng466685d2006-10-09 20:57:25 +00004232 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004233 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00004234 // Is V2 is a vector load, don't do this transformation. We will try to use
4235 // load folding shufps op.
4236 if (ISD::isNON_EXTLoad(V2))
4237 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004238
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004239 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004240
Evan Cheng533a0aa2006-04-19 20:35:22 +00004241 if (NumElems != 2 && NumElems != 4)
4242 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004243 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004244 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004245 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004246 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004247 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004248 return false;
4249 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004250}
4251
Evan Cheng39623da2006-04-20 08:58:49 +00004252/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4253/// all the same.
4254static bool isSplatVector(SDNode *N) {
4255 if (N->getOpcode() != ISD::BUILD_VECTOR)
4256 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004257
Dan Gohman475871a2008-07-27 21:46:04 +00004258 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00004259 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4260 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00004261 return false;
4262 return true;
4263}
4264
Evan Cheng213d2cf2007-05-17 18:45:50 +00004265/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00004266/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00004267/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00004268static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00004269 SDValue V1 = N->getOperand(0);
4270 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004271 unsigned NumElems = N->getValueType(0).getVectorNumElements();
4272 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004273 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004274 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004275 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00004276 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4277 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004278 if (Opc != ISD::BUILD_VECTOR ||
4279 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00004280 return false;
4281 } else if (Idx >= 0) {
4282 unsigned Opc = V1.getOpcode();
4283 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4284 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004285 if (Opc != ISD::BUILD_VECTOR ||
4286 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00004287 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00004288 }
4289 }
4290 return true;
4291}
4292
4293/// getZeroVector - Returns a vector of specified type with all zero elements.
4294///
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004295static SDValue getZeroVector(EVT VT, bool HasXMMInt, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00004296 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004297 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004298
Dale Johannesen0488fb62010-09-30 23:57:10 +00004299 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004300 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00004301 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00004302 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004303 if (HasXMMInt) { // SSE2
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004304 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4305 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4306 } else { // SSE1
4307 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4308 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4309 }
4310 } else if (VT.getSizeInBits() == 256) { // AVX
4311 // 256-bit logic and arithmetic instructions in AVX are
4312 // all floating-point, no support for integer ops. Default
4313 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00004314 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004315 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4316 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00004317 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004318 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004319}
4320
Chris Lattner8a594482007-11-25 00:24:49 +00004321/// getOnesVector - Returns a vector of specified type with all bits set.
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004322/// Always build ones vectors as <4 x i32>. For 256-bit types, use two
4323/// <4 x i32> inserted in a <8 x i32> appropriately. Then bitcast to their
4324/// original type, ensuring they get CSE'd.
Owen Andersone50ed302009-08-10 22:56:29 +00004325static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004326 assert(VT.isVector() && "Expected a vector type");
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004327 assert((VT.is128BitVector() || VT.is256BitVector())
4328 && "Expected a 128-bit or 256-bit vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004329
Owen Anderson825b72b2009-08-11 20:47:22 +00004330 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004331 SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
4332 Cst, Cst, Cst, Cst);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004333
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004334 if (VT.is256BitVector()) {
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004335 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, MVT::v8i32),
4336 Vec, DAG.getConstant(0, MVT::i32), DAG, dl);
4337 Vec = Insert128BitVector(InsV, Vec,
4338 DAG.getConstant(4 /* NumElems/2 */, MVT::i32), DAG, dl);
4339 }
4340
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004341 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00004342}
4343
Evan Cheng39623da2006-04-20 08:58:49 +00004344/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4345/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00004346static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004347 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004348 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004349
Evan Cheng39623da2006-04-20 08:58:49 +00004350 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00004351 SmallVector<int, 8> MaskVec;
4352 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00004353
Nate Begeman5a5ca152009-04-29 05:20:52 +00004354 for (unsigned i = 0; i != NumElems; ++i) {
4355 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004356 MaskVec[i] = NumElems;
4357 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00004358 }
Evan Cheng39623da2006-04-20 08:58:49 +00004359 }
Evan Cheng39623da2006-04-20 08:58:49 +00004360 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00004361 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
4362 SVOp->getOperand(1), &MaskVec[0]);
4363 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00004364}
4365
Evan Cheng017dcc62006-04-21 01:05:10 +00004366/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4367/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00004368static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004369 SDValue V2) {
4370 unsigned NumElems = VT.getVectorNumElements();
4371 SmallVector<int, 8> Mask;
4372 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00004373 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004374 Mask.push_back(i);
4375 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00004376}
4377
Nate Begeman9008ca62009-04-27 18:41:29 +00004378/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004379static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004380 SDValue V2) {
4381 unsigned NumElems = VT.getVectorNumElements();
4382 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00004383 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004384 Mask.push_back(i);
4385 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00004386 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004387 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00004388}
4389
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004390/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004391static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004392 SDValue V2) {
4393 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00004394 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00004395 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00004396 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004397 Mask.push_back(i + Half);
4398 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00004399 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004400 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004401}
4402
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004403// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004404// a generic shuffle instruction because the target has no such instructions.
4405// Generate shuffles which repeat i16 and i8 several times until they can be
4406// represented by v4f32 and then be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004407static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004408 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004409 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004410 DebugLoc dl = V.getDebugLoc();
Rafael Espindola15684b22009-04-24 12:40:33 +00004411
Nate Begeman9008ca62009-04-27 18:41:29 +00004412 while (NumElems > 4) {
4413 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004414 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004415 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004416 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004417 EltNo -= NumElems/2;
4418 }
4419 NumElems >>= 1;
4420 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004421 return V;
4422}
Eric Christopherfd179292009-08-27 18:07:15 +00004423
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004424/// getLegalSplat - Generate a legal splat with supported x86 shuffles
4425static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
4426 EVT VT = V.getValueType();
4427 DebugLoc dl = V.getDebugLoc();
4428 assert((VT.getSizeInBits() == 128 || VT.getSizeInBits() == 256)
4429 && "Vector size not supported");
4430
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004431 if (VT.getSizeInBits() == 128) {
4432 V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004433 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004434 V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
4435 &SplatMask[0]);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004436 } else {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004437 // To use VPERMILPS to splat scalars, the second half of indicies must
4438 // refer to the higher part, which is a duplication of the lower one,
4439 // because VPERMILPS can only handle in-lane permutations.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004440 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
4441 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004442
4443 V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
4444 V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
4445 &SplatMask[0]);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004446 }
4447
4448 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4449}
4450
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004451/// PromoteSplat - Splat is promoted to target supported vector shuffles.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004452static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4453 EVT SrcVT = SV->getValueType(0);
4454 SDValue V1 = SV->getOperand(0);
4455 DebugLoc dl = SV->getDebugLoc();
4456
4457 int EltNo = SV->getSplatIndex();
4458 int NumElems = SrcVT.getVectorNumElements();
4459 unsigned Size = SrcVT.getSizeInBits();
4460
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004461 assert(((Size == 128 && NumElems > 4) || Size == 256) &&
4462 "Unknown how to promote splat for type");
4463
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004464 // Extract the 128-bit part containing the splat element and update
4465 // the splat element index when it refers to the higher register.
4466 if (Size == 256) {
4467 unsigned Idx = (EltNo > NumElems/2) ? NumElems/2 : 0;
4468 V1 = Extract128BitVector(V1, DAG.getConstant(Idx, MVT::i32), DAG, dl);
4469 if (Idx > 0)
4470 EltNo -= NumElems/2;
4471 }
4472
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004473 // All i16 and i8 vector types can't be used directly by a generic shuffle
4474 // instruction because the target has no such instruction. Generate shuffles
4475 // which repeat i16 and i8 several times until they fit in i32, and then can
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004476 // be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004477 EVT EltVT = SrcVT.getVectorElementType();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004478 if (EltVT == MVT::i8 || EltVT == MVT::i16)
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004479 V1 = PromoteSplati8i16(V1, DAG, EltNo);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004480
4481 // Recreate the 256-bit vector and place the same 128-bit vector
4482 // into the low and high part. This is necessary because we want
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004483 // to use VPERM* to shuffle the vectors
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004484 if (Size == 256) {
4485 SDValue InsV = Insert128BitVector(DAG.getUNDEF(SrcVT), V1,
4486 DAG.getConstant(0, MVT::i32), DAG, dl);
4487 V1 = Insert128BitVector(InsV, V1,
4488 DAG.getConstant(NumElems/2, MVT::i32), DAG, dl);
4489 }
4490
4491 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004492}
4493
Evan Chengba05f722006-04-21 23:03:30 +00004494/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004495/// vector of zero or undef vector. This produces a shuffle where the low
4496/// element of V2 is swizzled into the zero/undef vector, landing at element
4497/// 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 +00004498static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004499 bool isZero, bool HasXMMInt,
4500 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004501 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00004502 SDValue V1 = isZero
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004503 ? getZeroVector(VT, HasXMMInt, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00004504 unsigned NumElems = VT.getVectorNumElements();
4505 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004506 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004507 // If this is the insertion idx, put the low elt of V2 here.
4508 MaskVec.push_back(i == Idx ? NumElems : i);
4509 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004510}
4511
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004512/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4513/// element of the result of the vector shuffle.
Benjamin Kramer050db522011-03-26 12:38:19 +00004514static SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
4515 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004516 if (Depth == 6)
4517 return SDValue(); // Limit search depth.
4518
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004519 SDValue V = SDValue(N, 0);
4520 EVT VT = V.getValueType();
4521 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004522
4523 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4524 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
4525 Index = SV->getMaskElt(Index);
4526
4527 if (Index < 0)
4528 return DAG.getUNDEF(VT.getVectorElementType());
4529
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004530 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004531 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004532 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004533 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004534
4535 // Recurse into target specific vector shuffles to find scalars.
4536 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004537 int NumElems = VT.getVectorNumElements();
4538 SmallVector<unsigned, 16> ShuffleMask;
4539 SDValue ImmN;
4540
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004541 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004542 case X86ISD::SHUFPS:
4543 case X86ISD::SHUFPD:
4544 ImmN = N->getOperand(N->getNumOperands()-1);
4545 DecodeSHUFPSMask(NumElems,
4546 cast<ConstantSDNode>(ImmN)->getZExtValue(),
4547 ShuffleMask);
4548 break;
4549 case X86ISD::PUNPCKHBW:
4550 case X86ISD::PUNPCKHWD:
4551 case X86ISD::PUNPCKHDQ:
4552 case X86ISD::PUNPCKHQDQ:
4553 DecodePUNPCKHMask(NumElems, ShuffleMask);
4554 break;
4555 case X86ISD::UNPCKHPS:
4556 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00004557 case X86ISD::VUNPCKHPSY:
4558 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004559 DecodeUNPCKHPMask(NumElems, ShuffleMask);
4560 break;
4561 case X86ISD::PUNPCKLBW:
4562 case X86ISD::PUNPCKLWD:
4563 case X86ISD::PUNPCKLDQ:
4564 case X86ISD::PUNPCKLQDQ:
David Greenec4db4e52011-02-28 19:06:56 +00004565 DecodePUNPCKLMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004566 break;
4567 case X86ISD::UNPCKLPS:
4568 case X86ISD::UNPCKLPD:
David Greenec4db4e52011-02-28 19:06:56 +00004569 case X86ISD::VUNPCKLPSY:
4570 case X86ISD::VUNPCKLPDY:
4571 DecodeUNPCKLPMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004572 break;
4573 case X86ISD::MOVHLPS:
4574 DecodeMOVHLPSMask(NumElems, ShuffleMask);
4575 break;
4576 case X86ISD::MOVLHPS:
4577 DecodeMOVLHPSMask(NumElems, ShuffleMask);
4578 break;
4579 case X86ISD::PSHUFD:
4580 ImmN = N->getOperand(N->getNumOperands()-1);
4581 DecodePSHUFMask(NumElems,
4582 cast<ConstantSDNode>(ImmN)->getZExtValue(),
4583 ShuffleMask);
4584 break;
4585 case X86ISD::PSHUFHW:
4586 ImmN = N->getOperand(N->getNumOperands()-1);
4587 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4588 ShuffleMask);
4589 break;
4590 case X86ISD::PSHUFLW:
4591 ImmN = N->getOperand(N->getNumOperands()-1);
4592 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4593 ShuffleMask);
4594 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004595 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004596 case X86ISD::MOVSD: {
4597 // The index 0 always comes from the first element of the second source,
4598 // this is why MOVSS and MOVSD are used in the first place. The other
4599 // elements come from the other positions of the first source vector.
4600 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004601 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
4602 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004603 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00004604 case X86ISD::VPERMILPS:
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00004605 ImmN = N->getOperand(N->getNumOperands()-1);
Bruno Cardoso Lopes2eb4c2b2011-07-29 01:31:11 +00004606 DecodeVPERMILPSMask(4, cast<ConstantSDNode>(ImmN)->getZExtValue(),
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00004607 ShuffleMask);
Bruno Cardoso Lopes2eb4c2b2011-07-29 01:31:11 +00004608 break;
4609 case X86ISD::VPERMILPSY:
4610 ImmN = N->getOperand(N->getNumOperands()-1);
4611 DecodeVPERMILPSMask(8, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4612 ShuffleMask);
4613 break;
4614 case X86ISD::VPERMILPD:
4615 ImmN = N->getOperand(N->getNumOperands()-1);
4616 DecodeVPERMILPDMask(2, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4617 ShuffleMask);
4618 break;
4619 case X86ISD::VPERMILPDY:
4620 ImmN = N->getOperand(N->getNumOperands()-1);
4621 DecodeVPERMILPDMask(4, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4622 ShuffleMask);
4623 break;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00004624 case X86ISD::VPERM2F128:
4625 ImmN = N->getOperand(N->getNumOperands()-1);
4626 DecodeVPERM2F128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4627 ShuffleMask);
4628 break;
Eli Friedman106f6e72011-09-10 02:01:42 +00004629 case X86ISD::MOVDDUP:
4630 case X86ISD::MOVLHPD:
4631 case X86ISD::MOVLPD:
4632 case X86ISD::MOVLPS:
4633 case X86ISD::MOVSHDUP:
4634 case X86ISD::MOVSLDUP:
4635 case X86ISD::PALIGN:
4636 return SDValue(); // Not yet implemented.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004637 default:
Eli Friedman106f6e72011-09-10 02:01:42 +00004638 assert(0 && "unknown target shuffle node");
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004639 return SDValue();
4640 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004641
4642 Index = ShuffleMask[Index];
4643 if (Index < 0)
4644 return DAG.getUNDEF(VT.getVectorElementType());
4645
4646 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
4647 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
4648 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004649 }
4650
4651 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004652 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004653 V = V.getOperand(0);
4654 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004655 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004656
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004657 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004658 return SDValue();
4659 }
4660
4661 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4662 return (Index == 0) ? V.getOperand(0)
4663 : DAG.getUNDEF(VT.getVectorElementType());
4664
4665 if (V.getOpcode() == ISD::BUILD_VECTOR)
4666 return V.getOperand(Index);
4667
4668 return SDValue();
4669}
4670
4671/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4672/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004673/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004674static
4675unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
4676 bool ZerosFromLeft, SelectionDAG &DAG) {
4677 int i = 0;
4678
4679 while (i < NumElems) {
4680 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004681 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004682 if (!(Elt.getNode() &&
4683 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4684 break;
4685 ++i;
4686 }
4687
4688 return i;
4689}
4690
4691/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
4692/// MaskE correspond consecutively to elements from one of the vector operands,
4693/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4694static
4695bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
4696 int OpIdx, int NumElems, unsigned &OpNum) {
4697 bool SeenV1 = false;
4698 bool SeenV2 = false;
4699
4700 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
4701 int Idx = SVOp->getMaskElt(i);
4702 // Ignore undef indicies
4703 if (Idx < 0)
4704 continue;
4705
4706 if (Idx < NumElems)
4707 SeenV1 = true;
4708 else
4709 SeenV2 = true;
4710
4711 // Only accept consecutive elements from the same vector
4712 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4713 return false;
4714 }
4715
4716 OpNum = SeenV1 ? 0 : 1;
4717 return true;
4718}
4719
4720/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4721/// logical left shift of a vector.
4722static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4723 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4724 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4725 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4726 false /* check zeros from right */, DAG);
4727 unsigned OpSrc;
4728
4729 if (!NumZeros)
4730 return false;
4731
4732 // Considering the elements in the mask that are not consecutive zeros,
4733 // check if they consecutively come from only one of the source vectors.
4734 //
4735 // V1 = {X, A, B, C} 0
4736 // \ \ \ /
4737 // vector_shuffle V1, V2 <1, 2, 3, X>
4738 //
4739 if (!isShuffleMaskConsecutive(SVOp,
4740 0, // Mask Start Index
4741 NumElems-NumZeros-1, // Mask End Index
4742 NumZeros, // Where to start looking in the src vector
4743 NumElems, // Number of elements in vector
4744 OpSrc)) // Which source operand ?
4745 return false;
4746
4747 isLeft = false;
4748 ShAmt = NumZeros;
4749 ShVal = SVOp->getOperand(OpSrc);
4750 return true;
4751}
4752
4753/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4754/// logical left shift of a vector.
4755static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4756 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4757 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4758 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4759 true /* check zeros from left */, DAG);
4760 unsigned OpSrc;
4761
4762 if (!NumZeros)
4763 return false;
4764
4765 // Considering the elements in the mask that are not consecutive zeros,
4766 // check if they consecutively come from only one of the source vectors.
4767 //
4768 // 0 { A, B, X, X } = V2
4769 // / \ / /
4770 // vector_shuffle V1, V2 <X, X, 4, 5>
4771 //
4772 if (!isShuffleMaskConsecutive(SVOp,
4773 NumZeros, // Mask Start Index
4774 NumElems-1, // Mask End Index
4775 0, // Where to start looking in the src vector
4776 NumElems, // Number of elements in vector
4777 OpSrc)) // Which source operand ?
4778 return false;
4779
4780 isLeft = true;
4781 ShAmt = NumZeros;
4782 ShVal = SVOp->getOperand(OpSrc);
4783 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004784}
4785
4786/// isVectorShift - Returns true if the shuffle can be implemented as a
4787/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004788static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004789 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004790 // Although the logic below support any bitwidth size, there are no
4791 // shift instructions which handle more than 128-bit vectors.
4792 if (SVOp->getValueType(0).getSizeInBits() > 128)
4793 return false;
4794
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004795 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4796 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4797 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004798
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004799 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004800}
4801
Evan Chengc78d3b42006-04-24 18:01:45 +00004802/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4803///
Dan Gohman475871a2008-07-27 21:46:04 +00004804static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004805 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004806 SelectionDAG &DAG,
4807 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004808 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004809 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004810
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004811 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004812 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004813 bool First = true;
4814 for (unsigned i = 0; i < 16; ++i) {
4815 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4816 if (ThisIsNonZero && First) {
4817 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004818 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004819 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004820 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004821 First = false;
4822 }
4823
4824 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004825 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004826 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4827 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004828 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004829 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004830 }
4831 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004832 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4833 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4834 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004835 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004836 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004837 } else
4838 ThisElt = LastElt;
4839
Gabor Greifba36cb52008-08-28 21:40:38 +00004840 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004841 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004842 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004843 }
4844 }
4845
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004846 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004847}
4848
Bill Wendlinga348c562007-03-22 18:42:45 +00004849/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004850///
Dan Gohman475871a2008-07-27 21:46:04 +00004851static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004852 unsigned NumNonZero, unsigned NumZero,
4853 SelectionDAG &DAG,
4854 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004855 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004856 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004857
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004858 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004859 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004860 bool First = true;
4861 for (unsigned i = 0; i < 8; ++i) {
4862 bool isNonZero = (NonZeros & (1 << i)) != 0;
4863 if (isNonZero) {
4864 if (First) {
4865 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004866 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004867 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004868 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004869 First = false;
4870 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004871 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004872 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004873 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004874 }
4875 }
4876
4877 return V;
4878}
4879
Evan Chengf26ffe92008-05-29 08:22:04 +00004880/// getVShift - Return a vector logical shift node.
4881///
Owen Andersone50ed302009-08-10 22:56:29 +00004882static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004883 unsigned NumBits, SelectionDAG &DAG,
4884 const TargetLowering &TLI, DebugLoc dl) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004885 assert(VT.getSizeInBits() == 128 && "Unknown type for VShift");
Dale Johannesen0488fb62010-09-30 23:57:10 +00004886 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00004887 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004888 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4889 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004890 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004891 DAG.getConstant(NumBits,
4892 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004893}
4894
Dan Gohman475871a2008-07-27 21:46:04 +00004895SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004896X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004897 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004898
Evan Chengc3630942009-12-09 21:00:30 +00004899 // Check if the scalar load can be widened into a vector load. And if
4900 // the address is "base + cst" see if the cst can be "absorbed" into
4901 // the shuffle mask.
4902 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4903 SDValue Ptr = LD->getBasePtr();
4904 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4905 return SDValue();
4906 EVT PVT = LD->getValueType(0);
4907 if (PVT != MVT::i32 && PVT != MVT::f32)
4908 return SDValue();
4909
4910 int FI = -1;
4911 int64_t Offset = 0;
4912 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4913 FI = FINode->getIndex();
4914 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004915 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004916 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4917 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4918 Offset = Ptr.getConstantOperandVal(1);
4919 Ptr = Ptr.getOperand(0);
4920 } else {
4921 return SDValue();
4922 }
4923
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004924 // FIXME: 256-bit vector instructions don't require a strict alignment,
4925 // improve this code to support it better.
4926 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00004927 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004928 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00004929 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004930 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00004931 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004932 // Can't change the alignment. FIXME: It's possible to compute
4933 // the exact stack offset and reference FI + adjust offset instead.
4934 // If someone *really* cares about this. That's the way to implement it.
4935 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004936 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004937 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00004938 }
4939 }
4940
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004941 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00004942 // Ptr + (Offset & ~15).
4943 if (Offset < 0)
4944 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004945 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00004946 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004947 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00004948 if (StartOffset)
4949 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4950 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4951
4952 int EltNo = (Offset - StartOffset) >> 2;
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004953 int NumElems = VT.getVectorNumElements();
4954
4955 EVT CanonVT = VT.getSizeInBits() == 128 ? MVT::v4i32 : MVT::v8i32;
4956 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
4957 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00004958 LD->getPointerInfo().getWithOffset(StartOffset),
David Greene67c9d422010-02-15 16:53:33 +00004959 false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004960
4961 // Canonicalize it to a v4i32 or v8i32 shuffle.
4962 SmallVector<int, 8> Mask;
4963 for (int i = 0; i < NumElems; ++i)
4964 Mask.push_back(EltNo);
4965
4966 V1 = DAG.getNode(ISD::BITCAST, dl, CanonVT, V1);
4967 return DAG.getNode(ISD::BITCAST, dl, NVT,
4968 DAG.getVectorShuffle(CanonVT, dl, V1,
4969 DAG.getUNDEF(CanonVT),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00004970 }
4971
4972 return SDValue();
4973}
4974
Michael J. Spencerec38de22010-10-10 22:04:20 +00004975/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4976/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004977/// load which has the same value as a build_vector whose operands are 'elts'.
4978///
4979/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004980///
Nate Begeman1449f292010-03-24 22:19:06 +00004981/// FIXME: we'd also like to handle the case where the last elements are zero
4982/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4983/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004984static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004985 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004986 EVT EltVT = VT.getVectorElementType();
4987 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004988
Nate Begemanfdea31a2010-03-24 20:49:50 +00004989 LoadSDNode *LDBase = NULL;
4990 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004991
Nate Begeman1449f292010-03-24 22:19:06 +00004992 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004993 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004994 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004995 for (unsigned i = 0; i < NumElems; ++i) {
4996 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004997
Nate Begemanfdea31a2010-03-24 20:49:50 +00004998 if (!Elt.getNode() ||
4999 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
5000 return SDValue();
5001 if (!LDBase) {
5002 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
5003 return SDValue();
5004 LDBase = cast<LoadSDNode>(Elt.getNode());
5005 LastLoadedElt = i;
5006 continue;
5007 }
5008 if (Elt.getOpcode() == ISD::UNDEF)
5009 continue;
5010
5011 LoadSDNode *LD = cast<LoadSDNode>(Elt);
5012 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
5013 return SDValue();
5014 LastLoadedElt = i;
5015 }
Nate Begeman1449f292010-03-24 22:19:06 +00005016
5017 // If we have found an entire vector of loads and undefs, then return a large
5018 // load of the entire vector width starting at the base pointer. If we found
5019 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005020 if (LastLoadedElt == NumElems - 1) {
5021 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00005022 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005023 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00005024 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00005025 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005026 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00005027 LDBase->isVolatile(), LDBase->isNonTemporal(),
5028 LDBase->getAlignment());
Eli Friedman61cc47e2011-07-26 21:02:58 +00005029 } else if (NumElems == 4 && LastLoadedElt == 1 &&
5030 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005031 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
5032 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Eli Friedman322ea082011-09-14 23:42:45 +00005033 SDValue ResNode =
5034 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys, Ops, 2, MVT::i64,
5035 LDBase->getPointerInfo(),
5036 LDBase->getAlignment(),
5037 false/*isVolatile*/, true/*ReadMem*/,
5038 false/*WriteMem*/);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005039 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00005040 }
5041 return SDValue();
5042}
5043
Evan Chengc3630942009-12-09 21:00:30 +00005044SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005045X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005046 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00005047
David Greenef125a292011-02-08 19:04:41 +00005048 EVT VT = Op.getValueType();
5049 EVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00005050 unsigned NumElems = Op.getNumOperands();
5051
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005052 // Vectors containing all zeros can be matched by pxor and xorps later
5053 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5054 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
5055 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00005056 if (Op.getValueType() == MVT::v4i32 ||
5057 Op.getValueType() == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00005058 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005059
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005060 return getZeroVector(Op.getValueType(), Subtarget->hasXMMInt(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00005061 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005062
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005063 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
5064 // vectors or broken into v4i32 operations on 256-bit vectors.
5065 if (ISD::isBuildVectorAllOnes(Op.getNode())) {
5066 if (Op.getValueType() == MVT::v4i32)
5067 return Op;
5068
5069 return getOnesVector(Op.getValueType(), DAG, dl);
5070 }
5071
Owen Andersone50ed302009-08-10 22:56:29 +00005072 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005073
Evan Cheng0db9fe62006-04-25 20:13:52 +00005074 unsigned NumZero = 0;
5075 unsigned NumNonZero = 0;
5076 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005077 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005078 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005079 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005080 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00005081 if (Elt.getOpcode() == ISD::UNDEF)
5082 continue;
5083 Values.insert(Elt);
5084 if (Elt.getOpcode() != ISD::Constant &&
5085 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005086 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00005087 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00005088 NumZero++;
5089 else {
5090 NonZeros |= (1 << i);
5091 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005092 }
5093 }
5094
Chris Lattner97a2a562010-08-26 05:24:29 +00005095 // All undef vector. Return an UNDEF. All zero vectors were handled above.
5096 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00005097 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005098
Chris Lattner67f453a2008-03-09 05:42:06 +00005099 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00005100 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005101 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00005102 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005103
Chris Lattner62098042008-03-09 01:05:04 +00005104 // If this is an insertion of an i64 value on x86-32, and if the top bits of
5105 // the value are obviously zero, truncate the value to i32 and do the
5106 // insertion that way. Only do this if the value is non-constant or if the
5107 // value is a constant being inserted into element 0. It is cheaper to do
5108 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00005109 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00005110 (!IsAllConstants || Idx == 0)) {
5111 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00005112 // Handle SSE only.
5113 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
5114 EVT VecVT = MVT::v4i32;
5115 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00005116
Chris Lattner62098042008-03-09 01:05:04 +00005117 // Truncate the value (which may itself be a constant) to i32, and
5118 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00005119 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00005120 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00005121 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005122 Subtarget->hasXMMInt(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005123
Chris Lattner62098042008-03-09 01:05:04 +00005124 // Now we have our 32-bit value zero extended in the low element of
5125 // a vector. If Idx != 0, swizzle it into place.
5126 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005127 SmallVector<int, 4> Mask;
5128 Mask.push_back(Idx);
5129 for (unsigned i = 1; i != VecElts; ++i)
5130 Mask.push_back(i);
5131 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00005132 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00005133 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00005134 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005135 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00005136 }
5137 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005138
Chris Lattner19f79692008-03-08 22:59:52 +00005139 // If we have a constant or non-constant insertion into the low element of
5140 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5141 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00005142 // depending on what the source datatype is.
5143 if (Idx == 0) {
5144 if (NumZero == 0) {
5145 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00005146 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
5147 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00005148 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5149 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005150 return getShuffleVectorZeroOrUndef(Item, 0, true,Subtarget->hasXMMInt(),
Eli Friedman10415532009-06-06 06:05:10 +00005151 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00005152 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
5153 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005154 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
5155 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00005156 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
5157 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005158 Subtarget->hasXMMInt(), DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005159 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00005160 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005161 }
Evan Chengf26ffe92008-05-29 08:22:04 +00005162
5163 // Is it a vector logical left shift?
5164 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00005165 X86::isZeroNode(Op.getOperand(0)) &&
5166 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005167 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00005168 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00005169 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005170 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00005171 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005172 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005173
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005174 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00005175 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005176
Chris Lattner19f79692008-03-08 22:59:52 +00005177 // Otherwise, if this is a vector with i32 or f32 elements, and the element
5178 // is a non-constant being inserted into an element other than the low one,
5179 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
5180 // movd/movss) to move this into the low element, then shuffle it into
5181 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005182 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00005183 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00005184
Evan Cheng0db9fe62006-04-25 20:13:52 +00005185 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00005186 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005187 Subtarget->hasXMMInt(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00005188 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005189 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00005190 MaskVec.push_back(i == Idx ? 0 : 1);
5191 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005192 }
5193 }
5194
Chris Lattner67f453a2008-03-09 05:42:06 +00005195 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00005196 if (Values.size() == 1) {
5197 if (EVTBits == 32) {
5198 // Instead of a shuffle like this:
5199 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5200 // Check if it's possible to issue this instead.
5201 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
5202 unsigned Idx = CountTrailingZeros_32(NonZeros);
5203 SDValue Item = Op.getOperand(Idx);
5204 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5205 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5206 }
Dan Gohman475871a2008-07-27 21:46:04 +00005207 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005208 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005209
Dan Gohmana3941172007-07-24 22:55:08 +00005210 // A vector full of immediates; various special cases are already
5211 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005212 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00005213 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00005214
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005215 // For AVX-length vectors, build the individual 128-bit pieces and use
5216 // shuffles to put them in place.
5217 if (VT.getSizeInBits() == 256 && !ISD::isBuildVectorAllZeros(Op.getNode())) {
5218 SmallVector<SDValue, 32> V;
5219 for (unsigned i = 0; i < NumElems; ++i)
5220 V.push_back(Op.getOperand(i));
5221
5222 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
5223
5224 // Build both the lower and upper subvector.
5225 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
5226 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
5227 NumElems/2);
5228
5229 // Recreate the wider vector with the lower and upper part.
Bruno Cardoso Lopes15d03fb2011-07-28 01:26:53 +00005230 SDValue Vec = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), Lower,
5231 DAG.getConstant(0, MVT::i32), DAG, dl);
5232 return Insert128BitVector(Vec, Upper, DAG.getConstant(NumElems/2, MVT::i32),
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005233 DAG, dl);
5234 }
5235
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005236 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005237 if (EVTBits == 64) {
5238 if (NumNonZero == 1) {
5239 // One half is zero or undef.
5240 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005241 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005242 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00005243 return getShuffleVectorZeroOrUndef(V2, Idx, true,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005244 Subtarget->hasXMMInt(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005245 }
Dan Gohman475871a2008-07-27 21:46:04 +00005246 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005247 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005248
5249 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005250 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005251 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00005252 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005253 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005254 }
5255
Bill Wendling826f36f2007-03-28 00:57:11 +00005256 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005257 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00005258 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005259 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005260 }
5261
5262 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00005263 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00005264 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005265 if (NumElems == 4 && NumZero > 0) {
5266 for (unsigned i = 0; i < 4; ++i) {
5267 bool isZero = !(NonZeros & (1 << i));
5268 if (isZero)
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005269 V[i] = getZeroVector(VT, Subtarget->hasXMMInt(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005270 else
Dale Johannesenace16102009-02-03 19:33:06 +00005271 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005272 }
5273
5274 for (unsigned i = 0; i < 2; ++i) {
5275 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5276 default: break;
5277 case 0:
5278 V[i] = V[i*2]; // Must be a zero vector.
5279 break;
5280 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005281 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005282 break;
5283 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005284 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005285 break;
5286 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005287 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005288 break;
5289 }
5290 }
5291
Nate Begeman9008ca62009-04-27 18:41:29 +00005292 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005293 bool Reverse = (NonZeros & 0x3) == 2;
5294 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005295 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005296 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5297 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005298 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
5299 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005300 }
5301
Nate Begemanfdea31a2010-03-24 20:49:50 +00005302 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
5303 // Check for a build vector of consecutive loads.
5304 for (unsigned i = 0; i < NumElems; ++i)
5305 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005306
Nate Begemanfdea31a2010-03-24 20:49:50 +00005307 // Check for elements which are consecutive loads.
5308 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5309 if (LD.getNode())
5310 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005311
5312 // For SSE 4.1, use insertps to put the high elements into the low element.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005313 if (getSubtarget()->hasSSE41() || getSubtarget()->hasAVX()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005314 SDValue Result;
5315 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5316 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5317 else
5318 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005319
Chris Lattner24faf612010-08-28 17:59:08 +00005320 for (unsigned i = 1; i < NumElems; ++i) {
5321 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5322 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005323 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005324 }
5325 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005326 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005327
Chris Lattner6e80e442010-08-28 17:15:43 +00005328 // Otherwise, expand into a number of unpckl*, start by extending each of
5329 // our (non-undef) elements to the full vector width with the element in the
5330 // bottom slot of the vector (which generates no code for SSE).
5331 for (unsigned i = 0; i < NumElems; ++i) {
5332 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5333 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5334 else
5335 V[i] = DAG.getUNDEF(VT);
5336 }
5337
5338 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005339 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5340 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5341 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005342 unsigned EltStride = NumElems >> 1;
5343 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005344 for (unsigned i = 0; i < EltStride; ++i) {
5345 // If V[i+EltStride] is undef and this is the first round of mixing,
5346 // then it is safe to just drop this shuffle: V[i] is already in the
5347 // right place, the one element (since it's the first round) being
5348 // inserted as undef can be dropped. This isn't safe for successive
5349 // rounds because they will permute elements within both vectors.
5350 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5351 EltStride == NumElems/2)
5352 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005353
Chris Lattner6e80e442010-08-28 17:15:43 +00005354 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005355 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005356 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005357 }
5358 return V[0];
5359 }
Dan Gohman475871a2008-07-27 21:46:04 +00005360 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005361}
5362
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005363// LowerMMXCONCAT_VECTORS - We support concatenate two MMX registers and place
5364// them in a MMX register. This is better than doing a stack convert.
5365static SDValue LowerMMXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005366 DebugLoc dl = Op.getDebugLoc();
5367 EVT ResVT = Op.getValueType();
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005368
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005369 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
5370 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
5371 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005372 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005373 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
5374 InVec = Op.getOperand(1);
5375 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5376 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005377 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005378 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
5379 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
5380 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005381 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005382 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
5383 Mask[0] = 0; Mask[1] = 2;
5384 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
5385 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005386 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005387}
5388
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005389// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5390// to create 256-bit vectors from two other 128-bit ones.
5391static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5392 DebugLoc dl = Op.getDebugLoc();
5393 EVT ResVT = Op.getValueType();
5394
5395 assert(ResVT.getSizeInBits() == 256 && "Value type must be 256-bit wide");
5396
5397 SDValue V1 = Op.getOperand(0);
5398 SDValue V2 = Op.getOperand(1);
5399 unsigned NumElems = ResVT.getVectorNumElements();
5400
5401 SDValue V = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, ResVT), V1,
5402 DAG.getConstant(0, MVT::i32), DAG, dl);
5403 return Insert128BitVector(V, V2, DAG.getConstant(NumElems/2, MVT::i32),
5404 DAG, dl);
5405}
5406
5407SDValue
5408X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005409 EVT ResVT = Op.getValueType();
5410
5411 assert(Op.getNumOperands() == 2);
5412 assert((ResVT.getSizeInBits() == 128 || ResVT.getSizeInBits() == 256) &&
5413 "Unsupported CONCAT_VECTORS for value type");
5414
5415 // We support concatenate two MMX registers and place them in a MMX register.
5416 // This is better than doing a stack convert.
5417 if (ResVT.is128BitVector())
5418 return LowerMMXCONCAT_VECTORS(Op, DAG);
5419
5420 // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
5421 // from two other 128-bit ones.
5422 return LowerAVXCONCAT_VECTORS(Op, DAG);
5423}
5424
Nate Begemanb9a47b82009-02-23 08:49:38 +00005425// v8i16 shuffles - Prefer shuffles in the following order:
5426// 1. [all] pshuflw, pshufhw, optional move
5427// 2. [ssse3] 1 x pshufb
5428// 3. [ssse3] 2 x pshufb + 1 x por
5429// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005430SDValue
5431X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
5432 SelectionDAG &DAG) const {
5433 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00005434 SDValue V1 = SVOp->getOperand(0);
5435 SDValue V2 = SVOp->getOperand(1);
5436 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005437 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00005438
Nate Begemanb9a47b82009-02-23 08:49:38 +00005439 // Determine if more than 1 of the words in each of the low and high quadwords
5440 // of the result come from the same quadword of one of the two inputs. Undef
5441 // mask values count as coming from any quadword, for better codegen.
Benjamin Kramer003fad92011-10-15 13:28:31 +00005442 unsigned LoQuad[] = { 0, 0, 0, 0 };
5443 unsigned HiQuad[] = { 0, 0, 0, 0 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005444 BitVector InputQuads(4);
5445 for (unsigned i = 0; i < 8; ++i) {
Benjamin Kramer003fad92011-10-15 13:28:31 +00005446 unsigned *Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00005447 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005448 MaskVals.push_back(EltIdx);
5449 if (EltIdx < 0) {
5450 ++Quad[0];
5451 ++Quad[1];
5452 ++Quad[2];
5453 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00005454 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005455 }
5456 ++Quad[EltIdx / 4];
5457 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00005458 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005459
Nate Begemanb9a47b82009-02-23 08:49:38 +00005460 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005461 unsigned MaxQuad = 1;
5462 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005463 if (LoQuad[i] > MaxQuad) {
5464 BestLoQuad = i;
5465 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005466 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005467 }
5468
Nate Begemanb9a47b82009-02-23 08:49:38 +00005469 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005470 MaxQuad = 1;
5471 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005472 if (HiQuad[i] > MaxQuad) {
5473 BestHiQuad = i;
5474 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005475 }
5476 }
5477
Nate Begemanb9a47b82009-02-23 08:49:38 +00005478 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00005479 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00005480 // single pshufb instruction is necessary. If There are more than 2 input
5481 // quads, disable the next transformation since it does not help SSSE3.
5482 bool V1Used = InputQuads[0] || InputQuads[1];
5483 bool V2Used = InputQuads[2] || InputQuads[3];
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005484 if (Subtarget->hasSSSE3() || Subtarget->hasAVX()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005485 if (InputQuads.count() == 2 && V1Used && V2Used) {
5486 BestLoQuad = InputQuads.find_first();
5487 BestHiQuad = InputQuads.find_next(BestLoQuad);
5488 }
5489 if (InputQuads.count() > 2) {
5490 BestLoQuad = -1;
5491 BestHiQuad = -1;
5492 }
5493 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005494
Nate Begemanb9a47b82009-02-23 08:49:38 +00005495 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5496 // the shuffle mask. If a quad is scored as -1, that means that it contains
5497 // words from all 4 input quadwords.
5498 SDValue NewV;
5499 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005500 SmallVector<int, 8> MaskV;
5501 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
5502 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00005503 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005504 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
5505 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
5506 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005507
Nate Begemanb9a47b82009-02-23 08:49:38 +00005508 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5509 // source words for the shuffle, to aid later transformations.
5510 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00005511 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00005512 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005513 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00005514 if (idx != (int)i)
5515 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005516 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00005517 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005518 AllWordsInNewV = false;
5519 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00005520 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005521
Nate Begemanb9a47b82009-02-23 08:49:38 +00005522 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5523 if (AllWordsInNewV) {
5524 for (int i = 0; i != 8; ++i) {
5525 int idx = MaskVals[i];
5526 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005527 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005528 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005529 if ((idx != i) && idx < 4)
5530 pshufhw = false;
5531 if ((idx != i) && idx > 3)
5532 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00005533 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005534 V1 = NewV;
5535 V2Used = false;
5536 BestLoQuad = 0;
5537 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005538 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005539
Nate Begemanb9a47b82009-02-23 08:49:38 +00005540 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5541 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005542 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005543 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5544 unsigned TargetMask = 0;
5545 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005546 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005547 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
5548 X86::getShufflePSHUFLWImmediate(NewV.getNode());
5549 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005550 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005551 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005552 }
Eric Christopherfd179292009-08-27 18:07:15 +00005553
Nate Begemanb9a47b82009-02-23 08:49:38 +00005554 // If we have SSSE3, and all words of the result are from 1 input vector,
5555 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5556 // is present, fall back to case 4.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005557 if (Subtarget->hasSSSE3() || Subtarget->hasAVX()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005558 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005559
Nate Begemanb9a47b82009-02-23 08:49:38 +00005560 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005561 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005562 // mask, and elements that come from V1 in the V2 mask, so that the two
5563 // results can be OR'd together.
5564 bool TwoInputs = V1Used && V2Used;
5565 for (unsigned i = 0; i != 8; ++i) {
5566 int EltIdx = MaskVals[i] * 2;
5567 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005568 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5569 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005570 continue;
5571 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005572 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
5573 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005574 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005575 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005576 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005577 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005578 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005579 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005580 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005581
Nate Begemanb9a47b82009-02-23 08:49:38 +00005582 // Calculate the shuffle mask for the second input, shuffle it, and
5583 // OR it with the first shuffled input.
5584 pshufbMask.clear();
5585 for (unsigned i = 0; i != 8; ++i) {
5586 int EltIdx = MaskVals[i] * 2;
5587 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005588 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5589 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005590 continue;
5591 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005592 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
5593 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005594 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005595 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005596 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005597 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005598 MVT::v16i8, &pshufbMask[0], 16));
5599 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005600 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005601 }
5602
5603 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5604 // and update MaskVals with new element order.
5605 BitVector InOrder(8);
5606 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005607 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005608 for (int i = 0; i != 4; ++i) {
5609 int idx = MaskVals[i];
5610 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005611 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005612 InOrder.set(i);
5613 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005614 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005615 InOrder.set(i);
5616 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005617 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005618 }
5619 }
5620 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005621 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00005622 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005623 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005624
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005625 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE &&
5626 (Subtarget->hasSSSE3() || Subtarget->hasAVX()))
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005627 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
5628 NewV.getOperand(0),
5629 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
5630 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005631 }
Eric Christopherfd179292009-08-27 18:07:15 +00005632
Nate Begemanb9a47b82009-02-23 08:49:38 +00005633 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5634 // and update MaskVals with the new element order.
5635 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005636 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005637 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005638 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005639 for (unsigned i = 4; i != 8; ++i) {
5640 int idx = MaskVals[i];
5641 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005642 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005643 InOrder.set(i);
5644 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005645 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005646 InOrder.set(i);
5647 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005648 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005649 }
5650 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005651 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005652 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005653
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005654 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE &&
5655 (Subtarget->hasSSSE3() || Subtarget->hasAVX()))
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005656 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
5657 NewV.getOperand(0),
5658 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
5659 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005660 }
Eric Christopherfd179292009-08-27 18:07:15 +00005661
Nate Begemanb9a47b82009-02-23 08:49:38 +00005662 // In case BestHi & BestLo were both -1, which means each quadword has a word
5663 // from each of the four input quadwords, calculate the InOrder bitvector now
5664 // before falling through to the insert/extract cleanup.
5665 if (BestLoQuad == -1 && BestHiQuad == -1) {
5666 NewV = V1;
5667 for (int i = 0; i != 8; ++i)
5668 if (MaskVals[i] < 0 || MaskVals[i] == i)
5669 InOrder.set(i);
5670 }
Eric Christopherfd179292009-08-27 18:07:15 +00005671
Nate Begemanb9a47b82009-02-23 08:49:38 +00005672 // The other elements are put in the right place using pextrw and pinsrw.
5673 for (unsigned i = 0; i != 8; ++i) {
5674 if (InOrder[i])
5675 continue;
5676 int EltIdx = MaskVals[i];
5677 if (EltIdx < 0)
5678 continue;
5679 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00005680 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005681 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00005682 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005683 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005684 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005685 DAG.getIntPtrConstant(i));
5686 }
5687 return NewV;
5688}
5689
5690// v16i8 shuffles - Prefer shuffles in the following order:
5691// 1. [ssse3] 1 x pshufb
5692// 2. [ssse3] 2 x pshufb + 1 x por
5693// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5694static
Nate Begeman9008ca62009-04-27 18:41:29 +00005695SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005696 SelectionDAG &DAG,
5697 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005698 SDValue V1 = SVOp->getOperand(0);
5699 SDValue V2 = SVOp->getOperand(1);
5700 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005701 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00005702 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00005703
Nate Begemanb9a47b82009-02-23 08:49:38 +00005704 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005705 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005706 // present, fall back to case 3.
5707 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
5708 bool V1Only = true;
5709 bool V2Only = true;
5710 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005711 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00005712 if (EltIdx < 0)
5713 continue;
5714 if (EltIdx < 16)
5715 V2Only = false;
5716 else
5717 V1Only = false;
5718 }
Eric Christopherfd179292009-08-27 18:07:15 +00005719
Nate Begemanb9a47b82009-02-23 08:49:38 +00005720 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005721 if (TLI.getSubtarget()->hasSSSE3() || TLI.getSubtarget()->hasAVX()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005722 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005723
Nate Begemanb9a47b82009-02-23 08:49:38 +00005724 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005725 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005726 //
5727 // Otherwise, we have elements from both input vectors, and must zero out
5728 // elements that come from V2 in the first mask, and V1 in the second mask
5729 // so that we can OR them together.
5730 bool TwoInputs = !(V1Only || V2Only);
5731 for (unsigned i = 0; i != 16; ++i) {
5732 int EltIdx = MaskVals[i];
5733 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005734 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005735 continue;
5736 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005737 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005738 }
5739 // If all the elements are from V2, assign it to V1 and return after
5740 // building the first pshufb.
5741 if (V2Only)
5742 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00005743 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005744 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005745 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005746 if (!TwoInputs)
5747 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005748
Nate Begemanb9a47b82009-02-23 08:49:38 +00005749 // Calculate the shuffle mask for the second input, shuffle it, and
5750 // OR it with the first shuffled input.
5751 pshufbMask.clear();
5752 for (unsigned i = 0; i != 16; ++i) {
5753 int EltIdx = MaskVals[i];
5754 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005755 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005756 continue;
5757 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005758 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005759 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005760 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005761 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005762 MVT::v16i8, &pshufbMask[0], 16));
5763 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005764 }
Eric Christopherfd179292009-08-27 18:07:15 +00005765
Nate Begemanb9a47b82009-02-23 08:49:38 +00005766 // No SSSE3 - Calculate in place words and then fix all out of place words
5767 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5768 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005769 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5770 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005771 SDValue NewV = V2Only ? V2 : V1;
5772 for (int i = 0; i != 8; ++i) {
5773 int Elt0 = MaskVals[i*2];
5774 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005775
Nate Begemanb9a47b82009-02-23 08:49:38 +00005776 // This word of the result is all undef, skip it.
5777 if (Elt0 < 0 && Elt1 < 0)
5778 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005779
Nate Begemanb9a47b82009-02-23 08:49:38 +00005780 // This word of the result is already in the correct place, skip it.
5781 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
5782 continue;
5783 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
5784 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005785
Nate Begemanb9a47b82009-02-23 08:49:38 +00005786 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5787 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5788 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005789
5790 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5791 // using a single extract together, load it and store it.
5792 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005793 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005794 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005795 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005796 DAG.getIntPtrConstant(i));
5797 continue;
5798 }
5799
Nate Begemanb9a47b82009-02-23 08:49:38 +00005800 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00005801 // source byte is not also odd, shift the extracted word left 8 bits
5802 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005803 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005804 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005805 DAG.getIntPtrConstant(Elt1 / 2));
5806 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005807 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00005808 DAG.getConstant(8,
5809 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005810 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005811 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
5812 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005813 }
5814 // If Elt0 is defined, extract it from the appropriate source. If the
5815 // source byte is not also even, shift the extracted word right 8 bits. If
5816 // Elt1 was also defined, OR the extracted values together before
5817 // inserting them in the result.
5818 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005819 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005820 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
5821 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005822 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00005823 DAG.getConstant(8,
5824 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005825 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005826 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
5827 DAG.getConstant(0x00FF, MVT::i16));
5828 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00005829 : InsElt0;
5830 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005831 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005832 DAG.getIntPtrConstant(i));
5833 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005834 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005835}
5836
Evan Cheng7a831ce2007-12-15 03:00:47 +00005837/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005838/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00005839/// done when every pair / quad of shuffle mask elements point to elements in
5840/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005841/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00005842static
Nate Begeman9008ca62009-04-27 18:41:29 +00005843SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005844 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00005845 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00005846 SDValue V1 = SVOp->getOperand(0);
5847 SDValue V2 = SVOp->getOperand(1);
5848 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00005849 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005850 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00005851 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005852 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005853 case MVT::v4f32: NewVT = MVT::v2f64; break;
5854 case MVT::v4i32: NewVT = MVT::v2i64; break;
5855 case MVT::v8i16: NewVT = MVT::v4i32; break;
5856 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00005857 }
5858
Nate Begeman9008ca62009-04-27 18:41:29 +00005859 int Scale = NumElems / NewWidth;
5860 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00005861 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005862 int StartIdx = -1;
5863 for (int j = 0; j < Scale; ++j) {
5864 int EltIdx = SVOp->getMaskElt(i+j);
5865 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005866 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00005867 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00005868 StartIdx = EltIdx - (EltIdx % Scale);
5869 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00005870 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005871 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005872 if (StartIdx == -1)
5873 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00005874 else
Nate Begeman9008ca62009-04-27 18:41:29 +00005875 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005876 }
5877
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005878 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
5879 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00005880 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005881}
5882
Evan Chengd880b972008-05-09 21:53:03 +00005883/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005884///
Owen Andersone50ed302009-08-10 22:56:29 +00005885static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00005886 SDValue SrcOp, SelectionDAG &DAG,
5887 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005888 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005889 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00005890 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00005891 LD = dyn_cast<LoadSDNode>(SrcOp);
5892 if (!LD) {
5893 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
5894 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00005895 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00005896 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00005897 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005898 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00005899 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005900 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00005901 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005902 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005903 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
5904 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
5905 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00005906 SrcOp.getOperand(0)
5907 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005908 }
5909 }
5910 }
5911
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005912 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005913 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005914 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00005915 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005916}
5917
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00005918/// areShuffleHalvesWithinDisjointLanes - Check whether each half of a vector
5919/// shuffle node referes to only one lane in the sources.
5920static bool areShuffleHalvesWithinDisjointLanes(ShuffleVectorSDNode *SVOp) {
5921 EVT VT = SVOp->getValueType(0);
5922 int NumElems = VT.getVectorNumElements();
5923 int HalfSize = NumElems/2;
5924 SmallVector<int, 16> M;
5925 SVOp->getMask(M);
5926 bool MatchA = false, MatchB = false;
5927
5928 for (int l = 0; l < NumElems*2; l += HalfSize) {
5929 if (isUndefOrInRange(M, 0, HalfSize, l, l+HalfSize)) {
5930 MatchA = true;
5931 break;
5932 }
5933 }
5934
5935 for (int l = 0; l < NumElems*2; l += HalfSize) {
5936 if (isUndefOrInRange(M, HalfSize, HalfSize, l, l+HalfSize)) {
5937 MatchB = true;
5938 break;
5939 }
5940 }
5941
5942 return MatchA && MatchB;
5943}
5944
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00005945/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
5946/// which could not be matched by any known target speficic shuffle
5947static SDValue
5948LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00005949 if (areShuffleHalvesWithinDisjointLanes(SVOp)) {
5950 // If each half of a vector shuffle node referes to only one lane in the
5951 // source vectors, extract each used 128-bit lane and shuffle them using
5952 // 128-bit shuffles. Then, concatenate the results. Otherwise leave
5953 // the work to the legalizer.
5954 DebugLoc dl = SVOp->getDebugLoc();
5955 EVT VT = SVOp->getValueType(0);
5956 int NumElems = VT.getVectorNumElements();
5957 int HalfSize = NumElems/2;
5958
5959 // Extract the reference for each half
5960 int FstVecExtractIdx = 0, SndVecExtractIdx = 0;
5961 int FstVecOpNum = 0, SndVecOpNum = 0;
5962 for (int i = 0; i < HalfSize; ++i) {
5963 int Elt = SVOp->getMaskElt(i);
5964 if (SVOp->getMaskElt(i) < 0)
5965 continue;
5966 FstVecOpNum = Elt/NumElems;
5967 FstVecExtractIdx = Elt % NumElems < HalfSize ? 0 : HalfSize;
5968 break;
5969 }
5970 for (int i = HalfSize; i < NumElems; ++i) {
5971 int Elt = SVOp->getMaskElt(i);
5972 if (SVOp->getMaskElt(i) < 0)
5973 continue;
5974 SndVecOpNum = Elt/NumElems;
5975 SndVecExtractIdx = Elt % NumElems < HalfSize ? 0 : HalfSize;
5976 break;
5977 }
5978
5979 // Extract the subvectors
5980 SDValue V1 = Extract128BitVector(SVOp->getOperand(FstVecOpNum),
5981 DAG.getConstant(FstVecExtractIdx, MVT::i32), DAG, dl);
5982 SDValue V2 = Extract128BitVector(SVOp->getOperand(SndVecOpNum),
5983 DAG.getConstant(SndVecExtractIdx, MVT::i32), DAG, dl);
5984
5985 // Generate 128-bit shuffles
5986 SmallVector<int, 16> MaskV1, MaskV2;
5987 for (int i = 0; i < HalfSize; ++i) {
5988 int Elt = SVOp->getMaskElt(i);
5989 MaskV1.push_back(Elt < 0 ? Elt : Elt % HalfSize);
5990 }
5991 for (int i = HalfSize; i < NumElems; ++i) {
5992 int Elt = SVOp->getMaskElt(i);
5993 MaskV2.push_back(Elt < 0 ? Elt : Elt % HalfSize);
5994 }
5995
5996 EVT NVT = V1.getValueType();
5997 V1 = DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &MaskV1[0]);
5998 V2 = DAG.getVectorShuffle(NVT, dl, V2, DAG.getUNDEF(NVT), &MaskV2[0]);
5999
6000 // Concatenate the result back
6001 SDValue V = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), V1,
6002 DAG.getConstant(0, MVT::i32), DAG, dl);
6003 return Insert128BitVector(V, V2, DAG.getConstant(NumElems/2, MVT::i32),
6004 DAG, dl);
6005 }
6006
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006007 return SDValue();
6008}
6009
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006010/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
6011/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00006012static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006013LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006014 SDValue V1 = SVOp->getOperand(0);
6015 SDValue V2 = SVOp->getOperand(1);
6016 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006017 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00006018
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006019 assert(VT.getSizeInBits() == 128 && "Unsupported vector size");
6020
Evan Chengace3c172008-07-22 21:13:36 +00006021 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00006022 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00006023 SmallVector<int, 8> Mask1(4U, -1);
6024 SmallVector<int, 8> PermMask;
6025 SVOp->getMask(PermMask);
6026
Evan Chengace3c172008-07-22 21:13:36 +00006027 unsigned NumHi = 0;
6028 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00006029 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006030 int Idx = PermMask[i];
6031 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006032 Locs[i] = std::make_pair(-1, -1);
6033 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006034 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
6035 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006036 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00006037 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006038 NumLo++;
6039 } else {
6040 Locs[i] = std::make_pair(1, NumHi);
6041 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00006042 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006043 NumHi++;
6044 }
6045 }
6046 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006047
Evan Chengace3c172008-07-22 21:13:36 +00006048 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006049 // If no more than two elements come from either vector. This can be
6050 // implemented with two shuffles. First shuffle gather the elements.
6051 // The second shuffle, which takes the first shuffle as both of its
6052 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00006053 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006054
Nate Begeman9008ca62009-04-27 18:41:29 +00006055 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00006056
Evan Chengace3c172008-07-22 21:13:36 +00006057 for (unsigned i = 0; i != 4; ++i) {
6058 if (Locs[i].first == -1)
6059 continue;
6060 else {
6061 unsigned Idx = (i < 2) ? 0 : 4;
6062 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006063 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006064 }
6065 }
6066
Nate Begeman9008ca62009-04-27 18:41:29 +00006067 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006068 } else if (NumLo == 3 || NumHi == 3) {
6069 // Otherwise, we must have three elements from one vector, call it X, and
6070 // one element from the other, call it Y. First, use a shufps to build an
6071 // intermediate vector with the one element from Y and the element from X
6072 // that will be in the same half in the final destination (the indexes don't
6073 // matter). Then, use a shufps to build the final vector, taking the half
6074 // containing the element from Y from the intermediate, and the other half
6075 // from X.
6076 if (NumHi == 3) {
6077 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00006078 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006079 std::swap(V1, V2);
6080 }
6081
6082 // Find the element from V2.
6083 unsigned HiIndex;
6084 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006085 int Val = PermMask[HiIndex];
6086 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006087 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006088 if (Val >= 4)
6089 break;
6090 }
6091
Nate Begeman9008ca62009-04-27 18:41:29 +00006092 Mask1[0] = PermMask[HiIndex];
6093 Mask1[1] = -1;
6094 Mask1[2] = PermMask[HiIndex^1];
6095 Mask1[3] = -1;
6096 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006097
6098 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006099 Mask1[0] = PermMask[0];
6100 Mask1[1] = PermMask[1];
6101 Mask1[2] = HiIndex & 1 ? 6 : 4;
6102 Mask1[3] = HiIndex & 1 ? 4 : 6;
6103 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006104 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006105 Mask1[0] = HiIndex & 1 ? 2 : 0;
6106 Mask1[1] = HiIndex & 1 ? 0 : 2;
6107 Mask1[2] = PermMask[2];
6108 Mask1[3] = PermMask[3];
6109 if (Mask1[2] >= 0)
6110 Mask1[2] += 4;
6111 if (Mask1[3] >= 0)
6112 Mask1[3] += 4;
6113 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006114 }
Evan Chengace3c172008-07-22 21:13:36 +00006115 }
6116
6117 // Break it into (shuffle shuffle_hi, shuffle_lo).
6118 Locs.clear();
David Greenec4db4e52011-02-28 19:06:56 +00006119 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00006120 SmallVector<int,8> LoMask(4U, -1);
6121 SmallVector<int,8> HiMask(4U, -1);
6122
6123 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00006124 unsigned MaskIdx = 0;
6125 unsigned LoIdx = 0;
6126 unsigned HiIdx = 2;
6127 for (unsigned i = 0; i != 4; ++i) {
6128 if (i == 2) {
6129 MaskPtr = &HiMask;
6130 MaskIdx = 1;
6131 LoIdx = 0;
6132 HiIdx = 2;
6133 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006134 int Idx = PermMask[i];
6135 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006136 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00006137 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006138 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00006139 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006140 LoIdx++;
6141 } else {
6142 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00006143 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006144 HiIdx++;
6145 }
6146 }
6147
Nate Begeman9008ca62009-04-27 18:41:29 +00006148 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6149 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
6150 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00006151 for (unsigned i = 0; i != 4; ++i) {
6152 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006153 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00006154 } else {
6155 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006156 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00006157 }
6158 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006159 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006160}
6161
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006162static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006163 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006164 V = V.getOperand(0);
6165 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6166 V = V.getOperand(0);
6167 if (MayFoldLoad(V))
6168 return true;
6169 return false;
6170}
6171
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006172// FIXME: the version above should always be used. Since there's
6173// a bug where several vector shuffles can't be folded because the
6174// DAG is not updated during lowering and a node claims to have two
6175// uses while it only has one, use this version, and let isel match
6176// another instruction if the load really happens to have more than
6177// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00006178// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006179static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006180 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006181 V = V.getOperand(0);
6182 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6183 V = V.getOperand(0);
6184 if (ISD::isNormalLoad(V.getNode()))
6185 return true;
6186 return false;
6187}
6188
6189/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
6190/// a vector extract, and if both can be later optimized into a single load.
6191/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
6192/// here because otherwise a target specific shuffle node is going to be
6193/// emitted for this shuffle, and the optimization not done.
6194/// FIXME: This is probably not the best approach, but fix the problem
6195/// until the right path is decided.
6196static
6197bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
6198 const TargetLowering &TLI) {
6199 EVT VT = V.getValueType();
6200 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
6201
6202 // Be sure that the vector shuffle is present in a pattern like this:
6203 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
6204 if (!V.hasOneUse())
6205 return false;
6206
6207 SDNode *N = *V.getNode()->use_begin();
6208 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
6209 return false;
6210
6211 SDValue EltNo = N->getOperand(1);
6212 if (!isa<ConstantSDNode>(EltNo))
6213 return false;
6214
6215 // If the bit convert changed the number of elements, it is unsafe
6216 // to examine the mask.
6217 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006218 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006219 EVT SrcVT = V.getOperand(0).getValueType();
6220 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
6221 return false;
6222 V = V.getOperand(0);
6223 HasShuffleIntoBitcast = true;
6224 }
6225
6226 // Select the input vector, guarding against out of range extract vector.
6227 unsigned NumElems = VT.getVectorNumElements();
6228 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
6229 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
6230 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
6231
6232 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006233 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006234 V = V.getOperand(0);
6235
6236 if (ISD::isNormalLoad(V.getNode())) {
6237 // Is the original load suitable?
6238 LoadSDNode *LN0 = cast<LoadSDNode>(V);
6239
6240 // FIXME: avoid the multi-use bug that is preventing lots of
6241 // of foldings to be detected, this is still wrong of course, but
6242 // give the temporary desired behavior, and if it happens that
6243 // the load has real more uses, during isel it will not fold, and
6244 // will generate poor code.
6245 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
6246 return false;
6247
6248 if (!HasShuffleIntoBitcast)
6249 return true;
6250
6251 // If there's a bitcast before the shuffle, check if the load type and
6252 // alignment is valid.
6253 unsigned Align = LN0->getAlignment();
6254 unsigned NewAlign =
6255 TLI.getTargetData()->getABITypeAlignment(
6256 VT.getTypeForEVT(*DAG.getContext()));
6257
6258 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
6259 return false;
6260 }
6261
6262 return true;
6263}
6264
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006265static
Evan Cheng835580f2010-10-07 20:50:20 +00006266SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
6267 EVT VT = Op.getValueType();
6268
6269 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006270 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6271 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006272 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6273 V1, DAG));
6274}
6275
6276static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006277SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006278 bool HasXMMInt) {
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006279 SDValue V1 = Op.getOperand(0);
6280 SDValue V2 = Op.getOperand(1);
6281 EVT VT = Op.getValueType();
6282
6283 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6284
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006285 if (HasXMMInt && VT == MVT::v2f64)
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006286 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6287
Evan Cheng0899f5c2011-08-31 02:05:24 +00006288 // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
6289 return DAG.getNode(ISD::BITCAST, dl, VT,
6290 getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
6291 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
6292 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006293}
6294
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006295static
6296SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
6297 SDValue V1 = Op.getOperand(0);
6298 SDValue V2 = Op.getOperand(1);
6299 EVT VT = Op.getValueType();
6300
6301 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6302 "unsupported shuffle type");
6303
6304 if (V2.getOpcode() == ISD::UNDEF)
6305 V2 = V1;
6306
6307 // v4i32 or v4f32
6308 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6309}
6310
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006311static inline unsigned getSHUFPOpcode(EVT VT) {
6312 switch(VT.getSimpleVT().SimpleTy) {
6313 case MVT::v8i32: // Use fp unit for int unpack.
6314 case MVT::v8f32:
6315 case MVT::v4i32: // Use fp unit for int unpack.
6316 case MVT::v4f32: return X86ISD::SHUFPS;
6317 case MVT::v4i64: // Use fp unit for int unpack.
6318 case MVT::v4f64:
6319 case MVT::v2i64: // Use fp unit for int unpack.
6320 case MVT::v2f64: return X86ISD::SHUFPD;
6321 default:
6322 llvm_unreachable("Unknown type for shufp*");
6323 }
6324 return 0;
6325}
6326
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006327static
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006328SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasXMMInt) {
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006329 SDValue V1 = Op.getOperand(0);
6330 SDValue V2 = Op.getOperand(1);
6331 EVT VT = Op.getValueType();
6332 unsigned NumElems = VT.getVectorNumElements();
6333
6334 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6335 // operand of these instructions is only memory, so check if there's a
6336 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6337 // same masks.
6338 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006339
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006340 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006341 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006342 CanFoldLoad = true;
6343
6344 // When V1 is a load, it can be folded later into a store in isel, example:
6345 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6346 // turns into:
6347 // (MOVLPSmr addr:$src1, VR128:$src2)
6348 // So, recognize this potential and also use MOVLPS or MOVLPD
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006349 if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006350 CanFoldLoad = true;
6351
Eric Christopher893a8822011-02-20 05:04:42 +00006352 // Both of them can't be memory operations though.
6353 if (MayFoldVectorLoad(V1) && MayFoldVectorLoad(V2))
6354 CanFoldLoad = false;
Owen Anderson95771af2011-02-25 21:41:48 +00006355
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006356 if (CanFoldLoad) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006357 if (HasXMMInt && NumElems == 2)
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006358 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6359
6360 if (NumElems == 4)
6361 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
6362 }
6363
6364 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6365 // movl and movlp will both match v2i64, but v2i64 is never matched by
6366 // movl earlier because we make it strict to avoid messing with the movlp load
6367 // folding logic (see the code above getMOVLP call). Match it here then,
6368 // this is horrible, but will stay like this until we move all shuffle
6369 // matching to x86 specific nodes. Note that for the 1st condition all
6370 // types are matched with movsd.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006371 if (HasXMMInt) {
Bruno Cardoso Lopes5ca0d142011-09-14 02:36:14 +00006372 // FIXME: isMOVLMask should be checked and matched before getMOVLP,
6373 // as to remove this logic from here, as much as possible
6374 if (NumElems == 2 || !X86::isMOVLMask(SVOp))
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006375 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006376 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006377 }
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006378
6379 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6380
6381 // Invert the operand order and use SHUFPS to match it.
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006382 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V2, V1,
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006383 X86::getShuffleSHUFImmediate(SVOp), DAG);
6384}
6385
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006386static inline unsigned getUNPCKLOpcode(EVT VT) {
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006387 switch(VT.getSimpleVT().SimpleTy) {
6388 case MVT::v4i32: return X86ISD::PUNPCKLDQ;
6389 case MVT::v2i64: return X86ISD::PUNPCKLQDQ;
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00006390 case MVT::v4f32: return X86ISD::UNPCKLPS;
6391 case MVT::v2f64: return X86ISD::UNPCKLPD;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006392 case MVT::v8i32: // Use fp unit for int unpack.
David Greenec4db4e52011-02-28 19:06:56 +00006393 case MVT::v8f32: return X86ISD::VUNPCKLPSY;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006394 case MVT::v4i64: // Use fp unit for int unpack.
David Greenec4db4e52011-02-28 19:06:56 +00006395 case MVT::v4f64: return X86ISD::VUNPCKLPDY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006396 case MVT::v16i8: return X86ISD::PUNPCKLBW;
6397 case MVT::v8i16: return X86ISD::PUNPCKLWD;
6398 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00006399 llvm_unreachable("Unknown type for unpckl");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006400 }
6401 return 0;
6402}
6403
6404static inline unsigned getUNPCKHOpcode(EVT VT) {
6405 switch(VT.getSimpleVT().SimpleTy) {
6406 case MVT::v4i32: return X86ISD::PUNPCKHDQ;
6407 case MVT::v2i64: return X86ISD::PUNPCKHQDQ;
6408 case MVT::v4f32: return X86ISD::UNPCKHPS;
6409 case MVT::v2f64: return X86ISD::UNPCKHPD;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006410 case MVT::v8i32: // Use fp unit for int unpack.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00006411 case MVT::v8f32: return X86ISD::VUNPCKHPSY;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006412 case MVT::v4i64: // Use fp unit for int unpack.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00006413 case MVT::v4f64: return X86ISD::VUNPCKHPDY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006414 case MVT::v16i8: return X86ISD::PUNPCKHBW;
6415 case MVT::v8i16: return X86ISD::PUNPCKHWD;
6416 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00006417 llvm_unreachable("Unknown type for unpckh");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006418 }
6419 return 0;
6420}
6421
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006422static inline unsigned getVPERMILOpcode(EVT VT) {
6423 switch(VT.getSimpleVT().SimpleTy) {
6424 case MVT::v4i32:
6425 case MVT::v4f32: return X86ISD::VPERMILPS;
6426 case MVT::v2i64:
6427 case MVT::v2f64: return X86ISD::VPERMILPD;
6428 case MVT::v8i32:
6429 case MVT::v8f32: return X86ISD::VPERMILPSY;
6430 case MVT::v4i64:
6431 case MVT::v4f64: return X86ISD::VPERMILPDY;
6432 default:
6433 llvm_unreachable("Unknown type for vpermil");
6434 }
6435 return 0;
6436}
6437
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006438/// isVectorBroadcast - Check if the node chain is suitable to be xformed to
6439/// a vbroadcast node. The nodes are suitable whenever we can fold a load coming
6440/// from a 32 or 64 bit scalar. Update Op to the desired load to be folded.
6441static bool isVectorBroadcast(SDValue &Op) {
6442 EVT VT = Op.getValueType();
6443 bool Is256 = VT.getSizeInBits() == 256;
6444
6445 assert((VT.getSizeInBits() == 128 || Is256) &&
6446 "Unsupported type for vbroadcast node");
6447
6448 SDValue V = Op;
6449 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
6450 V = V.getOperand(0);
6451
6452 if (Is256 && !(V.hasOneUse() &&
6453 V.getOpcode() == ISD::INSERT_SUBVECTOR &&
6454 V.getOperand(0).getOpcode() == ISD::UNDEF))
6455 return false;
6456
6457 if (Is256)
6458 V = V.getOperand(1);
Bruno Cardoso Lopesa39ccdb2011-09-01 18:15:06 +00006459
6460 if (!V.hasOneUse())
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006461 return false;
6462
6463 // Check the source scalar_to_vector type. 256-bit broadcasts are
6464 // supported for 32/64-bit sizes, while 128-bit ones are only supported
6465 // for 32-bit scalars.
Bruno Cardoso Lopesa39ccdb2011-09-01 18:15:06 +00006466 if (V.getOpcode() != ISD::SCALAR_TO_VECTOR)
6467 return false;
6468
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006469 unsigned ScalarSize = V.getOperand(0).getValueType().getSizeInBits();
6470 if (ScalarSize != 32 && ScalarSize != 64)
6471 return false;
6472 if (!Is256 && ScalarSize == 64)
6473 return false;
6474
6475 V = V.getOperand(0);
6476 if (!MayFoldLoad(V))
6477 return false;
6478
6479 // Return the load node
6480 Op = V;
6481 return true;
6482}
6483
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006484static
6485SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006486 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006487 const X86Subtarget *Subtarget) {
6488 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6489 EVT VT = Op.getValueType();
6490 DebugLoc dl = Op.getDebugLoc();
6491 SDValue V1 = Op.getOperand(0);
6492 SDValue V2 = Op.getOperand(1);
6493
6494 if (isZeroShuffle(SVOp))
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006495 return getZeroVector(VT, Subtarget->hasXMMInt(), DAG, dl);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006496
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006497 // Handle splat operations
6498 if (SVOp->isSplat()) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006499 unsigned NumElem = VT.getVectorNumElements();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006500 int Size = VT.getSizeInBits();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006501 // Special case, this is the only place now where it's allowed to return
6502 // a vector_shuffle operation without using a target specific node, because
6503 // *hopefully* it will be optimized away by the dag combiner. FIXME: should
6504 // this be moved to DAGCombine instead?
6505 if (NumElem <= 4 && CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006506 return Op;
6507
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006508 // Use vbroadcast whenever the splat comes from a foldable load
6509 if (Subtarget->hasAVX() && isVectorBroadcast(V1))
6510 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, V1);
6511
Bruno Cardoso Lopes6a32adc2011-07-25 23:05:25 +00006512 // Handle splats by matching through known shuffle masks
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006513 if ((Size == 128 && NumElem <= 4) ||
6514 (Size == 256 && NumElem < 8))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006515 return SDValue();
6516
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00006517 // All remaning splats are promoted to target supported vector shuffles.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006518 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006519 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006520
6521 // If the shuffle can be profitably rewritten as a narrower shuffle, then
6522 // do it!
6523 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
6524 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6525 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006526 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006527 } else if ((VT == MVT::v4i32 ||
6528 (VT == MVT::v4f32 && Subtarget->hasXMMInt()))) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006529 // FIXME: Figure out a cleaner way to do this.
6530 // Try to make use of movq to zero out the top part.
6531 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
6532 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6533 if (NewOp.getNode()) {
6534 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
6535 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
6536 DAG, Subtarget, dl);
6537 }
6538 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
6539 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6540 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
6541 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
6542 DAG, Subtarget, dl);
6543 }
6544 }
6545 return SDValue();
6546}
6547
Dan Gohman475871a2008-07-27 21:46:04 +00006548SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006549X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00006550 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00006551 SDValue V1 = Op.getOperand(0);
6552 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006553 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006554 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006555 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006556 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006557 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
6558 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00006559 bool V1IsSplat = false;
6560 bool V2IsSplat = false;
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006561 bool HasXMMInt = Subtarget->hasXMMInt();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006562 MachineFunction &MF = DAG.getMachineFunction();
6563 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006564
Dale Johannesen0488fb62010-09-30 23:57:10 +00006565 // Shuffle operations on MMX not supported.
6566 if (isMMX)
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00006567 return Op;
6568
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006569 // Vector shuffle lowering takes 3 steps:
6570 //
6571 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
6572 // narrowing and commutation of operands should be handled.
6573 // 2) Matching of shuffles with known shuffle masks to x86 target specific
6574 // shuffle nodes.
6575 // 3) Rewriting of unmatched masks into new generic shuffle operations,
6576 // so the shuffle can be broken into other shuffles and the legalizer can
6577 // try the lowering again.
6578 //
6579 // The general ideia is that no vector_shuffle operation should be left to
6580 // be matched during isel, all of them must be converted to a target specific
6581 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00006582
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006583 // Normalize the input vectors. Here splats, zeroed vectors, profitable
6584 // narrowing and commutation of operands should be handled. The actual code
6585 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006586 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006587 if (NewOp.getNode())
6588 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00006589
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006590 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
6591 // unpckh_undef). Only use pshufd if speed is more important than size.
6592 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006593 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006594 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
Rafael Espindola23e31012011-07-22 18:56:05 +00006595 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00006596
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006597 if (X86::isMOVDDUPMask(SVOp) &&
6598 (Subtarget->hasSSE3() || Subtarget->hasAVX()) &&
6599 V2IsUndef && RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00006600 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006601
Dale Johannesen0488fb62010-09-30 23:57:10 +00006602 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006603 return getMOVHighToLow(Op, dl, DAG);
6604
6605 // Use to match splats
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006606 if (HasXMMInt && X86::isUNPCKHMask(SVOp) && V2IsUndef &&
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006607 (VT == MVT::v2f64 || VT == MVT::v2i64))
6608 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
6609
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006610 if (X86::isPSHUFDMask(SVOp)) {
6611 // The actual implementation will match the mask in the if above and then
6612 // during isel it can match several different instructions, not only pshufd
6613 // as its name says, sad but true, emulate the behavior for now...
6614 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
6615 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
6616
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006617 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
6618
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006619 if (HasXMMInt && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006620 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
6621
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006622 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V1,
6623 TargetMask, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006624 }
Eric Christopherfd179292009-08-27 18:07:15 +00006625
Evan Chengf26ffe92008-05-29 08:22:04 +00006626 // Check if this can be converted into a logical shift.
6627 bool isLeft = false;
6628 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00006629 SDValue ShVal;
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006630 bool isShift = getSubtarget()->hasXMMInt() &&
6631 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00006632 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006633 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00006634 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006635 EVT EltVT = VT.getVectorElementType();
6636 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006637 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006638 }
Eric Christopherfd179292009-08-27 18:07:15 +00006639
Nate Begeman9008ca62009-04-27 18:41:29 +00006640 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006641 if (V1IsUndef)
6642 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00006643 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00006644 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00006645 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006646 if (HasXMMInt && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006647 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6648
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006649 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006650 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6651 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00006652 }
Eric Christopherfd179292009-08-27 18:07:15 +00006653
Nate Begeman9008ca62009-04-27 18:41:29 +00006654 // FIXME: fold these into legal mask.
Dale Johannesen0488fb62010-09-30 23:57:10 +00006655 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp))
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006656 return getMOVLowToHigh(Op, dl, DAG, HasXMMInt);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006657
Dale Johannesen0488fb62010-09-30 23:57:10 +00006658 if (X86::isMOVHLPSMask(SVOp))
6659 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006660
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00006661 if (X86::isMOVSHDUPMask(SVOp, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006662 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006663
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00006664 if (X86::isMOVSLDUPMask(SVOp, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006665 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00006666
Dale Johannesen0488fb62010-09-30 23:57:10 +00006667 if (X86::isMOVLPMask(SVOp))
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006668 return getMOVLP(Op, dl, DAG, HasXMMInt);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006669
Nate Begeman9008ca62009-04-27 18:41:29 +00006670 if (ShouldXformToMOVHLPS(SVOp) ||
6671 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
6672 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006673
Evan Chengf26ffe92008-05-29 08:22:04 +00006674 if (isShift) {
6675 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006676 EVT EltVT = VT.getVectorElementType();
6677 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006678 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006679 }
Eric Christopherfd179292009-08-27 18:07:15 +00006680
Evan Cheng9eca5e82006-10-25 21:49:50 +00006681 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00006682 // FIXME: This should also accept a bitcast of a splat? Be careful, not
6683 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00006684 V1IsSplat = isSplatVector(V1.getNode());
6685 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006686
Chris Lattner8a594482007-11-25 00:24:49 +00006687 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00006688 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006689 Op = CommuteVectorShuffle(SVOp, DAG);
6690 SVOp = cast<ShuffleVectorSDNode>(Op);
6691 V1 = SVOp->getOperand(0);
6692 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00006693 std::swap(V1IsSplat, V2IsSplat);
6694 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006695 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00006696 }
6697
Nate Begeman9008ca62009-04-27 18:41:29 +00006698 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
6699 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00006700 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00006701 return V1;
6702 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6703 // the instruction selector will not match, so get a canonical MOVL with
6704 // swapped operands to undo the commute.
6705 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006706 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006707
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006708 if (X86::isUNPCKLMask(SVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006709 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006710
6711 if (X86::isUNPCKHMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006712 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006713
Evan Cheng9bbbb982006-10-25 20:48:19 +00006714 if (V2IsSplat) {
6715 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006716 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00006717 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00006718 SDValue NewMask = NormalizeMask(SVOp, DAG);
6719 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
6720 if (NSVOp != SVOp) {
6721 if (X86::isUNPCKLMask(NSVOp, true)) {
6722 return NewMask;
6723 } else if (X86::isUNPCKHMask(NSVOp, true)) {
6724 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006725 }
6726 }
6727 }
6728
Evan Cheng9eca5e82006-10-25 21:49:50 +00006729 if (Commuted) {
6730 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006731 // FIXME: this seems wrong.
6732 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
6733 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006734
6735 if (X86::isUNPCKLMask(NewSVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006736 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V2, V1, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006737
6738 if (X86::isUNPCKHMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006739 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V2, V1, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006740 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006741
Nate Begeman9008ca62009-04-27 18:41:29 +00006742 // Normalize the node to match x86 shuffle ops if needed
Dale Johannesen0488fb62010-09-30 23:57:10 +00006743 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
Nate Begeman9008ca62009-04-27 18:41:29 +00006744 return CommuteVectorShuffle(SVOp, DAG);
6745
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006746 // The checks below are all present in isShuffleMaskLegal, but they are
6747 // inlined here right now to enable us to directly emit target specific
6748 // nodes, and remove one by one until they don't return Op anymore.
6749 SmallVector<int, 16> M;
6750 SVOp->getMask(M);
6751
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006752 if (isPALIGNRMask(M, VT, Subtarget->hasSSSE3() || Subtarget->hasAVX()))
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006753 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
6754 X86::getShufflePALIGNRImmediate(SVOp),
6755 DAG);
6756
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006757 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6758 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00006759 if (VT == MVT::v2f64)
6760 return getTargetShuffleNode(X86ISD::UNPCKLPD, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006761 if (VT == MVT::v2i64)
6762 return getTargetShuffleNode(X86ISD::PUNPCKLQDQ, dl, VT, V1, V1, DAG);
6763 }
6764
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006765 if (isPSHUFHWMask(M, VT))
6766 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
6767 X86::getShufflePSHUFHWImmediate(SVOp),
6768 DAG);
6769
6770 if (isPSHUFLWMask(M, VT))
6771 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
6772 X86::getShufflePSHUFLWImmediate(SVOp),
6773 DAG);
6774
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006775 if (isSHUFPMask(M, VT))
6776 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6777 X86::getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00006778
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006779 if (X86::isUNPCKL_v_undef_Mask(SVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006780 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006781 if (X86::isUNPCKH_v_undef_Mask(SVOp))
Rafael Espindola23e31012011-07-22 18:56:05 +00006782 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006783
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006784 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006785 // Generate target specific nodes for 128 or 256-bit shuffles only
6786 // supported in the AVX instruction set.
6787 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006788
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006789 // Handle VMOVDDUPY permutations
6790 if (isMOVDDUPYMask(SVOp, Subtarget))
6791 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
6792
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006793 // Handle VPERMILPS* permutations
6794 if (isVPERMILPSMask(M, VT, Subtarget))
6795 return getTargetShuffleNode(getVPERMILOpcode(VT), dl, VT, V1,
6796 getShuffleVPERMILPSImmediate(SVOp), DAG);
6797
6798 // Handle VPERMILPD* permutations
6799 if (isVPERMILPDMask(M, VT, Subtarget))
6800 return getTargetShuffleNode(getVPERMILOpcode(VT), dl, VT, V1,
6801 getShuffleVPERMILPDImmediate(SVOp), DAG);
6802
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00006803 // Handle VPERM2F128 permutations
6804 if (isVPERM2F128Mask(M, VT, Subtarget))
6805 return getTargetShuffleNode(X86ISD::VPERM2F128, dl, VT, V1, V2,
6806 getShuffleVPERM2F128Immediate(SVOp), DAG);
6807
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006808 // Handle VSHUFPSY permutations
6809 if (isVSHUFPSYMask(M, VT, Subtarget))
6810 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6811 getShuffleVSHUFPSYImmediate(SVOp), DAG);
6812
6813 // Handle VSHUFPDY permutations
6814 if (isVSHUFPDYMask(M, VT, Subtarget))
6815 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6816 getShuffleVSHUFPDYImmediate(SVOp), DAG);
6817
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006818 //===--------------------------------------------------------------------===//
6819 // Since no target specific shuffle was selected for this generic one,
6820 // lower it into other known shuffles. FIXME: this isn't true yet, but
6821 // this is the plan.
6822 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00006823
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006824 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
6825 if (VT == MVT::v8i16) {
6826 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
6827 if (NewOp.getNode())
6828 return NewOp;
6829 }
6830
6831 if (VT == MVT::v16i8) {
6832 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
6833 if (NewOp.getNode())
6834 return NewOp;
6835 }
6836
6837 // Handle all 128-bit wide vectors with 4 elements, and match them with
6838 // several different shuffle types.
6839 if (NumElems == 4 && VT.getSizeInBits() == 128)
6840 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
6841
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006842 // Handle general 256-bit shuffles
6843 if (VT.is256BitVector())
6844 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
6845
Dan Gohman475871a2008-07-27 21:46:04 +00006846 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006847}
6848
Dan Gohman475871a2008-07-27 21:46:04 +00006849SDValue
6850X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00006851 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006852 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006853 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006854
6855 if (Op.getOperand(0).getValueType().getSizeInBits() != 128)
6856 return SDValue();
6857
Duncan Sands83ec4b62008-06-06 12:08:01 +00006858 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006859 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006860 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006861 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006862 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006863 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006864 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00006865 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
6866 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
6867 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006868 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6869 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006870 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006871 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00006872 Op.getOperand(0)),
6873 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006874 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006875 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006876 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006877 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006878 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00006879 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00006880 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
6881 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006882 // result has a single use which is a store or a bitcast to i32. And in
6883 // the case of a store, it's not worth it if the index is a constant 0,
6884 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00006885 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00006886 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00006887 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006888 if ((User->getOpcode() != ISD::STORE ||
6889 (isa<ConstantSDNode>(Op.getOperand(1)) &&
6890 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006891 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00006892 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00006893 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00006894 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006895 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00006896 Op.getOperand(0)),
6897 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006898 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Owen Anderson825b72b2009-08-11 20:47:22 +00006899 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00006900 // ExtractPS works with constant index.
6901 if (isa<ConstantSDNode>(Op.getOperand(1)))
6902 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006903 }
Dan Gohman475871a2008-07-27 21:46:04 +00006904 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006905}
6906
6907
Dan Gohman475871a2008-07-27 21:46:04 +00006908SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006909X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
6910 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006911 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00006912 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006913
David Greene74a579d2011-02-10 16:57:36 +00006914 SDValue Vec = Op.getOperand(0);
6915 EVT VecVT = Vec.getValueType();
6916
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006917 // If this is a 256-bit vector result, first extract the 128-bit vector and
6918 // then extract the element from the 128-bit vector.
6919 if (VecVT.getSizeInBits() == 256) {
David Greene74a579d2011-02-10 16:57:36 +00006920 DebugLoc dl = Op.getNode()->getDebugLoc();
6921 unsigned NumElems = VecVT.getVectorNumElements();
6922 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00006923 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
6924
6925 // Get the 128-bit vector.
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006926 bool Upper = IdxVal >= NumElems/2;
6927 Vec = Extract128BitVector(Vec,
6928 DAG.getConstant(Upper ? NumElems/2 : 0, MVT::i32), DAG, dl);
David Greene74a579d2011-02-10 16:57:36 +00006929
David Greene74a579d2011-02-10 16:57:36 +00006930 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006931 Upper ? DAG.getConstant(IdxVal-NumElems/2, MVT::i32) : Idx);
David Greene74a579d2011-02-10 16:57:36 +00006932 }
6933
6934 assert(Vec.getValueSizeInBits() <= 128 && "Unexpected vector length");
6935
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006936 if (Subtarget->hasSSE41() || Subtarget->hasAVX()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006937 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006938 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00006939 return Res;
6940 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00006941
Owen Andersone50ed302009-08-10 22:56:29 +00006942 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006943 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006944 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006945 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00006946 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006947 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006948 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006949 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6950 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006951 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006952 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00006953 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006954 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00006955 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00006956 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006957 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00006958 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006959 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006960 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006961 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006962 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006963 if (Idx == 0)
6964 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00006965
Evan Cheng0db9fe62006-04-25 20:13:52 +00006966 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00006967 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006968 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006969 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006970 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006971 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006972 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00006973 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006974 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
6975 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
6976 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006977 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006978 if (Idx == 0)
6979 return Op;
6980
6981 // UNPCKHPD the element to the lowest double word, then movsd.
6982 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
6983 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00006984 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006985 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006986 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006987 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006988 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006989 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006990 }
6991
Dan Gohman475871a2008-07-27 21:46:04 +00006992 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006993}
6994
Dan Gohman475871a2008-07-27 21:46:04 +00006995SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006996X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
6997 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006998 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006999 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007000 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007001
Dan Gohman475871a2008-07-27 21:46:04 +00007002 SDValue N0 = Op.getOperand(0);
7003 SDValue N1 = Op.getOperand(1);
7004 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00007005
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007006 if (VT.getSizeInBits() == 256)
7007 return SDValue();
7008
Dan Gohman8a55ce42009-09-23 21:02:20 +00007009 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00007010 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007011 unsigned Opc;
7012 if (VT == MVT::v8i16)
7013 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007014 else if (VT == MVT::v16i8)
7015 Opc = X86ISD::PINSRB;
7016 else
7017 Opc = X86ISD::PINSRB;
7018
Nate Begeman14d12ca2008-02-11 04:19:36 +00007019 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
7020 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007021 if (N1.getValueType() != MVT::i32)
7022 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7023 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007024 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00007025 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00007026 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007027 // Bits [7:6] of the constant are the source select. This will always be
7028 // zero here. The DAG Combiner may combine an extract_elt index into these
7029 // bits. For example (insert (extract, 3), 2) could be matched by putting
7030 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00007031 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00007032 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00007033 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00007034 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007035 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00007036 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00007037 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00007038 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00007039 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00007040 // PINSR* works with constant index.
7041 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007042 }
Dan Gohman475871a2008-07-27 21:46:04 +00007043 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007044}
7045
Dan Gohman475871a2008-07-27 21:46:04 +00007046SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007047X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007048 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007049 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007050
David Greene6b381262011-02-09 15:32:06 +00007051 DebugLoc dl = Op.getDebugLoc();
7052 SDValue N0 = Op.getOperand(0);
7053 SDValue N1 = Op.getOperand(1);
7054 SDValue N2 = Op.getOperand(2);
7055
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007056 // If this is a 256-bit vector result, first extract the 128-bit vector,
7057 // insert the element into the extracted half and then place it back.
7058 if (VT.getSizeInBits() == 256) {
David Greene6b381262011-02-09 15:32:06 +00007059 if (!isa<ConstantSDNode>(N2))
7060 return SDValue();
7061
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007062 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00007063 unsigned NumElems = VT.getVectorNumElements();
7064 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007065 bool Upper = IdxVal >= NumElems/2;
7066 SDValue Ins128Idx = DAG.getConstant(Upper ? NumElems/2 : 0, MVT::i32);
7067 SDValue V = Extract128BitVector(N0, Ins128Idx, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007068
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007069 // Insert the element into the desired half.
7070 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V,
7071 N1, Upper ? DAG.getConstant(IdxVal-NumElems/2, MVT::i32) : N2);
David Greene6b381262011-02-09 15:32:06 +00007072
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007073 // Insert the changed part back to the 256-bit vector
7074 return Insert128BitVector(N0, V, Ins128Idx, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007075 }
7076
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007077 if (Subtarget->hasSSE41() || Subtarget->hasAVX())
Nate Begeman14d12ca2008-02-11 04:19:36 +00007078 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
7079
Dan Gohman8a55ce42009-09-23 21:02:20 +00007080 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00007081 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00007082
Dan Gohman8a55ce42009-09-23 21:02:20 +00007083 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00007084 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
7085 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007086 if (N1.getValueType() != MVT::i32)
7087 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7088 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007089 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00007090 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007091 }
Dan Gohman475871a2008-07-27 21:46:04 +00007092 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007093}
7094
Dan Gohman475871a2008-07-27 21:46:04 +00007095SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007096X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007097 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007098 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00007099 EVT OpVT = Op.getValueType();
7100
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007101 // If this is a 256-bit vector result, first insert into a 128-bit
7102 // vector and then insert into the 256-bit vector.
7103 if (OpVT.getSizeInBits() > 128) {
7104 // Insert into a 128-bit vector.
7105 EVT VT128 = EVT::getVectorVT(*Context,
7106 OpVT.getVectorElementType(),
7107 OpVT.getVectorNumElements() / 2);
7108
7109 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
7110
7111 // Insert the 128-bit vector.
7112 return Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, OpVT), Op,
7113 DAG.getConstant(0, MVT::i32),
7114 DAG, dl);
7115 }
7116
Chris Lattnerf172ecd2010-07-04 23:07:25 +00007117 if (Op.getValueType() == MVT::v1i64 &&
7118 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00007119 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00007120
Owen Anderson825b72b2009-08-11 20:47:22 +00007121 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00007122 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
7123 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007124 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00007125 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007126}
7127
David Greene91585092011-01-26 15:38:49 +00007128// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
7129// a simple subregister reference or explicit instructions to grab
7130// upper bits of a vector.
7131SDValue
7132X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7133 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00007134 DebugLoc dl = Op.getNode()->getDebugLoc();
7135 SDValue Vec = Op.getNode()->getOperand(0);
7136 SDValue Idx = Op.getNode()->getOperand(1);
7137
7138 if (Op.getNode()->getValueType(0).getSizeInBits() == 128
7139 && Vec.getNode()->getValueType(0).getSizeInBits() == 256) {
7140 return Extract128BitVector(Vec, Idx, DAG, dl);
7141 }
David Greene91585092011-01-26 15:38:49 +00007142 }
7143 return SDValue();
7144}
7145
David Greenecfe33c42011-01-26 19:13:22 +00007146// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
7147// simple superregister reference or explicit instructions to insert
7148// the upper bits of a vector.
7149SDValue
7150X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7151 if (Subtarget->hasAVX()) {
7152 DebugLoc dl = Op.getNode()->getDebugLoc();
7153 SDValue Vec = Op.getNode()->getOperand(0);
7154 SDValue SubVec = Op.getNode()->getOperand(1);
7155 SDValue Idx = Op.getNode()->getOperand(2);
7156
7157 if (Op.getNode()->getValueType(0).getSizeInBits() == 256
7158 && SubVec.getNode()->getValueType(0).getSizeInBits() == 128) {
David Greenea5f26012011-02-07 19:36:54 +00007159 return Insert128BitVector(Vec, SubVec, Idx, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00007160 }
7161 }
7162 return SDValue();
7163}
7164
Bill Wendling056292f2008-09-16 21:48:12 +00007165// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7166// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7167// one of the above mentioned nodes. It has to be wrapped because otherwise
7168// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7169// be used to form addressing mode. These wrapped nodes will be selected
7170// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00007171SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007172X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007173 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007174
Chris Lattner41621a22009-06-26 19:22:52 +00007175 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7176 // global base reg.
7177 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007178 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007179 CodeModel::Model M = getTargetMachine().getCodeModel();
7180
Chris Lattner4f066492009-07-11 20:29:19 +00007181 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007182 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007183 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007184 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007185 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007186 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007187 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007188
Evan Cheng1606e8e2009-03-13 07:51:59 +00007189 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00007190 CP->getAlignment(),
7191 CP->getOffset(), OpFlag);
7192 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00007193 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007194 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00007195 if (OpFlag) {
7196 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007197 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007198 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007199 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007200 }
7201
7202 return Result;
7203}
7204
Dan Gohmand858e902010-04-17 15:26:15 +00007205SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007206 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007207
Chris Lattner18c59872009-06-27 04:16:01 +00007208 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7209 // global base reg.
7210 unsigned char OpFlag = 0;
7211 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007212 CodeModel::Model M = getTargetMachine().getCodeModel();
7213
Chris Lattner4f066492009-07-11 20:29:19 +00007214 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007215 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007216 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007217 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007218 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007219 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007220 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007221
Chris Lattner18c59872009-06-27 04:16:01 +00007222 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7223 OpFlag);
7224 DebugLoc DL = JT->getDebugLoc();
7225 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007226
Chris Lattner18c59872009-06-27 04:16:01 +00007227 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00007228 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00007229 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7230 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007231 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007232 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007233
Chris Lattner18c59872009-06-27 04:16:01 +00007234 return Result;
7235}
7236
7237SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007238X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007239 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00007240
Chris Lattner18c59872009-06-27 04:16:01 +00007241 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7242 // global base reg.
7243 unsigned char OpFlag = 0;
7244 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007245 CodeModel::Model M = getTargetMachine().getCodeModel();
7246
Chris Lattner4f066492009-07-11 20:29:19 +00007247 if (Subtarget->isPICStyleRIPRel() &&
Eli Friedman586272d2011-08-11 01:48:05 +00007248 (M == CodeModel::Small || M == CodeModel::Kernel)) {
7249 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
7250 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00007251 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00007252 } else if (Subtarget->isPICStyleGOT()) {
7253 OpFlag = X86II::MO_GOT;
7254 } else if (Subtarget->isPICStyleStubPIC()) {
7255 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
7256 } else if (Subtarget->isPICStyleStubNoDynamic()) {
7257 OpFlag = X86II::MO_DARWIN_NONLAZY;
7258 }
Eric Christopherfd179292009-08-27 18:07:15 +00007259
Chris Lattner18c59872009-06-27 04:16:01 +00007260 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00007261
Chris Lattner18c59872009-06-27 04:16:01 +00007262 DebugLoc DL = Op.getDebugLoc();
7263 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007264
7265
Chris Lattner18c59872009-06-27 04:16:01 +00007266 // With PIC, the address is actually $g + Offset.
7267 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007268 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007269 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7270 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007271 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007272 Result);
7273 }
Eric Christopherfd179292009-08-27 18:07:15 +00007274
Eli Friedman586272d2011-08-11 01:48:05 +00007275 // For symbols that require a load from a stub to get the address, emit the
7276 // load.
7277 if (isGlobalStubReference(OpFlag))
7278 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
7279 MachinePointerInfo::getGOT(), false, false, 0);
7280
Chris Lattner18c59872009-06-27 04:16:01 +00007281 return Result;
7282}
7283
Dan Gohman475871a2008-07-27 21:46:04 +00007284SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007285X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007286 // Create the TargetBlockAddressAddress node.
7287 unsigned char OpFlags =
7288 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007289 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007290 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00007291 DebugLoc dl = Op.getDebugLoc();
7292 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
7293 /*isTarget=*/true, OpFlags);
7294
Dan Gohmanf705adb2009-10-30 01:28:02 +00007295 if (Subtarget->isPICStyleRIPRel() &&
7296 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007297 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7298 else
7299 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007300
Dan Gohman29cbade2009-11-20 23:18:13 +00007301 // With PIC, the address is actually $g + Offset.
7302 if (isGlobalRelativeToPICBase(OpFlags)) {
7303 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7304 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7305 Result);
7306 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007307
7308 return Result;
7309}
7310
7311SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00007312X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00007313 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00007314 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007315 // Create the TargetGlobalAddress node, folding in the constant
7316 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007317 unsigned char OpFlags =
7318 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007319 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007320 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007321 if (OpFlags == X86II::MO_NO_FLAG &&
7322 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007323 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007324 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007325 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007326 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007327 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007328 }
Eric Christopherfd179292009-08-27 18:07:15 +00007329
Chris Lattner4f066492009-07-11 20:29:19 +00007330 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007331 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007332 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7333 else
7334 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007335
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007336 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007337 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007338 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7339 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007340 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007341 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007342
Chris Lattner36c25012009-07-10 07:34:39 +00007343 // For globals that require a load from a stub to get the address, emit the
7344 // load.
7345 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007346 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00007347 MachinePointerInfo::getGOT(), false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007348
Dan Gohman6520e202008-10-18 02:06:02 +00007349 // If there was a non-zero offset that we didn't fold, create an explicit
7350 // addition for it.
7351 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007352 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007353 DAG.getConstant(Offset, getPointerTy()));
7354
Evan Cheng0db9fe62006-04-25 20:13:52 +00007355 return Result;
7356}
7357
Evan Chengda43bcf2008-09-24 00:05:32 +00007358SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007359X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007360 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007361 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007362 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007363}
7364
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007365static SDValue
7366GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007367 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00007368 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007369 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007370 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007371 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007372 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007373 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007374 GA->getOffset(),
7375 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007376 if (InFlag) {
7377 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00007378 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007379 } else {
7380 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00007381 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007382 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007383
7384 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007385 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007386
Rafael Espindola15f1b662009-04-24 12:59:40 +00007387 SDValue Flag = Chain.getValue(1);
7388 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007389}
7390
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007391// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007392static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007393LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007394 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007395 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00007396 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
7397 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007398 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007399 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007400 InFlag = Chain.getValue(1);
7401
Chris Lattnerb903bed2009-06-26 21:20:29 +00007402 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007403}
7404
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007405// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007406static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007407LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007408 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007409 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7410 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007411}
7412
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007413// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
7414// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007415static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007416 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00007417 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007418 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00007419
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007420 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
7421 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
7422 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00007423
Michael J. Spencerec38de22010-10-10 22:04:20 +00007424 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007425 DAG.getIntPtrConstant(0),
7426 MachinePointerInfo(Ptr), false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00007427
Chris Lattnerb903bed2009-06-26 21:20:29 +00007428 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007429 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
7430 // initialexec.
7431 unsigned WrapperKind = X86ISD::Wrapper;
7432 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007433 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00007434 } else if (is64Bit) {
7435 assert(model == TLSModel::InitialExec);
7436 OperandFlags = X86II::MO_GOTTPOFF;
7437 WrapperKind = X86ISD::WrapperRIP;
7438 } else {
7439 assert(model == TLSModel::InitialExec);
7440 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00007441 }
Eric Christopherfd179292009-08-27 18:07:15 +00007442
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007443 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
7444 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00007445 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00007446 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007447 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007448 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007449
Rafael Espindola9a580232009-02-27 13:37:18 +00007450 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007451 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00007452 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007453
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007454 // The address of the thread local variable is the add of the thread
7455 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00007456 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007457}
7458
Dan Gohman475871a2008-07-27 21:46:04 +00007459SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007460X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00007461
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007462 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00007463 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00007464
Eric Christopher30ef0e52010-06-03 04:07:48 +00007465 if (Subtarget->isTargetELF()) {
7466 // TODO: implement the "local dynamic" model
7467 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00007468
Eric Christopher30ef0e52010-06-03 04:07:48 +00007469 // If GV is an alias then use the aliasee for determining
7470 // thread-localness.
7471 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
7472 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007473
7474 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00007475 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00007476
Eric Christopher30ef0e52010-06-03 04:07:48 +00007477 switch (model) {
7478 case TLSModel::GeneralDynamic:
7479 case TLSModel::LocalDynamic: // not implemented
7480 if (Subtarget->is64Bit())
7481 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
7482 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00007483
Eric Christopher30ef0e52010-06-03 04:07:48 +00007484 case TLSModel::InitialExec:
7485 case TLSModel::LocalExec:
7486 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
7487 Subtarget->is64Bit());
7488 }
7489 } else if (Subtarget->isTargetDarwin()) {
7490 // Darwin only has one model of TLS. Lower to that.
7491 unsigned char OpFlag = 0;
7492 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7493 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007494
Eric Christopher30ef0e52010-06-03 04:07:48 +00007495 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7496 // global base reg.
7497 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7498 !Subtarget->is64Bit();
7499 if (PIC32)
7500 OpFlag = X86II::MO_TLVP_PIC_BASE;
7501 else
7502 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007503 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007504 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00007505 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007506 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007507 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007508
Eric Christopher30ef0e52010-06-03 04:07:48 +00007509 // With PIC32, the address is actually $g + Offset.
7510 if (PIC32)
7511 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7512 DAG.getNode(X86ISD::GlobalBaseReg,
7513 DebugLoc(), getPointerTy()),
7514 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007515
Eric Christopher30ef0e52010-06-03 04:07:48 +00007516 // Lowering the machine isd will make sure everything is in the right
7517 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007518 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007519 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00007520 SDValue Args[] = { Chain, Offset };
7521 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007522
Eric Christopher30ef0e52010-06-03 04:07:48 +00007523 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7524 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7525 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007526
Eric Christopher30ef0e52010-06-03 04:07:48 +00007527 // And our return value (tls address) is in the standard call return value
7528 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007529 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Evan Chengfd230df2011-10-19 22:22:54 +00007530 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(),
7531 Chain.getValue(1));
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007532 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007533
Eric Christopher30ef0e52010-06-03 04:07:48 +00007534 assert(false &&
7535 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00007536
Torok Edwinc23197a2009-07-14 16:55:14 +00007537 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00007538 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007539}
7540
Evan Cheng0db9fe62006-04-25 20:13:52 +00007541
Nadav Rotem43012222011-05-11 08:12:09 +00007542/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00007543/// take a 2 x i32 value to shift plus a shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +00007544SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00007545 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00007546 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007547 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007548 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007549 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00007550 SDValue ShOpLo = Op.getOperand(0);
7551 SDValue ShOpHi = Op.getOperand(1);
7552 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00007553 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00007554 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00007555 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00007556
Dan Gohman475871a2008-07-27 21:46:04 +00007557 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007558 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007559 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
7560 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007561 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007562 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
7563 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007564 }
Evan Chenge3413162006-01-09 18:33:28 +00007565
Owen Anderson825b72b2009-08-11 20:47:22 +00007566 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
7567 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00007568 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00007569 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00007570
Dan Gohman475871a2008-07-27 21:46:04 +00007571 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00007572 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00007573 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
7574 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00007575
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007576 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007577 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7578 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007579 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007580 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7581 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007582 }
7583
Dan Gohman475871a2008-07-27 21:46:04 +00007584 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00007585 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007586}
Evan Chenga3195e82006-01-12 22:54:21 +00007587
Dan Gohmand858e902010-04-17 15:26:15 +00007588SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
7589 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007590 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00007591
Dale Johannesen0488fb62010-09-30 23:57:10 +00007592 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007593 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007594
Owen Anderson825b72b2009-08-11 20:47:22 +00007595 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00007596 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007597
Eli Friedman36df4992009-05-27 00:47:34 +00007598 // These are really Legal; return the operand so the caller accepts it as
7599 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007600 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00007601 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00007602 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00007603 Subtarget->is64Bit()) {
7604 return Op;
7605 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007606
Bruno Cardoso Lopesa511b8e2011-08-09 17:39:01 +00007607 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007608 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007609 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00007610 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007611 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00007612 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00007613 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007614 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007615 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007616 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
7617}
Evan Cheng0db9fe62006-04-25 20:13:52 +00007618
Owen Andersone50ed302009-08-10 22:56:29 +00007619SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007620 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00007621 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007622 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00007623 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00007624 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00007625 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007626 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007627 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00007628 else
Owen Anderson825b72b2009-08-11 20:47:22 +00007629 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007630
Chris Lattner492a43e2010-09-22 01:28:21 +00007631 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007632
Stuart Hastings84be9582011-06-02 15:57:11 +00007633 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
7634 MachineMemOperand *MMO;
7635 if (FI) {
7636 int SSFI = FI->getIndex();
7637 MMO =
7638 DAG.getMachineFunction()
7639 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7640 MachineMemOperand::MOLoad, ByteSize, ByteSize);
7641 } else {
7642 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
7643 StackSlot = StackSlot.getOperand(1);
7644 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007645 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007646 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
7647 X86ISD::FILD, DL,
7648 Tys, Ops, array_lengthof(Ops),
7649 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007650
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007651 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007652 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00007653 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007654
7655 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
7656 // shouldn't be necessary except that RFP cannot be live across
7657 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007658 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007659 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
7660 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007661 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00007662 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007663 SDValue Ops[] = {
7664 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
7665 };
Chris Lattner492a43e2010-09-22 01:28:21 +00007666 MachineMemOperand *MMO =
7667 DAG.getMachineFunction()
7668 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007669 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007670
Chris Lattner492a43e2010-09-22 01:28:21 +00007671 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
7672 Ops, array_lengthof(Ops),
7673 Op.getValueType(), MMO);
7674 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007675 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007676 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007677 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007678
Evan Cheng0db9fe62006-04-25 20:13:52 +00007679 return Result;
7680}
7681
Bill Wendling8b8a6362009-01-17 03:56:04 +00007682// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007683SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
7684 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00007685 // This algorithm is not obvious. Here it is in C code, more or less:
7686 /*
7687 double uint64_to_double( uint32_t hi, uint32_t lo ) {
7688 static const __m128i exp = { 0x4330000045300000ULL, 0 };
7689 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00007690
Bill Wendling8b8a6362009-01-17 03:56:04 +00007691 // Copy ints to xmm registers.
7692 __m128i xh = _mm_cvtsi32_si128( hi );
7693 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00007694
Bill Wendling8b8a6362009-01-17 03:56:04 +00007695 // Combine into low half of a single xmm register.
7696 __m128i x = _mm_unpacklo_epi32( xh, xl );
7697 __m128d d;
7698 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00007699
Bill Wendling8b8a6362009-01-17 03:56:04 +00007700 // Merge in appropriate exponents to give the integer bits the right
7701 // magnitude.
7702 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00007703
Bill Wendling8b8a6362009-01-17 03:56:04 +00007704 // Subtract away the biases to deal with the IEEE-754 double precision
7705 // implicit 1.
7706 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00007707
Bill Wendling8b8a6362009-01-17 03:56:04 +00007708 // All conversions up to here are exact. The correctly rounded result is
7709 // calculated using the current rounding mode using the following
7710 // horizontal add.
7711 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
7712 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
7713 // store doesn't really need to be here (except
7714 // maybe to zero the other double)
7715 return sd;
7716 }
7717 */
Dale Johannesen040225f2008-10-21 23:07:49 +00007718
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007719 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00007720 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00007721
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007722 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00007723 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00007724 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
7725 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
7726 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
7727 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00007728 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007729 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007730
Bill Wendling8b8a6362009-01-17 03:56:04 +00007731 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00007732 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007733 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
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, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00007736 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007737 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007738
Owen Anderson825b72b2009-08-11 20:47:22 +00007739 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7740 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00007741 Op.getOperand(0),
7742 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007743 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7744 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00007745 Op.getOperand(0),
7746 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007747 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
7748 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00007749 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007750 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00007751 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007752 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
Owen Anderson825b72b2009-08-11 20:47:22 +00007753 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00007754 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007755 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00007756 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007757
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007758 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00007759 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00007760 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
7761 DAG.getUNDEF(MVT::v2f64), ShufMask);
7762 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
7763 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007764 DAG.getIntPtrConstant(0));
7765}
7766
Bill Wendling8b8a6362009-01-17 03:56:04 +00007767// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007768SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
7769 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007770 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007771 // FP constant to bias correct the final result.
7772 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00007773 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007774
7775 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00007776 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00007777 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007778
Eli Friedmanf3704762011-08-29 21:15:46 +00007779 // Zero out the upper parts of the register.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00007780 Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget->hasXMMInt(),
7781 DAG);
Eli Friedmanf3704762011-08-29 21:15:46 +00007782
Owen Anderson825b72b2009-08-11 20:47:22 +00007783 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007784 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007785 DAG.getIntPtrConstant(0));
7786
7787 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007788 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007789 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007790 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007791 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007792 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007793 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007794 MVT::v2f64, Bias)));
7795 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007796 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007797 DAG.getIntPtrConstant(0));
7798
7799 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007800 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007801
7802 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00007803 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00007804
Owen Anderson825b72b2009-08-11 20:47:22 +00007805 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007806 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00007807 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007808 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007809 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00007810 }
7811
7812 // Handle final rounding.
7813 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00007814}
7815
Dan Gohmand858e902010-04-17 15:26:15 +00007816SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
7817 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00007818 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007819 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007820
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007821 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00007822 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
7823 // the optimization here.
7824 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00007825 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00007826
Owen Andersone50ed302009-08-10 22:56:29 +00007827 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007828 EVT DstVT = Op.getValueType();
7829 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007830 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007831 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007832 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00007833
7834 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00007835 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007836 if (SrcVT == MVT::i32) {
7837 SDValue WordOff = DAG.getConstant(4, getPointerTy());
7838 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
7839 getPointerTy(), StackSlot, WordOff);
7840 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007841 StackSlot, MachinePointerInfo(),
7842 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007843 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007844 OffsetSlot, MachinePointerInfo(),
7845 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007846 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
7847 return Fild;
7848 }
7849
7850 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
7851 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007852 StackSlot, MachinePointerInfo(),
7853 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007854 // For i64 source, we need to add the appropriate power of 2 if the input
7855 // was negative. This is the same as the optimization in
7856 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
7857 // we must be careful to do the computation in x87 extended precision, not
7858 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00007859 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
7860 MachineMemOperand *MMO =
7861 DAG.getMachineFunction()
7862 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7863 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007864
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007865 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
7866 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007867 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
7868 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007869
7870 APInt FF(32, 0x5F800000ULL);
7871
7872 // Check whether the sign bit is set.
7873 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
7874 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
7875 ISD::SETLT);
7876
7877 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
7878 SDValue FudgePtr = DAG.getConstantPool(
7879 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
7880 getPointerTy());
7881
7882 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
7883 SDValue Zero = DAG.getIntPtrConstant(0);
7884 SDValue Four = DAG.getIntPtrConstant(4);
7885 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
7886 Zero, Four);
7887 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
7888
7889 // Load the value out, extending it from f32 to f80.
7890 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00007891 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00007892 FudgePtr, MachinePointerInfo::getConstantPool(),
7893 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007894 // Extend everything to 80 bits to force it to be done on x87.
7895 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
7896 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007897}
7898
Dan Gohman475871a2008-07-27 21:46:04 +00007899std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00007900FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00007901 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00007902
Owen Andersone50ed302009-08-10 22:56:29 +00007903 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00007904
7905 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007906 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
7907 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00007908 }
7909
Owen Anderson825b72b2009-08-11 20:47:22 +00007910 assert(DstTy.getSimpleVT() <= MVT::i64 &&
7911 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00007912 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00007913
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007914 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007915 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00007916 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007917 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00007918 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00007919 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00007920 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007921 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007922
Evan Cheng87c89352007-10-15 20:11:21 +00007923 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
7924 // stack slot.
7925 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00007926 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00007927 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007928 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00007929
Michael J. Spencerec38de22010-10-10 22:04:20 +00007930
7931
Evan Cheng0db9fe62006-04-25 20:13:52 +00007932 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00007933 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007934 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007935 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
7936 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
7937 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007938 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007939
Dan Gohman475871a2008-07-27 21:46:04 +00007940 SDValue Chain = DAG.getEntryNode();
7941 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00007942 EVT TheVT = Op.getOperand(0).getValueType();
7943 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007944 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00007945 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007946 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007947 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007948 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00007949 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00007950 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00007951 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00007952
Chris Lattner492a43e2010-09-22 01:28:21 +00007953 MachineMemOperand *MMO =
7954 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7955 MachineMemOperand::MOLoad, MemSize, MemSize);
7956 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
7957 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007958 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00007959 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007960 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
7961 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007962
Chris Lattner07290932010-09-22 01:05:16 +00007963 MachineMemOperand *MMO =
7964 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7965 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007966
Evan Cheng0db9fe62006-04-25 20:13:52 +00007967 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00007968 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00007969 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
7970 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00007971
Chris Lattner27a6c732007-11-24 07:07:01 +00007972 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007973}
7974
Dan Gohmand858e902010-04-17 15:26:15 +00007975SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
7976 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00007977 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007978 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007979
Eli Friedman948e95a2009-05-23 09:59:16 +00007980 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00007981 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00007982 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
7983 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00007984
Chris Lattner27a6c732007-11-24 07:07:01 +00007985 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007986 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007987 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00007988}
7989
Dan Gohmand858e902010-04-17 15:26:15 +00007990SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
7991 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00007992 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
7993 SDValue FIST = Vals.first, StackSlot = Vals.second;
7994 assert(FIST.getNode() && "Unexpected failure");
7995
7996 // Load the result.
7997 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007998 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007999}
8000
Dan Gohmand858e902010-04-17 15:26:15 +00008001SDValue X86TargetLowering::LowerFABS(SDValue Op,
8002 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008003 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008004 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008005 EVT VT = Op.getValueType();
8006 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008007 if (VT.isVector())
8008 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008009 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008010 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008011 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00008012 CV.push_back(C);
8013 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008014 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008015 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00008016 CV.push_back(C);
8017 CV.push_back(C);
8018 CV.push_back(C);
8019 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008020 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008021 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008022 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008023 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008024 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00008025 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008026 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008027}
8028
Dan Gohmand858e902010-04-17 15:26:15 +00008029SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008030 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008031 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008032 EVT VT = Op.getValueType();
8033 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00008034 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00008035 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008036 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008037 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008038 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00008039 CV.push_back(C);
8040 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008041 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008042 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00008043 CV.push_back(C);
8044 CV.push_back(C);
8045 CV.push_back(C);
8046 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008047 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008048 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008049 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008050 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008051 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00008052 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00008053 if (VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008054 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008055 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008056 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00008057 Op.getOperand(0)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008058 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00008059 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00008060 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00008061 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008062}
8063
Dan Gohmand858e902010-04-17 15:26:15 +00008064SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008065 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00008066 SDValue Op0 = Op.getOperand(0);
8067 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008068 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008069 EVT VT = Op.getValueType();
8070 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00008071
8072 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008073 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008074 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008075 SrcVT = VT;
8076 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008077 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008078 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008079 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008080 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008081 }
8082
8083 // At this point the operands and the result should have the same
8084 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00008085
Evan Cheng68c47cb2007-01-05 07:55:56 +00008086 // First get the sign bit of second operand.
8087 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008088 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008089 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
8090 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008091 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008092 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
8093 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8094 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8095 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008096 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008097 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008098 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008099 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008100 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00008101 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008102 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008103
8104 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008105 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008106 // Op0 is MVT::f32, Op1 is MVT::f64.
8107 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
8108 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
8109 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008110 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00008111 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00008112 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008113 }
8114
Evan Cheng73d6cf12007-01-05 21:37:56 +00008115 // Clear first operand sign bit.
8116 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00008117 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008118 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
8119 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008120 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008121 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
8122 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8123 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8124 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008125 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008126 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008127 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008128 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008129 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00008130 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008131 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008132
8133 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00008134 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008135}
8136
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008137SDValue X86TargetLowering::LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) const {
8138 SDValue N0 = Op.getOperand(0);
8139 DebugLoc dl = Op.getDebugLoc();
8140 EVT VT = Op.getValueType();
8141
8142 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
8143 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
8144 DAG.getConstant(1, VT));
8145 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
8146}
8147
Dan Gohman076aee32009-03-04 19:44:21 +00008148/// Emit nodes that will be selected as "test Op0,Op0", or something
8149/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008150SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008151 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008152 DebugLoc dl = Op.getDebugLoc();
8153
Dan Gohman31125812009-03-07 01:58:32 +00008154 // CF and OF aren't always set the way we want. Determine which
8155 // of these we need.
8156 bool NeedCF = false;
8157 bool NeedOF = false;
8158 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008159 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00008160 case X86::COND_A: case X86::COND_AE:
8161 case X86::COND_B: case X86::COND_BE:
8162 NeedCF = true;
8163 break;
8164 case X86::COND_G: case X86::COND_GE:
8165 case X86::COND_L: case X86::COND_LE:
8166 case X86::COND_O: case X86::COND_NO:
8167 NeedOF = true;
8168 break;
Dan Gohman31125812009-03-07 01:58:32 +00008169 }
8170
Dan Gohman076aee32009-03-04 19:44:21 +00008171 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00008172 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
8173 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008174 if (Op.getResNo() != 0 || NeedOF || NeedCF)
8175 // Emit a CMP with 0, which is the TEST pattern.
8176 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8177 DAG.getConstant(0, Op.getValueType()));
8178
8179 unsigned Opcode = 0;
8180 unsigned NumOperands = 0;
8181 switch (Op.getNode()->getOpcode()) {
8182 case ISD::ADD:
8183 // Due to an isel shortcoming, be conservative if this add is likely to be
8184 // selected as part of a load-modify-store instruction. When the root node
8185 // in a match is a store, isel doesn't know how to remap non-chain non-flag
8186 // uses of other nodes in the match, such as the ADD in this case. This
8187 // leads to the ADD being left around and reselected, with the result being
8188 // two adds in the output. Alas, even if none our users are stores, that
8189 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
8190 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
8191 // climbing the DAG back to the root, and it doesn't seem to be worth the
8192 // effort.
8193 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Dan Gohman076aee32009-03-04 19:44:21 +00008194 UE = Op.getNode()->use_end(); UI != UE; ++UI)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008195 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
8196 goto default_case;
8197
8198 if (ConstantSDNode *C =
8199 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
8200 // An add of one will be selected as an INC.
8201 if (C->getAPIntValue() == 1) {
8202 Opcode = X86ISD::INC;
8203 NumOperands = 1;
8204 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00008205 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008206
8207 // An add of negative one (subtract of one) will be selected as a DEC.
8208 if (C->getAPIntValue().isAllOnesValue()) {
8209 Opcode = X86ISD::DEC;
8210 NumOperands = 1;
8211 break;
8212 }
Dan Gohman076aee32009-03-04 19:44:21 +00008213 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008214
8215 // Otherwise use a regular EFLAGS-setting add.
8216 Opcode = X86ISD::ADD;
8217 NumOperands = 2;
8218 break;
8219 case ISD::AND: {
8220 // If the primary and result isn't used, don't bother using X86ISD::AND,
8221 // because a TEST instruction will be better.
8222 bool NonFlagUse = false;
8223 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8224 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
8225 SDNode *User = *UI;
8226 unsigned UOpNo = UI.getOperandNo();
8227 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
8228 // Look pass truncate.
8229 UOpNo = User->use_begin().getOperandNo();
8230 User = *User->use_begin();
8231 }
8232
8233 if (User->getOpcode() != ISD::BRCOND &&
8234 User->getOpcode() != ISD::SETCC &&
8235 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
8236 NonFlagUse = true;
8237 break;
8238 }
Dan Gohman076aee32009-03-04 19:44:21 +00008239 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008240
8241 if (!NonFlagUse)
8242 break;
8243 }
8244 // FALL THROUGH
8245 case ISD::SUB:
8246 case ISD::OR:
8247 case ISD::XOR:
8248 // Due to the ISEL shortcoming noted above, be conservative if this op is
8249 // likely to be selected as part of a load-modify-store instruction.
8250 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8251 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8252 if (UI->getOpcode() == ISD::STORE)
8253 goto default_case;
8254
8255 // Otherwise use a regular EFLAGS-setting instruction.
8256 switch (Op.getNode()->getOpcode()) {
8257 default: llvm_unreachable("unexpected operator!");
8258 case ISD::SUB: Opcode = X86ISD::SUB; break;
8259 case ISD::OR: Opcode = X86ISD::OR; break;
8260 case ISD::XOR: Opcode = X86ISD::XOR; break;
8261 case ISD::AND: Opcode = X86ISD::AND; break;
8262 }
8263
8264 NumOperands = 2;
8265 break;
8266 case X86ISD::ADD:
8267 case X86ISD::SUB:
8268 case X86ISD::INC:
8269 case X86ISD::DEC:
8270 case X86ISD::OR:
8271 case X86ISD::XOR:
8272 case X86ISD::AND:
8273 return SDValue(Op.getNode(), 1);
8274 default:
8275 default_case:
8276 break;
Dan Gohman076aee32009-03-04 19:44:21 +00008277 }
8278
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008279 if (Opcode == 0)
8280 // Emit a CMP with 0, which is the TEST pattern.
8281 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8282 DAG.getConstant(0, Op.getValueType()));
8283
8284 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
8285 SmallVector<SDValue, 4> Ops;
8286 for (unsigned i = 0; i != NumOperands; ++i)
8287 Ops.push_back(Op.getOperand(i));
8288
8289 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
8290 DAG.ReplaceAllUsesWith(Op, New);
8291 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00008292}
8293
8294/// Emit nodes that will be selected as "cmp Op0,Op1", or something
8295/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008296SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008297 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008298 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
8299 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00008300 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00008301
8302 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00008303 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00008304}
8305
Evan Chengd40d03e2010-01-06 19:38:29 +00008306/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
8307/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00008308SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
8309 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008310 SDValue Op0 = And.getOperand(0);
8311 SDValue Op1 = And.getOperand(1);
8312 if (Op0.getOpcode() == ISD::TRUNCATE)
8313 Op0 = Op0.getOperand(0);
8314 if (Op1.getOpcode() == ISD::TRUNCATE)
8315 Op1 = Op1.getOperand(0);
8316
Evan Chengd40d03e2010-01-06 19:38:29 +00008317 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008318 if (Op1.getOpcode() == ISD::SHL)
8319 std::swap(Op0, Op1);
8320 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008321 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
8322 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008323 // If we looked past a truncate, check that it's only truncating away
8324 // known zeros.
8325 unsigned BitWidth = Op0.getValueSizeInBits();
8326 unsigned AndBitWidth = And.getValueSizeInBits();
8327 if (BitWidth > AndBitWidth) {
8328 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
8329 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
8330 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
8331 return SDValue();
8332 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008333 LHS = Op1;
8334 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00008335 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008336 } else if (Op1.getOpcode() == ISD::Constant) {
8337 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
8338 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00008339 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
8340 LHS = AndLHS.getOperand(0);
8341 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008342 }
Evan Chengd40d03e2010-01-06 19:38:29 +00008343 }
Evan Cheng0488db92007-09-25 01:57:46 +00008344
Evan Chengd40d03e2010-01-06 19:38:29 +00008345 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00008346 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00008347 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00008348 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00008349 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00008350 // Also promote i16 to i32 for performance / code size reason.
8351 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00008352 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00008353 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00008354
Evan Chengd40d03e2010-01-06 19:38:29 +00008355 // If the operand types disagree, extend the shift amount to match. Since
8356 // BT ignores high bits (like shifts) we can use anyextend.
8357 if (LHS.getValueType() != RHS.getValueType())
8358 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008359
Evan Chengd40d03e2010-01-06 19:38:29 +00008360 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
8361 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
8362 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8363 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00008364 }
8365
Evan Cheng54de3ea2010-01-05 06:52:31 +00008366 return SDValue();
8367}
8368
Dan Gohmand858e902010-04-17 15:26:15 +00008369SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Duncan Sands28b77e92011-09-06 19:07:46 +00008370
8371 if (Op.getValueType().isVector()) return LowerVSETCC(Op, DAG);
8372
Evan Cheng54de3ea2010-01-05 06:52:31 +00008373 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
8374 SDValue Op0 = Op.getOperand(0);
8375 SDValue Op1 = Op.getOperand(1);
8376 DebugLoc dl = Op.getDebugLoc();
8377 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
8378
8379 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00008380 // Lower (X & (1 << N)) == 0 to BT(X, N).
8381 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
8382 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00008383 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008384 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00008385 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008386 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
8387 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
8388 if (NewSetCC.getNode())
8389 return NewSetCC;
8390 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00008391
Chris Lattner481eebc2010-12-19 21:23:48 +00008392 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
8393 // these.
8394 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00008395 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00008396 cast<ConstantSDNode>(Op1)->isNullValue()) &&
8397 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008398
Chris Lattner481eebc2010-12-19 21:23:48 +00008399 // If the input is a setcc, then reuse the input setcc or use a new one with
8400 // the inverted condition.
8401 if (Op0.getOpcode() == X86ISD::SETCC) {
8402 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
8403 bool Invert = (CC == ISD::SETNE) ^
8404 cast<ConstantSDNode>(Op1)->isNullValue();
8405 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008406
Evan Cheng2c755ba2010-02-27 07:36:59 +00008407 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00008408 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8409 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
8410 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008411 }
8412
Evan Chenge5b51ac2010-04-17 06:13:15 +00008413 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00008414 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008415 if (X86CC == X86::COND_INVALID)
8416 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008417
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008418 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00008419 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008420 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00008421}
8422
Craig Topper89af15e2011-09-18 08:03:58 +00008423// Lower256IntVSETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008424// ones, and then concatenate the result back.
Craig Topper89af15e2011-09-18 08:03:58 +00008425static SDValue Lower256IntVSETCC(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008426 EVT VT = Op.getValueType();
8427
Duncan Sands28b77e92011-09-06 19:07:46 +00008428 assert(VT.getSizeInBits() == 256 && Op.getOpcode() == ISD::SETCC &&
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008429 "Unsupported value type for operation");
8430
8431 int NumElems = VT.getVectorNumElements();
8432 DebugLoc dl = Op.getDebugLoc();
8433 SDValue CC = Op.getOperand(2);
8434 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
8435 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
8436
8437 // Extract the LHS vectors
8438 SDValue LHS = Op.getOperand(0);
8439 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
8440 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
8441
8442 // Extract the RHS vectors
8443 SDValue RHS = Op.getOperand(1);
8444 SDValue RHS1 = Extract128BitVector(RHS, Idx0, DAG, dl);
8445 SDValue RHS2 = Extract128BitVector(RHS, Idx1, DAG, dl);
8446
8447 // Issue the operation on the smaller types and concatenate the result back
8448 MVT EltVT = VT.getVectorElementType().getSimpleVT();
8449 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
8450 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
8451 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
8452 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
8453}
8454
8455
Dan Gohmand858e902010-04-17 15:26:15 +00008456SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008457 SDValue Cond;
8458 SDValue Op0 = Op.getOperand(0);
8459 SDValue Op1 = Op.getOperand(1);
8460 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00008461 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00008462 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
8463 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008464 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00008465
8466 if (isFP) {
8467 unsigned SSECC = 8;
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008468 EVT EltVT = Op0.getValueType().getVectorElementType();
8469 assert(EltVT == MVT::f32 || EltVT == MVT::f64);
8470
8471 unsigned Opc = EltVT == MVT::f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00008472 bool Swap = false;
8473
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008474 // SSE Condition code mapping:
8475 // 0 - EQ
8476 // 1 - LT
8477 // 2 - LE
8478 // 3 - UNORD
8479 // 4 - NEQ
8480 // 5 - NLT
8481 // 6 - NLE
8482 // 7 - ORD
Nate Begeman30a0de92008-07-17 16:51:19 +00008483 switch (SetCCOpcode) {
8484 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008485 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00008486 case ISD::SETEQ: SSECC = 0; break;
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008487 case ISD::SETOGT:
8488 case ISD::SETGT: Swap = true; // Fallthrough
Bruno Cardoso Lopes457d53d2011-09-12 21:24:07 +00008489 case ISD::SETLT:
8490 case ISD::SETOLT: SSECC = 1; break;
8491 case ISD::SETOGE:
8492 case ISD::SETGE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008493 case ISD::SETLE:
8494 case ISD::SETOLE: SSECC = 2; break;
8495 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008496 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00008497 case ISD::SETNE: SSECC = 4; break;
8498 case ISD::SETULE: Swap = true;
8499 case ISD::SETUGE: SSECC = 5; break;
8500 case ISD::SETULT: Swap = true;
8501 case ISD::SETUGT: SSECC = 6; break;
8502 case ISD::SETO: SSECC = 7; break;
8503 }
8504 if (Swap)
8505 std::swap(Op0, Op1);
8506
Nate Begemanfb8ead02008-07-25 19:05:58 +00008507 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00008508 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00008509 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00008510 SDValue UNORD, EQ;
Bruno Cardoso Lopes809f17f2011-09-13 19:33:00 +00008511 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
8512 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00008513 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00008514 }
8515 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00008516 SDValue ORD, NEQ;
Bruno Cardoso Lopes809f17f2011-09-13 19:33:00 +00008517 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
8518 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00008519 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00008520 }
Torok Edwinc23197a2009-07-14 16:55:14 +00008521 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00008522 }
8523 // Handle all other FP comparisons here.
Bruno Cardoso Lopes809f17f2011-09-13 19:33:00 +00008524 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00008525 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008526
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008527 // Break 256-bit integer vector compare into smaller ones.
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008528 if (!isFP && VT.getSizeInBits() == 256)
Craig Topper89af15e2011-09-18 08:03:58 +00008529 return Lower256IntVSETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008530
Nate Begeman30a0de92008-07-17 16:51:19 +00008531 // We are handling one of the integer comparisons here. Since SSE only has
8532 // GT and EQ comparisons for integer, swapping operands and multiple
8533 // operations may be required for some comparisons.
8534 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
8535 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00008536
Owen Anderson825b72b2009-08-11 20:47:22 +00008537 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00008538 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00008539 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00008540 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00008541 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
8542 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008543 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008544
Nate Begeman30a0de92008-07-17 16:51:19 +00008545 switch (SetCCOpcode) {
8546 default: break;
8547 case ISD::SETNE: Invert = true;
8548 case ISD::SETEQ: Opc = EQOpc; break;
8549 case ISD::SETLT: Swap = true;
8550 case ISD::SETGT: Opc = GTOpc; break;
8551 case ISD::SETGE: Swap = true;
8552 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
8553 case ISD::SETULT: Swap = true;
8554 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
8555 case ISD::SETUGE: Swap = true;
8556 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
8557 }
8558 if (Swap)
8559 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008560
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008561 // Check that the operation in question is available (most are plain SSE2,
8562 // but PCMPGTQ and PCMPEQQ have different requirements).
8563 if (Opc == X86ISD::PCMPGTQ && !Subtarget->hasSSE42() && !Subtarget->hasAVX())
8564 return SDValue();
8565 if (Opc == X86ISD::PCMPEQQ && !Subtarget->hasSSE41() && !Subtarget->hasAVX())
8566 return SDValue();
8567
Nate Begeman30a0de92008-07-17 16:51:19 +00008568 // Since SSE has no unsigned integer comparisons, we need to flip the sign
8569 // bits of the inputs before performing those operations.
8570 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00008571 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00008572 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
8573 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00008574 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00008575 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
8576 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00008577 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
8578 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00008579 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008580
Dale Johannesenace16102009-02-03 19:33:06 +00008581 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00008582
8583 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00008584 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00008585 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00008586
Nate Begeman30a0de92008-07-17 16:51:19 +00008587 return Result;
8588}
Evan Cheng0488db92007-09-25 01:57:46 +00008589
Evan Cheng370e5342008-12-03 08:38:43 +00008590// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00008591static bool isX86LogicalCmp(SDValue Op) {
8592 unsigned Opc = Op.getNode()->getOpcode();
8593 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
8594 return true;
8595 if (Op.getResNo() == 1 &&
8596 (Opc == X86ISD::ADD ||
8597 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00008598 Opc == X86ISD::ADC ||
8599 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00008600 Opc == X86ISD::SMUL ||
8601 Opc == X86ISD::UMUL ||
8602 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00008603 Opc == X86ISD::DEC ||
8604 Opc == X86ISD::OR ||
8605 Opc == X86ISD::XOR ||
8606 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00008607 return true;
8608
Chris Lattner9637d5b2010-12-05 07:49:54 +00008609 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
8610 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008611
Dan Gohman076aee32009-03-04 19:44:21 +00008612 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00008613}
8614
Chris Lattnera2b56002010-12-05 01:23:24 +00008615static bool isZero(SDValue V) {
8616 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8617 return C && C->isNullValue();
8618}
8619
Chris Lattner96908b12010-12-05 02:00:51 +00008620static bool isAllOnes(SDValue V) {
8621 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8622 return C && C->isAllOnesValue();
8623}
8624
Dan Gohmand858e902010-04-17 15:26:15 +00008625SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008626 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008627 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00008628 SDValue Op1 = Op.getOperand(1);
8629 SDValue Op2 = Op.getOperand(2);
8630 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008631 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00008632
Dan Gohman1a492952009-10-20 16:22:37 +00008633 if (Cond.getOpcode() == ISD::SETCC) {
8634 SDValue NewCond = LowerSETCC(Cond, DAG);
8635 if (NewCond.getNode())
8636 Cond = NewCond;
8637 }
Evan Cheng734503b2006-09-11 02:19:56 +00008638
Chris Lattnera2b56002010-12-05 01:23:24 +00008639 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008640 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00008641 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008642 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008643 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00008644 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
8645 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008646 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008647
Chris Lattnera2b56002010-12-05 01:23:24 +00008648 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008649
8650 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00008651 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
8652 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00008653
8654 SDValue CmpOp0 = Cmp.getOperand(0);
8655 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
8656 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008657
Chris Lattner96908b12010-12-05 02:00:51 +00008658 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00008659 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8660 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008661
Chris Lattner96908b12010-12-05 02:00:51 +00008662 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
8663 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008664
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008665 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00008666 if (N2C == 0 || !N2C->isNullValue())
8667 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
8668 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008669 }
8670 }
8671
Chris Lattnera2b56002010-12-05 01:23:24 +00008672 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00008673 if (Cond.getOpcode() == ISD::AND &&
8674 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8675 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008676 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008677 Cond = Cond.getOperand(0);
8678 }
8679
Evan Cheng3f41d662007-10-08 22:16:29 +00008680 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8681 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00008682 if (Cond.getOpcode() == X86ISD::SETCC ||
8683 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008684 CC = Cond.getOperand(0);
8685
Dan Gohman475871a2008-07-27 21:46:04 +00008686 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008687 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00008688 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00008689
Evan Cheng3f41d662007-10-08 22:16:29 +00008690 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008691 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00008692 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00008693 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00008694
Chris Lattnerd1980a52009-03-12 06:52:53 +00008695 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
8696 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00008697 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008698 addTest = false;
8699 }
8700 }
8701
8702 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008703 // Look pass the truncate.
8704 if (Cond.getOpcode() == ISD::TRUNCATE)
8705 Cond = Cond.getOperand(0);
8706
8707 // We know the result of AND is compared against zero. Try to match
8708 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00008709 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00008710 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00008711 if (NewSetCC.getNode()) {
8712 CC = NewSetCC.getOperand(0);
8713 Cond = NewSetCC.getOperand(1);
8714 addTest = false;
8715 }
8716 }
8717 }
8718
8719 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008720 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00008721 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00008722 }
8723
Benjamin Kramere915ff32010-12-22 23:09:28 +00008724 // a < b ? -1 : 0 -> RES = ~setcc_carry
8725 // a < b ? 0 : -1 -> RES = setcc_carry
8726 // a >= b ? -1 : 0 -> RES = setcc_carry
8727 // a >= b ? 0 : -1 -> RES = ~setcc_carry
8728 if (Cond.getOpcode() == X86ISD::CMP) {
8729 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
8730
8731 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
8732 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
8733 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8734 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
8735 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
8736 return DAG.getNOT(DL, Res, Res.getValueType());
8737 return Res;
8738 }
8739 }
8740
Evan Cheng0488db92007-09-25 01:57:46 +00008741 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
8742 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008743 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008744 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00008745 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00008746}
8747
Evan Cheng370e5342008-12-03 08:38:43 +00008748// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
8749// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
8750// from the AND / OR.
8751static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
8752 Opc = Op.getOpcode();
8753 if (Opc != ISD::OR && Opc != ISD::AND)
8754 return false;
8755 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8756 Op.getOperand(0).hasOneUse() &&
8757 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
8758 Op.getOperand(1).hasOneUse());
8759}
8760
Evan Cheng961d6d42009-02-02 08:19:07 +00008761// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
8762// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00008763static bool isXor1OfSetCC(SDValue Op) {
8764 if (Op.getOpcode() != ISD::XOR)
8765 return false;
8766 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
8767 if (N1C && N1C->getAPIntValue() == 1) {
8768 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8769 Op.getOperand(0).hasOneUse();
8770 }
8771 return false;
8772}
8773
Dan Gohmand858e902010-04-17 15:26:15 +00008774SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008775 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008776 SDValue Chain = Op.getOperand(0);
8777 SDValue Cond = Op.getOperand(1);
8778 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008779 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008780 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00008781
Dan Gohman1a492952009-10-20 16:22:37 +00008782 if (Cond.getOpcode() == ISD::SETCC) {
8783 SDValue NewCond = LowerSETCC(Cond, DAG);
8784 if (NewCond.getNode())
8785 Cond = NewCond;
8786 }
Chris Lattnere55484e2008-12-25 05:34:37 +00008787#if 0
8788 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00008789 else if (Cond.getOpcode() == X86ISD::ADD ||
8790 Cond.getOpcode() == X86ISD::SUB ||
8791 Cond.getOpcode() == X86ISD::SMUL ||
8792 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00008793 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00008794#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00008795
Evan Chengad9c0a32009-12-15 00:53:42 +00008796 // Look pass (and (setcc_carry (cmp ...)), 1).
8797 if (Cond.getOpcode() == ISD::AND &&
8798 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8799 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008800 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008801 Cond = Cond.getOperand(0);
8802 }
8803
Evan Cheng3f41d662007-10-08 22:16:29 +00008804 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8805 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00008806 if (Cond.getOpcode() == X86ISD::SETCC ||
8807 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008808 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008809
Dan Gohman475871a2008-07-27 21:46:04 +00008810 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008811 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00008812 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00008813 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00008814 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008815 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00008816 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00008817 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00008818 default: break;
8819 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00008820 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00008821 // These can only come from an arithmetic instruction with overflow,
8822 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00008823 Cond = Cond.getNode()->getOperand(1);
8824 addTest = false;
8825 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00008826 }
Evan Cheng0488db92007-09-25 01:57:46 +00008827 }
Evan Cheng370e5342008-12-03 08:38:43 +00008828 } else {
8829 unsigned CondOpc;
8830 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
8831 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00008832 if (CondOpc == ISD::OR) {
8833 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
8834 // two branches instead of an explicit OR instruction with a
8835 // separate test.
8836 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00008837 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00008838 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008839 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00008840 Chain, Dest, CC, Cmp);
8841 CC = Cond.getOperand(1).getOperand(0);
8842 Cond = Cmp;
8843 addTest = false;
8844 }
8845 } else { // ISD::AND
8846 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
8847 // two branches instead of an explicit AND instruction with a
8848 // separate test. However, we only do this if this block doesn't
8849 // have a fall-through edge, because this requires an explicit
8850 // jmp when the condition is false.
8851 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00008852 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00008853 Op.getNode()->hasOneUse()) {
8854 X86::CondCode CCode =
8855 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
8856 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008857 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00008858 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00008859 // Look for an unconditional branch following this conditional branch.
8860 // We need this because we need to reverse the successors in order
8861 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00008862 if (User->getOpcode() == ISD::BR) {
8863 SDValue FalseBB = User->getOperand(1);
8864 SDNode *NewBR =
8865 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00008866 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00008867 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00008868 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00008869
Dale Johannesene4d209d2009-02-03 20:21:25 +00008870 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00008871 Chain, Dest, CC, Cmp);
8872 X86::CondCode CCode =
8873 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
8874 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008875 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00008876 Cond = Cmp;
8877 addTest = false;
8878 }
8879 }
Dan Gohman279c22e2008-10-21 03:29:32 +00008880 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00008881 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
8882 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
8883 // It should be transformed during dag combiner except when the condition
8884 // is set by a arithmetics with overflow node.
8885 X86::CondCode CCode =
8886 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
8887 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008888 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00008889 Cond = Cond.getOperand(0).getOperand(1);
8890 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00008891 }
Evan Cheng0488db92007-09-25 01:57:46 +00008892 }
8893
8894 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008895 // Look pass the truncate.
8896 if (Cond.getOpcode() == ISD::TRUNCATE)
8897 Cond = Cond.getOperand(0);
8898
8899 // We know the result of AND is compared against zero. Try to match
8900 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00008901 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008902 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
8903 if (NewSetCC.getNode()) {
8904 CC = NewSetCC.getOperand(0);
8905 Cond = NewSetCC.getOperand(1);
8906 addTest = false;
8907 }
8908 }
8909 }
8910
8911 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008912 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00008913 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00008914 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00008915 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00008916 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00008917}
8918
Anton Korobeynikove060b532007-04-17 19:34:00 +00008919
8920// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
8921// Calls to _alloca is needed to probe the stack when allocating more than 4k
8922// bytes in one go. Touching the stack at 4K increments is necessary to ensure
8923// that the guard pages used by the OS virtual memory manager are allocated in
8924// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00008925SDValue
8926X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008927 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008928 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
8929 EnableSegmentedStacks) &&
8930 "This should be used only on Windows targets or when segmented stacks "
Rafael Espindola96428ce2011-09-06 18:43:08 +00008931 "are being used");
8932 assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008933 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008934
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008935 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00008936 SDValue Chain = Op.getOperand(0);
8937 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008938 // FIXME: Ensure alignment here
8939
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008940 bool Is64Bit = Subtarget->is64Bit();
8941 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008942
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008943 if (EnableSegmentedStacks) {
8944 MachineFunction &MF = DAG.getMachineFunction();
8945 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008946
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008947 if (Is64Bit) {
8948 // The 64 bit implementation of segmented stacks needs to clobber both r10
Rafael Espindola96428ce2011-09-06 18:43:08 +00008949 // r11. This makes it impossible to use it along with nested parameters.
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008950 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00008951
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008952 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
8953 I != E; I++)
8954 if (I->hasNestAttr())
8955 report_fatal_error("Cannot use segmented stacks with functions that "
8956 "have nested arguments.");
8957 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00008958
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008959 const TargetRegisterClass *AddrRegClass =
8960 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
8961 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
8962 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
8963 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
8964 DAG.getRegister(Vreg, SPTy));
8965 SDValue Ops1[2] = { Value, Chain };
8966 return DAG.getMergeValues(Ops1, 2, dl);
8967 } else {
8968 SDValue Flag;
8969 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008970
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008971 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
8972 Flag = Chain.getValue(1);
8973 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008974
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008975 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
8976 Flag = Chain.getValue(1);
8977
8978 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
8979
8980 SDValue Ops1[2] = { Chain.getValue(0), Chain };
8981 return DAG.getMergeValues(Ops1, 2, dl);
8982 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008983}
8984
Dan Gohmand858e902010-04-17 15:26:15 +00008985SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00008986 MachineFunction &MF = DAG.getMachineFunction();
8987 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
8988
Dan Gohman69de1932008-02-06 22:27:42 +00008989 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00008990 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00008991
Anton Korobeynikove7beda12010-10-03 22:52:07 +00008992 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00008993 // vastart just stores the address of the VarArgsFrameIndex slot into the
8994 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00008995 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
8996 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00008997 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
8998 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008999 }
9000
9001 // __va_list_tag:
9002 // gp_offset (0 - 6 * 8)
9003 // fp_offset (48 - 48 + 8 * 16)
9004 // overflow_arg_area (point to parameters coming in memory).
9005 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00009006 SmallVector<SDValue, 8> MemOps;
9007 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00009008 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009009 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009010 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
9011 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009012 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009013 MemOps.push_back(Store);
9014
9015 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009016 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009017 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009018 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009019 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
9020 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009021 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009022 MemOps.push_back(Store);
9023
9024 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00009025 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009026 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00009027 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9028 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009029 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
9030 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00009031 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009032 MemOps.push_back(Store);
9033
9034 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00009035 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009036 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00009037 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
9038 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009039 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
9040 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009041 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009042 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00009043 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00009044}
9045
Dan Gohmand858e902010-04-17 15:26:15 +00009046SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00009047 assert(Subtarget->is64Bit() &&
9048 "LowerVAARG only handles 64-bit va_arg!");
9049 assert((Subtarget->isTargetLinux() ||
9050 Subtarget->isTargetDarwin()) &&
9051 "Unhandled target in LowerVAARG");
9052 assert(Op.getNode()->getNumOperands() == 4);
9053 SDValue Chain = Op.getOperand(0);
9054 SDValue SrcPtr = Op.getOperand(1);
9055 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
9056 unsigned Align = Op.getConstantOperandVal(3);
9057 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00009058
Dan Gohman320afb82010-10-12 18:00:49 +00009059 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009060 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Dan Gohman320afb82010-10-12 18:00:49 +00009061 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
9062 uint8_t ArgMode;
9063
9064 // Decide which area this value should be read from.
9065 // TODO: Implement the AMD64 ABI in its entirety. This simple
9066 // selection mechanism works only for the basic types.
9067 if (ArgVT == MVT::f80) {
9068 llvm_unreachable("va_arg for f80 not yet implemented");
9069 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
9070 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
9071 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
9072 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
9073 } else {
9074 llvm_unreachable("Unhandled argument type in LowerVAARG");
9075 }
9076
9077 if (ArgMode == 2) {
9078 // Sanity Check: Make sure using fp_offset makes sense.
Michael J. Spencer87b86652010-10-19 07:32:42 +00009079 assert(!UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00009080 !(DAG.getMachineFunction()
9081 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00009082 Subtarget->hasXMM());
Dan Gohman320afb82010-10-12 18:00:49 +00009083 }
9084
9085 // Insert VAARG_64 node into the DAG
9086 // VAARG_64 returns two values: Variable Argument Address, Chain
9087 SmallVector<SDValue, 11> InstOps;
9088 InstOps.push_back(Chain);
9089 InstOps.push_back(SrcPtr);
9090 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
9091 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
9092 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
9093 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
9094 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
9095 VTs, &InstOps[0], InstOps.size(),
9096 MVT::i64,
9097 MachinePointerInfo(SV),
9098 /*Align=*/0,
9099 /*Volatile=*/false,
9100 /*ReadMem=*/true,
9101 /*WriteMem=*/true);
9102 Chain = VAARG.getValue(1);
9103
9104 // Load the next argument and return it
9105 return DAG.getLoad(ArgVT, dl,
9106 Chain,
9107 VAARG,
9108 MachinePointerInfo(),
9109 false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00009110}
9111
Dan Gohmand858e902010-04-17 15:26:15 +00009112SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00009113 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00009114 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00009115 SDValue Chain = Op.getOperand(0);
9116 SDValue DstPtr = Op.getOperand(1);
9117 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00009118 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
9119 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00009120 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00009121
Chris Lattnere72f2022010-09-21 05:40:29 +00009122 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00009123 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00009124 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00009125 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00009126}
9127
Dan Gohman475871a2008-07-27 21:46:04 +00009128SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00009129X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009130 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009131 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00009132 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00009133 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00009134 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00009135 case Intrinsic::x86_sse_comieq_ss:
9136 case Intrinsic::x86_sse_comilt_ss:
9137 case Intrinsic::x86_sse_comile_ss:
9138 case Intrinsic::x86_sse_comigt_ss:
9139 case Intrinsic::x86_sse_comige_ss:
9140 case Intrinsic::x86_sse_comineq_ss:
9141 case Intrinsic::x86_sse_ucomieq_ss:
9142 case Intrinsic::x86_sse_ucomilt_ss:
9143 case Intrinsic::x86_sse_ucomile_ss:
9144 case Intrinsic::x86_sse_ucomigt_ss:
9145 case Intrinsic::x86_sse_ucomige_ss:
9146 case Intrinsic::x86_sse_ucomineq_ss:
9147 case Intrinsic::x86_sse2_comieq_sd:
9148 case Intrinsic::x86_sse2_comilt_sd:
9149 case Intrinsic::x86_sse2_comile_sd:
9150 case Intrinsic::x86_sse2_comigt_sd:
9151 case Intrinsic::x86_sse2_comige_sd:
9152 case Intrinsic::x86_sse2_comineq_sd:
9153 case Intrinsic::x86_sse2_ucomieq_sd:
9154 case Intrinsic::x86_sse2_ucomilt_sd:
9155 case Intrinsic::x86_sse2_ucomile_sd:
9156 case Intrinsic::x86_sse2_ucomigt_sd:
9157 case Intrinsic::x86_sse2_ucomige_sd:
9158 case Intrinsic::x86_sse2_ucomineq_sd: {
9159 unsigned Opc = 0;
9160 ISD::CondCode CC = ISD::SETCC_INVALID;
9161 switch (IntNo) {
9162 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009163 case Intrinsic::x86_sse_comieq_ss:
9164 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009165 Opc = X86ISD::COMI;
9166 CC = ISD::SETEQ;
9167 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009168 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009169 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009170 Opc = X86ISD::COMI;
9171 CC = ISD::SETLT;
9172 break;
9173 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009174 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009175 Opc = X86ISD::COMI;
9176 CC = ISD::SETLE;
9177 break;
9178 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009179 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009180 Opc = X86ISD::COMI;
9181 CC = ISD::SETGT;
9182 break;
9183 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009184 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009185 Opc = X86ISD::COMI;
9186 CC = ISD::SETGE;
9187 break;
9188 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009189 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009190 Opc = X86ISD::COMI;
9191 CC = ISD::SETNE;
9192 break;
9193 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009194 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009195 Opc = X86ISD::UCOMI;
9196 CC = ISD::SETEQ;
9197 break;
9198 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009199 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009200 Opc = X86ISD::UCOMI;
9201 CC = ISD::SETLT;
9202 break;
9203 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009204 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009205 Opc = X86ISD::UCOMI;
9206 CC = ISD::SETLE;
9207 break;
9208 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009209 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009210 Opc = X86ISD::UCOMI;
9211 CC = ISD::SETGT;
9212 break;
9213 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009214 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009215 Opc = X86ISD::UCOMI;
9216 CC = ISD::SETGE;
9217 break;
9218 case Intrinsic::x86_sse_ucomineq_ss:
9219 case Intrinsic::x86_sse2_ucomineq_sd:
9220 Opc = X86ISD::UCOMI;
9221 CC = ISD::SETNE;
9222 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009223 }
Evan Cheng734503b2006-09-11 02:19:56 +00009224
Dan Gohman475871a2008-07-27 21:46:04 +00009225 SDValue LHS = Op.getOperand(1);
9226 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00009227 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00009228 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00009229 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
9230 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9231 DAG.getConstant(X86CC, MVT::i8), Cond);
9232 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00009233 }
Duncan Sands04aa4ae2011-09-23 16:10:22 +00009234 // Arithmetic intrinsics.
9235 case Intrinsic::x86_sse3_hadd_ps:
9236 case Intrinsic::x86_sse3_hadd_pd:
9237 case Intrinsic::x86_avx_hadd_ps_256:
9238 case Intrinsic::x86_avx_hadd_pd_256:
9239 return DAG.getNode(X86ISD::FHADD, dl, Op.getValueType(),
9240 Op.getOperand(1), Op.getOperand(2));
9241 case Intrinsic::x86_sse3_hsub_ps:
9242 case Intrinsic::x86_sse3_hsub_pd:
9243 case Intrinsic::x86_avx_hsub_ps_256:
9244 case Intrinsic::x86_avx_hsub_pd_256:
9245 return DAG.getNode(X86ISD::FHSUB, dl, Op.getValueType(),
9246 Op.getOperand(1), Op.getOperand(2));
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009247 // ptest and testp intrinsics. The intrinsic these come from are designed to
9248 // return an integer value, not just an instruction so lower it to the ptest
9249 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00009250 case Intrinsic::x86_sse41_ptestz:
9251 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009252 case Intrinsic::x86_sse41_ptestnzc:
9253 case Intrinsic::x86_avx_ptestz_256:
9254 case Intrinsic::x86_avx_ptestc_256:
9255 case Intrinsic::x86_avx_ptestnzc_256:
9256 case Intrinsic::x86_avx_vtestz_ps:
9257 case Intrinsic::x86_avx_vtestc_ps:
9258 case Intrinsic::x86_avx_vtestnzc_ps:
9259 case Intrinsic::x86_avx_vtestz_pd:
9260 case Intrinsic::x86_avx_vtestc_pd:
9261 case Intrinsic::x86_avx_vtestnzc_pd:
9262 case Intrinsic::x86_avx_vtestz_ps_256:
9263 case Intrinsic::x86_avx_vtestc_ps_256:
9264 case Intrinsic::x86_avx_vtestnzc_ps_256:
9265 case Intrinsic::x86_avx_vtestz_pd_256:
9266 case Intrinsic::x86_avx_vtestc_pd_256:
9267 case Intrinsic::x86_avx_vtestnzc_pd_256: {
9268 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00009269 unsigned X86CC = 0;
9270 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00009271 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009272 case Intrinsic::x86_avx_vtestz_ps:
9273 case Intrinsic::x86_avx_vtestz_pd:
9274 case Intrinsic::x86_avx_vtestz_ps_256:
9275 case Intrinsic::x86_avx_vtestz_pd_256:
9276 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009277 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009278 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009279 // ZF = 1
9280 X86CC = X86::COND_E;
9281 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009282 case Intrinsic::x86_avx_vtestc_ps:
9283 case Intrinsic::x86_avx_vtestc_pd:
9284 case Intrinsic::x86_avx_vtestc_ps_256:
9285 case Intrinsic::x86_avx_vtestc_pd_256:
9286 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009287 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009288 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009289 // CF = 1
9290 X86CC = X86::COND_B;
9291 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009292 case Intrinsic::x86_avx_vtestnzc_ps:
9293 case Intrinsic::x86_avx_vtestnzc_pd:
9294 case Intrinsic::x86_avx_vtestnzc_ps_256:
9295 case Intrinsic::x86_avx_vtestnzc_pd_256:
9296 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00009297 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009298 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009299 // ZF and CF = 0
9300 X86CC = X86::COND_A;
9301 break;
9302 }
Eric Christopherfd179292009-08-27 18:07:15 +00009303
Eric Christopher71c67532009-07-29 00:28:05 +00009304 SDValue LHS = Op.getOperand(1);
9305 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009306 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
9307 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00009308 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
9309 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
9310 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00009311 }
Evan Cheng5759f972008-05-04 09:15:50 +00009312
9313 // Fix vector shift instructions where the last operand is a non-immediate
9314 // i32 value.
9315 case Intrinsic::x86_sse2_pslli_w:
9316 case Intrinsic::x86_sse2_pslli_d:
9317 case Intrinsic::x86_sse2_pslli_q:
9318 case Intrinsic::x86_sse2_psrli_w:
9319 case Intrinsic::x86_sse2_psrli_d:
9320 case Intrinsic::x86_sse2_psrli_q:
9321 case Intrinsic::x86_sse2_psrai_w:
9322 case Intrinsic::x86_sse2_psrai_d:
9323 case Intrinsic::x86_mmx_pslli_w:
9324 case Intrinsic::x86_mmx_pslli_d:
9325 case Intrinsic::x86_mmx_pslli_q:
9326 case Intrinsic::x86_mmx_psrli_w:
9327 case Intrinsic::x86_mmx_psrli_d:
9328 case Intrinsic::x86_mmx_psrli_q:
9329 case Intrinsic::x86_mmx_psrai_w:
9330 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00009331 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00009332 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00009333 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00009334
9335 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00009336 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00009337 switch (IntNo) {
9338 case Intrinsic::x86_sse2_pslli_w:
9339 NewIntNo = Intrinsic::x86_sse2_psll_w;
9340 break;
9341 case Intrinsic::x86_sse2_pslli_d:
9342 NewIntNo = Intrinsic::x86_sse2_psll_d;
9343 break;
9344 case Intrinsic::x86_sse2_pslli_q:
9345 NewIntNo = Intrinsic::x86_sse2_psll_q;
9346 break;
9347 case Intrinsic::x86_sse2_psrli_w:
9348 NewIntNo = Intrinsic::x86_sse2_psrl_w;
9349 break;
9350 case Intrinsic::x86_sse2_psrli_d:
9351 NewIntNo = Intrinsic::x86_sse2_psrl_d;
9352 break;
9353 case Intrinsic::x86_sse2_psrli_q:
9354 NewIntNo = Intrinsic::x86_sse2_psrl_q;
9355 break;
9356 case Intrinsic::x86_sse2_psrai_w:
9357 NewIntNo = Intrinsic::x86_sse2_psra_w;
9358 break;
9359 case Intrinsic::x86_sse2_psrai_d:
9360 NewIntNo = Intrinsic::x86_sse2_psra_d;
9361 break;
9362 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00009363 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00009364 switch (IntNo) {
9365 case Intrinsic::x86_mmx_pslli_w:
9366 NewIntNo = Intrinsic::x86_mmx_psll_w;
9367 break;
9368 case Intrinsic::x86_mmx_pslli_d:
9369 NewIntNo = Intrinsic::x86_mmx_psll_d;
9370 break;
9371 case Intrinsic::x86_mmx_pslli_q:
9372 NewIntNo = Intrinsic::x86_mmx_psll_q;
9373 break;
9374 case Intrinsic::x86_mmx_psrli_w:
9375 NewIntNo = Intrinsic::x86_mmx_psrl_w;
9376 break;
9377 case Intrinsic::x86_mmx_psrli_d:
9378 NewIntNo = Intrinsic::x86_mmx_psrl_d;
9379 break;
9380 case Intrinsic::x86_mmx_psrli_q:
9381 NewIntNo = Intrinsic::x86_mmx_psrl_q;
9382 break;
9383 case Intrinsic::x86_mmx_psrai_w:
9384 NewIntNo = Intrinsic::x86_mmx_psra_w;
9385 break;
9386 case Intrinsic::x86_mmx_psrai_d:
9387 NewIntNo = Intrinsic::x86_mmx_psra_d;
9388 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00009389 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00009390 }
9391 break;
9392 }
9393 }
Mon P Wangefa42202009-09-03 19:56:25 +00009394
9395 // The vector shift intrinsics with scalars uses 32b shift amounts but
9396 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
9397 // to be zero.
9398 SDValue ShOps[4];
9399 ShOps[0] = ShAmt;
9400 ShOps[1] = DAG.getConstant(0, MVT::i32);
9401 if (ShAmtVT == MVT::v4i32) {
9402 ShOps[2] = DAG.getUNDEF(MVT::i32);
9403 ShOps[3] = DAG.getUNDEF(MVT::i32);
9404 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
9405 } else {
9406 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00009407// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00009408 }
9409
Owen Andersone50ed302009-08-10 22:56:29 +00009410 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009411 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009412 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009413 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00009414 Op.getOperand(1), ShAmt);
9415 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00009416 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00009417}
Evan Cheng72261582005-12-20 06:22:03 +00009418
Dan Gohmand858e902010-04-17 15:26:15 +00009419SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
9420 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00009421 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9422 MFI->setReturnAddressIsTaken(true);
9423
Bill Wendling64e87322009-01-16 19:25:27 +00009424 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009425 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00009426
9427 if (Depth > 0) {
9428 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
9429 SDValue Offset =
9430 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00009431 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009432 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00009433 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009434 FrameAddr, Offset),
Chris Lattner51abfe42010-09-21 06:02:19 +00009435 MachinePointerInfo(), false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00009436 }
9437
9438 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00009439 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00009440 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Chris Lattner51abfe42010-09-21 06:02:19 +00009441 RetAddrFI, MachinePointerInfo(), false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00009442}
9443
Dan Gohmand858e902010-04-17 15:26:15 +00009444SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00009445 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9446 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00009447
Owen Andersone50ed302009-08-10 22:56:29 +00009448 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009449 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00009450 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
9451 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00009452 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00009453 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00009454 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
9455 MachinePointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +00009456 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00009457 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00009458}
9459
Dan Gohman475871a2008-07-27 21:46:04 +00009460SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009461 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00009462 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009463}
9464
Dan Gohmand858e902010-04-17 15:26:15 +00009465SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009466 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00009467 SDValue Chain = Op.getOperand(0);
9468 SDValue Offset = Op.getOperand(1);
9469 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009470 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009471
Dan Gohmand8816272010-08-11 18:14:00 +00009472 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
9473 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
9474 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009475 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009476
Dan Gohmand8816272010-08-11 18:14:00 +00009477 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
9478 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009479 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009480 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
9481 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00009482 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009483 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009484
Dale Johannesene4d209d2009-02-03 20:21:25 +00009485 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00009486 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009487 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009488}
9489
Duncan Sands4a544a72011-09-06 13:37:06 +00009490SDValue X86TargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op,
9491 SelectionDAG &DAG) const {
9492 return Op.getOperand(0);
9493}
9494
9495SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
9496 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00009497 SDValue Root = Op.getOperand(0);
9498 SDValue Trmp = Op.getOperand(1); // trampoline
9499 SDValue FPtr = Op.getOperand(2); // nested function
9500 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009501 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009502
Dan Gohman69de1932008-02-06 22:27:42 +00009503 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009504
9505 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00009506 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00009507
9508 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00009509 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
9510 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00009511
Evan Cheng0e6a0522011-07-18 20:57:22 +00009512 const unsigned char N86R10 = X86_MC::getX86RegNum(X86::R10);
9513 const unsigned char N86R11 = X86_MC::getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00009514
9515 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
9516
9517 // Load the pointer to the nested function into R11.
9518 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00009519 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00009520 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009521 Addr, MachinePointerInfo(TrmpAddr),
9522 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009523
Owen Anderson825b72b2009-08-11 20:47:22 +00009524 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9525 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009526 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
9527 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00009528 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00009529
9530 // Load the 'nest' parameter value into R10.
9531 // R10 is specified in X86CallingConv.td
9532 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00009533 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9534 DAG.getConstant(10, MVT::i64));
9535 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009536 Addr, MachinePointerInfo(TrmpAddr, 10),
9537 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009538
Owen Anderson825b72b2009-08-11 20:47:22 +00009539 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9540 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009541 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
9542 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00009543 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00009544
9545 // Jump to the nested function.
9546 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00009547 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9548 DAG.getConstant(20, MVT::i64));
9549 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009550 Addr, MachinePointerInfo(TrmpAddr, 20),
9551 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009552
9553 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00009554 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9555 DAG.getConstant(22, MVT::i64));
9556 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00009557 MachinePointerInfo(TrmpAddr, 22),
9558 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009559
Duncan Sands4a544a72011-09-06 13:37:06 +00009560 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009561 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00009562 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00009563 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00009564 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00009565 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009566
9567 switch (CC) {
9568 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00009569 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00009570 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00009571 case CallingConv::X86_StdCall: {
9572 // Pass 'nest' parameter in ECX.
9573 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00009574 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009575
9576 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009577 FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00009578 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009579
Chris Lattner58d74912008-03-12 17:45:29 +00009580 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00009581 unsigned InRegCount = 0;
9582 unsigned Idx = 1;
9583
9584 for (FunctionType::param_iterator I = FTy->param_begin(),
9585 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00009586 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00009587 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00009588 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009589
9590 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00009591 report_fatal_error("Nest register in use - reduce number of inreg"
9592 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00009593 }
9594 }
9595 break;
9596 }
9597 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00009598 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00009599 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00009600 // Pass 'nest' parameter in EAX.
9601 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00009602 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009603 break;
9604 }
9605
Dan Gohman475871a2008-07-27 21:46:04 +00009606 SDValue OutChains[4];
9607 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009608
Owen Anderson825b72b2009-08-11 20:47:22 +00009609 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9610 DAG.getConstant(10, MVT::i32));
9611 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009612
Chris Lattnera62fe662010-02-05 19:20:30 +00009613 // This is storing the opcode for MOV32ri.
9614 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Evan Cheng0e6a0522011-07-18 20:57:22 +00009615 const unsigned char N86Reg = X86_MC::getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009616 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00009617 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009618 Trmp, MachinePointerInfo(TrmpAddr),
9619 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009620
Owen Anderson825b72b2009-08-11 20:47:22 +00009621 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9622 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009623 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
9624 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00009625 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009626
Chris Lattnera62fe662010-02-05 19:20:30 +00009627 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00009628 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9629 DAG.getConstant(5, MVT::i32));
9630 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00009631 MachinePointerInfo(TrmpAddr, 5),
9632 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009633
Owen Anderson825b72b2009-08-11 20:47:22 +00009634 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9635 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009636 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
9637 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00009638 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009639
Duncan Sands4a544a72011-09-06 13:37:06 +00009640 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009641 }
9642}
9643
Dan Gohmand858e902010-04-17 15:26:15 +00009644SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
9645 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009646 /*
9647 The rounding mode is in bits 11:10 of FPSR, and has the following
9648 settings:
9649 00 Round to nearest
9650 01 Round to -inf
9651 10 Round to +inf
9652 11 Round to 0
9653
9654 FLT_ROUNDS, on the other hand, expects the following:
9655 -1 Undefined
9656 0 Round to 0
9657 1 Round to nearest
9658 2 Round to +inf
9659 3 Round to -inf
9660
9661 To perform the conversion, we do:
9662 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
9663 */
9664
9665 MachineFunction &MF = DAG.getMachineFunction();
9666 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00009667 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009668 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00009669 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00009670 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009671
9672 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00009673 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00009674 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009675
Michael J. Spencerec38de22010-10-10 22:04:20 +00009676
Chris Lattner2156b792010-09-22 01:11:26 +00009677 MachineMemOperand *MMO =
9678 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
9679 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009680
Chris Lattner2156b792010-09-22 01:11:26 +00009681 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
9682 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
9683 DAG.getVTList(MVT::Other),
9684 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009685
9686 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00009687 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Chris Lattner51abfe42010-09-21 06:02:19 +00009688 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009689
9690 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00009691 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00009692 DAG.getNode(ISD::SRL, DL, MVT::i16,
9693 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00009694 CWD, DAG.getConstant(0x800, MVT::i16)),
9695 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00009696 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00009697 DAG.getNode(ISD::SRL, DL, MVT::i16,
9698 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00009699 CWD, DAG.getConstant(0x400, MVT::i16)),
9700 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009701
Dan Gohman475871a2008-07-27 21:46:04 +00009702 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00009703 DAG.getNode(ISD::AND, DL, MVT::i16,
9704 DAG.getNode(ISD::ADD, DL, MVT::i16,
9705 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00009706 DAG.getConstant(1, MVT::i16)),
9707 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009708
9709
Duncan Sands83ec4b62008-06-06 12:08:01 +00009710 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00009711 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009712}
9713
Dan Gohmand858e902010-04-17 15:26:15 +00009714SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009715 EVT VT = Op.getValueType();
9716 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009717 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009718 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00009719
9720 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009721 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00009722 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00009723 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00009724 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009725 }
Evan Cheng18efe262007-12-14 02:13:44 +00009726
Evan Cheng152804e2007-12-14 08:30:15 +00009727 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00009728 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009729 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00009730
9731 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00009732 SDValue Ops[] = {
9733 Op,
9734 DAG.getConstant(NumBits+NumBits-1, OpVT),
9735 DAG.getConstant(X86::COND_E, MVT::i8),
9736 Op.getValue(1)
9737 };
9738 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00009739
9740 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00009741 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00009742
Owen Anderson825b72b2009-08-11 20:47:22 +00009743 if (VT == MVT::i8)
9744 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009745 return Op;
9746}
9747
Dan Gohmand858e902010-04-17 15:26:15 +00009748SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009749 EVT VT = Op.getValueType();
9750 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009751 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009752 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00009753
9754 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009755 if (VT == MVT::i8) {
9756 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00009757 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009758 }
Evan Cheng152804e2007-12-14 08:30:15 +00009759
9760 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00009761 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009762 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00009763
9764 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00009765 SDValue Ops[] = {
9766 Op,
9767 DAG.getConstant(NumBits, OpVT),
9768 DAG.getConstant(X86::COND_E, MVT::i8),
9769 Op.getValue(1)
9770 };
9771 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00009772
Owen Anderson825b72b2009-08-11 20:47:22 +00009773 if (VT == MVT::i8)
9774 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009775 return Op;
9776}
9777
Craig Topper13894fa2011-08-24 06:14:18 +00009778// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
9779// ones, and then concatenate the result back.
9780static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00009781 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +00009782
9783 assert(VT.getSizeInBits() == 256 && VT.isInteger() &&
9784 "Unsupported value type for operation");
9785
9786 int NumElems = VT.getVectorNumElements();
9787 DebugLoc dl = Op.getDebugLoc();
9788 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
9789 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
9790
9791 // Extract the LHS vectors
9792 SDValue LHS = Op.getOperand(0);
9793 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
9794 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
9795
9796 // Extract the RHS vectors
9797 SDValue RHS = Op.getOperand(1);
9798 SDValue RHS1 = Extract128BitVector(RHS, Idx0, DAG, dl);
9799 SDValue RHS2 = Extract128BitVector(RHS, Idx1, DAG, dl);
9800
9801 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9802 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
9803
9804 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
9805 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
9806 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
9807}
9808
9809SDValue X86TargetLowering::LowerADD(SDValue Op, SelectionDAG &DAG) const {
9810 assert(Op.getValueType().getSizeInBits() == 256 &&
9811 Op.getValueType().isInteger() &&
9812 "Only handle AVX 256-bit vector integer operation");
9813 return Lower256IntArith(Op, DAG);
9814}
9815
9816SDValue X86TargetLowering::LowerSUB(SDValue Op, SelectionDAG &DAG) const {
9817 assert(Op.getValueType().getSizeInBits() == 256 &&
9818 Op.getValueType().isInteger() &&
9819 "Only handle AVX 256-bit vector integer operation");
9820 return Lower256IntArith(Op, DAG);
9821}
9822
9823SDValue X86TargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
9824 EVT VT = Op.getValueType();
9825
9826 // Decompose 256-bit ops into smaller 128-bit ops.
9827 if (VT.getSizeInBits() == 256)
9828 return Lower256IntArith(Op, DAG);
9829
Owen Anderson825b72b2009-08-11 20:47:22 +00009830 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009831 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00009832
Mon P Wangaf9b9522008-12-18 21:42:19 +00009833 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
9834 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
9835 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
9836 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
9837 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
9838 //
9839 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
9840 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
9841 // return AloBlo + AloBhi + AhiBlo;
9842
9843 SDValue A = Op.getOperand(0);
9844 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009845
Dale Johannesene4d209d2009-02-03 20:21:25 +00009846 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009847 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
9848 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009849 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009850 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
9851 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009852 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009853 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00009854 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009855 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009856 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00009857 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009858 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009859 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00009860 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009861 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009862 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
9863 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009864 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009865 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
9866 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009867 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
9868 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00009869 return Res;
9870}
9871
Nadav Rotem43012222011-05-11 08:12:09 +00009872SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
9873
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009874 EVT VT = Op.getValueType();
9875 DebugLoc dl = Op.getDebugLoc();
9876 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +00009877 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009878 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009879
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00009880 if (!Subtarget->hasXMMInt())
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00009881 return SDValue();
9882
9883 // Decompose 256-bit shifts into smaller 128-bit shifts.
9884 if (VT.getSizeInBits() == 256) {
9885 int NumElems = VT.getVectorNumElements();
9886 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9887 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
9888
9889 // Extract the two vectors
9890 SDValue V1 = Extract128BitVector(R, DAG.getConstant(0, MVT::i32), DAG, dl);
9891 SDValue V2 = Extract128BitVector(R, DAG.getConstant(NumElems/2, MVT::i32),
9892 DAG, dl);
9893
9894 // Recreate the shift amount vectors
Bruno Cardoso Lopes0dd80b02011-08-17 22:12:20 +00009895 SDValue Amt1, Amt2;
9896 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
9897 // Constant shift amount
9898 SmallVector<SDValue, 4> Amt1Csts;
9899 SmallVector<SDValue, 4> Amt2Csts;
9900 for (int i = 0; i < NumElems/2; ++i)
9901 Amt1Csts.push_back(Amt->getOperand(i));
9902 for (int i = NumElems/2; i < NumElems; ++i)
9903 Amt2Csts.push_back(Amt->getOperand(i));
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00009904
Bruno Cardoso Lopes0dd80b02011-08-17 22:12:20 +00009905 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
9906 &Amt1Csts[0], NumElems/2);
9907 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
9908 &Amt2Csts[0], NumElems/2);
9909 } else {
9910 // Variable shift amount
9911 Amt1 = Extract128BitVector(Amt, DAG.getConstant(0, MVT::i32), DAG, dl);
9912 Amt2 = Extract128BitVector(Amt, DAG.getConstant(NumElems/2, MVT::i32),
9913 DAG, dl);
9914 }
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00009915
9916 // Issue new vector shifts for the smaller types
9917 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
9918 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
9919
9920 // Concatenate the result back
9921 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
9922 }
Nate Begeman51409212010-07-28 00:21:48 +00009923
Nadav Rotem43012222011-05-11 08:12:09 +00009924 // Optimize shl/srl/sra with constant shift amount.
9925 if (isSplatVector(Amt.getNode())) {
9926 SDValue SclrAmt = Amt->getOperand(0);
9927 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
9928 uint64_t ShiftAmt = C->getZExtValue();
9929
9930 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SHL)
9931 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9932 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
9933 R, DAG.getConstant(ShiftAmt, MVT::i32));
9934
9935 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SHL)
9936 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9937 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
9938 R, DAG.getConstant(ShiftAmt, MVT::i32));
9939
9940 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SHL)
9941 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9942 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
9943 R, DAG.getConstant(ShiftAmt, MVT::i32));
9944
9945 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SRL)
9946 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9947 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
9948 R, DAG.getConstant(ShiftAmt, MVT::i32));
9949
9950 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRL)
9951 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9952 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
9953 R, DAG.getConstant(ShiftAmt, MVT::i32));
9954
9955 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRL)
9956 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9957 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
9958 R, DAG.getConstant(ShiftAmt, MVT::i32));
9959
9960 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRA)
9961 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9962 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
9963 R, DAG.getConstant(ShiftAmt, MVT::i32));
9964
9965 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRA)
9966 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9967 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
9968 R, DAG.getConstant(ShiftAmt, MVT::i32));
9969 }
9970 }
9971
9972 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +00009973 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +00009974 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9975 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
9976 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
9977
9978 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009979
Nate Begeman51409212010-07-28 00:21:48 +00009980 std::vector<Constant*> CV(4, CI);
9981 Constant *C = ConstantVector::get(CV);
9982 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
9983 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00009984 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00009985 false, false, 16);
9986
9987 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009988 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +00009989 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
9990 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
9991 }
Nadav Rotem43012222011-05-11 08:12:09 +00009992 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +00009993 // a = a << 5;
9994 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9995 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
9996 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
9997
9998 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
9999 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
10000
10001 std::vector<Constant*> CVM1(16, CM1);
10002 std::vector<Constant*> CVM2(16, CM2);
10003 Constant *C = ConstantVector::get(CVM1);
10004 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10005 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010006 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +000010007 false, false, 16);
10008
10009 // r = pblendv(r, psllw(r & (char16)15, 4), a);
10010 M = DAG.getNode(ISD::AND, dl, VT, R, M);
10011 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10012 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
10013 DAG.getConstant(4, MVT::i32));
Nadav Rotemfbad25e2011-09-11 15:02:23 +000010014 R = DAG.getNode(ISD::VSELECT, dl, VT, Op, R, M);
Nate Begeman51409212010-07-28 00:21:48 +000010015 // a += a
10016 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010017
Nate Begeman51409212010-07-28 00:21:48 +000010018 C = ConstantVector::get(CVM2);
10019 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10020 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010021 MachinePointerInfo::getConstantPool(),
Chris Lattner51abfe42010-09-21 06:02:19 +000010022 false, false, 16);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010023
Nate Begeman51409212010-07-28 00:21:48 +000010024 // r = pblendv(r, psllw(r & (char16)63, 2), a);
10025 M = DAG.getNode(ISD::AND, dl, VT, R, M);
10026 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10027 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
10028 DAG.getConstant(2, MVT::i32));
Nadav Rotemfbad25e2011-09-11 15:02:23 +000010029 R = DAG.getNode(ISD::VSELECT, dl, VT, Op, R, M);
Nate Begeman51409212010-07-28 00:21:48 +000010030 // a += a
10031 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010032
Nate Begeman51409212010-07-28 00:21:48 +000010033 // return pblendv(r, r+r, a);
Nadav Rotemfbad25e2011-09-11 15:02:23 +000010034 R = DAG.getNode(ISD::VSELECT, dl, VT, Op,
10035 R, DAG.getNode(ISD::ADD, dl, VT, R, R));
Nate Begeman51409212010-07-28 00:21:48 +000010036 return R;
10037 }
10038 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010039}
Mon P Wangaf9b9522008-12-18 21:42:19 +000010040
Dan Gohmand858e902010-04-17 15:26:15 +000010041SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +000010042 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
10043 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +000010044 // looks for this combo and may remove the "setcc" instruction if the "setcc"
10045 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +000010046 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +000010047 SDValue LHS = N->getOperand(0);
10048 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +000010049 unsigned BaseOp = 0;
10050 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010051 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +000010052 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010053 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +000010054 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +000010055 // A subtract of one will be selected as a INC. Note that INC doesn't
10056 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010057 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10058 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010059 BaseOp = X86ISD::INC;
10060 Cond = X86::COND_O;
10061 break;
10062 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010063 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +000010064 Cond = X86::COND_O;
10065 break;
10066 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010067 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +000010068 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010069 break;
10070 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +000010071 // A subtract of one will be selected as a DEC. Note that DEC doesn't
10072 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010073 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10074 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010075 BaseOp = X86ISD::DEC;
10076 Cond = X86::COND_O;
10077 break;
10078 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010079 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +000010080 Cond = X86::COND_O;
10081 break;
10082 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010083 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +000010084 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010085 break;
10086 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000010087 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000010088 Cond = X86::COND_O;
10089 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010090 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
10091 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
10092 MVT::i32);
10093 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010094
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010095 SDValue SetCC =
10096 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10097 DAG.getConstant(X86::COND_O, MVT::i32),
10098 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010099
Dan Gohman6e5fda22011-07-22 18:45:15 +000010100 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010101 }
Bill Wendling74c37652008-12-09 22:08:41 +000010102 }
Bill Wendling3fafd932008-11-26 22:37:40 +000010103
Bill Wendling61edeb52008-12-02 01:06:39 +000010104 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010105 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010106 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000010107
Bill Wendling61edeb52008-12-02 01:06:39 +000010108 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010109 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
10110 DAG.getConstant(Cond, MVT::i32),
10111 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000010112
Dan Gohman6e5fda22011-07-22 18:45:15 +000010113 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000010114}
10115
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010116SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op, SelectionDAG &DAG) const{
10117 DebugLoc dl = Op.getDebugLoc();
10118 SDNode* Node = Op.getNode();
10119 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
10120 EVT VT = Node->getValueType(0);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010121 if (Subtarget->hasXMMInt() && VT.isVector()) {
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010122 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
10123 ExtraVT.getScalarType().getSizeInBits();
10124 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
10125
10126 unsigned SHLIntrinsicsID = 0;
10127 unsigned SRAIntrinsicsID = 0;
10128 switch (VT.getSimpleVT().SimpleTy) {
10129 default:
10130 return SDValue();
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010131 case MVT::v4i32: {
10132 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_d;
10133 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_d;
10134 break;
10135 }
10136 case MVT::v8i16: {
10137 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_w;
10138 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_w;
10139 break;
10140 }
10141 }
10142
10143 SDValue Tmp1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10144 DAG.getConstant(SHLIntrinsicsID, MVT::i32),
10145 Node->getOperand(0), ShAmt);
10146
10147 // In case of 1 bit sext, no need to shr
10148 if (ExtraVT.getScalarType().getSizeInBits() == 1) return Tmp1;
10149
Nadav Rotema7934dd2011-10-10 19:31:45 +000010150 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10151 DAG.getConstant(SRAIntrinsicsID, MVT::i32),
10152 Tmp1, ShAmt);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010153 }
10154
10155 return SDValue();
10156}
10157
10158
Eric Christopher9a9d2752010-07-22 02:48:34 +000010159SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
10160 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010161
Eric Christopher77ed1352011-07-08 00:04:56 +000010162 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
10163 // There isn't any reason to disable it if the target processor supports it.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010164 if (!Subtarget->hasXMMInt() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +000010165 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +000010166 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +000010167 SDValue Ops[] = {
10168 DAG.getRegister(X86::ESP, MVT::i32), // Base
10169 DAG.getTargetConstant(1, MVT::i8), // Scale
10170 DAG.getRegister(0, MVT::i32), // Index
10171 DAG.getTargetConstant(0, MVT::i32), // Disp
10172 DAG.getRegister(0, MVT::i32), // Segment.
10173 Zero,
10174 Chain
10175 };
Michael J. Spencerec38de22010-10-10 22:04:20 +000010176 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +000010177 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
10178 array_lengthof(Ops));
10179 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +000010180 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010181
Eric Christopher9a9d2752010-07-22 02:48:34 +000010182 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +000010183 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +000010184 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010185
Chris Lattner132929a2010-08-14 17:26:09 +000010186 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10187 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
10188 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
10189 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010190
Chris Lattner132929a2010-08-14 17:26:09 +000010191 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
10192 if (!Op1 && !Op2 && !Op3 && Op4)
10193 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010194
Chris Lattner132929a2010-08-14 17:26:09 +000010195 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
10196 if (Op1 && !Op2 && !Op3 && !Op4)
10197 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010198
10199 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +000010200 // (MFENCE)>;
10201 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +000010202}
10203
Eli Friedman14648462011-07-27 22:21:52 +000010204SDValue X86TargetLowering::LowerATOMIC_FENCE(SDValue Op,
10205 SelectionDAG &DAG) const {
10206 DebugLoc dl = Op.getDebugLoc();
10207 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
10208 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
10209 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
10210 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
10211
10212 // The only fence that needs an instruction is a sequentially-consistent
10213 // cross-thread fence.
10214 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
10215 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
10216 // no-sse2). There isn't any reason to disable it if the target processor
10217 // supports it.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010218 if (Subtarget->hasXMMInt() || Subtarget->is64Bit())
Eli Friedman14648462011-07-27 22:21:52 +000010219 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
10220
10221 SDValue Chain = Op.getOperand(0);
10222 SDValue Zero = DAG.getConstant(0, MVT::i32);
10223 SDValue Ops[] = {
10224 DAG.getRegister(X86::ESP, MVT::i32), // Base
10225 DAG.getTargetConstant(1, MVT::i8), // Scale
10226 DAG.getRegister(0, MVT::i32), // Index
10227 DAG.getTargetConstant(0, MVT::i32), // Disp
10228 DAG.getRegister(0, MVT::i32), // Segment.
10229 Zero,
10230 Chain
10231 };
10232 SDNode *Res =
10233 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
10234 array_lengthof(Ops));
10235 return SDValue(Res, 0);
10236 }
10237
10238 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
10239 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
10240}
10241
10242
Dan Gohmand858e902010-04-17 15:26:15 +000010243SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010244 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010245 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000010246 unsigned Reg = 0;
10247 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000010248 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000010249 default:
10250 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000010251 case MVT::i8: Reg = X86::AL; size = 1; break;
10252 case MVT::i16: Reg = X86::AX; size = 2; break;
10253 case MVT::i32: Reg = X86::EAX; size = 4; break;
10254 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000010255 assert(Subtarget->is64Bit() && "Node not type legal!");
10256 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000010257 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000010258 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010259 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000010260 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000010261 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000010262 Op.getOperand(1),
10263 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000010264 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000010265 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010266 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010267 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
10268 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
10269 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000010270 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010271 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000010272 return cpOut;
10273}
10274
Duncan Sands1607f052008-12-01 11:39:25 +000010275SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010276 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +000010277 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010278 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000010279 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010280 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +000010281 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010282 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
10283 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000010284 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000010285 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
10286 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000010287 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000010288 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000010289 rdx.getValue(1)
10290 };
Dale Johannesene4d209d2009-02-03 20:21:25 +000010291 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010292}
10293
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010294SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +000010295 SelectionDAG &DAG) const {
10296 EVT SrcVT = Op.getOperand(0).getValueType();
10297 EVT DstVT = Op.getValueType();
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010298 assert(Subtarget->is64Bit() && !Subtarget->hasXMMInt() &&
Chris Lattner2a786eb2010-12-19 20:19:20 +000010299 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000010300 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000010301 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010302 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000010303 // i64 <=> MMX conversions are Legal.
10304 if (SrcVT==MVT::i64 && DstVT.isVector())
10305 return Op;
10306 if (DstVT==MVT::i64 && SrcVT.isVector())
10307 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000010308 // MMX <=> MMX conversions are Legal.
10309 if (SrcVT.isVector() && DstVT.isVector())
10310 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000010311 // All other conversions need to be expanded.
10312 return SDValue();
10313}
Chris Lattner5b856542010-12-20 00:59:46 +000010314
Dan Gohmand858e902010-04-17 15:26:15 +000010315SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000010316 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +000010317 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000010318 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010319 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000010320 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010321 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010322 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000010323 Node->getOperand(0),
10324 Node->getOperand(1), negOp,
10325 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000010326 cast<AtomicSDNode>(Node)->getAlignment(),
10327 cast<AtomicSDNode>(Node)->getOrdering(),
10328 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000010329}
10330
Eli Friedman327236c2011-08-24 20:50:09 +000010331static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
10332 SDNode *Node = Op.getNode();
10333 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010334 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000010335
10336 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010337 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
10338 // FIXME: On 32-bit, store -> fist or movq would be more efficient
10339 // (The only way to get a 16-byte store is cmpxchg16b)
10340 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
10341 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
10342 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000010343 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
10344 cast<AtomicSDNode>(Node)->getMemoryVT(),
10345 Node->getOperand(0),
10346 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010347 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000010348 cast<AtomicSDNode>(Node)->getOrdering(),
10349 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000010350 return Swap.getValue(1);
10351 }
10352 // Other atomic stores have a simple pattern.
10353 return Op;
10354}
10355
Chris Lattner5b856542010-12-20 00:59:46 +000010356static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
10357 EVT VT = Op.getNode()->getValueType(0);
10358
10359 // Let legalize expand this if it isn't a legal type yet.
10360 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
10361 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010362
Chris Lattner5b856542010-12-20 00:59:46 +000010363 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010364
Chris Lattner5b856542010-12-20 00:59:46 +000010365 unsigned Opc;
10366 bool ExtraOp = false;
10367 switch (Op.getOpcode()) {
10368 default: assert(0 && "Invalid code");
10369 case ISD::ADDC: Opc = X86ISD::ADD; break;
10370 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
10371 case ISD::SUBC: Opc = X86ISD::SUB; break;
10372 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
10373 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010374
Chris Lattner5b856542010-12-20 00:59:46 +000010375 if (!ExtraOp)
10376 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
10377 Op.getOperand(1));
10378 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
10379 Op.getOperand(1), Op.getOperand(2));
10380}
10381
Evan Cheng0db9fe62006-04-25 20:13:52 +000010382/// LowerOperation - Provide custom lowering hooks for some operations.
10383///
Dan Gohmand858e902010-04-17 15:26:15 +000010384SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000010385 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010386 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010387 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Eric Christopher9a9d2752010-07-22 02:48:34 +000010388 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Eli Friedman14648462011-07-27 22:21:52 +000010389 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010390 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
10391 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000010392 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010393 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000010394 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010395 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
10396 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
10397 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +000010398 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +000010399 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010400 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
10401 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
10402 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000010403 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000010404 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000010405 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010406 case ISD::SHL_PARTS:
10407 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000010408 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010409 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000010410 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010411 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000010412 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010413 case ISD::FABS: return LowerFABS(Op, DAG);
10414 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000010415 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000010416 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000010417 case ISD::SETCC: return LowerSETCC(Op, DAG);
10418 case ISD::SELECT: return LowerSELECT(Op, DAG);
10419 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010420 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010421 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000010422 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +000010423 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010424 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000010425 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
10426 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010427 case ISD::FRAME_TO_ARGS_OFFSET:
10428 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010429 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010430 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +000010431 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
10432 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000010433 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000010434 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
10435 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000010436 case ISD::MUL: return LowerMUL(Op, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000010437 case ISD::SRA:
10438 case ISD::SRL:
10439 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000010440 case ISD::SADDO:
10441 case ISD::UADDO:
10442 case ISD::SSUBO:
10443 case ISD::USUBO:
10444 case ISD::SMULO:
10445 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +000010446 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010447 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000010448 case ISD::ADDC:
10449 case ISD::ADDE:
10450 case ISD::SUBC:
10451 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000010452 case ISD::ADD: return LowerADD(Op, DAG);
10453 case ISD::SUB: return LowerSUB(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010454 }
Chris Lattner27a6c732007-11-24 07:07:01 +000010455}
10456
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010457static void ReplaceATOMIC_LOAD(SDNode *Node,
10458 SmallVectorImpl<SDValue> &Results,
10459 SelectionDAG &DAG) {
10460 DebugLoc dl = Node->getDebugLoc();
10461 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
10462
10463 // Convert wide load -> cmpxchg8b/cmpxchg16b
10464 // FIXME: On 32-bit, load -> fild or movq would be more efficient
10465 // (The only way to get a 16-byte load is cmpxchg16b)
10466 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000010467 SDValue Zero = DAG.getConstant(0, VT);
10468 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010469 Node->getOperand(0),
10470 Node->getOperand(1), Zero, Zero,
10471 cast<AtomicSDNode>(Node)->getMemOperand(),
10472 cast<AtomicSDNode>(Node)->getOrdering(),
10473 cast<AtomicSDNode>(Node)->getSynchScope());
10474 Results.push_back(Swap.getValue(0));
10475 Results.push_back(Swap.getValue(1));
10476}
10477
Duncan Sands1607f052008-12-01 11:39:25 +000010478void X86TargetLowering::
10479ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000010480 SelectionDAG &DAG, unsigned NewOp) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010481 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +000010482 assert (Node->getValueType(0) == MVT::i64 &&
10483 "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000010484
10485 SDValue Chain = Node->getOperand(0);
10486 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010487 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010488 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000010489 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010490 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000010491 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000010492 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000010493 SDValue Result =
10494 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
10495 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000010496 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000010497 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010498 Results.push_back(Result.getValue(2));
10499}
10500
Duncan Sands126d9072008-07-04 11:47:58 +000010501/// ReplaceNodeResults - Replace a node with an illegal result type
10502/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000010503void X86TargetLowering::ReplaceNodeResults(SDNode *N,
10504 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000010505 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010506 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +000010507 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000010508 default:
Duncan Sands1607f052008-12-01 11:39:25 +000010509 assert(false && "Do not know how to custom type legalize this operation!");
10510 return;
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010511 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000010512 case ISD::ADDC:
10513 case ISD::ADDE:
10514 case ISD::SUBC:
10515 case ISD::SUBE:
10516 // We don't want to expand or promote these.
10517 return;
Duncan Sands1607f052008-12-01 11:39:25 +000010518 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +000010519 std::pair<SDValue,SDValue> Vals =
10520 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +000010521 SDValue FIST = Vals.first, StackSlot = Vals.second;
10522 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000010523 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000010524 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +000010525 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
10526 MachinePointerInfo(), false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +000010527 }
10528 return;
10529 }
10530 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010531 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000010532 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010533 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010534 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000010535 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000010536 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010537 eax.getValue(2));
10538 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
10539 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +000010540 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010541 Results.push_back(edx.getValue(1));
10542 return;
10543 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010544 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000010545 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000010546 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000010547 bool Regs64bit = T == MVT::i128;
10548 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000010549 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000010550 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
10551 DAG.getConstant(0, HalfT));
10552 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
10553 DAG.getConstant(1, HalfT));
10554 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
10555 Regs64bit ? X86::RAX : X86::EAX,
10556 cpInL, SDValue());
10557 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
10558 Regs64bit ? X86::RDX : X86::EDX,
10559 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000010560 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000010561 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
10562 DAG.getConstant(0, HalfT));
10563 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
10564 DAG.getConstant(1, HalfT));
10565 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
10566 Regs64bit ? X86::RBX : X86::EBX,
10567 swapInL, cpInH.getValue(1));
10568 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
10569 Regs64bit ? X86::RCX : X86::ECX,
10570 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000010571 SDValue Ops[] = { swapInH.getValue(0),
10572 N->getOperand(1),
10573 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010574 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000010575 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000010576 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
10577 X86ISD::LCMPXCHG8_DAG;
10578 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000010579 Ops, 3, T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000010580 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
10581 Regs64bit ? X86::RAX : X86::EAX,
10582 HalfT, Result.getValue(1));
10583 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
10584 Regs64bit ? X86::RDX : X86::EDX,
10585 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000010586 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000010587 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010588 Results.push_back(cpOutH.getValue(1));
10589 return;
10590 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010591 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +000010592 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
10593 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010594 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +000010595 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
10596 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010597 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +000010598 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
10599 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010600 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +000010601 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
10602 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010603 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +000010604 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
10605 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010606 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +000010607 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
10608 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010609 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +000010610 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
10611 return;
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010612 case ISD::ATOMIC_LOAD:
10613 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000010614 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000010615}
10616
Evan Cheng72261582005-12-20 06:22:03 +000010617const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
10618 switch (Opcode) {
10619 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000010620 case X86ISD::BSF: return "X86ISD::BSF";
10621 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000010622 case X86ISD::SHLD: return "X86ISD::SHLD";
10623 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000010624 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000010625 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000010626 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000010627 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000010628 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000010629 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000010630 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
10631 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
10632 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000010633 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000010634 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000010635 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000010636 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000010637 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000010638 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000010639 case X86ISD::COMI: return "X86ISD::COMI";
10640 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000010641 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000010642 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000010643 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
10644 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000010645 case X86ISD::CMOV: return "X86ISD::CMOV";
10646 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000010647 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000010648 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
10649 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000010650 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000010651 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000010652 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000010653 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000010654 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000010655 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
10656 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000010657 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000010658 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000010659 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Nate Begemanb65c1752010-12-17 22:55:37 +000010660 case X86ISD::PSIGNB: return "X86ISD::PSIGNB";
10661 case X86ISD::PSIGNW: return "X86ISD::PSIGNW";
10662 case X86ISD::PSIGND: return "X86ISD::PSIGND";
Evan Cheng8ca29322006-11-10 21:43:37 +000010663 case X86ISD::FMAX: return "X86ISD::FMAX";
10664 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +000010665 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
10666 case X86ISD::FRCP: return "X86ISD::FRCP";
Duncan Sands17470be2011-09-22 20:15:48 +000010667 case X86ISD::FHADD: return "X86ISD::FHADD";
10668 case X86ISD::FHSUB: return "X86ISD::FHSUB";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000010669 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000010670 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010671 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000010672 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010673 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +000010674 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
10675 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010676 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
10677 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
10678 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
10679 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
10680 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
10681 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000010682 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
10683 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +000010684 case X86ISD::VSHL: return "X86ISD::VSHL";
10685 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +000010686 case X86ISD::CMPPD: return "X86ISD::CMPPD";
10687 case X86ISD::CMPPS: return "X86ISD::CMPPS";
10688 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
10689 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
10690 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
10691 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
10692 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
10693 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
10694 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
10695 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010696 case X86ISD::ADD: return "X86ISD::ADD";
10697 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000010698 case X86ISD::ADC: return "X86ISD::ADC";
10699 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000010700 case X86ISD::SMUL: return "X86ISD::SMUL";
10701 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000010702 case X86ISD::INC: return "X86ISD::INC";
10703 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000010704 case X86ISD::OR: return "X86ISD::OR";
10705 case X86ISD::XOR: return "X86ISD::XOR";
10706 case X86ISD::AND: return "X86ISD::AND";
Craig Topper54a11172011-10-14 07:06:56 +000010707 case X86ISD::ANDN: return "X86ISD::ANDN";
Evan Cheng73f24c92009-03-30 21:36:47 +000010708 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000010709 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010710 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010711 case X86ISD::PALIGN: return "X86ISD::PALIGN";
10712 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
10713 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
10714 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
10715 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
10716 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
10717 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
10718 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
10719 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010720 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000010721 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010722 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000010723 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
10724 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010725 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
10726 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
10727 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
10728 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
10729 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
10730 case X86ISD::MOVSD: return "X86ISD::MOVSD";
10731 case X86ISD::MOVSS: return "X86ISD::MOVSS";
10732 case X86ISD::UNPCKLPS: return "X86ISD::UNPCKLPS";
10733 case X86ISD::UNPCKLPD: return "X86ISD::UNPCKLPD";
David Greenefbf05d32011-02-22 23:31:46 +000010734 case X86ISD::VUNPCKLPDY: return "X86ISD::VUNPCKLPDY";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010735 case X86ISD::UNPCKHPS: return "X86ISD::UNPCKHPS";
10736 case X86ISD::UNPCKHPD: return "X86ISD::UNPCKHPD";
10737 case X86ISD::PUNPCKLBW: return "X86ISD::PUNPCKLBW";
10738 case X86ISD::PUNPCKLWD: return "X86ISD::PUNPCKLWD";
10739 case X86ISD::PUNPCKLDQ: return "X86ISD::PUNPCKLDQ";
10740 case X86ISD::PUNPCKLQDQ: return "X86ISD::PUNPCKLQDQ";
10741 case X86ISD::PUNPCKHBW: return "X86ISD::PUNPCKHBW";
10742 case X86ISD::PUNPCKHWD: return "X86ISD::PUNPCKHWD";
10743 case X86ISD::PUNPCKHDQ: return "X86ISD::PUNPCKHDQ";
10744 case X86ISD::PUNPCKHQDQ: return "X86ISD::PUNPCKHQDQ";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000010745 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +000010746 case X86ISD::VPERMILPS: return "X86ISD::VPERMILPS";
10747 case X86ISD::VPERMILPSY: return "X86ISD::VPERMILPSY";
10748 case X86ISD::VPERMILPD: return "X86ISD::VPERMILPD";
10749 case X86ISD::VPERMILPDY: return "X86ISD::VPERMILPDY";
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +000010750 case X86ISD::VPERM2F128: return "X86ISD::VPERM2F128";
Dan Gohmand6708ea2009-08-15 01:38:56 +000010751 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000010752 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010753 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000010754 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000010755 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +000010756 }
10757}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010758
Chris Lattnerc9addb72007-03-30 23:15:24 +000010759// isLegalAddressingMode - Return true if the addressing mode represented
10760// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000010761bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010762 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000010763 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010764 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000010765 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000010766
Chris Lattnerc9addb72007-03-30 23:15:24 +000010767 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010768 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000010769 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000010770
Chris Lattnerc9addb72007-03-30 23:15:24 +000010771 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000010772 unsigned GVFlags =
10773 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010774
Chris Lattnerdfed4132009-07-10 07:38:24 +000010775 // If a reference to this global requires an extra load, we can't fold it.
10776 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000010777 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010778
Chris Lattnerdfed4132009-07-10 07:38:24 +000010779 // If BaseGV requires a register for the PIC base, we cannot also have a
10780 // BaseReg specified.
10781 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000010782 return false;
Evan Cheng52787842007-08-01 23:46:47 +000010783
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010784 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000010785 if ((M != CodeModel::Small || R != Reloc::Static) &&
10786 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010787 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000010788 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010789
Chris Lattnerc9addb72007-03-30 23:15:24 +000010790 switch (AM.Scale) {
10791 case 0:
10792 case 1:
10793 case 2:
10794 case 4:
10795 case 8:
10796 // These scales always work.
10797 break;
10798 case 3:
10799 case 5:
10800 case 9:
10801 // These scales are formed with basereg+scalereg. Only accept if there is
10802 // no basereg yet.
10803 if (AM.HasBaseReg)
10804 return false;
10805 break;
10806 default: // Other stuff never works.
10807 return false;
10808 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010809
Chris Lattnerc9addb72007-03-30 23:15:24 +000010810 return true;
10811}
10812
10813
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010814bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000010815 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000010816 return false;
Evan Chenge127a732007-10-29 07:57:50 +000010817 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
10818 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000010819 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +000010820 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000010821 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +000010822}
10823
Owen Andersone50ed302009-08-10 22:56:29 +000010824bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000010825 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000010826 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000010827 unsigned NumBits1 = VT1.getSizeInBits();
10828 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000010829 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +000010830 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000010831 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000010832}
Evan Cheng2bd122c2007-10-26 01:56:11 +000010833
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010834bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000010835 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000010836 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000010837}
10838
Owen Andersone50ed302009-08-10 22:56:29 +000010839bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000010840 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000010841 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000010842}
10843
Owen Andersone50ed302009-08-10 22:56:29 +000010844bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000010845 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000010846 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000010847}
10848
Evan Cheng60c07e12006-07-05 22:17:51 +000010849/// isShuffleMaskLegal - Targets can use this to indicate that they only
10850/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
10851/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
10852/// are assumed to be legal.
10853bool
Eric Christopherfd179292009-08-27 18:07:15 +000010854X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000010855 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000010856 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000010857 if (VT.getSizeInBits() == 64)
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010858 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3() || Subtarget->hasAVX());
Nate Begeman9008ca62009-04-27 18:41:29 +000010859
Nate Begemana09008b2009-10-19 02:17:23 +000010860 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000010861 return (VT.getVectorNumElements() == 2 ||
10862 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
10863 isMOVLMask(M, VT) ||
10864 isSHUFPMask(M, VT) ||
10865 isPSHUFDMask(M, VT) ||
10866 isPSHUFHWMask(M, VT) ||
10867 isPSHUFLWMask(M, VT) ||
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010868 isPALIGNRMask(M, VT, Subtarget->hasSSSE3() || Subtarget->hasAVX()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000010869 isUNPCKLMask(M, VT) ||
10870 isUNPCKHMask(M, VT) ||
10871 isUNPCKL_v_undef_Mask(M, VT) ||
10872 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +000010873}
10874
Dan Gohman7d8143f2008-04-09 20:09:42 +000010875bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000010876X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000010877 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000010878 unsigned NumElts = VT.getVectorNumElements();
10879 // FIXME: This collection of masks seems suspect.
10880 if (NumElts == 2)
10881 return true;
10882 if (NumElts == 4 && VT.getSizeInBits() == 128) {
10883 return (isMOVLMask(Mask, VT) ||
10884 isCommutedMOVLMask(Mask, VT, true) ||
10885 isSHUFPMask(Mask, VT) ||
10886 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +000010887 }
10888 return false;
10889}
10890
10891//===----------------------------------------------------------------------===//
10892// X86 Scheduler Hooks
10893//===----------------------------------------------------------------------===//
10894
Mon P Wang63307c32008-05-05 19:05:59 +000010895// private utility function
10896MachineBasicBlock *
10897X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
10898 MachineBasicBlock *MBB,
10899 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010900 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010901 unsigned LoadOpc,
10902 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010903 unsigned notOpc,
10904 unsigned EAXreg,
10905 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000010906 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000010907 // For the atomic bitwise operator, we generate
10908 // thisMBB:
10909 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000010910 // ld t1 = [bitinstr.addr]
10911 // op t2 = t1, [bitinstr.val]
10912 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000010913 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
10914 // bz newMBB
10915 // fallthrough -->nextMBB
10916 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10917 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000010918 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000010919 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000010920
Mon P Wang63307c32008-05-05 19:05:59 +000010921 /// First build the CFG
10922 MachineFunction *F = MBB->getParent();
10923 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000010924 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
10925 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
10926 F->insert(MBBIter, newMBB);
10927 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010928
Dan Gohman14152b42010-07-06 20:24:04 +000010929 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
10930 nextMBB->splice(nextMBB->begin(), thisMBB,
10931 llvm::next(MachineBasicBlock::iterator(bInstr)),
10932 thisMBB->end());
10933 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010934
Mon P Wang63307c32008-05-05 19:05:59 +000010935 // Update thisMBB to fall through to newMBB
10936 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010937
Mon P Wang63307c32008-05-05 19:05:59 +000010938 // newMBB jumps to itself and fall through to nextMBB
10939 newMBB->addSuccessor(nextMBB);
10940 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010941
Mon P Wang63307c32008-05-05 19:05:59 +000010942 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010943 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000010944 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +000010945 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000010946 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010947 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000010948 int numArgs = bInstr->getNumOperands() - 1;
10949 for (int i=0; i < numArgs; ++i)
10950 argOpers[i] = &bInstr->getOperand(i+1);
10951
10952 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010953 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000010954 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000010955
Dale Johannesen140be2d2008-08-19 18:47:28 +000010956 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010957 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000010958 for (int i=0; i <= lastAddrIndx; ++i)
10959 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010960
Dale Johannesen140be2d2008-08-19 18:47:28 +000010961 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010962 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010963 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010964 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010965 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010966 tt = t1;
10967
Dale Johannesen140be2d2008-08-19 18:47:28 +000010968 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +000010969 assert((argOpers[valArgIndx]->isReg() ||
10970 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000010971 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +000010972 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000010973 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000010974 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000010975 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010976 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +000010977 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010978
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010979 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +000010980 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +000010981
Dale Johannesene4d209d2009-02-03 20:21:25 +000010982 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +000010983 for (int i=0; i <= lastAddrIndx; ++i)
10984 (*MIB).addOperand(*argOpers[i]);
10985 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +000010986 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000010987 (*MIB).setMemRefs(bInstr->memoperands_begin(),
10988 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +000010989
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010990 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +000010991 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +000010992
Mon P Wang63307c32008-05-05 19:05:59 +000010993 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000010994 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000010995
Dan Gohman14152b42010-07-06 20:24:04 +000010996 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000010997 return nextMBB;
10998}
10999
Dale Johannesen1b54c7f2008-10-03 19:41:08 +000011000// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +000011001MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011002X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
11003 MachineBasicBlock *MBB,
11004 unsigned regOpcL,
11005 unsigned regOpcH,
11006 unsigned immOpcL,
11007 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011008 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011009 // For the atomic bitwise operator, we generate
11010 // thisMBB (instructions are in pairs, except cmpxchg8b)
11011 // ld t1,t2 = [bitinstr.addr]
11012 // newMBB:
11013 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
11014 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +000011015 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011016 // mov ECX, EBX <- t5, t6
11017 // mov EAX, EDX <- t1, t2
11018 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
11019 // mov t3, t4 <- EAX, EDX
11020 // bz newMBB
11021 // result in out1, out2
11022 // fallthrough -->nextMBB
11023
11024 const TargetRegisterClass *RC = X86::GR32RegisterClass;
11025 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011026 const unsigned NotOpc = X86::NOT32r;
11027 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11028 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11029 MachineFunction::iterator MBBIter = MBB;
11030 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011031
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011032 /// First build the CFG
11033 MachineFunction *F = MBB->getParent();
11034 MachineBasicBlock *thisMBB = MBB;
11035 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11036 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11037 F->insert(MBBIter, newMBB);
11038 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011039
Dan Gohman14152b42010-07-06 20:24:04 +000011040 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11041 nextMBB->splice(nextMBB->begin(), thisMBB,
11042 llvm::next(MachineBasicBlock::iterator(bInstr)),
11043 thisMBB->end());
11044 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011045
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011046 // Update thisMBB to fall through to newMBB
11047 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011048
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011049 // newMBB jumps to itself and fall through to nextMBB
11050 newMBB->addSuccessor(nextMBB);
11051 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011052
Dale Johannesene4d209d2009-02-03 20:21:25 +000011053 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011054 // Insert instructions into newMBB based on incoming instruction
11055 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011056 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011057 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011058 MachineOperand& dest1Oper = bInstr->getOperand(0);
11059 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011060 MachineOperand* argOpers[2 + X86::AddrNumOperands];
11061 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011062 argOpers[i] = &bInstr->getOperand(i+2);
11063
Dan Gohman71ea4e52010-05-14 21:01:44 +000011064 // We use some of the operands multiple times, so conservatively just
11065 // clear any kill flags that might be present.
11066 if (argOpers[i]->isReg() && argOpers[i]->isUse())
11067 argOpers[i]->setIsKill(false);
11068 }
11069
Evan Chengad5b52f2010-01-08 19:14:57 +000011070 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011071 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +000011072
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011073 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011074 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011075 for (int i=0; i <= lastAddrIndx; ++i)
11076 (*MIB).addOperand(*argOpers[i]);
11077 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011078 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +000011079 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +000011080 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011081 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +000011082 MachineOperand newOp3 = *(argOpers[3]);
11083 if (newOp3.isImm())
11084 newOp3.setImm(newOp3.getImm()+4);
11085 else
11086 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011087 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +000011088 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011089
11090 // t3/4 are defined later, at the bottom of the loop
11091 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
11092 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011093 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011094 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011095 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011096 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
11097
Evan Cheng306b4ca2010-01-08 23:41:50 +000011098 // The subsequent operations should be using the destination registers of
11099 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +000011100 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +000011101 t1 = F->getRegInfo().createVirtualRegister(RC);
11102 t2 = F->getRegInfo().createVirtualRegister(RC);
11103 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
11104 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011105 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +000011106 t1 = dest1Oper.getReg();
11107 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011108 }
11109
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011110 int valArgIndx = lastAddrIndx + 1;
11111 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +000011112 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011113 "invalid operand");
11114 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
11115 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011116 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011117 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011118 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011119 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +000011120 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011121 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011122 (*MIB).addOperand(*argOpers[valArgIndx]);
11123 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011124 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011125 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011126 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011127 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011128 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011129 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011130 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +000011131 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011132 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011133 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011134
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011135 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011136 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011137 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011138 MIB.addReg(t2);
11139
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011140 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011141 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011142 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011143 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +000011144
Dale Johannesene4d209d2009-02-03 20:21:25 +000011145 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011146 for (int i=0; i <= lastAddrIndx; ++i)
11147 (*MIB).addOperand(*argOpers[i]);
11148
11149 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011150 (*MIB).setMemRefs(bInstr->memoperands_begin(),
11151 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011152
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011153 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011154 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011155 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011156 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000011157
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011158 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011159 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011160
Dan Gohman14152b42010-07-06 20:24:04 +000011161 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011162 return nextMBB;
11163}
11164
11165// private utility function
11166MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +000011167X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
11168 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011169 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000011170 // For the atomic min/max operator, we generate
11171 // thisMBB:
11172 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000011173 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +000011174 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +000011175 // cmp t1, t2
11176 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +000011177 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000011178 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
11179 // bz newMBB
11180 // fallthrough -->nextMBB
11181 //
11182 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11183 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011184 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000011185 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011186
Mon P Wang63307c32008-05-05 19:05:59 +000011187 /// First build the CFG
11188 MachineFunction *F = MBB->getParent();
11189 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011190 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11191 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11192 F->insert(MBBIter, newMBB);
11193 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011194
Dan Gohman14152b42010-07-06 20:24:04 +000011195 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11196 nextMBB->splice(nextMBB->begin(), thisMBB,
11197 llvm::next(MachineBasicBlock::iterator(mInstr)),
11198 thisMBB->end());
11199 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011200
Mon P Wang63307c32008-05-05 19:05:59 +000011201 // Update thisMBB to fall through to newMBB
11202 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011203
Mon P Wang63307c32008-05-05 19:05:59 +000011204 // newMBB jumps to newMBB and fall through to nextMBB
11205 newMBB->addSuccessor(nextMBB);
11206 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011207
Dale Johannesene4d209d2009-02-03 20:21:25 +000011208 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000011209 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011210 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011211 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +000011212 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011213 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000011214 int numArgs = mInstr->getNumOperands() - 1;
11215 for (int i=0; i < numArgs; ++i)
11216 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +000011217
Mon P Wang63307c32008-05-05 19:05:59 +000011218 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011219 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011220 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000011221
Mon P Wangab3e7472008-05-05 22:56:23 +000011222 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011223 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011224 for (int i=0; i <= lastAddrIndx; ++i)
11225 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +000011226
Mon P Wang63307c32008-05-05 19:05:59 +000011227 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +000011228 assert((argOpers[valArgIndx]->isReg() ||
11229 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000011230 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +000011231
11232 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +000011233 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011234 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +000011235 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011236 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000011237 (*MIB).addOperand(*argOpers[valArgIndx]);
11238
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011239 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +000011240 MIB.addReg(t1);
11241
Dale Johannesene4d209d2009-02-03 20:21:25 +000011242 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +000011243 MIB.addReg(t1);
11244 MIB.addReg(t2);
11245
11246 // Generate movc
11247 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011248 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +000011249 MIB.addReg(t2);
11250 MIB.addReg(t1);
11251
11252 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +000011253 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +000011254 for (int i=0; i <= lastAddrIndx; ++i)
11255 (*MIB).addOperand(*argOpers[i]);
11256 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +000011257 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011258 (*MIB).setMemRefs(mInstr->memoperands_begin(),
11259 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +000011260
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011261 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +000011262 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +000011263
Mon P Wang63307c32008-05-05 19:05:59 +000011264 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011265 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000011266
Dan Gohman14152b42010-07-06 20:24:04 +000011267 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000011268 return nextMBB;
11269}
11270
Eric Christopherf83a5de2009-08-27 18:08:16 +000011271// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011272// or XMM0_V32I8 in AVX all of this code can be replaced with that
11273// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +000011274MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +000011275X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +000011276 unsigned numArgs, bool memArg) const {
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011277 assert((Subtarget->hasSSE42() || Subtarget->hasAVX()) &&
11278 "Target must have SSE4.2 or AVX features enabled");
11279
Eric Christopherb120ab42009-08-18 22:50:32 +000011280 DebugLoc dl = MI->getDebugLoc();
11281 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +000011282 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011283 if (!Subtarget->hasAVX()) {
11284 if (memArg)
11285 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
11286 else
11287 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
11288 } else {
11289 if (memArg)
11290 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
11291 else
11292 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
11293 }
Eric Christopherb120ab42009-08-18 22:50:32 +000011294
Eric Christopher41c902f2010-11-30 08:20:21 +000011295 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +000011296 for (unsigned i = 0; i < numArgs; ++i) {
11297 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +000011298 if (!(Op.isReg() && Op.isImplicit()))
11299 MIB.addOperand(Op);
11300 }
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011301 BuildMI(*BB, MI, dl,
11302 TII->get(Subtarget->hasAVX() ? X86::VMOVAPSrr : X86::MOVAPSrr),
11303 MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000011304 .addReg(X86::XMM0);
11305
Dan Gohman14152b42010-07-06 20:24:04 +000011306 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000011307 return BB;
11308}
11309
11310MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +000011311X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000011312 DebugLoc dl = MI->getDebugLoc();
11313 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011314
Eric Christopher228232b2010-11-30 07:20:12 +000011315 // Address into RAX/EAX, other two args into ECX, EDX.
11316 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
11317 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
11318 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
11319 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000011320 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011321
Eric Christopher228232b2010-11-30 07:20:12 +000011322 unsigned ValOps = X86::AddrNumOperands;
11323 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
11324 .addReg(MI->getOperand(ValOps).getReg());
11325 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
11326 .addReg(MI->getOperand(ValOps+1).getReg());
11327
11328 // The instruction doesn't actually take any operands though.
11329 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011330
Eric Christopher228232b2010-11-30 07:20:12 +000011331 MI->eraseFromParent(); // The pseudo is gone now.
11332 return BB;
11333}
11334
11335MachineBasicBlock *
11336X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000011337 DebugLoc dl = MI->getDebugLoc();
11338 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011339
Eric Christopher228232b2010-11-30 07:20:12 +000011340 // First arg in ECX, the second in EAX.
11341 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
11342 .addReg(MI->getOperand(0).getReg());
11343 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
11344 .addReg(MI->getOperand(1).getReg());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011345
Eric Christopher228232b2010-11-30 07:20:12 +000011346 // The instruction doesn't actually take any operands though.
11347 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011348
Eric Christopher228232b2010-11-30 07:20:12 +000011349 MI->eraseFromParent(); // The pseudo is gone now.
11350 return BB;
11351}
11352
11353MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000011354X86TargetLowering::EmitVAARG64WithCustomInserter(
11355 MachineInstr *MI,
11356 MachineBasicBlock *MBB) const {
11357 // Emit va_arg instruction on X86-64.
11358
11359 // Operands to this pseudo-instruction:
11360 // 0 ) Output : destination address (reg)
11361 // 1-5) Input : va_list address (addr, i64mem)
11362 // 6 ) ArgSize : Size (in bytes) of vararg type
11363 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
11364 // 8 ) Align : Alignment of type
11365 // 9 ) EFLAGS (implicit-def)
11366
11367 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
11368 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
11369
11370 unsigned DestReg = MI->getOperand(0).getReg();
11371 MachineOperand &Base = MI->getOperand(1);
11372 MachineOperand &Scale = MI->getOperand(2);
11373 MachineOperand &Index = MI->getOperand(3);
11374 MachineOperand &Disp = MI->getOperand(4);
11375 MachineOperand &Segment = MI->getOperand(5);
11376 unsigned ArgSize = MI->getOperand(6).getImm();
11377 unsigned ArgMode = MI->getOperand(7).getImm();
11378 unsigned Align = MI->getOperand(8).getImm();
11379
11380 // Memory Reference
11381 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
11382 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
11383 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
11384
11385 // Machine Information
11386 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11387 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
11388 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
11389 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
11390 DebugLoc DL = MI->getDebugLoc();
11391
11392 // struct va_list {
11393 // i32 gp_offset
11394 // i32 fp_offset
11395 // i64 overflow_area (address)
11396 // i64 reg_save_area (address)
11397 // }
11398 // sizeof(va_list) = 24
11399 // alignment(va_list) = 8
11400
11401 unsigned TotalNumIntRegs = 6;
11402 unsigned TotalNumXMMRegs = 8;
11403 bool UseGPOffset = (ArgMode == 1);
11404 bool UseFPOffset = (ArgMode == 2);
11405 unsigned MaxOffset = TotalNumIntRegs * 8 +
11406 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
11407
11408 /* Align ArgSize to a multiple of 8 */
11409 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
11410 bool NeedsAlign = (Align > 8);
11411
11412 MachineBasicBlock *thisMBB = MBB;
11413 MachineBasicBlock *overflowMBB;
11414 MachineBasicBlock *offsetMBB;
11415 MachineBasicBlock *endMBB;
11416
11417 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
11418 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
11419 unsigned OffsetReg = 0;
11420
11421 if (!UseGPOffset && !UseFPOffset) {
11422 // If we only pull from the overflow region, we don't create a branch.
11423 // We don't need to alter control flow.
11424 OffsetDestReg = 0; // unused
11425 OverflowDestReg = DestReg;
11426
11427 offsetMBB = NULL;
11428 overflowMBB = thisMBB;
11429 endMBB = thisMBB;
11430 } else {
11431 // First emit code to check if gp_offset (or fp_offset) is below the bound.
11432 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
11433 // If not, pull from overflow_area. (branch to overflowMBB)
11434 //
11435 // thisMBB
11436 // | .
11437 // | .
11438 // offsetMBB overflowMBB
11439 // | .
11440 // | .
11441 // endMBB
11442
11443 // Registers for the PHI in endMBB
11444 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
11445 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
11446
11447 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11448 MachineFunction *MF = MBB->getParent();
11449 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11450 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11451 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11452
11453 MachineFunction::iterator MBBIter = MBB;
11454 ++MBBIter;
11455
11456 // Insert the new basic blocks
11457 MF->insert(MBBIter, offsetMBB);
11458 MF->insert(MBBIter, overflowMBB);
11459 MF->insert(MBBIter, endMBB);
11460
11461 // Transfer the remainder of MBB and its successor edges to endMBB.
11462 endMBB->splice(endMBB->begin(), thisMBB,
11463 llvm::next(MachineBasicBlock::iterator(MI)),
11464 thisMBB->end());
11465 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
11466
11467 // Make offsetMBB and overflowMBB successors of thisMBB
11468 thisMBB->addSuccessor(offsetMBB);
11469 thisMBB->addSuccessor(overflowMBB);
11470
11471 // endMBB is a successor of both offsetMBB and overflowMBB
11472 offsetMBB->addSuccessor(endMBB);
11473 overflowMBB->addSuccessor(endMBB);
11474
11475 // Load the offset value into a register
11476 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
11477 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
11478 .addOperand(Base)
11479 .addOperand(Scale)
11480 .addOperand(Index)
11481 .addDisp(Disp, UseFPOffset ? 4 : 0)
11482 .addOperand(Segment)
11483 .setMemRefs(MMOBegin, MMOEnd);
11484
11485 // Check if there is enough room left to pull this argument.
11486 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
11487 .addReg(OffsetReg)
11488 .addImm(MaxOffset + 8 - ArgSizeA8);
11489
11490 // Branch to "overflowMBB" if offset >= max
11491 // Fall through to "offsetMBB" otherwise
11492 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
11493 .addMBB(overflowMBB);
11494 }
11495
11496 // In offsetMBB, emit code to use the reg_save_area.
11497 if (offsetMBB) {
11498 assert(OffsetReg != 0);
11499
11500 // Read the reg_save_area address.
11501 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
11502 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
11503 .addOperand(Base)
11504 .addOperand(Scale)
11505 .addOperand(Index)
11506 .addDisp(Disp, 16)
11507 .addOperand(Segment)
11508 .setMemRefs(MMOBegin, MMOEnd);
11509
11510 // Zero-extend the offset
11511 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
11512 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
11513 .addImm(0)
11514 .addReg(OffsetReg)
11515 .addImm(X86::sub_32bit);
11516
11517 // Add the offset to the reg_save_area to get the final address.
11518 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
11519 .addReg(OffsetReg64)
11520 .addReg(RegSaveReg);
11521
11522 // Compute the offset for the next argument
11523 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
11524 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
11525 .addReg(OffsetReg)
11526 .addImm(UseFPOffset ? 16 : 8);
11527
11528 // Store it back into the va_list.
11529 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
11530 .addOperand(Base)
11531 .addOperand(Scale)
11532 .addOperand(Index)
11533 .addDisp(Disp, UseFPOffset ? 4 : 0)
11534 .addOperand(Segment)
11535 .addReg(NextOffsetReg)
11536 .setMemRefs(MMOBegin, MMOEnd);
11537
11538 // Jump to endMBB
11539 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
11540 .addMBB(endMBB);
11541 }
11542
11543 //
11544 // Emit code to use overflow area
11545 //
11546
11547 // Load the overflow_area address into a register.
11548 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
11549 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
11550 .addOperand(Base)
11551 .addOperand(Scale)
11552 .addOperand(Index)
11553 .addDisp(Disp, 8)
11554 .addOperand(Segment)
11555 .setMemRefs(MMOBegin, MMOEnd);
11556
11557 // If we need to align it, do so. Otherwise, just copy the address
11558 // to OverflowDestReg.
11559 if (NeedsAlign) {
11560 // Align the overflow address
11561 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
11562 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
11563
11564 // aligned_addr = (addr + (align-1)) & ~(align-1)
11565 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
11566 .addReg(OverflowAddrReg)
11567 .addImm(Align-1);
11568
11569 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
11570 .addReg(TmpReg)
11571 .addImm(~(uint64_t)(Align-1));
11572 } else {
11573 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
11574 .addReg(OverflowAddrReg);
11575 }
11576
11577 // Compute the next overflow address after this argument.
11578 // (the overflow address should be kept 8-byte aligned)
11579 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
11580 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
11581 .addReg(OverflowDestReg)
11582 .addImm(ArgSizeA8);
11583
11584 // Store the new overflow address.
11585 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
11586 .addOperand(Base)
11587 .addOperand(Scale)
11588 .addOperand(Index)
11589 .addDisp(Disp, 8)
11590 .addOperand(Segment)
11591 .addReg(NextAddrReg)
11592 .setMemRefs(MMOBegin, MMOEnd);
11593
11594 // If we branched, emit the PHI to the front of endMBB.
11595 if (offsetMBB) {
11596 BuildMI(*endMBB, endMBB->begin(), DL,
11597 TII->get(X86::PHI), DestReg)
11598 .addReg(OffsetDestReg).addMBB(offsetMBB)
11599 .addReg(OverflowDestReg).addMBB(overflowMBB);
11600 }
11601
11602 // Erase the pseudo instruction
11603 MI->eraseFromParent();
11604
11605 return endMBB;
11606}
11607
11608MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000011609X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
11610 MachineInstr *MI,
11611 MachineBasicBlock *MBB) const {
11612 // Emit code to save XMM registers to the stack. The ABI says that the
11613 // number of registers to save is given in %al, so it's theoretically
11614 // possible to do an indirect jump trick to avoid saving all of them,
11615 // however this code takes a simpler approach and just executes all
11616 // of the stores if %al is non-zero. It's less code, and it's probably
11617 // easier on the hardware branch predictor, and stores aren't all that
11618 // expensive anyway.
11619
11620 // Create the new basic blocks. One block contains all the XMM stores,
11621 // and one block is the final destination regardless of whether any
11622 // stores were performed.
11623 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11624 MachineFunction *F = MBB->getParent();
11625 MachineFunction::iterator MBBIter = MBB;
11626 ++MBBIter;
11627 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
11628 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
11629 F->insert(MBBIter, XMMSaveMBB);
11630 F->insert(MBBIter, EndMBB);
11631
Dan Gohman14152b42010-07-06 20:24:04 +000011632 // Transfer the remainder of MBB and its successor edges to EndMBB.
11633 EndMBB->splice(EndMBB->begin(), MBB,
11634 llvm::next(MachineBasicBlock::iterator(MI)),
11635 MBB->end());
11636 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
11637
Dan Gohmand6708ea2009-08-15 01:38:56 +000011638 // The original block will now fall through to the XMM save block.
11639 MBB->addSuccessor(XMMSaveMBB);
11640 // The XMMSaveMBB will fall through to the end block.
11641 XMMSaveMBB->addSuccessor(EndMBB);
11642
11643 // Now add the instructions.
11644 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11645 DebugLoc DL = MI->getDebugLoc();
11646
11647 unsigned CountReg = MI->getOperand(0).getReg();
11648 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
11649 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
11650
11651 if (!Subtarget->isTargetWin64()) {
11652 // If %al is 0, branch around the XMM save block.
11653 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011654 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000011655 MBB->addSuccessor(EndMBB);
11656 }
11657
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011658 unsigned MOVOpc = Subtarget->hasAVX() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000011659 // In the XMM save block, save all the XMM argument registers.
11660 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
11661 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000011662 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000011663 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000011664 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000011665 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000011666 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011667 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000011668 .addFrameIndex(RegSaveFrameIndex)
11669 .addImm(/*Scale=*/1)
11670 .addReg(/*IndexReg=*/0)
11671 .addImm(/*Disp=*/Offset)
11672 .addReg(/*Segment=*/0)
11673 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000011674 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000011675 }
11676
Dan Gohman14152b42010-07-06 20:24:04 +000011677 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000011678
11679 return EndMBB;
11680}
Mon P Wang63307c32008-05-05 19:05:59 +000011681
Evan Cheng60c07e12006-07-05 22:17:51 +000011682MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000011683X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000011684 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000011685 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11686 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000011687
Chris Lattner52600972009-09-02 05:57:00 +000011688 // To "insert" a SELECT_CC instruction, we actually have to insert the
11689 // diamond control-flow pattern. The incoming instruction knows the
11690 // destination vreg to set, the condition code register to branch on, the
11691 // true/false values to select between, and a branch opcode to use.
11692 const BasicBlock *LLVM_BB = BB->getBasicBlock();
11693 MachineFunction::iterator It = BB;
11694 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000011695
Chris Lattner52600972009-09-02 05:57:00 +000011696 // thisMBB:
11697 // ...
11698 // TrueVal = ...
11699 // cmpTY ccX, r1, r2
11700 // bCC copy1MBB
11701 // fallthrough --> copy0MBB
11702 MachineBasicBlock *thisMBB = BB;
11703 MachineFunction *F = BB->getParent();
11704 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
11705 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000011706 F->insert(It, copy0MBB);
11707 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000011708
Bill Wendling730c07e2010-06-25 20:48:10 +000011709 // If the EFLAGS register isn't dead in the terminator, then claim that it's
11710 // live into the sink and copy blocks.
Jakob Stoklund Olesen4a1b9d82011-09-02 23:52:49 +000011711 if (!MI->killsRegister(X86::EFLAGS)) {
11712 copy0MBB->addLiveIn(X86::EFLAGS);
11713 sinkMBB->addLiveIn(X86::EFLAGS);
Bill Wendling730c07e2010-06-25 20:48:10 +000011714 }
11715
Dan Gohman14152b42010-07-06 20:24:04 +000011716 // Transfer the remainder of BB and its successor edges to sinkMBB.
11717 sinkMBB->splice(sinkMBB->begin(), BB,
11718 llvm::next(MachineBasicBlock::iterator(MI)),
11719 BB->end());
11720 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
11721
11722 // Add the true and fallthrough blocks as its successors.
11723 BB->addSuccessor(copy0MBB);
11724 BB->addSuccessor(sinkMBB);
11725
11726 // Create the conditional branch instruction.
11727 unsigned Opc =
11728 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
11729 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
11730
Chris Lattner52600972009-09-02 05:57:00 +000011731 // copy0MBB:
11732 // %FalseValue = ...
11733 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000011734 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000011735
Chris Lattner52600972009-09-02 05:57:00 +000011736 // sinkMBB:
11737 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
11738 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000011739 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
11740 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000011741 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
11742 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
11743
Dan Gohman14152b42010-07-06 20:24:04 +000011744 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000011745 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000011746}
11747
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011748MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000011749X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
11750 bool Is64Bit) const {
11751 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11752 DebugLoc DL = MI->getDebugLoc();
11753 MachineFunction *MF = BB->getParent();
11754 const BasicBlock *LLVM_BB = BB->getBasicBlock();
11755
11756 assert(EnableSegmentedStacks);
11757
11758 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
11759 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
11760
11761 // BB:
11762 // ... [Till the alloca]
11763 // If stacklet is not large enough, jump to mallocMBB
11764 //
11765 // bumpMBB:
11766 // Allocate by subtracting from RSP
11767 // Jump to continueMBB
11768 //
11769 // mallocMBB:
11770 // Allocate by call to runtime
11771 //
11772 // continueMBB:
11773 // ...
11774 // [rest of original BB]
11775 //
11776
11777 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11778 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11779 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11780
11781 MachineRegisterInfo &MRI = MF->getRegInfo();
11782 const TargetRegisterClass *AddrRegClass =
11783 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
11784
11785 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
11786 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
11787 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
11788 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);
11805 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), tmpSPVReg)
11806 .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)
11809 .addReg(tmpSPVReg);
11810 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)
11815 .addReg(tmpSPVReg);
11816 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
11817 .addReg(tmpSPVReg);
11818 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
13303 // Create ANDN instructions
13304 if (Subtarget->hasBMI() && (VT == MVT::i32 || VT == MVT::i64)) {
13305 SDValue N0 = N->getOperand(0);
13306 SDValue N1 = N->getOperand(1);
13307 DebugLoc DL = N->getDebugLoc();
13308
13309 // Check LHS for not
13310 if (N0.getOpcode() == ISD::XOR && isAllOnes(N0.getOperand(1)))
13311 return DAG.getNode(X86ISD::ANDN, DL, VT, N0.getOperand(0), N1);
13312 // Check RHS for not
13313 if (N1.getOpcode() == ISD::XOR && isAllOnes(N1.getOperand(1)))
13314 return DAG.getNode(X86ISD::ANDN, DL, VT, N1.getOperand(0), N0);
13315
13316 return SDValue();
13317 }
13318
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000013319 // Want to form ANDNP nodes:
13320 // 1) In the hopes of then easily combining them with OR and AND nodes
13321 // to form PBLEND/PSIGN.
13322 // 2) To match ANDN packed intrinsics
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000013323 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000013324 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013325
Nate Begemanb65c1752010-12-17 22:55:37 +000013326 SDValue N0 = N->getOperand(0);
13327 SDValue N1 = N->getOperand(1);
13328 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013329
Nate Begemanb65c1752010-12-17 22:55:37 +000013330 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013331 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013332 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
13333 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013334 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000013335
13336 // Check RHS for vnot
13337 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013338 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
13339 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013340 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013341
Nate Begemanb65c1752010-12-17 22:55:37 +000013342 return SDValue();
13343}
13344
Evan Cheng760d1942010-01-04 21:22:48 +000013345static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000013346 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000013347 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000013348 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000013349 return SDValue();
13350
Stuart Hastings865f0932011-06-03 23:53:54 +000013351 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
13352 if (R.getNode())
13353 return R;
13354
Evan Cheng760d1942010-01-04 21:22:48 +000013355 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000013356 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64 && VT != MVT::v2i64)
Evan Cheng760d1942010-01-04 21:22:48 +000013357 return SDValue();
13358
Evan Cheng760d1942010-01-04 21:22:48 +000013359 SDValue N0 = N->getOperand(0);
13360 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013361
Nate Begemanb65c1752010-12-17 22:55:37 +000013362 // look for psign/blend
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013363 if (Subtarget->hasSSSE3() || Subtarget->hasAVX()) {
Nate Begemanb65c1752010-12-17 22:55:37 +000013364 if (VT == MVT::v2i64) {
13365 // Canonicalize pandn to RHS
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013366 if (N0.getOpcode() == X86ISD::ANDNP)
Nate Begemanb65c1752010-12-17 22:55:37 +000013367 std::swap(N0, N1);
13368 // or (and (m, x), (pandn m, y))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013369 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
Nate Begemanb65c1752010-12-17 22:55:37 +000013370 SDValue Mask = N1.getOperand(0);
13371 SDValue X = N1.getOperand(1);
13372 SDValue Y;
13373 if (N0.getOperand(0) == Mask)
13374 Y = N0.getOperand(1);
13375 if (N0.getOperand(1) == Mask)
13376 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013377
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013378 // Check to see if the mask appeared in both the AND and ANDNP and
Nate Begemanb65c1752010-12-17 22:55:37 +000013379 if (!Y.getNode())
13380 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013381
Nate Begemanb65c1752010-12-17 22:55:37 +000013382 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
13383 if (Mask.getOpcode() != ISD::BITCAST ||
13384 X.getOpcode() != ISD::BITCAST ||
13385 Y.getOpcode() != ISD::BITCAST)
13386 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013387
Nate Begemanb65c1752010-12-17 22:55:37 +000013388 // Look through mask bitcast.
13389 Mask = Mask.getOperand(0);
13390 EVT MaskVT = Mask.getValueType();
13391
13392 // Validate that the Mask operand is a vector sra node. The sra node
13393 // will be an intrinsic.
13394 if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
13395 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013396
Nate Begemanb65c1752010-12-17 22:55:37 +000013397 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
13398 // there is no psrai.b
13399 switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
13400 case Intrinsic::x86_sse2_psrai_w:
13401 case Intrinsic::x86_sse2_psrai_d:
13402 break;
13403 default: return SDValue();
13404 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013405
Nate Begemanb65c1752010-12-17 22:55:37 +000013406 // Check that the SRA is all signbits.
13407 SDValue SraC = Mask.getOperand(2);
13408 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
13409 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
13410 if ((SraAmt + 1) != EltBits)
13411 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013412
Nate Begemanb65c1752010-12-17 22:55:37 +000013413 DebugLoc DL = N->getDebugLoc();
13414
13415 // Now we know we at least have a plendvb with the mask val. See if
13416 // we can form a psignb/w/d.
13417 // psign = x.type == y.type == mask.type && y = sub(0, x);
13418 X = X.getOperand(0);
13419 Y = Y.getOperand(0);
13420 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
13421 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
13422 X.getValueType() == MaskVT && X.getValueType() == Y.getValueType()){
13423 unsigned Opc = 0;
13424 switch (EltBits) {
13425 case 8: Opc = X86ISD::PSIGNB; break;
13426 case 16: Opc = X86ISD::PSIGNW; break;
13427 case 32: Opc = X86ISD::PSIGND; break;
13428 default: break;
13429 }
13430 if (Opc) {
13431 SDValue Sign = DAG.getNode(Opc, DL, MaskVT, X, Mask.getOperand(1));
13432 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Sign);
13433 }
13434 }
13435 // PBLENDVB only available on SSE 4.1
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013436 if (!(Subtarget->hasSSE41() || Subtarget->hasAVX()))
Nate Begemanb65c1752010-12-17 22:55:37 +000013437 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013438
Nate Begemanb65c1752010-12-17 22:55:37 +000013439 X = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, X);
13440 Y = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Y);
13441 Mask = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Mask);
Nadav Rotemfbad25e2011-09-11 15:02:23 +000013442 Mask = DAG.getNode(ISD::VSELECT, DL, MVT::v16i8, Mask, X, Y);
Nate Begemanb65c1752010-12-17 22:55:37 +000013443 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Mask);
13444 }
13445 }
13446 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013447
Nate Begemanb65c1752010-12-17 22:55:37 +000013448 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000013449 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
13450 std::swap(N0, N1);
13451 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
13452 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000013453 if (!N0.hasOneUse() || !N1.hasOneUse())
13454 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000013455
13456 SDValue ShAmt0 = N0.getOperand(1);
13457 if (ShAmt0.getValueType() != MVT::i8)
13458 return SDValue();
13459 SDValue ShAmt1 = N1.getOperand(1);
13460 if (ShAmt1.getValueType() != MVT::i8)
13461 return SDValue();
13462 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
13463 ShAmt0 = ShAmt0.getOperand(0);
13464 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
13465 ShAmt1 = ShAmt1.getOperand(0);
13466
13467 DebugLoc DL = N->getDebugLoc();
13468 unsigned Opc = X86ISD::SHLD;
13469 SDValue Op0 = N0.getOperand(0);
13470 SDValue Op1 = N1.getOperand(0);
13471 if (ShAmt0.getOpcode() == ISD::SUB) {
13472 Opc = X86ISD::SHRD;
13473 std::swap(Op0, Op1);
13474 std::swap(ShAmt0, ShAmt1);
13475 }
13476
Evan Cheng8b1190a2010-04-28 01:18:01 +000013477 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000013478 if (ShAmt1.getOpcode() == ISD::SUB) {
13479 SDValue Sum = ShAmt1.getOperand(0);
13480 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000013481 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
13482 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
13483 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
13484 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000013485 return DAG.getNode(Opc, DL, VT,
13486 Op0, Op1,
13487 DAG.getNode(ISD::TRUNCATE, DL,
13488 MVT::i8, ShAmt0));
13489 }
13490 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
13491 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
13492 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000013493 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000013494 return DAG.getNode(Opc, DL, VT,
13495 N0.getOperand(0), N1.getOperand(0),
13496 DAG.getNode(ISD::TRUNCATE, DL,
13497 MVT::i8, ShAmt0));
13498 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013499
Evan Cheng760d1942010-01-04 21:22:48 +000013500 return SDValue();
13501}
13502
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013503/// PerformLOADCombine - Do target-specific dag combines on LOAD nodes.
13504static SDValue PerformLOADCombine(SDNode *N, SelectionDAG &DAG,
13505 const X86Subtarget *Subtarget) {
13506 LoadSDNode *Ld = cast<LoadSDNode>(N);
13507 EVT RegVT = Ld->getValueType(0);
13508 EVT MemVT = Ld->getMemoryVT();
13509 DebugLoc dl = Ld->getDebugLoc();
13510 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13511
13512 ISD::LoadExtType Ext = Ld->getExtensionType();
13513
Nadav Rotemca6f2962011-09-18 19:00:23 +000013514 // If this is a vector EXT Load then attempt to optimize it using a
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013515 // shuffle. We need SSE4 for the shuffles.
13516 // TODO: It is possible to support ZExt by zeroing the undef values
13517 // during the shuffle phase or after the shuffle.
13518 if (RegVT.isVector() && Ext == ISD::EXTLOAD && Subtarget->hasSSE41()) {
13519 assert(MemVT != RegVT && "Cannot extend to the same type");
13520 assert(MemVT.isVector() && "Must load a vector from memory");
13521
13522 unsigned NumElems = RegVT.getVectorNumElements();
13523 unsigned RegSz = RegVT.getSizeInBits();
13524 unsigned MemSz = MemVT.getSizeInBits();
13525 assert(RegSz > MemSz && "Register size must be greater than the mem size");
Nadav Rotemca6f2962011-09-18 19:00:23 +000013526 // All sizes must be a power of two
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013527 if (!isPowerOf2_32(RegSz * MemSz * NumElems)) return SDValue();
13528
13529 // Attempt to load the original value using a single load op.
13530 // Find a scalar type which is equal to the loaded word size.
13531 MVT SclrLoadTy = MVT::i8;
13532 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
13533 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
13534 MVT Tp = (MVT::SimpleValueType)tp;
13535 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() == MemSz) {
13536 SclrLoadTy = Tp;
13537 break;
13538 }
13539 }
13540
13541 // Proceed if a load word is found.
13542 if (SclrLoadTy.getSizeInBits() != MemSz) return SDValue();
13543
13544 EVT LoadUnitVecVT = EVT::getVectorVT(*DAG.getContext(), SclrLoadTy,
13545 RegSz/SclrLoadTy.getSizeInBits());
13546
13547 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
13548 RegSz/MemVT.getScalarType().getSizeInBits());
13549 // Can't shuffle using an illegal type.
13550 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
13551
13552 // Perform a single load.
13553 SDValue ScalarLoad = DAG.getLoad(SclrLoadTy, dl, Ld->getChain(),
13554 Ld->getBasePtr(),
13555 Ld->getPointerInfo(), Ld->isVolatile(),
13556 Ld->isNonTemporal(), Ld->getAlignment());
13557
13558 // Insert the word loaded into a vector.
13559 SDValue ScalarInVector = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
13560 LoadUnitVecVT, ScalarLoad);
13561
13562 // Bitcast the loaded value to a vector of the original element type, in
13563 // the size of the target vector type.
13564 SDValue SlicedVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, ScalarInVector);
13565 unsigned SizeRatio = RegSz/MemSz;
13566
13567 // Redistribute the loaded elements into the different locations.
13568 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
13569 for (unsigned i = 0; i < NumElems; i++) ShuffleVec[i*SizeRatio] = i;
13570
13571 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
13572 DAG.getUNDEF(SlicedVec.getValueType()),
13573 ShuffleVec.data());
13574
13575 // Bitcast to the requested type.
13576 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
13577 // Replace the original load with the new sequence
13578 // and return the new chain.
13579 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Shuff);
13580 return SDValue(ScalarLoad.getNode(), 1);
13581 }
13582
13583 return SDValue();
13584}
13585
Chris Lattner149a4e52008-02-22 02:09:43 +000013586/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000013587static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000013588 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000013589 StoreSDNode *St = cast<StoreSDNode>(N);
13590 EVT VT = St->getValue().getValueType();
13591 EVT StVT = St->getMemoryVT();
13592 DebugLoc dl = St->getDebugLoc();
Nadav Rotem5e742a32011-08-11 16:41:21 +000013593 SDValue StoredVal = St->getOperand(1);
13594 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13595
13596 // If we are saving a concatination of two XMM registers, perform two stores.
Nadav Rotem6236f7f2011-08-11 17:05:47 +000013597 // This is better in Sandy Bridge cause one 256-bit mem op is done via two
13598 // 128-bit ones. If in the future the cost becomes only one memory access the
13599 // first version would be better.
Nadav Rotem5e742a32011-08-11 16:41:21 +000013600 if (VT.getSizeInBits() == 256 &&
13601 StoredVal.getNode()->getOpcode() == ISD::CONCAT_VECTORS &&
13602 StoredVal.getNumOperands() == 2) {
13603
13604 SDValue Value0 = StoredVal.getOperand(0);
13605 SDValue Value1 = StoredVal.getOperand(1);
13606
13607 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
13608 SDValue Ptr0 = St->getBasePtr();
13609 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
13610
13611 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
13612 St->getPointerInfo(), St->isVolatile(),
13613 St->isNonTemporal(), St->getAlignment());
13614 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
13615 St->getPointerInfo(), St->isVolatile(),
13616 St->isNonTemporal(), St->getAlignment());
13617 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
13618 }
Nadav Rotem614061b2011-08-10 19:30:14 +000013619
13620 // Optimize trunc store (of multiple scalars) to shuffle and store.
13621 // First, pack all of the elements in one place. Next, store to memory
13622 // in fewer chunks.
13623 if (St->isTruncatingStore() && VT.isVector()) {
13624 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13625 unsigned NumElems = VT.getVectorNumElements();
13626 assert(StVT != VT && "Cannot truncate to the same type");
13627 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
13628 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
13629
13630 // From, To sizes and ElemCount must be pow of two
13631 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000013632 // We are going to use the original vector elt for storing.
Nadav Rotem64ac73b2011-09-21 17:14:40 +000013633 // Accumulated smaller vector elements must be a multiple of the store size.
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000013634 if (0 != (NumElems * FromSz) % ToSz) return SDValue();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013635
Nadav Rotem614061b2011-08-10 19:30:14 +000013636 unsigned SizeRatio = FromSz / ToSz;
13637
13638 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
13639
13640 // Create a type on which we perform the shuffle
13641 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
13642 StVT.getScalarType(), NumElems*SizeRatio);
13643
13644 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
13645
13646 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
13647 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
13648 for (unsigned i = 0; i < NumElems; i++ ) ShuffleVec[i] = i * SizeRatio;
13649
13650 // Can't shuffle using an illegal type
13651 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
13652
13653 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
13654 DAG.getUNDEF(WideVec.getValueType()),
13655 ShuffleVec.data());
13656 // At this point all of the data is stored at the bottom of the
13657 // register. We now need to save it to mem.
13658
13659 // Find the largest store unit
13660 MVT StoreType = MVT::i8;
13661 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
13662 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
13663 MVT Tp = (MVT::SimpleValueType)tp;
13664 if (TLI.isTypeLegal(Tp) && StoreType.getSizeInBits() < NumElems * ToSz)
13665 StoreType = Tp;
13666 }
13667
13668 // Bitcast the original vector into a vector of store-size units
13669 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
13670 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
13671 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
13672 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
13673 SmallVector<SDValue, 8> Chains;
13674 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
13675 TLI.getPointerTy());
13676 SDValue Ptr = St->getBasePtr();
13677
13678 // Perform one or more big stores into memory.
13679 for (unsigned i = 0; i < (ToSz*NumElems)/StoreType.getSizeInBits() ; i++) {
13680 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
13681 StoreType, ShuffWide,
13682 DAG.getIntPtrConstant(i));
13683 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
13684 St->getPointerInfo(), St->isVolatile(),
13685 St->isNonTemporal(), St->getAlignment());
13686 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
13687 Chains.push_back(Ch);
13688 }
13689
13690 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
13691 Chains.size());
13692 }
13693
13694
Chris Lattner149a4e52008-02-22 02:09:43 +000013695 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
13696 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000013697 // A preferable solution to the general problem is to figure out the right
13698 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000013699
13700 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000013701 if (VT.getSizeInBits() != 64)
13702 return SDValue();
13703
Devang Patel578efa92009-06-05 21:57:13 +000013704 const Function *F = DAG.getMachineFunction().getFunction();
13705 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +000013706 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013707 && Subtarget->hasXMMInt();
Evan Cheng536e6672009-03-12 05:59:15 +000013708 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000013709 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000013710 isa<LoadSDNode>(St->getValue()) &&
13711 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
13712 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000013713 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000013714 LoadSDNode *Ld = 0;
13715 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000013716 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000013717 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000013718 // Must be a store of a load. We currently handle two cases: the load
13719 // is a direct child, and it's under an intervening TokenFactor. It is
13720 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000013721 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000013722 Ld = cast<LoadSDNode>(St->getChain());
13723 else if (St->getValue().hasOneUse() &&
13724 ChainVal->getOpcode() == ISD::TokenFactor) {
13725 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000013726 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000013727 TokenFactorIndex = i;
13728 Ld = cast<LoadSDNode>(St->getValue());
13729 } else
13730 Ops.push_back(ChainVal->getOperand(i));
13731 }
13732 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000013733
Evan Cheng536e6672009-03-12 05:59:15 +000013734 if (!Ld || !ISD::isNormalLoad(Ld))
13735 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000013736
Evan Cheng536e6672009-03-12 05:59:15 +000013737 // If this is not the MMX case, i.e. we are just turning i64 load/store
13738 // into f64 load/store, avoid the transformation if there are multiple
13739 // uses of the loaded value.
13740 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
13741 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000013742
Evan Cheng536e6672009-03-12 05:59:15 +000013743 DebugLoc LdDL = Ld->getDebugLoc();
13744 DebugLoc StDL = N->getDebugLoc();
13745 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
13746 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
13747 // pair instead.
13748 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000013749 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000013750 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
13751 Ld->getPointerInfo(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000013752 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000013753 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000013754 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000013755 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000013756 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000013757 Ops.size());
13758 }
Evan Cheng536e6672009-03-12 05:59:15 +000013759 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000013760 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000013761 St->isVolatile(), St->isNonTemporal(),
13762 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000013763 }
Evan Cheng536e6672009-03-12 05:59:15 +000013764
13765 // Otherwise, lower to two pairs of 32-bit loads / stores.
13766 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000013767 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
13768 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000013769
Owen Anderson825b72b2009-08-11 20:47:22 +000013770 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000013771 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000013772 Ld->isVolatile(), Ld->isNonTemporal(),
13773 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000013774 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000013775 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000013776 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000013777 MinAlign(Ld->getAlignment(), 4));
13778
13779 SDValue NewChain = LoLd.getValue(1);
13780 if (TokenFactorIndex != -1) {
13781 Ops.push_back(LoLd);
13782 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000013783 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000013784 Ops.size());
13785 }
13786
13787 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000013788 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
13789 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000013790
13791 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000013792 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000013793 St->isVolatile(), St->isNonTemporal(),
13794 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000013795 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000013796 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000013797 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000013798 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000013799 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000013800 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000013801 }
Dan Gohman475871a2008-07-27 21:46:04 +000013802 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000013803}
13804
Duncan Sands17470be2011-09-22 20:15:48 +000013805/// isHorizontalBinOp - Return 'true' if this vector operation is "horizontal"
13806/// and return the operands for the horizontal operation in LHS and RHS. A
13807/// horizontal operation performs the binary operation on successive elements
13808/// of its first operand, then on successive elements of its second operand,
13809/// returning the resulting values in a vector. For example, if
13810/// A = < float a0, float a1, float a2, float a3 >
13811/// and
13812/// B = < float b0, float b1, float b2, float b3 >
13813/// then the result of doing a horizontal operation on A and B is
13814/// A horizontal-op B = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >.
13815/// In short, LHS and RHS are inspected to see if LHS op RHS is of the form
13816/// A horizontal-op B, for some already available A and B, and if so then LHS is
13817/// set to A, RHS to B, and the routine returns 'true'.
13818/// Note that the binary operation should have the property that if one of the
13819/// operands is UNDEF then the result is UNDEF.
13820static bool isHorizontalBinOp(SDValue &LHS, SDValue &RHS, bool isCommutative) {
13821 // Look for the following pattern: if
13822 // A = < float a0, float a1, float a2, float a3 >
13823 // B = < float b0, float b1, float b2, float b3 >
13824 // and
13825 // LHS = VECTOR_SHUFFLE A, B, <0, 2, 4, 6>
13826 // RHS = VECTOR_SHUFFLE A, B, <1, 3, 5, 7>
13827 // then LHS op RHS = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >
13828 // which is A horizontal-op B.
13829
13830 // At least one of the operands should be a vector shuffle.
13831 if (LHS.getOpcode() != ISD::VECTOR_SHUFFLE &&
13832 RHS.getOpcode() != ISD::VECTOR_SHUFFLE)
13833 return false;
13834
13835 EVT VT = LHS.getValueType();
13836 unsigned N = VT.getVectorNumElements();
13837
13838 // View LHS in the form
13839 // LHS = VECTOR_SHUFFLE A, B, LMask
13840 // If LHS is not a shuffle then pretend it is the shuffle
13841 // LHS = VECTOR_SHUFFLE LHS, undef, <0, 1, ..., N-1>
13842 // NOTE: in what follows a default initialized SDValue represents an UNDEF of
13843 // type VT.
13844 SDValue A, B;
13845 SmallVector<int, 8> LMask(N);
13846 if (LHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
13847 if (LHS.getOperand(0).getOpcode() != ISD::UNDEF)
13848 A = LHS.getOperand(0);
13849 if (LHS.getOperand(1).getOpcode() != ISD::UNDEF)
13850 B = LHS.getOperand(1);
13851 cast<ShuffleVectorSDNode>(LHS.getNode())->getMask(LMask);
13852 } else {
13853 if (LHS.getOpcode() != ISD::UNDEF)
13854 A = LHS;
13855 for (unsigned i = 0; i != N; ++i)
13856 LMask[i] = i;
13857 }
13858
13859 // Likewise, view RHS in the form
13860 // RHS = VECTOR_SHUFFLE C, D, RMask
13861 SDValue C, D;
13862 SmallVector<int, 8> RMask(N);
13863 if (RHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
13864 if (RHS.getOperand(0).getOpcode() != ISD::UNDEF)
13865 C = RHS.getOperand(0);
13866 if (RHS.getOperand(1).getOpcode() != ISD::UNDEF)
13867 D = RHS.getOperand(1);
13868 cast<ShuffleVectorSDNode>(RHS.getNode())->getMask(RMask);
13869 } else {
13870 if (RHS.getOpcode() != ISD::UNDEF)
13871 C = RHS;
13872 for (unsigned i = 0; i != N; ++i)
13873 RMask[i] = i;
13874 }
13875
13876 // Check that the shuffles are both shuffling the same vectors.
13877 if (!(A == C && B == D) && !(A == D && B == C))
13878 return false;
13879
13880 // If everything is UNDEF then bail out: it would be better to fold to UNDEF.
13881 if (!A.getNode() && !B.getNode())
13882 return false;
13883
13884 // If A and B occur in reverse order in RHS, then "swap" them (which means
13885 // rewriting the mask).
13886 if (A != C)
13887 for (unsigned i = 0; i != N; ++i) {
13888 unsigned Idx = RMask[i];
13889 if (Idx < N)
13890 RMask[i] += N;
13891 else if (Idx < 2*N)
13892 RMask[i] -= N;
13893 }
13894
13895 // At this point LHS and RHS are equivalent to
13896 // LHS = VECTOR_SHUFFLE A, B, LMask
13897 // RHS = VECTOR_SHUFFLE A, B, RMask
13898 // Check that the masks correspond to performing a horizontal operation.
13899 for (unsigned i = 0; i != N; ++i) {
13900 unsigned LIdx = LMask[i], RIdx = RMask[i];
13901
13902 // Ignore any UNDEF components.
13903 if (LIdx >= 2*N || RIdx >= 2*N || (!A.getNode() && (LIdx < N || RIdx < N))
13904 || (!B.getNode() && (LIdx >= N || RIdx >= N)))
13905 continue;
13906
13907 // Check that successive elements are being operated on. If not, this is
13908 // not a horizontal operation.
13909 if (!(LIdx == 2*i && RIdx == 2*i + 1) &&
13910 !(isCommutative && LIdx == 2*i + 1 && RIdx == 2*i))
13911 return false;
13912 }
13913
13914 LHS = A.getNode() ? A : B; // If A is 'UNDEF', use B for it.
13915 RHS = B.getNode() ? B : A; // If B is 'UNDEF', use A for it.
13916 return true;
13917}
13918
13919/// PerformFADDCombine - Do target-specific dag combines on floating point adds.
13920static SDValue PerformFADDCombine(SDNode *N, SelectionDAG &DAG,
13921 const X86Subtarget *Subtarget) {
13922 EVT VT = N->getValueType(0);
13923 SDValue LHS = N->getOperand(0);
13924 SDValue RHS = N->getOperand(1);
13925
13926 // Try to synthesize horizontal adds from adds of shuffles.
13927 if ((Subtarget->hasSSE3() || Subtarget->hasAVX()) &&
13928 (VT == MVT::v4f32 || VT == MVT::v2f64) &&
13929 isHorizontalBinOp(LHS, RHS, true))
13930 return DAG.getNode(X86ISD::FHADD, N->getDebugLoc(), VT, LHS, RHS);
13931 return SDValue();
13932}
13933
13934/// PerformFSUBCombine - Do target-specific dag combines on floating point subs.
13935static SDValue PerformFSUBCombine(SDNode *N, SelectionDAG &DAG,
13936 const X86Subtarget *Subtarget) {
13937 EVT VT = N->getValueType(0);
13938 SDValue LHS = N->getOperand(0);
13939 SDValue RHS = N->getOperand(1);
13940
13941 // Try to synthesize horizontal subs from subs of shuffles.
13942 if ((Subtarget->hasSSE3() || Subtarget->hasAVX()) &&
13943 (VT == MVT::v4f32 || VT == MVT::v2f64) &&
13944 isHorizontalBinOp(LHS, RHS, false))
13945 return DAG.getNode(X86ISD::FHSUB, N->getDebugLoc(), VT, LHS, RHS);
13946 return SDValue();
13947}
13948
Chris Lattner6cf73262008-01-25 06:14:17 +000013949/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
13950/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000013951static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000013952 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
13953 // F[X]OR(0.0, x) -> x
13954 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000013955 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
13956 if (C->getValueAPF().isPosZero())
13957 return N->getOperand(1);
13958 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
13959 if (C->getValueAPF().isPosZero())
13960 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000013961 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000013962}
13963
13964/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000013965static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000013966 // FAND(0.0, x) -> 0.0
13967 // FAND(x, 0.0) -> 0.0
13968 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
13969 if (C->getValueAPF().isPosZero())
13970 return N->getOperand(0);
13971 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
13972 if (C->getValueAPF().isPosZero())
13973 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000013974 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000013975}
13976
Dan Gohmane5af2d32009-01-29 01:59:02 +000013977static SDValue PerformBTCombine(SDNode *N,
13978 SelectionDAG &DAG,
13979 TargetLowering::DAGCombinerInfo &DCI) {
13980 // BT ignores high bits in the bit index operand.
13981 SDValue Op1 = N->getOperand(1);
13982 if (Op1.hasOneUse()) {
13983 unsigned BitWidth = Op1.getValueSizeInBits();
13984 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
13985 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000013986 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
13987 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000013988 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000013989 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
13990 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
13991 DCI.CommitTargetLoweringOpt(TLO);
13992 }
13993 return SDValue();
13994}
Chris Lattner83e6c992006-10-04 06:57:07 +000013995
Eli Friedman7a5e5552009-06-07 06:52:44 +000013996static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
13997 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000013998 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000013999 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000014000 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000014001 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000014002 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000014003 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000014004 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000014005 }
14006 return SDValue();
14007}
14008
Evan Cheng2e489c42009-12-16 00:53:11 +000014009static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
14010 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
14011 // (and (i32 x86isd::setcc_carry), 1)
14012 // This eliminates the zext. This transformation is necessary because
14013 // ISD::SETCC is always legalized to i8.
14014 DebugLoc dl = N->getDebugLoc();
14015 SDValue N0 = N->getOperand(0);
14016 EVT VT = N->getValueType(0);
14017 if (N0.getOpcode() == ISD::AND &&
14018 N0.hasOneUse() &&
14019 N0.getOperand(0).hasOneUse()) {
14020 SDValue N00 = N0.getOperand(0);
14021 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
14022 return SDValue();
14023 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
14024 if (!C || C->getZExtValue() != 1)
14025 return SDValue();
14026 return DAG.getNode(ISD::AND, dl, VT,
14027 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
14028 N00.getOperand(0), N00.getOperand(1)),
14029 DAG.getConstant(1, VT));
14030 }
14031
14032 return SDValue();
14033}
14034
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014035// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
14036static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
14037 unsigned X86CC = N->getConstantOperandVal(0);
14038 SDValue EFLAG = N->getOperand(1);
14039 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014040
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014041 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
14042 // a zext and produces an all-ones bit which is more useful than 0/1 in some
14043 // cases.
14044 if (X86CC == X86::COND_B)
14045 return DAG.getNode(ISD::AND, DL, MVT::i8,
14046 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
14047 DAG.getConstant(X86CC, MVT::i8), EFLAG),
14048 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014049
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014050 return SDValue();
14051}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014052
Benjamin Kramer1396c402011-06-18 11:09:41 +000014053static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
14054 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000014055 SDValue Op0 = N->getOperand(0);
14056 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
14057 // a 32-bit target where SSE doesn't support i64->FP operations.
14058 if (Op0.getOpcode() == ISD::LOAD) {
14059 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
14060 EVT VT = Ld->getValueType(0);
14061 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
14062 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
14063 !XTLI->getSubtarget()->is64Bit() &&
14064 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000014065 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
14066 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000014067 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
14068 return FILDChain;
14069 }
14070 }
14071 return SDValue();
14072}
14073
Chris Lattner23a01992010-12-20 01:37:09 +000014074// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
14075static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
14076 X86TargetLowering::DAGCombinerInfo &DCI) {
14077 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
14078 // the result is either zero or one (depending on the input carry bit).
14079 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
14080 if (X86::isZeroNode(N->getOperand(0)) &&
14081 X86::isZeroNode(N->getOperand(1)) &&
14082 // We don't have a good way to replace an EFLAGS use, so only do this when
14083 // dead right now.
14084 SDValue(N, 1).use_empty()) {
14085 DebugLoc DL = N->getDebugLoc();
14086 EVT VT = N->getValueType(0);
14087 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
14088 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
14089 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
14090 DAG.getConstant(X86::COND_B,MVT::i8),
14091 N->getOperand(2)),
14092 DAG.getConstant(1, VT));
14093 return DCI.CombineTo(N, Res1, CarryOut);
14094 }
14095
14096 return SDValue();
14097}
14098
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014099// fold (add Y, (sete X, 0)) -> adc 0, Y
14100// (add Y, (setne X, 0)) -> sbb -1, Y
14101// (sub (sete X, 0), Y) -> sbb 0, Y
14102// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014103static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014104 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014105
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014106 // Look through ZExts.
14107 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
14108 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
14109 return SDValue();
14110
14111 SDValue SetCC = Ext.getOperand(0);
14112 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
14113 return SDValue();
14114
14115 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
14116 if (CC != X86::COND_E && CC != X86::COND_NE)
14117 return SDValue();
14118
14119 SDValue Cmp = SetCC.getOperand(1);
14120 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000014121 !X86::isZeroNode(Cmp.getOperand(1)) ||
14122 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014123 return SDValue();
14124
14125 SDValue CmpOp0 = Cmp.getOperand(0);
14126 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
14127 DAG.getConstant(1, CmpOp0.getValueType()));
14128
14129 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
14130 if (CC == X86::COND_NE)
14131 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
14132 DL, OtherVal.getValueType(), OtherVal,
14133 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
14134 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
14135 DL, OtherVal.getValueType(), OtherVal,
14136 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
14137}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014138
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014139static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG) {
14140 SDValue Op0 = N->getOperand(0);
14141 SDValue Op1 = N->getOperand(1);
14142
14143 // X86 can't encode an immediate LHS of a sub. See if we can push the
14144 // negation into a preceding instruction.
14145 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014146 // If the RHS of the sub is a XOR with one use and a constant, invert the
14147 // immediate. Then add one to the LHS of the sub so we can turn
14148 // X-Y -> X+~Y+1, saving one register.
14149 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
14150 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000014151 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014152 EVT VT = Op0.getValueType();
14153 SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
14154 Op1.getOperand(0),
14155 DAG.getConstant(~XorC, VT));
14156 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000014157 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014158 }
14159 }
14160
14161 return OptimizeConditionalInDecrement(N, DAG);
14162}
14163
Dan Gohman475871a2008-07-27 21:46:04 +000014164SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000014165 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000014166 SelectionDAG &DAG = DCI.DAG;
14167 switch (N->getOpcode()) {
14168 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014169 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014170 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Duncan Sands6bcd2192011-09-17 16:49:39 +000014171 case ISD::VSELECT:
Chris Lattneraf723b92008-01-25 05:46:26 +000014172 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000014173 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014174 case ISD::ADD: return OptimizeConditionalInDecrement(N, DAG);
14175 case ISD::SUB: return PerformSubCombine(N, DAG);
Chris Lattner23a01992010-12-20 01:37:09 +000014176 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000014177 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000014178 case ISD::SHL:
14179 case ISD::SRA:
14180 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000014181 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000014182 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014183 case ISD::LOAD: return PerformLOADCombine(N, DAG, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000014184 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000014185 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Duncan Sands17470be2011-09-22 20:15:48 +000014186 case ISD::FADD: return PerformFADDCombine(N, DAG, Subtarget);
14187 case ISD::FSUB: return PerformFSUBCombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000014188 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000014189 case X86ISD::FOR: return PerformFORCombine(N, DAG);
14190 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000014191 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000014192 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000014193 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014194 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014195 case X86ISD::SHUFPS: // Handle all target specific shuffles
14196 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000014197 case X86ISD::PALIGN:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014198 case X86ISD::PUNPCKHBW:
14199 case X86ISD::PUNPCKHWD:
14200 case X86ISD::PUNPCKHDQ:
14201 case X86ISD::PUNPCKHQDQ:
14202 case X86ISD::UNPCKHPS:
14203 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +000014204 case X86ISD::VUNPCKHPSY:
14205 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014206 case X86ISD::PUNPCKLBW:
14207 case X86ISD::PUNPCKLWD:
14208 case X86ISD::PUNPCKLDQ:
14209 case X86ISD::PUNPCKLQDQ:
14210 case X86ISD::UNPCKLPS:
14211 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +000014212 case X86ISD::VUNPCKLPSY:
14213 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014214 case X86ISD::MOVHLPS:
14215 case X86ISD::MOVLHPS:
14216 case X86ISD::PSHUFD:
14217 case X86ISD::PSHUFHW:
14218 case X86ISD::PSHUFLW:
14219 case X86ISD::MOVSS:
14220 case X86ISD::MOVSD:
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +000014221 case X86ISD::VPERMILPS:
14222 case X86ISD::VPERMILPSY:
14223 case X86ISD::VPERMILPD:
14224 case X86ISD::VPERMILPDY:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +000014225 case X86ISD::VPERM2F128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014226 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000014227 }
14228
Dan Gohman475871a2008-07-27 21:46:04 +000014229 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000014230}
14231
Evan Chenge5b51ac2010-04-17 06:13:15 +000014232/// isTypeDesirableForOp - Return true if the target has native support for
14233/// the specified value type and it is 'desirable' to use the type for the
14234/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
14235/// instruction encodings are longer and some i16 instructions are slow.
14236bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
14237 if (!isTypeLegal(VT))
14238 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014239 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000014240 return true;
14241
14242 switch (Opc) {
14243 default:
14244 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000014245 case ISD::LOAD:
14246 case ISD::SIGN_EXTEND:
14247 case ISD::ZERO_EXTEND:
14248 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000014249 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000014250 case ISD::SRL:
14251 case ISD::SUB:
14252 case ISD::ADD:
14253 case ISD::MUL:
14254 case ISD::AND:
14255 case ISD::OR:
14256 case ISD::XOR:
14257 return false;
14258 }
14259}
14260
14261/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000014262/// beneficial for dag combiner to promote the specified node. If true, it
14263/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000014264bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000014265 EVT VT = Op.getValueType();
14266 if (VT != MVT::i16)
14267 return false;
14268
Evan Cheng4c26e932010-04-19 19:29:22 +000014269 bool Promote = false;
14270 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014271 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000014272 default: break;
14273 case ISD::LOAD: {
14274 LoadSDNode *LD = cast<LoadSDNode>(Op);
14275 // If the non-extending load has a single use and it's not live out, then it
14276 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014277 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
14278 Op.hasOneUse()*/) {
14279 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
14280 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
14281 // The only case where we'd want to promote LOAD (rather then it being
14282 // promoted as an operand is when it's only use is liveout.
14283 if (UI->getOpcode() != ISD::CopyToReg)
14284 return false;
14285 }
14286 }
Evan Cheng4c26e932010-04-19 19:29:22 +000014287 Promote = true;
14288 break;
14289 }
14290 case ISD::SIGN_EXTEND:
14291 case ISD::ZERO_EXTEND:
14292 case ISD::ANY_EXTEND:
14293 Promote = true;
14294 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014295 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014296 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000014297 SDValue N0 = Op.getOperand(0);
14298 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000014299 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000014300 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000014301 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014302 break;
14303 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000014304 case ISD::ADD:
14305 case ISD::MUL:
14306 case ISD::AND:
14307 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000014308 case ISD::XOR:
14309 Commute = true;
14310 // fallthrough
14311 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000014312 SDValue N0 = Op.getOperand(0);
14313 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000014314 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014315 return false;
14316 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000014317 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014318 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000014319 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014320 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000014321 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014322 }
14323 }
14324
14325 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000014326 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014327}
14328
Evan Cheng60c07e12006-07-05 22:17:51 +000014329//===----------------------------------------------------------------------===//
14330// X86 Inline Assembly Support
14331//===----------------------------------------------------------------------===//
14332
Chris Lattnerb8105652009-07-20 17:51:36 +000014333bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
14334 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000014335
14336 std::string AsmStr = IA->getAsmString();
14337
14338 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000014339 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000014340 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000014341
14342 switch (AsmPieces.size()) {
14343 default: return false;
14344 case 1:
14345 AsmStr = AsmPieces[0];
14346 AsmPieces.clear();
14347 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
14348
Chris Lattner7a2bdde2011-04-15 05:18:47 +000014349 // FIXME: this should verify that we are targeting a 486 or better. If not,
Evan Cheng55d42002011-01-08 01:24:27 +000014350 // we will turn this bswap into something that will be lowered to logical ops
14351 // instead of emitting the bswap asm. For now, we don't support 486 or lower
14352 // so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000014353 // bswap $0
14354 if (AsmPieces.size() == 2 &&
14355 (AsmPieces[0] == "bswap" ||
14356 AsmPieces[0] == "bswapq" ||
14357 AsmPieces[0] == "bswapl") &&
14358 (AsmPieces[1] == "$0" ||
14359 AsmPieces[1] == "${0:q}")) {
14360 // No need to check constraints, nothing other than the equivalent of
14361 // "=r,0" would be valid here.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014362 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014363 if (!Ty || Ty->getBitWidth() % 16 != 0)
14364 return false;
14365 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000014366 }
14367 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000014368 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000014369 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000014370 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000014371 AsmPieces[1] == "$$8," &&
14372 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000014373 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
14374 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014375 const std::string &ConstraintsStr = IA->getConstraintString();
14376 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000014377 std::sort(AsmPieces.begin(), AsmPieces.end());
14378 if (AsmPieces.size() == 4 &&
14379 AsmPieces[0] == "~{cc}" &&
14380 AsmPieces[1] == "~{dirflag}" &&
14381 AsmPieces[2] == "~{flags}" &&
14382 AsmPieces[3] == "~{fpsr}") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014383 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014384 if (!Ty || Ty->getBitWidth() % 16 != 0)
14385 return false;
14386 return IntrinsicLowering::LowerToByteSwap(CI);
Dan Gohman0ef701e2010-03-04 19:58:08 +000014387 }
Chris Lattnerb8105652009-07-20 17:51:36 +000014388 }
14389 break;
14390 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000014391 if (CI->getType()->isIntegerTy(32) &&
14392 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
14393 SmallVector<StringRef, 4> Words;
14394 SplitString(AsmPieces[0], Words, " \t,");
14395 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
14396 Words[2] == "${0:w}") {
14397 Words.clear();
14398 SplitString(AsmPieces[1], Words, " \t,");
14399 if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
14400 Words[2] == "$0") {
14401 Words.clear();
14402 SplitString(AsmPieces[2], Words, " \t,");
14403 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
14404 Words[2] == "${0:w}") {
14405 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014406 const std::string &ConstraintsStr = IA->getConstraintString();
14407 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Peter Collingbourne948cf022010-11-13 19:54:30 +000014408 std::sort(AsmPieces.begin(), AsmPieces.end());
14409 if (AsmPieces.size() == 4 &&
14410 AsmPieces[0] == "~{cc}" &&
14411 AsmPieces[1] == "~{dirflag}" &&
14412 AsmPieces[2] == "~{flags}" &&
14413 AsmPieces[3] == "~{fpsr}") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014414 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014415 if (!Ty || Ty->getBitWidth() % 16 != 0)
14416 return false;
14417 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000014418 }
14419 }
14420 }
14421 }
14422 }
Evan Cheng55d42002011-01-08 01:24:27 +000014423
14424 if (CI->getType()->isIntegerTy(64)) {
14425 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
14426 if (Constraints.size() >= 2 &&
14427 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
14428 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
14429 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
14430 SmallVector<StringRef, 4> Words;
14431 SplitString(AsmPieces[0], Words, " \t");
14432 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
Chris Lattnerb8105652009-07-20 17:51:36 +000014433 Words.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014434 SplitString(AsmPieces[1], Words, " \t");
14435 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
14436 Words.clear();
14437 SplitString(AsmPieces[2], Words, " \t,");
14438 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
14439 Words[2] == "%edx") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014440 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014441 if (!Ty || Ty->getBitWidth() % 16 != 0)
14442 return false;
14443 return IntrinsicLowering::LowerToByteSwap(CI);
14444 }
Chris Lattnerb8105652009-07-20 17:51:36 +000014445 }
14446 }
14447 }
14448 }
14449 break;
14450 }
14451 return false;
14452}
14453
14454
14455
Chris Lattnerf4dff842006-07-11 02:54:03 +000014456/// getConstraintType - Given a constraint letter, return the type of
14457/// constraint it is for this target.
14458X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000014459X86TargetLowering::getConstraintType(const std::string &Constraint) const {
14460 if (Constraint.size() == 1) {
14461 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000014462 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000014463 case 'q':
14464 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000014465 case 'f':
14466 case 't':
14467 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000014468 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000014469 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000014470 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000014471 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000014472 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000014473 case 'a':
14474 case 'b':
14475 case 'c':
14476 case 'd':
14477 case 'S':
14478 case 'D':
14479 case 'A':
14480 return C_Register;
14481 case 'I':
14482 case 'J':
14483 case 'K':
14484 case 'L':
14485 case 'M':
14486 case 'N':
14487 case 'G':
14488 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000014489 case 'e':
14490 case 'Z':
14491 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000014492 default:
14493 break;
14494 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000014495 }
Chris Lattner4234f572007-03-25 02:14:49 +000014496 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000014497}
14498
John Thompson44ab89e2010-10-29 17:29:13 +000014499/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000014500/// This object must already have been set up with the operand type
14501/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000014502TargetLowering::ConstraintWeight
14503 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000014504 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000014505 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014506 Value *CallOperandVal = info.CallOperandVal;
14507 // If we don't have a value, we can't do a match,
14508 // but allow it at the lowest weight.
14509 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000014510 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014511 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000014512 // Look at the constraint type.
14513 switch (*constraint) {
14514 default:
John Thompson44ab89e2010-10-29 17:29:13 +000014515 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
14516 case 'R':
14517 case 'q':
14518 case 'Q':
14519 case 'a':
14520 case 'b':
14521 case 'c':
14522 case 'd':
14523 case 'S':
14524 case 'D':
14525 case 'A':
14526 if (CallOperandVal->getType()->isIntegerTy())
14527 weight = CW_SpecificReg;
14528 break;
14529 case 'f':
14530 case 't':
14531 case 'u':
14532 if (type->isFloatingPointTy())
14533 weight = CW_SpecificReg;
14534 break;
14535 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000014536 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000014537 weight = CW_SpecificReg;
14538 break;
14539 case 'x':
14540 case 'Y':
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014541 if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
John Thompson44ab89e2010-10-29 17:29:13 +000014542 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014543 break;
14544 case 'I':
14545 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
14546 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000014547 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014548 }
14549 break;
John Thompson44ab89e2010-10-29 17:29:13 +000014550 case 'J':
14551 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14552 if (C->getZExtValue() <= 63)
14553 weight = CW_Constant;
14554 }
14555 break;
14556 case 'K':
14557 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14558 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
14559 weight = CW_Constant;
14560 }
14561 break;
14562 case 'L':
14563 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14564 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
14565 weight = CW_Constant;
14566 }
14567 break;
14568 case 'M':
14569 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14570 if (C->getZExtValue() <= 3)
14571 weight = CW_Constant;
14572 }
14573 break;
14574 case 'N':
14575 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14576 if (C->getZExtValue() <= 0xff)
14577 weight = CW_Constant;
14578 }
14579 break;
14580 case 'G':
14581 case 'C':
14582 if (dyn_cast<ConstantFP>(CallOperandVal)) {
14583 weight = CW_Constant;
14584 }
14585 break;
14586 case 'e':
14587 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14588 if ((C->getSExtValue() >= -0x80000000LL) &&
14589 (C->getSExtValue() <= 0x7fffffffLL))
14590 weight = CW_Constant;
14591 }
14592 break;
14593 case 'Z':
14594 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14595 if (C->getZExtValue() <= 0xffffffff)
14596 weight = CW_Constant;
14597 }
14598 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014599 }
14600 return weight;
14601}
14602
Dale Johannesenba2a0b92008-01-29 02:21:21 +000014603/// LowerXConstraint - try to replace an X constraint, which matches anything,
14604/// with another that has more specific requirements based on the type of the
14605/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000014606const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000014607LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000014608 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
14609 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000014610 if (ConstraintVT.isFloatingPoint()) {
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014611 if (Subtarget->hasXMMInt())
Chris Lattner5e764232008-04-26 23:02:14 +000014612 return "Y";
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014613 if (Subtarget->hasXMM())
Chris Lattner5e764232008-04-26 23:02:14 +000014614 return "x";
14615 }
Scott Michelfdc40a02009-02-17 22:15:04 +000014616
Chris Lattner5e764232008-04-26 23:02:14 +000014617 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000014618}
14619
Chris Lattner48884cd2007-08-25 00:47:38 +000014620/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
14621/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000014622void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000014623 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000014624 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000014625 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000014626 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000014627
Eric Christopher100c8332011-06-02 23:16:42 +000014628 // Only support length 1 constraints for now.
14629 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000014630
Eric Christopher100c8332011-06-02 23:16:42 +000014631 char ConstraintLetter = Constraint[0];
14632 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000014633 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000014634 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000014635 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000014636 if (C->getZExtValue() <= 31) {
14637 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000014638 break;
14639 }
Devang Patel84f7fd22007-03-17 00:13:28 +000014640 }
Chris Lattner48884cd2007-08-25 00:47:38 +000014641 return;
Evan Cheng364091e2008-09-22 23:57:37 +000014642 case 'J':
14643 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000014644 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000014645 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
14646 break;
14647 }
14648 }
14649 return;
14650 case 'K':
14651 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000014652 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000014653 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
14654 break;
14655 }
14656 }
14657 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000014658 case 'N':
14659 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000014660 if (C->getZExtValue() <= 255) {
14661 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000014662 break;
14663 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000014664 }
Chris Lattner48884cd2007-08-25 00:47:38 +000014665 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000014666 case 'e': {
14667 // 32-bit signed value
14668 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000014669 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
14670 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000014671 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000014672 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000014673 break;
14674 }
14675 // FIXME gcc accepts some relocatable values here too, but only in certain
14676 // memory models; it's complicated.
14677 }
14678 return;
14679 }
14680 case 'Z': {
14681 // 32-bit unsigned value
14682 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000014683 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
14684 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000014685 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
14686 break;
14687 }
14688 }
14689 // FIXME gcc accepts some relocatable values here too, but only in certain
14690 // memory models; it's complicated.
14691 return;
14692 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000014693 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000014694 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000014695 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000014696 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000014697 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000014698 break;
14699 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014700
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000014701 // In any sort of PIC mode addresses need to be computed at runtime by
14702 // adding in a register or some sort of table lookup. These can't
14703 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000014704 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000014705 return;
14706
Chris Lattnerdc43a882007-05-03 16:52:29 +000014707 // If we are in non-pic codegen mode, we allow the address of a global (with
14708 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000014709 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000014710 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000014711
Chris Lattner49921962009-05-08 18:23:14 +000014712 // Match either (GA), (GA+C), (GA+C1+C2), etc.
14713 while (1) {
14714 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
14715 Offset += GA->getOffset();
14716 break;
14717 } else if (Op.getOpcode() == ISD::ADD) {
14718 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
14719 Offset += C->getZExtValue();
14720 Op = Op.getOperand(0);
14721 continue;
14722 }
14723 } else if (Op.getOpcode() == ISD::SUB) {
14724 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
14725 Offset += -C->getZExtValue();
14726 Op = Op.getOperand(0);
14727 continue;
14728 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000014729 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000014730
Chris Lattner49921962009-05-08 18:23:14 +000014731 // Otherwise, this isn't something we can handle, reject it.
14732 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000014733 }
Eric Christopherfd179292009-08-27 18:07:15 +000014734
Dan Gohman46510a72010-04-15 01:51:59 +000014735 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000014736 // If we require an extra load to get this address, as in PIC mode, we
14737 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000014738 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
14739 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000014740 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000014741
Devang Patel0d881da2010-07-06 22:08:15 +000014742 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
14743 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000014744 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000014745 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000014746 }
Scott Michelfdc40a02009-02-17 22:15:04 +000014747
Gabor Greifba36cb52008-08-28 21:40:38 +000014748 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000014749 Ops.push_back(Result);
14750 return;
14751 }
Dale Johannesen1784d162010-06-25 21:55:36 +000014752 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000014753}
14754
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014755std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000014756X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000014757 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000014758 // First, see if this is a constraint that directly corresponds to an LLVM
14759 // register class.
14760 if (Constraint.size() == 1) {
14761 // GCC Constraint Letters
14762 switch (Constraint[0]) {
14763 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000014764 // TODO: Slight differences here in allocation order and leaving
14765 // RIP in the class. Do they matter any more here than they do
14766 // in the normal allocation?
14767 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
14768 if (Subtarget->is64Bit()) {
Nick Lewycky9bf45d02011-07-08 00:19:27 +000014769 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000014770 return std::make_pair(0U, X86::GR32RegisterClass);
14771 else if (VT == MVT::i16)
14772 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000014773 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000014774 return std::make_pair(0U, X86::GR8RegisterClass);
Nick Lewycky9bf45d02011-07-08 00:19:27 +000014775 else if (VT == MVT::i64 || VT == MVT::f64)
Eric Christopherd176af82011-06-29 17:23:50 +000014776 return std::make_pair(0U, X86::GR64RegisterClass);
14777 break;
14778 }
14779 // 32-bit fallthrough
14780 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000014781 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000014782 return std::make_pair(0U, X86::GR32_ABCDRegisterClass);
14783 else if (VT == MVT::i16)
14784 return std::make_pair(0U, X86::GR16_ABCDRegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000014785 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000014786 return std::make_pair(0U, X86::GR8_ABCD_LRegisterClass);
14787 else if (VT == MVT::i64)
14788 return std::make_pair(0U, X86::GR64_ABCDRegisterClass);
14789 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000014790 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000014791 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000014792 if (VT == MVT::i8 || VT == MVT::i1)
Chris Lattner0f65cad2007-04-09 05:49:22 +000014793 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000014794 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000014795 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000014796 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000014797 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000014798 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000014799 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000014800 if (VT == MVT::i8 || VT == MVT::i1)
Dale Johannesen5f3663e2009-10-07 22:47:20 +000014801 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
14802 if (VT == MVT::i16)
14803 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
14804 if (VT == MVT::i32 || !Subtarget->is64Bit())
14805 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
14806 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000014807 case 'f': // FP Stack registers.
14808 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
14809 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000014810 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000014811 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000014812 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000014813 return std::make_pair(0U, X86::RFP64RegisterClass);
14814 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000014815 case 'y': // MMX_REGS if MMX allowed.
14816 if (!Subtarget->hasMMX()) break;
14817 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000014818 case 'Y': // SSE_REGS if SSE2 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014819 if (!Subtarget->hasXMMInt()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000014820 // FALL THROUGH.
14821 case 'x': // SSE_REGS if SSE1 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014822 if (!Subtarget->hasXMM()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000014823
Owen Anderson825b72b2009-08-11 20:47:22 +000014824 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000014825 default: break;
14826 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000014827 case MVT::f32:
14828 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000014829 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000014830 case MVT::f64:
14831 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000014832 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000014833 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000014834 case MVT::v16i8:
14835 case MVT::v8i16:
14836 case MVT::v4i32:
14837 case MVT::v2i64:
14838 case MVT::v4f32:
14839 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000014840 return std::make_pair(0U, X86::VR128RegisterClass);
14841 }
Chris Lattnerad043e82007-04-09 05:11:28 +000014842 break;
14843 }
14844 }
Scott Michelfdc40a02009-02-17 22:15:04 +000014845
Chris Lattnerf76d1802006-07-31 23:26:50 +000014846 // Use the default implementation in TargetLowering to convert the register
14847 // constraint into a member of a register class.
14848 std::pair<unsigned, const TargetRegisterClass*> Res;
14849 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000014850
14851 // Not found as a standard register?
14852 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000014853 // Map st(0) -> st(7) -> ST0
14854 if (Constraint.size() == 7 && Constraint[0] == '{' &&
14855 tolower(Constraint[1]) == 's' &&
14856 tolower(Constraint[2]) == 't' &&
14857 Constraint[3] == '(' &&
14858 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
14859 Constraint[5] == ')' &&
14860 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000014861
Chris Lattner56d77c72009-09-13 22:41:48 +000014862 Res.first = X86::ST0+Constraint[4]-'0';
14863 Res.second = X86::RFP80RegisterClass;
14864 return Res;
14865 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000014866
Chris Lattner56d77c72009-09-13 22:41:48 +000014867 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000014868 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000014869 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000014870 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000014871 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000014872 }
Chris Lattner56d77c72009-09-13 22:41:48 +000014873
14874 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000014875 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000014876 Res.first = X86::EFLAGS;
14877 Res.second = X86::CCRRegisterClass;
14878 return Res;
14879 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000014880
Dale Johannesen330169f2008-11-13 21:52:36 +000014881 // 'A' means EAX + EDX.
14882 if (Constraint == "A") {
14883 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000014884 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000014885 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000014886 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000014887 return Res;
14888 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014889
Chris Lattnerf76d1802006-07-31 23:26:50 +000014890 // Otherwise, check to see if this is a register class of the wrong value
14891 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
14892 // turn into {ax},{dx}.
14893 if (Res.second->hasType(VT))
14894 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014895
Chris Lattnerf76d1802006-07-31 23:26:50 +000014896 // All of the single-register GCC register classes map their values onto
14897 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
14898 // really want an 8-bit or 32-bit register, map to the appropriate register
14899 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000014900 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000014901 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000014902 unsigned DestReg = 0;
14903 switch (Res.first) {
14904 default: break;
14905 case X86::AX: DestReg = X86::AL; break;
14906 case X86::DX: DestReg = X86::DL; break;
14907 case X86::CX: DestReg = X86::CL; break;
14908 case X86::BX: DestReg = X86::BL; break;
14909 }
14910 if (DestReg) {
14911 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000014912 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000014913 }
Owen Anderson825b72b2009-08-11 20:47:22 +000014914 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000014915 unsigned DestReg = 0;
14916 switch (Res.first) {
14917 default: break;
14918 case X86::AX: DestReg = X86::EAX; break;
14919 case X86::DX: DestReg = X86::EDX; break;
14920 case X86::CX: DestReg = X86::ECX; break;
14921 case X86::BX: DestReg = X86::EBX; break;
14922 case X86::SI: DestReg = X86::ESI; break;
14923 case X86::DI: DestReg = X86::EDI; break;
14924 case X86::BP: DestReg = X86::EBP; break;
14925 case X86::SP: DestReg = X86::ESP; break;
14926 }
14927 if (DestReg) {
14928 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000014929 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000014930 }
Owen Anderson825b72b2009-08-11 20:47:22 +000014931 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000014932 unsigned DestReg = 0;
14933 switch (Res.first) {
14934 default: break;
14935 case X86::AX: DestReg = X86::RAX; break;
14936 case X86::DX: DestReg = X86::RDX; break;
14937 case X86::CX: DestReg = X86::RCX; break;
14938 case X86::BX: DestReg = X86::RBX; break;
14939 case X86::SI: DestReg = X86::RSI; break;
14940 case X86::DI: DestReg = X86::RDI; break;
14941 case X86::BP: DestReg = X86::RBP; break;
14942 case X86::SP: DestReg = X86::RSP; break;
14943 }
14944 if (DestReg) {
14945 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000014946 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000014947 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000014948 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000014949 } else if (Res.second == X86::FR32RegisterClass ||
14950 Res.second == X86::FR64RegisterClass ||
14951 Res.second == X86::VR128RegisterClass) {
14952 // Handle references to XMM physical registers that got mapped into the
14953 // wrong class. This can happen with constraints like {xmm0} where the
14954 // target independent register mapper will just pick the first match it can
14955 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000014956 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000014957 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000014958 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000014959 Res.second = X86::FR64RegisterClass;
14960 else if (X86::VR128RegisterClass->hasType(VT))
14961 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000014962 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014963
Chris Lattnerf76d1802006-07-31 23:26:50 +000014964 return Res;
14965}