blob: cf90490703e0917fb640b52cbb16de738d127753 [file] [log] [blame]
Arnold Schwaighofer92226dd2007-10-12 21:53:12 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
Evan Chengb1712452010-01-27 06:25:16 +000015#define DEBUG_TYPE "x86-isel"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000016#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000017#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000018#include "X86ISelLowering.h"
19#include "X86TargetMachine.h"
Chris Lattner8c6ed052009-09-16 01:46:41 +000020#include "X86TargetObjectFile.h"
David Greene583b68f2011-02-17 19:18:59 +000021#include "Utils/X86ShuffleDecode.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000022#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000023#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000024#include "llvm/DerivedTypes.h"
Chris Lattnerb903bed2009-06-26 21:20:29 +000025#include "llvm/GlobalAlias.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000026#include "llvm/GlobalVariable.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000027#include "llvm/Function.h"
Chris Lattnerb8105652009-07-20 17:51:36 +000028#include "llvm/Instructions.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000029#include "llvm/Intrinsics.h"
Owen Andersona90b3dc2009-07-15 21:51:10 +000030#include "llvm/LLVMContext.h"
Evan Cheng55d42002011-01-08 01:24:27 +000031#include "llvm/CodeGen/IntrinsicLowering.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000032#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000033#include "llvm/CodeGen/MachineFunction.h"
34#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner5e1df8d2010-01-25 23:38:14 +000035#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000036#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000037#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000038#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattner589c6f62010-01-26 06:28:43 +000039#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000040#include "llvm/MC/MCContext.h"
Daniel Dunbar4e815f82010-03-15 23:51:06 +000041#include "llvm/MC/MCExpr.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000042#include "llvm/MC/MCSymbol.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000043#include "llvm/ADT/BitVector.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000044#include "llvm/ADT/SmallSet.h"
Evan Chengb1712452010-01-27 06:25:16 +000045#include "llvm/ADT/Statistic.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000046#include "llvm/ADT/StringExtras.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000047#include "llvm/ADT/VectorExtras.h"
Evan Cheng485fafc2011-03-21 01:19:09 +000048#include "llvm/Support/CallSite.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000049#include "llvm/Support/Debug.h"
Bill Wendlingec041eb2010-03-12 19:20:40 +000050#include "llvm/Support/Dwarf.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000051#include "llvm/Support/ErrorHandling.h"
52#include "llvm/Support/MathExtras.h"
Torok Edwindac237e2009-07-08 20:53:28 +000053#include "llvm/Support/raw_ostream.h"
Rafael Espindola151ab3e2011-08-30 19:47:04 +000054#include "llvm/Target/TargetOptions.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000055using namespace llvm;
Bill Wendlingec041eb2010-03-12 19:20:40 +000056using namespace dwarf;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000057
Evan Chengb1712452010-01-27 06:25:16 +000058STATISTIC(NumTailCalls, "Number of tail calls");
59
Evan Cheng10e86422008-04-25 19:11:04 +000060// Forward declarations.
Owen Andersone50ed302009-08-10 22:56:29 +000061static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +000062 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000063
David Greenea5f26012011-02-07 19:36:54 +000064static SDValue Insert128BitVector(SDValue Result,
65 SDValue Vec,
66 SDValue Idx,
67 SelectionDAG &DAG,
68 DebugLoc dl);
David Greenef125a292011-02-08 19:04:41 +000069
David Greenea5f26012011-02-07 19:36:54 +000070static SDValue Extract128BitVector(SDValue Vec,
71 SDValue Idx,
72 SelectionDAG &DAG,
73 DebugLoc dl);
74
75/// Generate a DAG to grab 128-bits from a vector > 128 bits. This
76/// sets things up to match to an AVX VEXTRACTF128 instruction or a
David Greene74a579d2011-02-10 16:57:36 +000077/// simple subregister reference. Idx is an index in the 128 bits we
78/// want. It need not be aligned to a 128-bit bounday. That makes
79/// lowering EXTRACT_VECTOR_ELT operations easier.
David Greenea5f26012011-02-07 19:36:54 +000080static SDValue Extract128BitVector(SDValue Vec,
81 SDValue Idx,
82 SelectionDAG &DAG,
83 DebugLoc dl) {
84 EVT VT = Vec.getValueType();
85 assert(VT.getSizeInBits() == 256 && "Unexpected vector size!");
David Greenea5f26012011-02-07 19:36:54 +000086 EVT ElVT = VT.getVectorElementType();
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +000087 int Factor = VT.getSizeInBits()/128;
88 EVT ResultVT = EVT::getVectorVT(*DAG.getContext(), ElVT,
89 VT.getVectorNumElements()/Factor);
David Greenea5f26012011-02-07 19:36:54 +000090
91 // Extract from UNDEF is UNDEF.
92 if (Vec.getOpcode() == ISD::UNDEF)
93 return DAG.getNode(ISD::UNDEF, dl, ResultVT);
94
95 if (isa<ConstantSDNode>(Idx)) {
96 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
97
98 // Extract the relevant 128 bits. Generate an EXTRACT_SUBVECTOR
99 // we can match to VEXTRACTF128.
100 unsigned ElemsPerChunk = 128 / ElVT.getSizeInBits();
101
102 // This is the index of the first element of the 128-bit chunk
103 // we want.
104 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits()) / 128)
105 * ElemsPerChunk);
106
107 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
David Greenea5f26012011-02-07 19:36:54 +0000108 SDValue Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, ResultVT, Vec,
109 VecIdx);
110
111 return Result;
112 }
113
114 return SDValue();
115}
116
117/// Generate a DAG to put 128-bits into a vector > 128 bits. This
118/// sets things up to match to an AVX VINSERTF128 instruction or a
David Greene6b381262011-02-09 15:32:06 +0000119/// simple superregister reference. Idx is an index in the 128 bits
120/// we want. It need not be aligned to a 128-bit bounday. That makes
121/// lowering INSERT_VECTOR_ELT operations easier.
David Greenea5f26012011-02-07 19:36:54 +0000122static SDValue Insert128BitVector(SDValue Result,
123 SDValue Vec,
124 SDValue Idx,
125 SelectionDAG &DAG,
126 DebugLoc dl) {
127 if (isa<ConstantSDNode>(Idx)) {
128 EVT VT = Vec.getValueType();
129 assert(VT.getSizeInBits() == 128 && "Unexpected vector size!");
130
131 EVT ElVT = VT.getVectorElementType();
David Greenea5f26012011-02-07 19:36:54 +0000132 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
David Greenea5f26012011-02-07 19:36:54 +0000133 EVT ResultVT = Result.getValueType();
134
135 // Insert the relevant 128 bits.
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +0000136 unsigned ElemsPerChunk = 128/ElVT.getSizeInBits();
David Greenea5f26012011-02-07 19:36:54 +0000137
138 // This is the index of the first element of the 128-bit chunk
139 // we want.
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +0000140 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits())/128)
David Greenea5f26012011-02-07 19:36:54 +0000141 * ElemsPerChunk);
142
143 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
David Greenea5f26012011-02-07 19:36:54 +0000144 Result = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResultVT, Result, Vec,
145 VecIdx);
146 return Result;
147 }
148
149 return SDValue();
150}
151
Chris Lattnerf0144122009-07-28 03:13:23 +0000152static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
Evan Cheng2bffee22011-02-01 01:14:13 +0000153 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
154 bool is64Bit = Subtarget->is64Bit();
NAKAMURA Takumi27635382011-02-05 15:10:54 +0000155
Evan Cheng2bffee22011-02-01 01:14:13 +0000156 if (Subtarget->isTargetEnvMacho()) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000157 if (is64Bit)
158 return new X8664_MachoTargetObjectFile();
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000159 return new TargetLoweringObjectFileMachO();
Michael J. Spencerec38de22010-10-10 22:04:20 +0000160 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000161
Evan Cheng203576a2011-07-20 19:50:42 +0000162 if (Subtarget->isTargetELF())
163 return new TargetLoweringObjectFileELF();
Evan Cheng2bffee22011-02-01 01:14:13 +0000164 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
Chris Lattnere019ec12010-12-19 20:07:10 +0000165 return new TargetLoweringObjectFileCOFF();
Eric Christopher62f35a22010-07-05 19:26:33 +0000166 llvm_unreachable("unknown subtarget type");
Chris Lattnerf0144122009-07-28 03:13:23 +0000167}
168
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +0000169X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +0000170 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +0000171 Subtarget = &TM.getSubtarget<X86Subtarget>();
Bruno Cardoso Lopesaed890b2011-08-01 21:54:05 +0000172 X86ScalarSSEf64 = Subtarget->hasXMMInt() || Subtarget->hasAVX();
173 X86ScalarSSEf32 = Subtarget->hasXMM() || Subtarget->hasAVX();
Evan Cheng25ab6902006-09-08 06:48:29 +0000174 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000175
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000176 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000177 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000178
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000179 // Set up the TargetLowering object.
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000180 static MVT IntVTs[] = { MVT::i8, MVT::i16, MVT::i32, MVT::i64 };
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000181
182 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Duncan Sands03228082008-11-23 15:47:28 +0000183 setBooleanContents(ZeroOrOneBooleanContent);
Duncan Sands28b77e92011-09-06 19:07:46 +0000184 // X86-SSE is even stranger. It uses -1 or 0 for vector masks.
185 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Eric Christopher471e4222011-06-08 23:55:35 +0000186
Eric Christopherde5e1012011-03-11 01:05:58 +0000187 // For 64-bit since we have so many registers use the ILP scheduler, for
188 // 32-bit code use the register pressure specific scheduling.
189 if (Subtarget->is64Bit())
190 setSchedulingPreference(Sched::ILP);
191 else
192 setSchedulingPreference(Sched::RegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +0000193 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +0000194
Michael J. Spencer92bf38c2010-10-10 23:11:06 +0000195 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing()) {
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000196 // Setup Windows compiler runtime calls.
197 setLibcallName(RTLIB::SDIV_I64, "_alldiv");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000198 setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
Julien Lerougef2960822011-07-08 21:40:25 +0000199 setLibcallName(RTLIB::SREM_I64, "_allrem");
200 setLibcallName(RTLIB::UREM_I64, "_aullrem");
201 setLibcallName(RTLIB::MUL_I64, "_allmul");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000202 setLibcallName(RTLIB::FPTOUINT_F64_I64, "_ftol2");
Michael J. Spencer94f7eeb2010-10-19 07:32:52 +0000203 setLibcallName(RTLIB::FPTOUINT_F32_I64, "_ftol2");
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000204 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer335b8062010-10-11 05:29:15 +0000205 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
Julien Lerougef2960822011-07-08 21:40:25 +0000206 setLibcallCallingConv(RTLIB::SREM_I64, CallingConv::X86_StdCall);
207 setLibcallCallingConv(RTLIB::UREM_I64, CallingConv::X86_StdCall);
208 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::X86_StdCall);
Michael J. Spencer6dad10e2010-10-27 18:52:38 +0000209 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::C);
210 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::C);
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000211 }
212
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000213 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000214 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000215 setUseUnderscoreSetJmp(false);
216 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000217 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000218 // MS runtime is weird: it exports _setjmp, but longjmp!
219 setUseUnderscoreSetJmp(true);
220 setUseUnderscoreLongJmp(false);
221 } else {
222 setUseUnderscoreSetJmp(true);
223 setUseUnderscoreLongJmp(true);
224 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000225
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000226 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000227 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
Dan Gohman71edb242010-04-30 18:30:26 +0000228 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000229 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000230 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000231 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000232
Owen Anderson825b72b2009-08-11 20:47:22 +0000233 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000234
Scott Michelfdc40a02009-02-17 22:15:04 +0000235 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000236 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000237 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000238 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000239 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000240 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
241 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000242
243 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000244 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
245 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
246 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
247 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
248 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
249 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000250
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000251 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
252 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000253 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
254 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
255 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000256
Evan Cheng25ab6902006-09-08 06:48:29 +0000257 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000258 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
259 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Eli Friedman948e95a2009-05-23 09:59:16 +0000260 } else if (!UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000261 // We have an algorithm for SSE2->double, and we turn this into a
262 // 64-bit FILD followed by conditional FADD for other targets.
263 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000264 // We have an algorithm for SSE2, and we turn this into a 64-bit
265 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000266 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000267 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000268
269 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
270 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000271 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
272 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000273
Devang Patel6a784892009-06-05 18:48:29 +0000274 if (!UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000275 // SSE has no i16 to fp conversion, only i32
276 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000277 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000278 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000279 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000280 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000281 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
282 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000283 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000284 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000285 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
286 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000287 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000288
Dale Johannesen73328d12007-09-19 23:55:34 +0000289 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
290 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000291 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
292 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000293
Evan Cheng02568ff2006-01-30 22:13:22 +0000294 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
295 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000296 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
297 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000298
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000299 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000300 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000301 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000302 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000303 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000304 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
305 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000306 }
307
308 // Handle FP_TO_UINT by promoting the destination to a larger signed
309 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000310 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
311 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
312 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000313
Evan Cheng25ab6902006-09-08 06:48:29 +0000314 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000315 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
316 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman948e95a2009-05-23 09:59:16 +0000317 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000318 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000319 // Expand FP_TO_UINT into a select.
320 // FIXME: We would like to use a Custom expander here eventually to do
321 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000322 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000323 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000324 // With SSE3 we can use fisttpll to convert to a signed i64; without
325 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000326 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000327 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000328
Chris Lattner399610a2006-12-05 18:22:22 +0000329 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000330 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000331 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
332 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000333 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000334 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000335 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000336 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000337 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000338 }
Chris Lattner21f66852005-12-23 05:15:23 +0000339
Dan Gohmanb00ee212008-02-18 19:34:53 +0000340 // Scalar integer divide and remainder are lowered to use operations that
341 // produce two results, to match the available instructions. This exposes
342 // the two-result form to trivial CSE, which is able to combine x/y and x%y
343 // into a single instruction.
344 //
345 // Scalar integer multiply-high is also lowered to use two-result
346 // operations, to match the available instructions. However, plain multiply
347 // (low) operations are left as Legal, as there are single-result
348 // instructions for this in x86. Using the two-result multiply instructions
349 // when both high and low results are needed must be arranged by dagcombine.
Chris Lattnere019ec12010-12-19 20:07:10 +0000350 for (unsigned i = 0, e = 4; i != e; ++i) {
351 MVT VT = IntVTs[i];
352 setOperationAction(ISD::MULHS, VT, Expand);
353 setOperationAction(ISD::MULHU, VT, Expand);
354 setOperationAction(ISD::SDIV, VT, Expand);
355 setOperationAction(ISD::UDIV, VT, Expand);
356 setOperationAction(ISD::SREM, VT, Expand);
357 setOperationAction(ISD::UREM, VT, Expand);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000358
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000359 // Add/Sub overflow ops with MVT::Glues are lowered to EFLAGS dependences.
Chris Lattnerd8ff7ec2010-12-20 01:03:27 +0000360 setOperationAction(ISD::ADDC, VT, Custom);
361 setOperationAction(ISD::ADDE, VT, Custom);
362 setOperationAction(ISD::SUBC, VT, Custom);
363 setOperationAction(ISD::SUBE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000364 }
Dan Gohmana37c9f72007-09-25 18:23:27 +0000365
Owen Anderson825b72b2009-08-11 20:47:22 +0000366 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
367 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
368 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
369 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000370 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000371 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
372 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
373 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
374 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
375 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
376 setOperationAction(ISD::FREM , MVT::f32 , Expand);
377 setOperationAction(ISD::FREM , MVT::f64 , Expand);
378 setOperationAction(ISD::FREM , MVT::f80 , Expand);
379 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000380
Owen Anderson825b72b2009-08-11 20:47:22 +0000381 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
382 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000383 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
384 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000385 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
386 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000387 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000388 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
389 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000390 }
391
Benjamin Kramer1292c222010-12-04 20:32:23 +0000392 if (Subtarget->hasPOPCNT()) {
393 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
394 } else {
395 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
396 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
397 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
398 if (Subtarget->is64Bit())
399 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
400 }
401
Owen Anderson825b72b2009-08-11 20:47:22 +0000402 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
403 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000404
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000405 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000406 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000407 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000408 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000409 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000410 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
411 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
412 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
413 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
414 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000415 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000416 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
417 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
418 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
419 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000420 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000421 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
Andrew Trickf6c39412011-03-23 23:11:02 +0000422 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000423 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000424 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000425
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000426 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000427 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
428 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
429 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
430 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000431 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000432 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
433 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000434 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000435 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000436 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
437 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
438 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
439 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000440 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000441 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000442 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000443 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
444 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
445 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000446 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000447 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
448 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
449 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000450 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000451
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000452 if (Subtarget->hasXMM())
Owen Anderson825b72b2009-08-11 20:47:22 +0000453 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000454
Eric Christopher9a9d2752010-07-22 02:48:34 +0000455 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000456 setOperationAction(ISD::ATOMIC_FENCE , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000457
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000458 // On X86 and X86-64, atomic operations are lowered to locked instructions.
459 // Locked instructions, in turn, have implicit fence semantics (all memory
460 // operations are flushed before issuing the locked instruction, and they
461 // are not buffered), so we can fold away the common pattern of
462 // fence-atomic-fence.
463 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000464
Mon P Wang63307c32008-05-05 19:05:59 +0000465 // Expand certain atomics
Chris Lattnere019ec12010-12-19 20:07:10 +0000466 for (unsigned i = 0, e = 4; i != e; ++i) {
467 MVT VT = IntVTs[i];
468 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
469 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
Eli Friedman327236c2011-08-24 20:50:09 +0000470 setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000471 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000472
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000473 if (!Subtarget->is64Bit()) {
Eli Friedmanf8f90f02011-08-24 22:33:28 +0000474 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000475 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
476 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
477 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
478 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
479 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
480 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
481 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000482 }
483
Eli Friedman43f51ae2011-08-26 21:21:21 +0000484 if (Subtarget->hasCmpxchg16b()) {
485 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i128, Custom);
486 }
487
Evan Cheng3c992d22006-03-07 02:02:57 +0000488 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000489 if (!Subtarget->isTargetDarwin() &&
490 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000491 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000492 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000493 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000494
Owen Anderson825b72b2009-08-11 20:47:22 +0000495 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
496 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
497 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
498 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000499 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000500 setExceptionPointerRegister(X86::RAX);
501 setExceptionSelectorRegister(X86::RDX);
502 } else {
503 setExceptionPointerRegister(X86::EAX);
504 setExceptionSelectorRegister(X86::EDX);
505 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000506 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
507 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000508
Duncan Sands4a544a72011-09-06 13:37:06 +0000509 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
510 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000511
Owen Anderson825b72b2009-08-11 20:47:22 +0000512 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000513
Nate Begemanacc398c2006-01-25 18:21:52 +0000514 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000515 setOperationAction(ISD::VASTART , MVT::Other, Custom);
516 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000517 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000518 setOperationAction(ISD::VAARG , MVT::Other, Custom);
519 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000520 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000521 setOperationAction(ISD::VAARG , MVT::Other, Expand);
522 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000523 }
Evan Chengae642192007-03-02 23:16:35 +0000524
Owen Anderson825b72b2009-08-11 20:47:22 +0000525 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
526 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eric Christopherc967ad82011-08-31 04:17:21 +0000527
528 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
529 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
530 MVT::i64 : MVT::i32, Custom);
531 else if (EnableSegmentedStacks)
532 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
533 MVT::i64 : MVT::i32, Custom);
534 else
535 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
536 MVT::i64 : MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000537
Evan Chengc7ce29b2009-02-13 22:36:38 +0000538 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000539 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000540 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000541 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
542 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000543
Evan Cheng223547a2006-01-31 22:28:30 +0000544 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000545 setOperationAction(ISD::FABS , MVT::f64, Custom);
546 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000547
548 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000549 setOperationAction(ISD::FNEG , MVT::f64, Custom);
550 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000551
Evan Cheng68c47cb2007-01-05 07:55:56 +0000552 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000553 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
554 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000555
Stuart Hastings4fd0dee2011-06-01 04:39:42 +0000556 // Lower this to FGETSIGNx86 plus an AND.
557 setOperationAction(ISD::FGETSIGN, MVT::i64, Custom);
558 setOperationAction(ISD::FGETSIGN, MVT::i32, Custom);
559
Evan Chengd25e9e82006-02-02 00:28:23 +0000560 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000561 setOperationAction(ISD::FSIN , MVT::f64, Expand);
562 setOperationAction(ISD::FCOS , MVT::f64, Expand);
563 setOperationAction(ISD::FSIN , MVT::f32, Expand);
564 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000565
Chris Lattnera54aa942006-01-29 06:26:08 +0000566 // Expand FP immediates into loads from the stack, except for the special
567 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000568 addLegalFPImmediate(APFloat(+0.0)); // xorpd
569 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000570 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000571 // Use SSE for f32, x87 for f64.
572 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000573 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
574 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000575
576 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000577 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000578
579 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000580 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000581
Owen Anderson825b72b2009-08-11 20:47:22 +0000582 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000583
584 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000585 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
586 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000587
588 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000589 setOperationAction(ISD::FSIN , MVT::f32, Expand);
590 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000591
Nate Begemane1795842008-02-14 08:57:00 +0000592 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000593 addLegalFPImmediate(APFloat(+0.0f)); // xorps
594 addLegalFPImmediate(APFloat(+0.0)); // FLD0
595 addLegalFPImmediate(APFloat(+1.0)); // FLD1
596 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
597 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
598
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000599 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000600 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
601 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000602 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000603 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000604 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000605 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000606 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
607 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000608
Owen Anderson825b72b2009-08-11 20:47:22 +0000609 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
610 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
611 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
612 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000613
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000614 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000615 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
616 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000617 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000618 addLegalFPImmediate(APFloat(+0.0)); // FLD0
619 addLegalFPImmediate(APFloat(+1.0)); // FLD1
620 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
621 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000622 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
623 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
624 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
625 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000626 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000627
Cameron Zwarich33390842011-07-08 21:39:21 +0000628 // We don't support FMA.
629 setOperationAction(ISD::FMA, MVT::f64, Expand);
630 setOperationAction(ISD::FMA, MVT::f32, Expand);
631
Dale Johannesen59a58732007-08-05 18:49:15 +0000632 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000633 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000634 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
635 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
636 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000637 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000638 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000639 addLegalFPImmediate(TmpFlt); // FLD0
640 TmpFlt.changeSign();
641 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000642
643 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000644 APFloat TmpFlt2(+1.0);
645 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
646 &ignored);
647 addLegalFPImmediate(TmpFlt2); // FLD1
648 TmpFlt2.changeSign();
649 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
650 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000651
Evan Chengc7ce29b2009-02-13 22:36:38 +0000652 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000653 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
654 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000655 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000656
657 setOperationAction(ISD::FMA, MVT::f80, Expand);
Dale Johannesen2f429012007-09-26 21:10:55 +0000658 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000659
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000660 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000661 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
662 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
663 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000664
Owen Anderson825b72b2009-08-11 20:47:22 +0000665 setOperationAction(ISD::FLOG, MVT::f80, Expand);
666 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
667 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
668 setOperationAction(ISD::FEXP, MVT::f80, Expand);
669 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000670
Mon P Wangf007a8b2008-11-06 05:31:54 +0000671 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000672 // (for widening) or expand (for scalarization). Then we will selectively
673 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000674 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
675 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
676 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
677 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
678 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
679 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
680 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
681 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
682 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
683 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
684 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
685 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
686 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
687 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
688 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
689 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
690 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000691 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
David Greenecfe33c42011-01-26 19:13:22 +0000692 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
693 setOperationAction(ISD::INSERT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000694 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
695 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
696 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
697 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
698 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
699 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
700 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
701 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
702 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
703 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
704 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
705 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
706 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
707 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
708 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
709 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
710 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
711 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
712 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
713 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
714 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
Duncan Sands28b77e92011-09-06 19:07:46 +0000715 setOperationAction(ISD::SETCC, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000716 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
717 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
718 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
719 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
720 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
721 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
722 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
723 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
724 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000725 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000726 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
727 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
728 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
729 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
730 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
731 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
732 setTruncStoreAction((MVT::SimpleValueType)VT,
733 (MVT::SimpleValueType)InnerVT, Expand);
734 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
735 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
736 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000737 }
738
Evan Chengc7ce29b2009-02-13 22:36:38 +0000739 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
740 // with -msoft-float, disable use of MMX as well.
Chris Lattner2a786eb2010-12-19 20:19:20 +0000741 if (!UseSoftFloat && Subtarget->hasMMX()) {
Dale Johannesene93d99c2010-10-20 21:32:10 +0000742 addRegisterClass(MVT::x86mmx, X86::VR64RegisterClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000743 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000744 }
745
Dale Johannesen0488fb62010-09-30 23:57:10 +0000746 // MMX-sized vectors (other than x86mmx) are expected to be expanded
747 // into smaller operations.
748 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
749 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
750 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
751 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
752 setOperationAction(ISD::AND, MVT::v8i8, Expand);
753 setOperationAction(ISD::AND, MVT::v4i16, Expand);
754 setOperationAction(ISD::AND, MVT::v2i32, Expand);
755 setOperationAction(ISD::AND, MVT::v1i64, Expand);
756 setOperationAction(ISD::OR, MVT::v8i8, Expand);
757 setOperationAction(ISD::OR, MVT::v4i16, Expand);
758 setOperationAction(ISD::OR, MVT::v2i32, Expand);
759 setOperationAction(ISD::OR, MVT::v1i64, Expand);
760 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
761 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
762 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
763 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
764 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
765 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
766 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
767 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
768 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
769 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
770 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
771 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
772 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000773 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
774 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
775 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
776 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000777
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000778 if (!UseSoftFloat && Subtarget->hasXMM()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000779 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000780
Owen Anderson825b72b2009-08-11 20:47:22 +0000781 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
782 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
783 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
784 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
785 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
786 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
787 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
788 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
789 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
790 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
791 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000792 setOperationAction(ISD::SETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000793 }
794
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000795 if (!UseSoftFloat && Subtarget->hasXMMInt()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000796 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000797
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000798 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
799 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000800 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
801 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
802 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
803 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000804
Owen Anderson825b72b2009-08-11 20:47:22 +0000805 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
806 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
807 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
808 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
809 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
810 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
811 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
812 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
813 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
814 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
815 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
816 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
817 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
818 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
819 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
820 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000821
Duncan Sands28b77e92011-09-06 19:07:46 +0000822 setOperationAction(ISD::SETCC, MVT::v2f64, Custom);
823 setOperationAction(ISD::SETCC, MVT::v16i8, Custom);
824 setOperationAction(ISD::SETCC, MVT::v8i16, Custom);
825 setOperationAction(ISD::SETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000826
Owen Anderson825b72b2009-08-11 20:47:22 +0000827 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
828 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
829 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
830 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
831 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000832
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000833 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
834 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
835 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
836 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
837 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
838
Evan Cheng2c3ae372006-04-12 21:21:57 +0000839 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000840 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
841 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000842 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000843 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000844 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000845 // Do not attempt to custom lower non-128-bit vectors
846 if (!VT.is128BitVector())
847 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000848 setOperationAction(ISD::BUILD_VECTOR,
849 VT.getSimpleVT().SimpleTy, Custom);
850 setOperationAction(ISD::VECTOR_SHUFFLE,
851 VT.getSimpleVT().SimpleTy, Custom);
852 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
853 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000854 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000855
Owen Anderson825b72b2009-08-11 20:47:22 +0000856 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
857 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
858 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
859 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
860 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
861 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000862
Nate Begemancdd1eec2008-02-12 22:51:28 +0000863 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000864 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
865 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000866 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000867
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000868 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000869 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
870 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000871 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000872
873 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000874 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000875 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000876
Owen Andersond6662ad2009-08-10 20:46:15 +0000877 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000878 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000879 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000880 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000881 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000882 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000883 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000884 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000885 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000886 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000887 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000888
Owen Anderson825b72b2009-08-11 20:47:22 +0000889 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000890
Evan Cheng2c3ae372006-04-12 21:21:57 +0000891 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000892 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
893 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
894 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
895 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000896
Owen Anderson825b72b2009-08-11 20:47:22 +0000897 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
898 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000899 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000900
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000901 if (Subtarget->hasSSE41() || Subtarget->hasAVX()) {
Dale Johannesen54feef22010-05-27 20:12:41 +0000902 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
903 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
904 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
905 setOperationAction(ISD::FRINT, MVT::f32, Legal);
906 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
907 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
908 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
909 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
910 setOperationAction(ISD::FRINT, MVT::f64, Legal);
911 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
912
Nate Begeman14d12ca2008-02-11 04:19:36 +0000913 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000914 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000915
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000916 // Can turn SHL into an integer multiply.
917 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
Nate Begeman51409212010-07-28 00:21:48 +0000918 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000919
Nadav Rotemffe3e7d2011-09-08 08:11:19 +0000920 setOperationAction(ISD::VSELECT, MVT::v2f64, Custom);
921 setOperationAction(ISD::VSELECT, MVT::v2i64, Custom);
922 setOperationAction(ISD::VSELECT, MVT::v16i8, Custom);
923 setOperationAction(ISD::VSELECT, MVT::v8i16, Custom);
924 setOperationAction(ISD::VSELECT, MVT::v4i32, Custom);
925 setOperationAction(ISD::VSELECT, MVT::v4f32, Custom);
926
Nate Begeman14d12ca2008-02-11 04:19:36 +0000927 // i8 and i16 vectors are custom , because the source register and source
928 // source memory operand types are not the same width. f32 vectors are
929 // custom since the immediate controlling the insert encodes additional
930 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000931 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
932 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
933 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
934 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000935
Owen Anderson825b72b2009-08-11 20:47:22 +0000936 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
937 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
938 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
939 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000940
941 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000942 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
943 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000944 }
945 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000946
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000947 if (Subtarget->hasSSE2() || Subtarget->hasAVX()) {
Nadav Rotem43012222011-05-11 08:12:09 +0000948 setOperationAction(ISD::SRL, MVT::v2i64, Custom);
949 setOperationAction(ISD::SRL, MVT::v4i32, Custom);
950 setOperationAction(ISD::SRL, MVT::v16i8, Custom);
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000951 setOperationAction(ISD::SRL, MVT::v8i16, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +0000952
953 setOperationAction(ISD::SHL, MVT::v2i64, Custom);
954 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
955 setOperationAction(ISD::SHL, MVT::v8i16, Custom);
956
957 setOperationAction(ISD::SRA, MVT::v4i32, Custom);
958 setOperationAction(ISD::SRA, MVT::v8i16, Custom);
959 }
960
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000961 if (Subtarget->hasSSE42() || Subtarget->hasAVX())
Duncan Sands28b77e92011-09-06 19:07:46 +0000962 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000963
David Greene9b9838d2009-06-29 16:47:10 +0000964 if (!UseSoftFloat && Subtarget->hasAVX()) {
Bruno Cardoso Lopesdbd4fe22011-07-21 02:24:08 +0000965 addRegisterClass(MVT::v32i8, X86::VR256RegisterClass);
966 addRegisterClass(MVT::v16i16, X86::VR256RegisterClass);
967 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
968 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
969 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
970 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000971
Owen Anderson825b72b2009-08-11 20:47:22 +0000972 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +0000973 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
974 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +0000975
Owen Anderson825b72b2009-08-11 20:47:22 +0000976 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
977 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
978 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
979 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
980 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
981 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000982
Owen Anderson825b72b2009-08-11 20:47:22 +0000983 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
984 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
985 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
986 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
987 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
988 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000989
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +0000990 setOperationAction(ISD::FP_TO_SINT, MVT::v8i32, Legal);
991 setOperationAction(ISD::SINT_TO_FP, MVT::v8i32, Legal);
Bruno Cardoso Lopes55244ce2011-08-01 21:54:09 +0000992 setOperationAction(ISD::FP_ROUND, MVT::v4f32, Legal);
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +0000993
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +0000994 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4f64, Custom);
995 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i64, Custom);
996 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8f32, Custom);
997 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i32, Custom);
998 setOperationAction(ISD::CONCAT_VECTORS, MVT::v32i8, Custom);
999 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i16, Custom);
1000
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001001 setOperationAction(ISD::SRL, MVT::v4i64, Custom);
1002 setOperationAction(ISD::SRL, MVT::v8i32, Custom);
1003 setOperationAction(ISD::SRL, MVT::v16i16, Custom);
1004 setOperationAction(ISD::SRL, MVT::v32i8, Custom);
1005
1006 setOperationAction(ISD::SHL, MVT::v4i64, Custom);
1007 setOperationAction(ISD::SHL, MVT::v8i32, Custom);
1008 setOperationAction(ISD::SHL, MVT::v16i16, Custom);
1009 setOperationAction(ISD::SHL, MVT::v32i8, Custom);
1010
1011 setOperationAction(ISD::SRA, MVT::v8i32, Custom);
1012 setOperationAction(ISD::SRA, MVT::v16i16, Custom);
1013
Duncan Sands28b77e92011-09-06 19:07:46 +00001014 setOperationAction(ISD::SETCC, MVT::v32i8, Custom);
1015 setOperationAction(ISD::SETCC, MVT::v16i16, Custom);
1016 setOperationAction(ISD::SETCC, MVT::v8i32, Custom);
1017 setOperationAction(ISD::SETCC, MVT::v4i64, Custom);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00001018
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +00001019 setOperationAction(ISD::SELECT, MVT::v4f64, Custom);
1020 setOperationAction(ISD::SELECT, MVT::v4i64, Custom);
1021 setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
1022
Craig Topper13894fa2011-08-24 06:14:18 +00001023 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
1024 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
1025 setOperationAction(ISD::ADD, MVT::v16i16, Custom);
1026 setOperationAction(ISD::ADD, MVT::v32i8, Custom);
1027
1028 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
1029 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
1030 setOperationAction(ISD::SUB, MVT::v16i16, Custom);
1031 setOperationAction(ISD::SUB, MVT::v32i8, Custom);
1032
1033 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1034 setOperationAction(ISD::MUL, MVT::v8i32, Custom);
1035 setOperationAction(ISD::MUL, MVT::v16i16, Custom);
1036 // Don't lower v32i8 because there is no 128-bit byte mul
1037
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001038 // Custom lower several nodes for 256-bit types.
David Greene54d8eba2011-01-27 22:38:56 +00001039 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001040 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
1041 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
1042 EVT VT = SVT;
1043
1044 // Extract subvector is special because the value type
1045 // (result) is 128-bit but the source is 256-bit wide.
1046 if (VT.is128BitVector())
1047 setOperationAction(ISD::EXTRACT_SUBVECTOR, SVT, Custom);
1048
1049 // Do not attempt to custom lower other non-256-bit vectors
1050 if (!VT.is256BitVector())
David Greene9b9838d2009-06-29 16:47:10 +00001051 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001052
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001053 setOperationAction(ISD::BUILD_VECTOR, SVT, Custom);
1054 setOperationAction(ISD::VECTOR_SHUFFLE, SVT, Custom);
1055 setOperationAction(ISD::INSERT_VECTOR_ELT, SVT, Custom);
1056 setOperationAction(ISD::EXTRACT_VECTOR_ELT, SVT, Custom);
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00001057 setOperationAction(ISD::SCALAR_TO_VECTOR, SVT, Custom);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001058 setOperationAction(ISD::INSERT_SUBVECTOR, SVT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001059 }
1060
David Greene54d8eba2011-01-27 22:38:56 +00001061 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001062 for (unsigned i = (unsigned)MVT::v32i8; i != (unsigned)MVT::v4i64; ++i) {
1063 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
1064 EVT VT = SVT;
David Greene54d8eba2011-01-27 22:38:56 +00001065
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001066 // Do not attempt to promote non-256-bit vectors
1067 if (!VT.is256BitVector())
David Greene54d8eba2011-01-27 22:38:56 +00001068 continue;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001069
1070 setOperationAction(ISD::AND, SVT, Promote);
1071 AddPromotedToType (ISD::AND, SVT, MVT::v4i64);
1072 setOperationAction(ISD::OR, SVT, Promote);
1073 AddPromotedToType (ISD::OR, SVT, MVT::v4i64);
1074 setOperationAction(ISD::XOR, SVT, Promote);
1075 AddPromotedToType (ISD::XOR, SVT, MVT::v4i64);
1076 setOperationAction(ISD::LOAD, SVT, Promote);
1077 AddPromotedToType (ISD::LOAD, SVT, MVT::v4i64);
1078 setOperationAction(ISD::SELECT, SVT, Promote);
1079 AddPromotedToType (ISD::SELECT, SVT, MVT::v4i64);
David Greene54d8eba2011-01-27 22:38:56 +00001080 }
David Greene9b9838d2009-06-29 16:47:10 +00001081 }
1082
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001083 // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
1084 // of this type with custom code.
1085 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1086 VT != (unsigned)MVT::LAST_VECTOR_VALUETYPE; VT++) {
1087 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT, Custom);
1088 }
1089
Evan Cheng6be2c582006-04-05 23:38:46 +00001090 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001091 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001092
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001093
Eli Friedman962f5492010-06-02 19:35:46 +00001094 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1095 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001096 //
Eli Friedman962f5492010-06-02 19:35:46 +00001097 // FIXME: We really should do custom legalization for addition and
1098 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1099 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001100 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1101 // Add/Sub/Mul with overflow operations are custom lowered.
1102 MVT VT = IntVTs[i];
1103 setOperationAction(ISD::SADDO, VT, Custom);
1104 setOperationAction(ISD::UADDO, VT, Custom);
1105 setOperationAction(ISD::SSUBO, VT, Custom);
1106 setOperationAction(ISD::USUBO, VT, Custom);
1107 setOperationAction(ISD::SMULO, VT, Custom);
1108 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001109 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001110
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001111 // There are no 8-bit 3-address imul/mul instructions
1112 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1113 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001114
Evan Chengd54f2d52009-03-31 19:38:51 +00001115 if (!Subtarget->is64Bit()) {
1116 // These libcalls are not available in 32-bit.
1117 setLibcallName(RTLIB::SHL_I128, 0);
1118 setLibcallName(RTLIB::SRL_I128, 0);
1119 setLibcallName(RTLIB::SRA_I128, 0);
1120 }
1121
Evan Cheng206ee9d2006-07-07 08:33:52 +00001122 // We have target-specific dag combine patterns for the following nodes:
1123 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001124 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +00001125 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +00001126 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001127 setTargetDAGCombine(ISD::SHL);
1128 setTargetDAGCombine(ISD::SRA);
1129 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001130 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001131 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001132 setTargetDAGCombine(ISD::ADD);
1133 setTargetDAGCombine(ISD::SUB);
Chris Lattner149a4e52008-02-22 02:09:43 +00001134 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001135 setTargetDAGCombine(ISD::ZERO_EXTEND);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001136 setTargetDAGCombine(ISD::SINT_TO_FP);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001137 if (Subtarget->is64Bit())
1138 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001139
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001140 computeRegisterProperties();
1141
Evan Cheng05219282011-01-06 06:52:41 +00001142 // On Darwin, -Os means optimize for size without hurting performance,
1143 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001144 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001145 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001146 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001147 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1148 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1149 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengfb8075d2008-02-28 00:43:03 +00001150 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001151 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001152
1153 setPrefFunctionAlignment(4);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001154}
1155
Scott Michel5b8f82e2008-03-10 15:42:14 +00001156
Duncan Sands28b77e92011-09-06 19:07:46 +00001157EVT X86TargetLowering::getSetCCResultType(EVT VT) const {
1158 if (!VT.isVector()) return MVT::i8;
1159 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +00001160}
1161
1162
Evan Cheng29286502008-01-23 23:17:41 +00001163/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1164/// the desired ByVal argument alignment.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001165static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
Evan Cheng29286502008-01-23 23:17:41 +00001166 if (MaxAlign == 16)
1167 return;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001168 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001169 if (VTy->getBitWidth() == 128)
1170 MaxAlign = 16;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001171 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001172 unsigned EltAlign = 0;
1173 getMaxByValAlign(ATy->getElementType(), EltAlign);
1174 if (EltAlign > MaxAlign)
1175 MaxAlign = EltAlign;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001176 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001177 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1178 unsigned EltAlign = 0;
1179 getMaxByValAlign(STy->getElementType(i), EltAlign);
1180 if (EltAlign > MaxAlign)
1181 MaxAlign = EltAlign;
1182 if (MaxAlign == 16)
1183 break;
1184 }
1185 }
1186 return;
1187}
1188
1189/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1190/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001191/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1192/// are at 4-byte boundaries.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001193unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001194 if (Subtarget->is64Bit()) {
1195 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001196 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001197 if (TyAlign > 8)
1198 return TyAlign;
1199 return 8;
1200 }
1201
Evan Cheng29286502008-01-23 23:17:41 +00001202 unsigned Align = 4;
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001203 if (Subtarget->hasXMM())
Dale Johannesen0c191872008-02-08 19:48:20 +00001204 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001205 return Align;
1206}
Chris Lattner2b02a442007-02-25 08:29:00 +00001207
Evan Chengf0df0312008-05-15 08:39:06 +00001208/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001209/// and store operations as a result of memset, memcpy, and memmove
1210/// lowering. If DstAlign is zero that means it's safe to destination
1211/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1212/// means there isn't a need to check it against alignment requirement,
1213/// probably because the source does not need to be loaded. If
1214/// 'NonScalarIntSafe' is true, that means it's safe to return a
1215/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1216/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1217/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001218/// It returns EVT::Other if the type should be determined using generic
1219/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001220EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001221X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1222 unsigned DstAlign, unsigned SrcAlign,
Evan Chengf28f8bc2010-04-02 19:36:14 +00001223 bool NonScalarIntSafe,
Evan Chengc3b0c342010-04-08 07:37:57 +00001224 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001225 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001226 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1227 // linux. This is because the stack realignment code can't handle certain
1228 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001229 const Function *F = MF.getFunction();
Evan Chenga5e13622011-01-07 19:35:30 +00001230 if (NonScalarIntSafe &&
1231 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001232 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001233 (Subtarget->isUnalignedMemAccessFast() ||
1234 ((DstAlign == 0 || DstAlign >= 16) &&
1235 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001236 Subtarget->getStackAlignment() >= 16) {
1237 if (Subtarget->hasSSE2())
1238 return MVT::v4i32;
Evan Chengf28f8bc2010-04-02 19:36:14 +00001239 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001240 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001241 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001242 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001243 Subtarget->getStackAlignment() >= 8 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001244 Subtarget->hasXMMInt()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001245 // Do not use f64 to lower memcpy if source is string constant. It's
1246 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001247 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001248 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001249 }
Evan Chengf0df0312008-05-15 08:39:06 +00001250 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001251 return MVT::i64;
1252 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001253}
1254
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001255/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1256/// current function. The returned value is a member of the
1257/// MachineJumpTableInfo::JTEntryKind enum.
1258unsigned X86TargetLowering::getJumpTableEncoding() const {
1259 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1260 // symbol.
1261 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1262 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001263 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001264
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001265 // Otherwise, use the normal jump table encoding heuristics.
1266 return TargetLowering::getJumpTableEncoding();
1267}
1268
Chris Lattnerc64daab2010-01-26 05:02:42 +00001269const MCExpr *
1270X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1271 const MachineBasicBlock *MBB,
1272 unsigned uid,MCContext &Ctx) const{
1273 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1274 Subtarget->isPICStyleGOT());
1275 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1276 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001277 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1278 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001279}
1280
Evan Chengcc415862007-11-09 01:32:10 +00001281/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1282/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001283SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001284 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001285 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001286 // This doesn't have DebugLoc associated with it, but is not really the
1287 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001288 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001289 return Table;
1290}
1291
Chris Lattner589c6f62010-01-26 06:28:43 +00001292/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1293/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1294/// MCExpr.
1295const MCExpr *X86TargetLowering::
1296getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1297 MCContext &Ctx) const {
1298 // X86-64 uses RIP relative addressing based on the jump table label.
1299 if (Subtarget->isPICStyleRIPRel())
1300 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1301
1302 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001303 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001304}
1305
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001306// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001307std::pair<const TargetRegisterClass*, uint8_t>
1308X86TargetLowering::findRepresentativeClass(EVT VT) const{
1309 const TargetRegisterClass *RRC = 0;
1310 uint8_t Cost = 1;
1311 switch (VT.getSimpleVT().SimpleTy) {
1312 default:
1313 return TargetLowering::findRepresentativeClass(VT);
1314 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
1315 RRC = (Subtarget->is64Bit()
1316 ? X86::GR64RegisterClass : X86::GR32RegisterClass);
1317 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001318 case MVT::x86mmx:
Evan Chengdee81012010-07-26 21:50:05 +00001319 RRC = X86::VR64RegisterClass;
1320 break;
1321 case MVT::f32: case MVT::f64:
1322 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1323 case MVT::v4f32: case MVT::v2f64:
1324 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1325 case MVT::v4f64:
1326 RRC = X86::VR128RegisterClass;
1327 break;
1328 }
1329 return std::make_pair(RRC, Cost);
1330}
1331
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001332bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1333 unsigned &Offset) const {
1334 if (!Subtarget->isTargetLinux())
1335 return false;
1336
1337 if (Subtarget->is64Bit()) {
1338 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1339 Offset = 0x28;
1340 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1341 AddressSpace = 256;
1342 else
1343 AddressSpace = 257;
1344 } else {
1345 // %gs:0x14 on i386
1346 Offset = 0x14;
1347 AddressSpace = 256;
1348 }
1349 return true;
1350}
1351
1352
Chris Lattner2b02a442007-02-25 08:29:00 +00001353//===----------------------------------------------------------------------===//
1354// Return Value Calling Convention Implementation
1355//===----------------------------------------------------------------------===//
1356
Chris Lattner59ed56b2007-02-28 04:55:35 +00001357#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001358
Michael J. Spencerec38de22010-10-10 22:04:20 +00001359bool
Eric Christopher471e4222011-06-08 23:55:35 +00001360X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
1361 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001362 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001363 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001364 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001365 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001366 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001367 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001368}
1369
Dan Gohman98ca4f22009-08-05 01:29:28 +00001370SDValue
1371X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001372 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001373 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001374 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001375 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001376 MachineFunction &MF = DAG.getMachineFunction();
1377 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001378
Chris Lattner9774c912007-02-27 05:28:59 +00001379 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001380 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001381 RVLocs, *DAG.getContext());
1382 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001383
Evan Chengdcea1632010-02-04 02:40:39 +00001384 // Add the regs to the liveout set for the function.
1385 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1386 for (unsigned i = 0; i != RVLocs.size(); ++i)
1387 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1388 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001389
Dan Gohman475871a2008-07-27 21:46:04 +00001390 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001391
Dan Gohman475871a2008-07-27 21:46:04 +00001392 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001393 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1394 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001395 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1396 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001397
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001398 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001399 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1400 CCValAssign &VA = RVLocs[i];
1401 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001402 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001403 EVT ValVT = ValToCopy.getValueType();
1404
Dale Johannesenc4510512010-09-24 19:05:48 +00001405 // If this is x86-64, and we disabled SSE, we can't return FP values,
1406 // or SSE or MMX vectors.
1407 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1408 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001409 (Subtarget->is64Bit() && !Subtarget->hasXMM())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001410 report_fatal_error("SSE register return with SSE disabled");
1411 }
1412 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1413 // llvm-gcc has never done it right and no one has noticed, so this
1414 // should be OK for now.
1415 if (ValVT == MVT::f64 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001416 (Subtarget->is64Bit() && !Subtarget->hasXMMInt()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001417 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001418
Chris Lattner447ff682008-03-11 03:23:40 +00001419 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1420 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001421 if (VA.getLocReg() == X86::ST0 ||
1422 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001423 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1424 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001425 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001426 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001427 RetOps.push_back(ValToCopy);
1428 // Don't emit a copytoreg.
1429 continue;
1430 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001431
Evan Cheng242b38b2009-02-23 09:03:22 +00001432 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1433 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001434 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001435 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001436 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001437 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001438 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1439 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001440 // If we don't have SSE2 available, convert to v4f32 so the generated
1441 // register is legal.
1442 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001443 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001444 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001445 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001446 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001447
Dale Johannesendd64c412009-02-04 00:33:20 +00001448 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001449 Flag = Chain.getValue(1);
1450 }
Dan Gohman61a92132008-04-21 23:59:07 +00001451
1452 // The x86-64 ABI for returning structs by value requires that we copy
1453 // the sret argument into %rax for the return. We saved the argument into
1454 // a virtual register in the entry block, so now we copy the value out
1455 // and into %rax.
1456 if (Subtarget->is64Bit() &&
1457 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1458 MachineFunction &MF = DAG.getMachineFunction();
1459 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1460 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001461 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001462 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001463 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001464
Dale Johannesendd64c412009-02-04 00:33:20 +00001465 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001466 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001467
1468 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001469 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001470 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001471
Chris Lattner447ff682008-03-11 03:23:40 +00001472 RetOps[0] = Chain; // Update chain.
1473
1474 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001475 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001476 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001477
1478 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001479 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001480}
1481
Evan Cheng3d2125c2010-11-30 23:55:39 +00001482bool X86TargetLowering::isUsedByReturnOnly(SDNode *N) const {
1483 if (N->getNumValues() != 1)
1484 return false;
1485 if (!N->hasNUsesOfValue(1, 0))
1486 return false;
1487
1488 SDNode *Copy = *N->use_begin();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001489 if (Copy->getOpcode() != ISD::CopyToReg &&
1490 Copy->getOpcode() != ISD::FP_EXTEND)
Evan Cheng3d2125c2010-11-30 23:55:39 +00001491 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001492
1493 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001494 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001495 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001496 if (UI->getOpcode() != X86ISD::RET_FLAG)
1497 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001498 HasRet = true;
1499 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001500
Evan Cheng1bf891a2010-12-01 22:59:46 +00001501 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001502}
1503
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001504EVT
1505X86TargetLowering::getTypeForExtArgOrReturn(LLVMContext &Context, EVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001506 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001507 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001508 // TODO: Is this also valid on 32-bit?
1509 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001510 ReturnMVT = MVT::i8;
1511 else
1512 ReturnMVT = MVT::i32;
1513
1514 EVT MinVT = getRegisterType(Context, ReturnMVT);
1515 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001516}
1517
Dan Gohman98ca4f22009-08-05 01:29:28 +00001518/// LowerCallResult - Lower the result values of a call into the
1519/// appropriate copies out of appropriate physical registers.
1520///
1521SDValue
1522X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001523 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001524 const SmallVectorImpl<ISD::InputArg> &Ins,
1525 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001526 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001527
Chris Lattnere32bbf62007-02-28 07:09:55 +00001528 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001529 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001530 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001531 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1532 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001533 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001534
Chris Lattner3085e152007-02-25 08:59:22 +00001535 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001536 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001537 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001538 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001539
Torok Edwin3f142c32009-02-01 18:15:56 +00001540 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001541 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001542 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasXMM())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001543 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001544 }
1545
Evan Cheng79fb3b42009-02-20 20:43:02 +00001546 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001547
1548 // If this is a call to a function that returns an fp value on the floating
1549 // point stack, we must guarantee the the value is popped from the stack, so
1550 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001551 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001552 // instead.
1553 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1554 // If we prefer to use the value in xmm registers, copy it out as f80 and
1555 // use a truncate to move it from fp stack reg to xmm reg.
1556 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001557 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001558 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1559 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001560 Val = Chain.getValue(0);
1561
1562 // Round the f80 to the right size, which also moves it to the appropriate
1563 // xmm register.
1564 if (CopyVT != VA.getValVT())
1565 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1566 // This truncation won't change the value.
1567 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001568 } else {
1569 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1570 CopyVT, InFlag).getValue(1);
1571 Val = Chain.getValue(0);
1572 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001573 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001574 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001575 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001576
Dan Gohman98ca4f22009-08-05 01:29:28 +00001577 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001578}
1579
1580
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001581//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001582// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001583//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001584// StdCall calling convention seems to be standard for many Windows' API
1585// routines and around. It differs from C calling convention just a little:
1586// callee should clean up the stack, not caller. Symbols should be also
1587// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001588// For info on fast calling convention see Fast Calling Convention (tail call)
1589// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001590
Dan Gohman98ca4f22009-08-05 01:29:28 +00001591/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001592/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001593static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1594 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001595 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001596
Dan Gohman98ca4f22009-08-05 01:29:28 +00001597 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001598}
1599
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001600/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001601/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001602static bool
1603ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1604 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001605 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001606
Dan Gohman98ca4f22009-08-05 01:29:28 +00001607 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001608}
1609
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001610/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1611/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001612/// the specific parameter attribute. The copy will be passed as a byval
1613/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001614static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001615CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001616 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1617 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001618 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001619
Dale Johannesendd64c412009-02-04 00:33:20 +00001620 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001621 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001622 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001623}
1624
Chris Lattner29689432010-03-11 00:22:57 +00001625/// IsTailCallConvention - Return true if the calling convention is one that
1626/// supports tail call optimization.
1627static bool IsTailCallConvention(CallingConv::ID CC) {
1628 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1629}
1630
Evan Cheng485fafc2011-03-21 01:19:09 +00001631bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1632 if (!CI->isTailCall())
1633 return false;
1634
1635 CallSite CS(CI);
1636 CallingConv::ID CalleeCC = CS.getCallingConv();
1637 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1638 return false;
1639
1640 return true;
1641}
1642
Evan Cheng0c439eb2010-01-27 00:07:07 +00001643/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1644/// a tailcall target by changing its ABI.
1645static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001646 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001647}
1648
Dan Gohman98ca4f22009-08-05 01:29:28 +00001649SDValue
1650X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001651 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001652 const SmallVectorImpl<ISD::InputArg> &Ins,
1653 DebugLoc dl, SelectionDAG &DAG,
1654 const CCValAssign &VA,
1655 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001656 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001657 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001658 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001659 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001660 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001661 EVT ValVT;
1662
1663 // If value is passed by pointer we have address passed instead of the value
1664 // itself.
1665 if (VA.getLocInfo() == CCValAssign::Indirect)
1666 ValVT = VA.getLocVT();
1667 else
1668 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001669
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001670 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001671 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001672 // In case of tail call optimization mark all arguments mutable. Since they
1673 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001674 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001675 unsigned Bytes = Flags.getByValSize();
1676 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1677 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001678 return DAG.getFrameIndex(FI, getPointerTy());
1679 } else {
1680 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001681 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001682 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1683 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001684 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00001685 false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001686 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001687}
1688
Dan Gohman475871a2008-07-27 21:46:04 +00001689SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001690X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001691 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001692 bool isVarArg,
1693 const SmallVectorImpl<ISD::InputArg> &Ins,
1694 DebugLoc dl,
1695 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001696 SmallVectorImpl<SDValue> &InVals)
1697 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001698 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001699 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001700
Gordon Henriksen86737662008-01-05 16:56:59 +00001701 const Function* Fn = MF.getFunction();
1702 if (Fn->hasExternalLinkage() &&
1703 Subtarget->isTargetCygMing() &&
1704 Fn->getName() == "main")
1705 FuncInfo->setForceFramePointer(true);
1706
Evan Cheng1bc78042006-04-26 01:20:17 +00001707 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001708 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001709 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001710
Chris Lattner29689432010-03-11 00:22:57 +00001711 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1712 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001713
Chris Lattner638402b2007-02-28 07:00:42 +00001714 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001715 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001716 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001717 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001718
1719 // Allocate shadow area for Win64
1720 if (IsWin64) {
1721 CCInfo.AllocateStack(32, 8);
1722 }
1723
Duncan Sands45907662010-10-31 13:21:44 +00001724 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001725
Chris Lattnerf39f7712007-02-28 05:46:49 +00001726 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001727 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001728 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1729 CCValAssign &VA = ArgLocs[i];
1730 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1731 // places.
1732 assert(VA.getValNo() != LastVal &&
1733 "Don't support value assigned to multiple locs yet");
1734 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001735
Chris Lattnerf39f7712007-02-28 05:46:49 +00001736 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001737 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001738 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001739 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001740 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001741 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001742 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001743 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001744 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001745 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001746 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001747 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1748 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001749 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001750 RC = X86::VR128RegisterClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001751 else if (RegVT == MVT::x86mmx)
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001752 RC = X86::VR64RegisterClass;
1753 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001754 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001755
Devang Patel68e6bee2011-02-21 23:21:26 +00001756 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001757 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001758
Chris Lattnerf39f7712007-02-28 05:46:49 +00001759 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1760 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1761 // right size.
1762 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001763 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001764 DAG.getValueType(VA.getValVT()));
1765 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001766 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001767 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001768 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001769 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001770
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001771 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001772 // Handle MMX values passed in XMM regs.
1773 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001774 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1775 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001776 } else
1777 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001778 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001779 } else {
1780 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001781 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001782 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001783
1784 // If value is passed via pointer - do a load.
1785 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001786 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
1787 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001788
Dan Gohman98ca4f22009-08-05 01:29:28 +00001789 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001790 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001791
Dan Gohman61a92132008-04-21 23:59:07 +00001792 // The x86-64 ABI for returning structs by value requires that we copy
1793 // the sret argument into %rax for the return. Save the argument into
1794 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001795 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001796 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1797 unsigned Reg = FuncInfo->getSRetReturnReg();
1798 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001799 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001800 FuncInfo->setSRetReturnReg(Reg);
1801 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001802 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001803 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001804 }
1805
Chris Lattnerf39f7712007-02-28 05:46:49 +00001806 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001807 // Align stack specially for tail calls.
1808 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001809 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001810
Evan Cheng1bc78042006-04-26 01:20:17 +00001811 // If the function takes variable number of arguments, make a frame index for
1812 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001813 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001814 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1815 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001816 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001817 }
1818 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001819 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1820
1821 // FIXME: We should really autogenerate these arrays
1822 static const unsigned GPR64ArgRegsWin64[] = {
1823 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001824 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001825 static const unsigned GPR64ArgRegs64Bit[] = {
1826 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1827 };
1828 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001829 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1830 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1831 };
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001832 const unsigned *GPR64ArgRegs;
1833 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001834
1835 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001836 // The XMM registers which might contain var arg parameters are shadowed
1837 // in their paired GPR. So we only need to save the GPR to their home
1838 // slots.
1839 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001840 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001841 } else {
1842 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1843 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001844
1845 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit, TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001846 }
1847 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1848 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001849
Devang Patel578efa92009-06-05 21:57:13 +00001850 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001851 assert(!(NumXMMRegs && !Subtarget->hasXMM()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001852 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001853 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001854 "SSE register cannot be used when SSE is disabled!");
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001855 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasXMM())
Torok Edwin3f142c32009-02-01 18:15:56 +00001856 // Kernel mode asks for SSE to be disabled, so don't push them
1857 // on the stack.
1858 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001859
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001860 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001861 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001862 // Get to the caller-allocated home save location. Add 8 to account
1863 // for the return address.
1864 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001865 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001866 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001867 // Fixup to set vararg frame on shadow area (4 x i64).
1868 if (NumIntRegs < 4)
1869 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001870 } else {
1871 // For X86-64, if there are vararg parameters that are passed via
1872 // registers, then we must store them to their spots on the stack so they
1873 // may be loaded by deferencing the result of va_next.
1874 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1875 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1876 FuncInfo->setRegSaveFrameIndex(
1877 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00001878 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001879 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001880
Gordon Henriksen86737662008-01-05 16:56:59 +00001881 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001882 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001883 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1884 getPointerTy());
1885 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001886 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001887 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1888 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001889 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001890 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001891 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001892 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001893 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001894 MachinePointerInfo::getFixedStack(
1895 FuncInfo->getRegSaveFrameIndex(), Offset),
1896 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001897 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001898 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001899 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001900
Dan Gohmanface41a2009-08-16 21:24:25 +00001901 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1902 // Now store the XMM (fp + vector) parameter registers.
1903 SmallVector<SDValue, 11> SaveXMMOps;
1904 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001905
Devang Patel68e6bee2011-02-21 23:21:26 +00001906 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001907 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1908 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001909
Dan Gohman1e93df62010-04-17 14:41:14 +00001910 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1911 FuncInfo->getRegSaveFrameIndex()));
1912 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1913 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001914
Dan Gohmanface41a2009-08-16 21:24:25 +00001915 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001916 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001917 X86::VR128RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001918 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1919 SaveXMMOps.push_back(Val);
1920 }
1921 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1922 MVT::Other,
1923 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001924 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001925
1926 if (!MemOps.empty())
1927 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1928 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001929 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001930 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001931
Gordon Henriksen86737662008-01-05 16:56:59 +00001932 // Some CCs need callee pop.
Evan Chengef41ff62011-06-23 17:54:54 +00001933 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg, GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001934 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001935 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001936 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001937 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001938 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001939 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001940 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001941
Gordon Henriksen86737662008-01-05 16:56:59 +00001942 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001943 // RegSaveFrameIndex is X86-64 only.
1944 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001945 if (CallConv == CallingConv::X86_FastCall ||
1946 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001947 // fastcc functions can't have varargs.
1948 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001949 }
Evan Cheng25caf632006-05-23 21:06:34 +00001950
Rafael Espindola76927d752011-08-30 19:39:58 +00001951 FuncInfo->setArgumentStackSize(StackSize);
1952
Dan Gohman98ca4f22009-08-05 01:29:28 +00001953 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001954}
1955
Dan Gohman475871a2008-07-27 21:46:04 +00001956SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001957X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1958 SDValue StackPtr, SDValue Arg,
1959 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001960 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001961 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001962 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001963 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001964 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001965 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00001966 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001967
1968 return DAG.getStore(Chain, dl, Arg, PtrOff,
1969 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00001970 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001971}
1972
Bill Wendling64e87322009-01-16 19:25:27 +00001973/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001974/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001975SDValue
1976X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001977 SDValue &OutRetAddr, SDValue Chain,
1978 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00001979 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001980 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001981 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001982 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001983
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001984 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00001985 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
1986 false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001987 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001988}
1989
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001990/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001991/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001992static SDValue
1993EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001994 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001995 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001996 // Store the return address to the appropriate stack slot.
1997 if (!FPDiff) return Chain;
1998 // Calculate the new stack slot for the return address.
1999 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00002000 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00002001 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00002002 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00002003 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002004 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00002005 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00002006 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002007 return Chain;
2008}
2009
Dan Gohman98ca4f22009-08-05 01:29:28 +00002010SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00002011X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002012 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00002013 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002014 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002015 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002016 const SmallVectorImpl<ISD::InputArg> &Ins,
2017 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002018 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002019 MachineFunction &MF = DAG.getMachineFunction();
2020 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002021 bool IsWin64 = Subtarget->isTargetWin64();
Dan Gohman98ca4f22009-08-05 01:29:28 +00002022 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00002023 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002024
Evan Cheng5f941932010-02-05 02:21:12 +00002025 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002026 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00002027 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
2028 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00002029 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00002030
2031 // Sibcalls are automatically detected tailcalls which do not require
2032 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00002033 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002034 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002035
2036 if (isTailCall)
2037 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002038 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002039
Chris Lattner29689432010-03-11 00:22:57 +00002040 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
2041 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002042
Chris Lattner638402b2007-02-28 07:00:42 +00002043 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002044 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002045 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002046 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002047
2048 // Allocate shadow area for Win64
2049 if (IsWin64) {
2050 CCInfo.AllocateStack(32, 8);
2051 }
2052
Duncan Sands45907662010-10-31 13:21:44 +00002053 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002054
Chris Lattner423c5f42007-02-28 05:31:48 +00002055 // Get a count of how many bytes are to be pushed on the stack.
2056 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002057 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002058 // This is a sibcall. The memory operands are available in caller's
2059 // own caller's stack.
2060 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00002061 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002062 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002063
Gordon Henriksen86737662008-01-05 16:56:59 +00002064 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002065 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002066 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00002067 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00002068 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2069 FPDiff = NumBytesCallerPushed - NumBytes;
2070
2071 // Set the delta of movement of the returnaddr stackslot.
2072 // But only set if delta is greater than previous delta.
2073 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2074 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2075 }
2076
Evan Chengf22f9b32010-02-06 03:28:46 +00002077 if (!IsSibcall)
2078 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002079
Dan Gohman475871a2008-07-27 21:46:04 +00002080 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002081 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002082 if (isTailCall && FPDiff)
2083 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2084 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002085
Dan Gohman475871a2008-07-27 21:46:04 +00002086 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2087 SmallVector<SDValue, 8> MemOpChains;
2088 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002089
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002090 // Walk the register/memloc assignments, inserting copies/loads. In the case
2091 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002092 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2093 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002094 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002095 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002096 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002097 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002098
Chris Lattner423c5f42007-02-28 05:31:48 +00002099 // Promote the value if needed.
2100 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002101 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002102 case CCValAssign::Full: break;
2103 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002104 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002105 break;
2106 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002107 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002108 break;
2109 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002110 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
2111 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002112 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002113 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2114 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002115 } else
2116 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2117 break;
2118 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002119 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002120 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002121 case CCValAssign::Indirect: {
2122 // Store the argument.
2123 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002124 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002125 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002126 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002127 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002128 Arg = SpillSlot;
2129 break;
2130 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002131 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002132
Chris Lattner423c5f42007-02-28 05:31:48 +00002133 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002134 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2135 if (isVarArg && IsWin64) {
2136 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2137 // shadow reg if callee is a varargs function.
2138 unsigned ShadowReg = 0;
2139 switch (VA.getLocReg()) {
2140 case X86::XMM0: ShadowReg = X86::RCX; break;
2141 case X86::XMM1: ShadowReg = X86::RDX; break;
2142 case X86::XMM2: ShadowReg = X86::R8; break;
2143 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002144 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002145 if (ShadowReg)
2146 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002147 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002148 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002149 assert(VA.isMemLoc());
2150 if (StackPtr.getNode() == 0)
2151 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2152 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2153 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002154 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002155 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002156
Evan Cheng32fe1032006-05-25 00:59:30 +00002157 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002158 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002159 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002160
Evan Cheng347d5f72006-04-28 21:29:37 +00002161 // Build a sequence of copy-to-reg nodes chained together with token chain
2162 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002163 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002164 // Tail call byval lowering might overwrite argument registers so in case of
2165 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002166 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002167 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002168 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002169 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002170 InFlag = Chain.getValue(1);
2171 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002172
Chris Lattner88e1fd52009-07-09 04:24:46 +00002173 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002174 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2175 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002176 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002177 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2178 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00002179 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002180 InFlag);
2181 InFlag = Chain.getValue(1);
2182 } else {
2183 // If we are tail calling and generating PIC/GOT style code load the
2184 // address of the callee into ECX. The value in ecx is used as target of
2185 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2186 // for tail calls on PIC/GOT architectures. Normally we would just put the
2187 // address of GOT into ebx and then call target@PLT. But for tail calls
2188 // ebx would be restored (since ebx is callee saved) before jumping to the
2189 // target@PLT.
2190
2191 // Note: The actual moving to ECX is done further down.
2192 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2193 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2194 !G->getGlobal()->hasProtectedVisibility())
2195 Callee = LowerGlobalAddress(Callee, DAG);
2196 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002197 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002198 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002199 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002200
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002201 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002202 // From AMD64 ABI document:
2203 // For calls that may call functions that use varargs or stdargs
2204 // (prototype-less calls or calls to functions containing ellipsis (...) in
2205 // the declaration) %al is used as hidden argument to specify the number
2206 // of SSE registers used. The contents of %al do not need to match exactly
2207 // the number of registers, but must be an ubound on the number of SSE
2208 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002209
Gordon Henriksen86737662008-01-05 16:56:59 +00002210 // Count the number of XMM registers allocated.
2211 static const unsigned XMMArgRegs[] = {
2212 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2213 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2214 };
2215 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00002216 assert((Subtarget->hasXMM() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002217 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002218
Dale Johannesendd64c412009-02-04 00:33:20 +00002219 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002220 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002221 InFlag = Chain.getValue(1);
2222 }
2223
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002224
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002225 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002226 if (isTailCall) {
2227 // Force all the incoming stack arguments to be loaded from the stack
2228 // before any new outgoing arguments are stored to the stack, because the
2229 // outgoing stack slots may alias the incoming argument stack slots, and
2230 // the alias isn't otherwise explicit. This is slightly more conservative
2231 // than necessary, because it means that each store effectively depends
2232 // on every argument instead of just those arguments it would clobber.
2233 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2234
Dan Gohman475871a2008-07-27 21:46:04 +00002235 SmallVector<SDValue, 8> MemOpChains2;
2236 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002237 int FI = 0;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002238 // Do not flag preceding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002239 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002240 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002241 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2242 CCValAssign &VA = ArgLocs[i];
2243 if (VA.isRegLoc())
2244 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002245 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002246 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002247 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002248 // Create frame index.
2249 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002250 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002251 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002252 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002253
Duncan Sands276dcbd2008-03-21 09:14:45 +00002254 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002255 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002256 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002257 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002258 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002259 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002260 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002261
Dan Gohman98ca4f22009-08-05 01:29:28 +00002262 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2263 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002264 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002265 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002266 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002267 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002268 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002269 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002270 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002271 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002272 }
2273 }
2274
2275 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002276 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002277 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002278
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002279 // Copy arguments to their registers.
2280 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002281 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002282 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002283 InFlag = Chain.getValue(1);
2284 }
Dan Gohman475871a2008-07-27 21:46:04 +00002285 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002286
Gordon Henriksen86737662008-01-05 16:56:59 +00002287 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002288 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002289 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002290 }
2291
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002292 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2293 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2294 // In the 64-bit large code model, we have to make all calls
2295 // through a register, since the call instruction's 32-bit
2296 // pc-relative offset may not be large enough to hold the whole
2297 // address.
2298 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002299 // If the callee is a GlobalAddress node (quite common, every direct call
2300 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2301 // it.
2302
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002303 // We should use extra load for direct calls to dllimported functions in
2304 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002305 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002306 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002307 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002308 bool ExtraLoad = false;
2309 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002310
Chris Lattner48a7d022009-07-09 05:02:21 +00002311 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2312 // external symbols most go through the PLT in PIC mode. If the symbol
2313 // has hidden or protected visibility, or if it is static or local, then
2314 // we don't need to use the PLT - we can directly call it.
2315 if (Subtarget->isTargetELF() &&
2316 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002317 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002318 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002319 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002320 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002321 (!Subtarget->getTargetTriple().isMacOSX() ||
2322 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002323 // PC-relative references to external symbols should go through $stub,
2324 // unless we're building with the leopard linker or later, which
2325 // automatically synthesizes these stubs.
2326 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002327 } else if (Subtarget->isPICStyleRIPRel() &&
2328 isa<Function>(GV) &&
2329 cast<Function>(GV)->hasFnAttr(Attribute::NonLazyBind)) {
2330 // If the function is marked as non-lazy, generate an indirect call
2331 // which loads from the GOT directly. This avoids runtime overhead
2332 // at the cost of eager binding (and one extra byte of encoding).
2333 OpFlags = X86II::MO_GOTPCREL;
2334 WrapperKind = X86ISD::WrapperRIP;
2335 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002336 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002337
Devang Patel0d881da2010-07-06 22:08:15 +00002338 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002339 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002340
2341 // Add a wrapper if needed.
2342 if (WrapperKind != ISD::DELETED_NODE)
2343 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2344 // Add extra indirection if needed.
2345 if (ExtraLoad)
2346 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2347 MachinePointerInfo::getGOT(),
2348 false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002349 }
Bill Wendling056292f2008-09-16 21:48:12 +00002350 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002351 unsigned char OpFlags = 0;
2352
Evan Cheng1bf891a2010-12-01 22:59:46 +00002353 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2354 // external symbols should go through the PLT.
2355 if (Subtarget->isTargetELF() &&
2356 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2357 OpFlags = X86II::MO_PLT;
2358 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002359 (!Subtarget->getTargetTriple().isMacOSX() ||
2360 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002361 // PC-relative references to external symbols should go through $stub,
2362 // unless we're building with the leopard linker or later, which
2363 // automatically synthesizes these stubs.
2364 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002365 }
Eric Christopherfd179292009-08-27 18:07:15 +00002366
Chris Lattner48a7d022009-07-09 05:02:21 +00002367 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2368 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002369 }
2370
Chris Lattnerd96d0722007-02-25 06:40:16 +00002371 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002372 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002373 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002374
Evan Chengf22f9b32010-02-06 03:28:46 +00002375 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002376 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2377 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002378 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002379 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002380
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002381 Ops.push_back(Chain);
2382 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002383
Dan Gohman98ca4f22009-08-05 01:29:28 +00002384 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002385 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002386
Gordon Henriksen86737662008-01-05 16:56:59 +00002387 // Add argument registers to the end of the list so that they are known live
2388 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002389 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2390 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2391 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002392
Evan Cheng586ccac2008-03-18 23:36:35 +00002393 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002394 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002395 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2396
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002397 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002398 if (Is64Bit && isVarArg && !IsWin64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002399 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002400
Gabor Greifba36cb52008-08-28 21:40:38 +00002401 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002402 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002403
Dan Gohman98ca4f22009-08-05 01:29:28 +00002404 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002405 // We used to do:
2406 //// If this is the first return lowered for this function, add the regs
2407 //// to the liveout set for the function.
2408 // This isn't right, although it's probably harmless on x86; liveouts
2409 // should be computed from returns not tail calls. Consider a void
2410 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002411 return DAG.getNode(X86ISD::TC_RETURN, dl,
2412 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002413 }
2414
Dale Johannesenace16102009-02-03 19:33:06 +00002415 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002416 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002417
Chris Lattner2d297092006-05-23 18:50:38 +00002418 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002419 unsigned NumBytesForCalleeToPush;
Evan Chengef41ff62011-06-23 17:54:54 +00002420 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg, GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002421 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002422 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002423 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002424 // pops the hidden struct pointer, so we have to push it back.
2425 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002426 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002427 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002428 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002429
Gordon Henriksenae636f82008-01-03 16:47:34 +00002430 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002431 if (!IsSibcall) {
2432 Chain = DAG.getCALLSEQ_END(Chain,
2433 DAG.getIntPtrConstant(NumBytes, true),
2434 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2435 true),
2436 InFlag);
2437 InFlag = Chain.getValue(1);
2438 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002439
Chris Lattner3085e152007-02-25 08:59:22 +00002440 // Handle result values, copying them out of physregs into vregs that we
2441 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002442 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2443 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002444}
2445
Evan Cheng25ab6902006-09-08 06:48:29 +00002446
2447//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002448// Fast Calling Convention (tail call) implementation
2449//===----------------------------------------------------------------------===//
2450
2451// Like std call, callee cleans arguments, convention except that ECX is
2452// reserved for storing the tail called function address. Only 2 registers are
2453// free for argument passing (inreg). Tail call optimization is performed
2454// provided:
2455// * tailcallopt is enabled
2456// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002457// On X86_64 architecture with GOT-style position independent code only local
2458// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002459// To keep the stack aligned according to platform abi the function
2460// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2461// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002462// If a tail called function callee has more arguments than the caller the
2463// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002464// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002465// original REtADDR, but before the saved framepointer or the spilled registers
2466// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2467// stack layout:
2468// arg1
2469// arg2
2470// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002471// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002472// move area ]
2473// (possible EBP)
2474// ESI
2475// EDI
2476// local1 ..
2477
2478/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2479/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002480unsigned
2481X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2482 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002483 MachineFunction &MF = DAG.getMachineFunction();
2484 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002485 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002486 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002487 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002488 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002489 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002490 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2491 // Number smaller than 12 so just add the difference.
2492 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2493 } else {
2494 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002495 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002496 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002497 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002498 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002499}
2500
Evan Cheng5f941932010-02-05 02:21:12 +00002501/// MatchingStackOffset - Return true if the given stack call argument is
2502/// already available in the same position (relatively) of the caller's
2503/// incoming argument stack.
2504static
2505bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2506 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2507 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002508 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2509 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002510 if (Arg.getOpcode() == ISD::CopyFromReg) {
2511 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002512 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002513 return false;
2514 MachineInstr *Def = MRI->getVRegDef(VR);
2515 if (!Def)
2516 return false;
2517 if (!Flags.isByVal()) {
2518 if (!TII->isLoadFromStackSlot(Def, FI))
2519 return false;
2520 } else {
2521 unsigned Opcode = Def->getOpcode();
2522 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2523 Def->getOperand(1).isFI()) {
2524 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002525 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002526 } else
2527 return false;
2528 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002529 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2530 if (Flags.isByVal())
2531 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002532 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002533 // define @foo(%struct.X* %A) {
2534 // tail call @bar(%struct.X* byval %A)
2535 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002536 return false;
2537 SDValue Ptr = Ld->getBasePtr();
2538 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2539 if (!FINode)
2540 return false;
2541 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002542 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002543 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002544 FI = FINode->getIndex();
2545 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002546 } else
2547 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002548
Evan Cheng4cae1332010-03-05 08:38:04 +00002549 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002550 if (!MFI->isFixedObjectIndex(FI))
2551 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002552 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002553}
2554
Dan Gohman98ca4f22009-08-05 01:29:28 +00002555/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2556/// for tail call optimization. Targets which want to do tail call
2557/// optimization should implement this function.
2558bool
2559X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002560 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002561 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002562 bool isCalleeStructRet,
2563 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002564 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002565 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002566 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002567 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002568 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002569 CalleeCC != CallingConv::C)
2570 return false;
2571
Evan Cheng7096ae42010-01-29 06:45:59 +00002572 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002573 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002574 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002575 CallingConv::ID CallerCC = CallerF->getCallingConv();
2576 bool CCMatch = CallerCC == CalleeCC;
2577
Dan Gohman1797ed52010-02-08 20:27:50 +00002578 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002579 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002580 return true;
2581 return false;
2582 }
2583
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002584 // Look for obvious safe cases to perform tail call optimization that do not
2585 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002586
Evan Cheng2c12cb42010-03-26 16:26:03 +00002587 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2588 // emit a special epilogue.
2589 if (RegInfo->needsStackRealignment(MF))
2590 return false;
2591
Evan Chenga375d472010-03-15 18:54:48 +00002592 // Also avoid sibcall optimization if either caller or callee uses struct
2593 // return semantics.
2594 if (isCalleeStructRet || isCallerStructRet)
2595 return false;
2596
Chad Rosier2416da32011-06-24 21:15:36 +00002597 // An stdcall caller is expected to clean up its arguments; the callee
2598 // isn't going to do that.
2599 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2600 return false;
2601
Chad Rosier871f6642011-05-18 19:59:50 +00002602 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002603 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002604 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002605
2606 // Optimizing for varargs on Win64 is unlikely to be safe without
2607 // additional testing.
2608 if (Subtarget->isTargetWin64())
2609 return false;
2610
Chad Rosier871f6642011-05-18 19:59:50 +00002611 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002612 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2613 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002614
Chad Rosier871f6642011-05-18 19:59:50 +00002615 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2616 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2617 if (!ArgLocs[i].isRegLoc())
2618 return false;
2619 }
2620
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002621 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2622 // Therefore if it's not used by the call it is not safe to optimize this into
2623 // a sibcall.
2624 bool Unused = false;
2625 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2626 if (!Ins[i].Used) {
2627 Unused = true;
2628 break;
2629 }
2630 }
2631 if (Unused) {
2632 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002633 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
2634 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002635 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002636 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002637 CCValAssign &VA = RVLocs[i];
2638 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2639 return false;
2640 }
2641 }
2642
Evan Cheng13617962010-04-30 01:12:32 +00002643 // If the calling conventions do not match, then we'd better make sure the
2644 // results are returned in the same way as what the caller expects.
2645 if (!CCMatch) {
2646 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002647 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
2648 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002649 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2650
2651 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002652 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
2653 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002654 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2655
2656 if (RVLocs1.size() != RVLocs2.size())
2657 return false;
2658 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2659 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2660 return false;
2661 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2662 return false;
2663 if (RVLocs1[i].isRegLoc()) {
2664 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2665 return false;
2666 } else {
2667 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2668 return false;
2669 }
2670 }
2671 }
2672
Evan Chenga6bff982010-01-30 01:22:00 +00002673 // If the callee takes no arguments then go on to check the results of the
2674 // call.
2675 if (!Outs.empty()) {
2676 // Check if stack adjustment is needed. For now, do not do this if any
2677 // argument is passed on the stack.
2678 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002679 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2680 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002681
2682 // Allocate shadow area for Win64
2683 if (Subtarget->isTargetWin64()) {
2684 CCInfo.AllocateStack(32, 8);
2685 }
2686
Duncan Sands45907662010-10-31 13:21:44 +00002687 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002688 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002689 MachineFunction &MF = DAG.getMachineFunction();
2690 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2691 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002692
2693 // Check if the arguments are already laid out in the right way as
2694 // the caller's fixed stack objects.
2695 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002696 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2697 const X86InstrInfo *TII =
2698 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002699 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2700 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002701 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002702 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002703 if (VA.getLocInfo() == CCValAssign::Indirect)
2704 return false;
2705 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002706 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2707 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002708 return false;
2709 }
2710 }
2711 }
Evan Cheng9c044672010-05-29 01:35:22 +00002712
2713 // If the tailcall address may be in a register, then make sure it's
2714 // possible to register allocate for it. In 32-bit, the call address can
2715 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002716 // callee-saved registers are restored. These happen to be the same
2717 // registers used to pass 'inreg' arguments so watch out for those.
2718 if (!Subtarget->is64Bit() &&
2719 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002720 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002721 unsigned NumInRegs = 0;
2722 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2723 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002724 if (!VA.isRegLoc())
2725 continue;
2726 unsigned Reg = VA.getLocReg();
2727 switch (Reg) {
2728 default: break;
2729 case X86::EAX: case X86::EDX: case X86::ECX:
2730 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002731 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002732 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002733 }
2734 }
2735 }
Evan Chenga6bff982010-01-30 01:22:00 +00002736 }
Evan Chengb1712452010-01-27 06:25:16 +00002737
Evan Cheng86809cc2010-02-03 03:28:02 +00002738 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002739}
2740
Dan Gohman3df24e62008-09-03 23:12:08 +00002741FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002742X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2743 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002744}
2745
2746
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002747//===----------------------------------------------------------------------===//
2748// Other Lowering Hooks
2749//===----------------------------------------------------------------------===//
2750
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002751static bool MayFoldLoad(SDValue Op) {
2752 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2753}
2754
2755static bool MayFoldIntoStore(SDValue Op) {
2756 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2757}
2758
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002759static bool isTargetShuffle(unsigned Opcode) {
2760 switch(Opcode) {
2761 default: return false;
2762 case X86ISD::PSHUFD:
2763 case X86ISD::PSHUFHW:
2764 case X86ISD::PSHUFLW:
2765 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002766 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002767 case X86ISD::SHUFPS:
2768 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002769 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002770 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002771 case X86ISD::MOVLPS:
2772 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002773 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002774 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002775 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002776 case X86ISD::MOVSS:
2777 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002778 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002779 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002780 case X86ISD::VUNPCKLPSY:
2781 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002782 case X86ISD::PUNPCKLWD:
2783 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002784 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002785 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002786 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002787 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00002788 case X86ISD::VUNPCKHPSY:
2789 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002790 case X86ISD::PUNPCKHWD:
2791 case X86ISD::PUNPCKHBW:
2792 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002793 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00002794 case X86ISD::VPERMILPS:
2795 case X86ISD::VPERMILPSY:
2796 case X86ISD::VPERMILPD:
2797 case X86ISD::VPERMILPDY:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00002798 case X86ISD::VPERM2F128:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002799 return true;
2800 }
2801 return false;
2802}
2803
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002804static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002805 SDValue V1, SelectionDAG &DAG) {
2806 switch(Opc) {
2807 default: llvm_unreachable("Unknown x86 shuffle node");
2808 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002809 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002810 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002811 return DAG.getNode(Opc, dl, VT, V1);
2812 }
2813
2814 return SDValue();
2815}
2816
2817static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002818 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002819 switch(Opc) {
2820 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002821 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002822 case X86ISD::PSHUFHW:
2823 case X86ISD::PSHUFLW:
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00002824 case X86ISD::VPERMILPS:
2825 case X86ISD::VPERMILPSY:
2826 case X86ISD::VPERMILPD:
2827 case X86ISD::VPERMILPDY:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002828 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2829 }
2830
2831 return SDValue();
2832}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002833
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002834static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2835 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2836 switch(Opc) {
2837 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002838 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002839 case X86ISD::SHUFPD:
2840 case X86ISD::SHUFPS:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00002841 case X86ISD::VPERM2F128:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002842 return DAG.getNode(Opc, dl, VT, V1, V2,
2843 DAG.getConstant(TargetMask, MVT::i8));
2844 }
2845 return SDValue();
2846}
2847
2848static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2849 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2850 switch(Opc) {
2851 default: llvm_unreachable("Unknown x86 shuffle node");
2852 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002853 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002854 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002855 case X86ISD::MOVLPS:
2856 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002857 case X86ISD::MOVSS:
2858 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002859 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002860 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002861 case X86ISD::VUNPCKLPSY:
2862 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002863 case X86ISD::PUNPCKLWD:
2864 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002865 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002866 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002867 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002868 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00002869 case X86ISD::VUNPCKHPSY:
2870 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002871 case X86ISD::PUNPCKHWD:
2872 case X86ISD::PUNPCKHBW:
2873 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002874 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002875 return DAG.getNode(Opc, dl, VT, V1, V2);
2876 }
2877 return SDValue();
2878}
2879
Dan Gohmand858e902010-04-17 15:26:15 +00002880SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002881 MachineFunction &MF = DAG.getMachineFunction();
2882 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2883 int ReturnAddrIndex = FuncInfo->getRAIndex();
2884
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002885 if (ReturnAddrIndex == 0) {
2886 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002887 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002888 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002889 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002890 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002891 }
2892
Evan Cheng25ab6902006-09-08 06:48:29 +00002893 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002894}
2895
2896
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002897bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2898 bool hasSymbolicDisplacement) {
2899 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002900 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002901 return false;
2902
2903 // If we don't have a symbolic displacement - we don't have any extra
2904 // restrictions.
2905 if (!hasSymbolicDisplacement)
2906 return true;
2907
2908 // FIXME: Some tweaks might be needed for medium code model.
2909 if (M != CodeModel::Small && M != CodeModel::Kernel)
2910 return false;
2911
2912 // For small code model we assume that latest object is 16MB before end of 31
2913 // bits boundary. We may also accept pretty large negative constants knowing
2914 // that all objects are in the positive half of address space.
2915 if (M == CodeModel::Small && Offset < 16*1024*1024)
2916 return true;
2917
2918 // For kernel code model we know that all object resist in the negative half
2919 // of 32bits address space. We may not accept negative offsets, since they may
2920 // be just off and we may accept pretty large positive ones.
2921 if (M == CodeModel::Kernel && Offset > 0)
2922 return true;
2923
2924 return false;
2925}
2926
Evan Chengef41ff62011-06-23 17:54:54 +00002927/// isCalleePop - Determines whether the callee is required to pop its
2928/// own arguments. Callee pop is necessary to support tail calls.
2929bool X86::isCalleePop(CallingConv::ID CallingConv,
2930 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
2931 if (IsVarArg)
2932 return false;
2933
2934 switch (CallingConv) {
2935 default:
2936 return false;
2937 case CallingConv::X86_StdCall:
2938 return !is64Bit;
2939 case CallingConv::X86_FastCall:
2940 return !is64Bit;
2941 case CallingConv::X86_ThisCall:
2942 return !is64Bit;
2943 case CallingConv::Fast:
2944 return TailCallOpt;
2945 case CallingConv::GHC:
2946 return TailCallOpt;
2947 }
2948}
2949
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002950/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2951/// specific condition code, returning the condition code and the LHS/RHS of the
2952/// comparison to make.
2953static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2954 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002955 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002956 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2957 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2958 // X > -1 -> X == 0, jump !sign.
2959 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002960 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002961 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2962 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002963 return X86::COND_S;
Andrew Trickf6c39412011-03-23 23:11:02 +00002964 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002965 // X < 1 -> X <= 0
2966 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002967 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002968 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002969 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002970
Evan Chengd9558e02006-01-06 00:43:03 +00002971 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002972 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002973 case ISD::SETEQ: return X86::COND_E;
2974 case ISD::SETGT: return X86::COND_G;
2975 case ISD::SETGE: return X86::COND_GE;
2976 case ISD::SETLT: return X86::COND_L;
2977 case ISD::SETLE: return X86::COND_LE;
2978 case ISD::SETNE: return X86::COND_NE;
2979 case ISD::SETULT: return X86::COND_B;
2980 case ISD::SETUGT: return X86::COND_A;
2981 case ISD::SETULE: return X86::COND_BE;
2982 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002983 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002984 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002985
Chris Lattner4c78e022008-12-23 23:42:27 +00002986 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002987
Chris Lattner4c78e022008-12-23 23:42:27 +00002988 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00002989 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
2990 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00002991 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2992 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002993 }
2994
Chris Lattner4c78e022008-12-23 23:42:27 +00002995 switch (SetCCOpcode) {
2996 default: break;
2997 case ISD::SETOLT:
2998 case ISD::SETOLE:
2999 case ISD::SETUGT:
3000 case ISD::SETUGE:
3001 std::swap(LHS, RHS);
3002 break;
3003 }
3004
3005 // On a floating point condition, the flags are set as follows:
3006 // ZF PF CF op
3007 // 0 | 0 | 0 | X > Y
3008 // 0 | 0 | 1 | X < Y
3009 // 1 | 0 | 0 | X == Y
3010 // 1 | 1 | 1 | unordered
3011 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003012 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00003013 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003014 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00003015 case ISD::SETOLT: // flipped
3016 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003017 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00003018 case ISD::SETOLE: // flipped
3019 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003020 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003021 case ISD::SETUGT: // flipped
3022 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003023 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00003024 case ISD::SETUGE: // flipped
3025 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003026 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003027 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003028 case ISD::SETNE: return X86::COND_NE;
3029 case ISD::SETUO: return X86::COND_P;
3030 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003031 case ISD::SETOEQ:
3032 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003033 }
Evan Chengd9558e02006-01-06 00:43:03 +00003034}
3035
Evan Cheng4a460802006-01-11 00:33:36 +00003036/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3037/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003038/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003039static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003040 switch (X86CC) {
3041 default:
3042 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003043 case X86::COND_B:
3044 case X86::COND_BE:
3045 case X86::COND_E:
3046 case X86::COND_P:
3047 case X86::COND_A:
3048 case X86::COND_AE:
3049 case X86::COND_NE:
3050 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003051 return true;
3052 }
3053}
3054
Evan Chengeb2f9692009-10-27 19:56:55 +00003055/// isFPImmLegal - Returns true if the target can instruction select the
3056/// specified FP immediate natively. If false, the legalizer will
3057/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003058bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003059 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3060 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3061 return true;
3062 }
3063 return false;
3064}
3065
Nate Begeman9008ca62009-04-27 18:41:29 +00003066/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3067/// the specified range (L, H].
3068static bool isUndefOrInRange(int Val, int Low, int Hi) {
3069 return (Val < 0) || (Val >= Low && Val < Hi);
3070}
3071
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00003072/// isUndefOrInRange - Return true if every element in Mask, begining
3073/// from position Pos and ending in Pos+Size, falls within the specified
3074/// range (L, L+Pos]. or is undef.
3075static bool isUndefOrInRange(const SmallVectorImpl<int> &Mask,
3076 int Pos, int Size, int Low, int Hi) {
3077 for (int i = Pos, e = Pos+Size; i != e; ++i)
3078 if (!isUndefOrInRange(Mask[i], Low, Hi))
3079 return false;
3080 return true;
3081}
3082
Nate Begeman9008ca62009-04-27 18:41:29 +00003083/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3084/// specified value.
3085static bool isUndefOrEqual(int Val, int CmpVal) {
3086 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003087 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003088 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00003089}
3090
Bruno Cardoso Lopes4002d7e2011-08-12 21:54:42 +00003091/// isSequentialOrUndefInRange - Return true if every element in Mask, begining
3092/// from position Pos and ending in Pos+Size, falls within the specified
3093/// sequential range (L, L+Pos]. or is undef.
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003094static bool isSequentialOrUndefInRange(const SmallVectorImpl<int> &Mask,
3095 int Pos, int Size, int Low) {
3096 for (int i = Pos, e = Pos+Size; i != e; ++i, ++Low)
3097 if (!isUndefOrEqual(Mask[i], Low))
3098 return false;
3099 return true;
3100}
3101
Nate Begeman9008ca62009-04-27 18:41:29 +00003102/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3103/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3104/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00003105static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003106 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003107 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003108 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003109 return (Mask[0] < 2 && Mask[1] < 2);
3110 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003111}
3112
Nate Begeman9008ca62009-04-27 18:41:29 +00003113bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003114 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003115 N->getMask(M);
3116 return ::isPSHUFDMask(M, N->getValueType(0));
3117}
Evan Cheng0188ecb2006-03-22 18:59:22 +00003118
Nate Begeman9008ca62009-04-27 18:41:29 +00003119/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3120/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00003121static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003122 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00003123 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003124
Nate Begeman9008ca62009-04-27 18:41:29 +00003125 // Lower quadword copied in order or undef.
3126 for (int i = 0; i != 4; ++i)
3127 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00003128 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003129
Evan Cheng506d3df2006-03-29 23:07:14 +00003130 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003131 for (int i = 4; i != 8; ++i)
3132 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00003133 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003134
Evan Cheng506d3df2006-03-29 23:07:14 +00003135 return true;
3136}
3137
Nate Begeman9008ca62009-04-27 18:41:29 +00003138bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003139 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003140 N->getMask(M);
3141 return ::isPSHUFHWMask(M, N->getValueType(0));
3142}
Evan Cheng506d3df2006-03-29 23:07:14 +00003143
Nate Begeman9008ca62009-04-27 18:41:29 +00003144/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3145/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00003146static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003147 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00003148 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003149
Rafael Espindola15684b22009-04-24 12:40:33 +00003150 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00003151 for (int i = 4; i != 8; ++i)
3152 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00003153 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003154
Rafael Espindola15684b22009-04-24 12:40:33 +00003155 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003156 for (int i = 0; i != 4; ++i)
3157 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00003158 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003159
Rafael Espindola15684b22009-04-24 12:40:33 +00003160 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003161}
3162
Nate Begeman9008ca62009-04-27 18:41:29 +00003163bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003164 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003165 N->getMask(M);
3166 return ::isPSHUFLWMask(M, N->getValueType(0));
3167}
3168
Nate Begemana09008b2009-10-19 02:17:23 +00003169/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3170/// is suitable for input to PALIGNR.
3171static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
3172 bool hasSSSE3) {
3173 int i, e = VT.getVectorNumElements();
Bruno Cardoso Lopes9065d4b2011-07-29 01:30:59 +00003174 if (VT.getSizeInBits() != 128 && VT.getSizeInBits() != 64)
3175 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003176
Nate Begemana09008b2009-10-19 02:17:23 +00003177 // Do not handle v2i64 / v2f64 shuffles with palignr.
3178 if (e < 4 || !hasSSSE3)
3179 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003180
Nate Begemana09008b2009-10-19 02:17:23 +00003181 for (i = 0; i != e; ++i)
3182 if (Mask[i] >= 0)
3183 break;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003184
Nate Begemana09008b2009-10-19 02:17:23 +00003185 // All undef, not a palignr.
3186 if (i == e)
3187 return false;
3188
Eli Friedman63f8dde2011-07-25 21:36:45 +00003189 // Make sure we're shifting in the right direction.
3190 if (Mask[i] <= i)
3191 return false;
Nate Begemana09008b2009-10-19 02:17:23 +00003192
3193 int s = Mask[i] - i;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003194
Nate Begemana09008b2009-10-19 02:17:23 +00003195 // Check the rest of the elements to see if they are consecutive.
3196 for (++i; i != e; ++i) {
3197 int m = Mask[i];
Eli Friedman63f8dde2011-07-25 21:36:45 +00003198 if (m >= 0 && m != s+i)
Nate Begemana09008b2009-10-19 02:17:23 +00003199 return false;
3200 }
3201 return true;
3202}
3203
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003204/// isVSHUFPSYMask - Return true if the specified VECTOR_SHUFFLE operand
3205/// specifies a shuffle of elements that is suitable for input to 256-bit
3206/// VSHUFPSY.
3207static bool isVSHUFPSYMask(const SmallVectorImpl<int> &Mask, EVT VT,
3208 const X86Subtarget *Subtarget) {
3209 int NumElems = VT.getVectorNumElements();
3210
3211 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256)
3212 return false;
3213
3214 if (NumElems != 8)
3215 return false;
3216
3217 // VSHUFPSY divides the resulting vector into 4 chunks.
3218 // The sources are also splitted into 4 chunks, and each destination
3219 // chunk must come from a different source chunk.
3220 //
3221 // SRC1 => X7 X6 X5 X4 X3 X2 X1 X0
3222 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3223 //
3224 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3225 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3226 //
3227 int QuarterSize = NumElems/4;
3228 int HalfSize = QuarterSize*2;
3229 for (int i = 0; i < QuarterSize; ++i)
3230 if (!isUndefOrInRange(Mask[i], 0, HalfSize))
3231 return false;
3232 for (int i = QuarterSize; i < QuarterSize*2; ++i)
3233 if (!isUndefOrInRange(Mask[i], NumElems, NumElems+HalfSize))
3234 return false;
3235
3236 // The mask of the second half must be the same as the first but with
3237 // the appropriate offsets. This works in the same way as VPERMILPS
3238 // works with masks.
3239 for (int i = QuarterSize*2; i < QuarterSize*3; ++i) {
3240 if (!isUndefOrInRange(Mask[i], HalfSize, NumElems))
3241 return false;
3242 int FstHalfIdx = i-HalfSize;
3243 if (Mask[FstHalfIdx] < 0)
3244 continue;
3245 if (!isUndefOrEqual(Mask[i], Mask[FstHalfIdx]+HalfSize))
3246 return false;
3247 }
3248 for (int i = QuarterSize*3; i < NumElems; ++i) {
3249 if (!isUndefOrInRange(Mask[i], NumElems+HalfSize, NumElems*2))
3250 return false;
3251 int FstHalfIdx = i-HalfSize;
3252 if (Mask[FstHalfIdx] < 0)
3253 continue;
3254 if (!isUndefOrEqual(Mask[i], Mask[FstHalfIdx]+HalfSize))
3255 return false;
3256
3257 }
3258
3259 return true;
3260}
3261
3262/// getShuffleVSHUFPSYImmediate - Return the appropriate immediate to shuffle
3263/// the specified VECTOR_MASK mask with VSHUFPSY instruction.
3264static unsigned getShuffleVSHUFPSYImmediate(SDNode *N) {
3265 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3266 EVT VT = SVOp->getValueType(0);
3267 int NumElems = VT.getVectorNumElements();
3268
3269 assert(NumElems == 8 && VT.getSizeInBits() == 256 &&
3270 "Only supports v8i32 and v8f32 types");
3271
3272 int HalfSize = NumElems/2;
3273 unsigned Mask = 0;
3274 for (int i = 0; i != NumElems ; ++i) {
3275 if (SVOp->getMaskElt(i) < 0)
3276 continue;
3277 // The mask of the first half must be equal to the second one.
3278 unsigned Shamt = (i%HalfSize)*2;
3279 unsigned Elt = SVOp->getMaskElt(i) % HalfSize;
3280 Mask |= Elt << Shamt;
3281 }
3282
3283 return Mask;
3284}
3285
3286/// isVSHUFPDYMask - Return true if the specified VECTOR_SHUFFLE operand
3287/// specifies a shuffle of elements that is suitable for input to 256-bit
3288/// VSHUFPDY. This shuffle doesn't have the same restriction as the PS
3289/// version and the mask of the second half isn't binded with the first
3290/// one.
3291static bool isVSHUFPDYMask(const SmallVectorImpl<int> &Mask, EVT VT,
3292 const X86Subtarget *Subtarget) {
3293 int NumElems = VT.getVectorNumElements();
3294
3295 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256)
3296 return false;
3297
3298 if (NumElems != 4)
3299 return false;
3300
3301 // VSHUFPSY divides the resulting vector into 4 chunks.
3302 // The sources are also splitted into 4 chunks, and each destination
3303 // chunk must come from a different source chunk.
3304 //
3305 // SRC1 => X3 X2 X1 X0
3306 // SRC2 => Y3 Y2 Y1 Y0
3307 //
3308 // DST => Y2..Y3, X2..X3, Y1..Y0, X1..X0
3309 //
3310 int QuarterSize = NumElems/4;
3311 int HalfSize = QuarterSize*2;
3312 for (int i = 0; i < QuarterSize; ++i)
3313 if (!isUndefOrInRange(Mask[i], 0, HalfSize))
3314 return false;
3315 for (int i = QuarterSize; i < QuarterSize*2; ++i)
3316 if (!isUndefOrInRange(Mask[i], NumElems, NumElems+HalfSize))
3317 return false;
3318 for (int i = QuarterSize*2; i < QuarterSize*3; ++i)
3319 if (!isUndefOrInRange(Mask[i], HalfSize, NumElems))
3320 return false;
3321 for (int i = QuarterSize*3; i < NumElems; ++i)
3322 if (!isUndefOrInRange(Mask[i], NumElems+HalfSize, NumElems*2))
3323 return false;
3324
3325 return true;
3326}
3327
3328/// getShuffleVSHUFPDYImmediate - Return the appropriate immediate to shuffle
3329/// the specified VECTOR_MASK mask with VSHUFPDY instruction.
3330static unsigned getShuffleVSHUFPDYImmediate(SDNode *N) {
3331 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3332 EVT VT = SVOp->getValueType(0);
3333 int NumElems = VT.getVectorNumElements();
3334
3335 assert(NumElems == 4 && VT.getSizeInBits() == 256 &&
3336 "Only supports v4i64 and v4f64 types");
3337
3338 int HalfSize = NumElems/2;
3339 unsigned Mask = 0;
3340 for (int i = 0; i != NumElems ; ++i) {
3341 if (SVOp->getMaskElt(i) < 0)
3342 continue;
3343 int Elt = SVOp->getMaskElt(i) % HalfSize;
3344 Mask |= Elt << i;
3345 }
3346
3347 return Mask;
3348}
3349
Evan Cheng14aed5e2006-03-24 01:18:28 +00003350/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopesaf002d82011-08-24 23:17:55 +00003351/// specifies a shuffle of elements that is suitable for input to 128-bit
3352/// SHUFPS and SHUFPD.
Owen Andersone50ed302009-08-10 22:56:29 +00003353static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003354 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesaf002d82011-08-24 23:17:55 +00003355
3356 if (VT.getSizeInBits() != 128)
3357 return false;
3358
Nate Begeman9008ca62009-04-27 18:41:29 +00003359 if (NumElems != 2 && NumElems != 4)
3360 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003361
Nate Begeman9008ca62009-04-27 18:41:29 +00003362 int Half = NumElems / 2;
3363 for (int i = 0; i < Half; ++i)
3364 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003365 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003366 for (int i = Half; i < NumElems; ++i)
3367 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003368 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003369
Evan Cheng14aed5e2006-03-24 01:18:28 +00003370 return true;
3371}
3372
Nate Begeman9008ca62009-04-27 18:41:29 +00003373bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
3374 SmallVector<int, 8> M;
3375 N->getMask(M);
3376 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003377}
3378
Evan Cheng213d2cf2007-05-17 18:45:50 +00003379/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00003380/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
3381/// half elements to come from vector 1 (which would equal the dest.) and
3382/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00003383static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003384 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003385
3386 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00003387 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003388
Nate Begeman9008ca62009-04-27 18:41:29 +00003389 int Half = NumElems / 2;
3390 for (int i = 0; i < Half; ++i)
3391 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003392 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003393 for (int i = Half; i < NumElems; ++i)
3394 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003395 return false;
3396 return true;
3397}
3398
Nate Begeman9008ca62009-04-27 18:41:29 +00003399static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
3400 SmallVector<int, 8> M;
3401 N->getMask(M);
3402 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003403}
3404
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003405/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3406/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00003407bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003408 EVT VT = N->getValueType(0);
3409 unsigned NumElems = VT.getVectorNumElements();
3410
3411 if (VT.getSizeInBits() != 128)
3412 return false;
3413
3414 if (NumElems != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003415 return false;
3416
Evan Cheng2064a2b2006-03-28 06:50:32 +00003417 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00003418 return isUndefOrEqual(N->getMaskElt(0), 6) &&
3419 isUndefOrEqual(N->getMaskElt(1), 7) &&
3420 isUndefOrEqual(N->getMaskElt(2), 2) &&
3421 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003422}
3423
Nate Begeman0b10b912009-11-07 23:17:15 +00003424/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3425/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3426/// <2, 3, 2, 3>
3427bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003428 EVT VT = N->getValueType(0);
3429 unsigned NumElems = VT.getVectorNumElements();
3430
3431 if (VT.getSizeInBits() != 128)
3432 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003433
Nate Begeman0b10b912009-11-07 23:17:15 +00003434 if (NumElems != 4)
3435 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003436
Nate Begeman0b10b912009-11-07 23:17:15 +00003437 return isUndefOrEqual(N->getMaskElt(0), 2) &&
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003438 isUndefOrEqual(N->getMaskElt(1), 3) &&
3439 isUndefOrEqual(N->getMaskElt(2), 2) &&
3440 isUndefOrEqual(N->getMaskElt(3), 3);
Nate Begeman0b10b912009-11-07 23:17:15 +00003441}
3442
Evan Cheng5ced1d82006-04-06 23:23:56 +00003443/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3444/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003445bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3446 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003447
Evan Cheng5ced1d82006-04-06 23:23:56 +00003448 if (NumElems != 2 && NumElems != 4)
3449 return false;
3450
Evan Chengc5cdff22006-04-07 21:53:05 +00003451 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003452 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003453 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003454
Evan Chengc5cdff22006-04-07 21:53:05 +00003455 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003456 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003457 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003458
3459 return true;
3460}
3461
Nate Begeman0b10b912009-11-07 23:17:15 +00003462/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3463/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3464bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003465 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003466
David Greenea20244d2011-03-02 17:23:43 +00003467 if ((NumElems != 2 && NumElems != 4)
3468 || N->getValueType(0).getSizeInBits() > 128)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003469 return false;
3470
Evan Chengc5cdff22006-04-07 21:53:05 +00003471 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003472 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003473 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003474
Nate Begeman9008ca62009-04-27 18:41:29 +00003475 for (unsigned i = 0; i < NumElems/2; ++i)
3476 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003477 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003478
3479 return true;
3480}
3481
Evan Cheng0038e592006-03-28 00:39:58 +00003482/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3483/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003484static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003485 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003486 int NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003487
3488 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3489 "Unsupported vector type for unpckh");
3490
3491 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8)
Evan Cheng0038e592006-03-28 00:39:58 +00003492 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003493
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003494 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3495 // independently on 128-bit lanes.
3496 unsigned NumLanes = VT.getSizeInBits()/128;
3497 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003498
3499 unsigned Start = 0;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003500 unsigned End = NumLaneElts;
3501 for (unsigned s = 0; s < NumLanes; ++s) {
3502 for (unsigned i = Start, j = s * NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003503 i != End;
3504 i += 2, ++j) {
3505 int BitI = Mask[i];
3506 int BitI1 = Mask[i+1];
3507 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003508 return false;
David Greenea20244d2011-03-02 17:23:43 +00003509 if (V2IsSplat) {
3510 if (!isUndefOrEqual(BitI1, NumElts))
3511 return false;
3512 } else {
3513 if (!isUndefOrEqual(BitI1, j + NumElts))
3514 return false;
3515 }
Evan Cheng39623da2006-04-20 08:58:49 +00003516 }
David Greenea20244d2011-03-02 17:23:43 +00003517 // Process the next 128 bits.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003518 Start += NumLaneElts;
3519 End += NumLaneElts;
Evan Cheng0038e592006-03-28 00:39:58 +00003520 }
David Greenea20244d2011-03-02 17:23:43 +00003521
Evan Cheng0038e592006-03-28 00:39:58 +00003522 return true;
3523}
3524
Nate Begeman9008ca62009-04-27 18:41:29 +00003525bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3526 SmallVector<int, 8> M;
3527 N->getMask(M);
3528 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003529}
3530
Evan Cheng4fcb9222006-03-28 02:43:26 +00003531/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3532/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003533static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003534 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003535 int NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003536
3537 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3538 "Unsupported vector type for unpckh");
3539
3540 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8)
Evan Cheng4fcb9222006-03-28 02:43:26 +00003541 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003542
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003543 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3544 // independently on 128-bit lanes.
3545 unsigned NumLanes = VT.getSizeInBits()/128;
3546 unsigned NumLaneElts = NumElts/NumLanes;
3547
3548 unsigned Start = 0;
3549 unsigned End = NumLaneElts;
3550 for (unsigned l = 0; l != NumLanes; ++l) {
3551 for (unsigned i = Start, j = (l*NumLaneElts)+NumLaneElts/2;
3552 i != End; i += 2, ++j) {
3553 int BitI = Mask[i];
3554 int BitI1 = Mask[i+1];
3555 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003556 return false;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003557 if (V2IsSplat) {
3558 if (isUndefOrEqual(BitI1, NumElts))
3559 return false;
3560 } else {
3561 if (!isUndefOrEqual(BitI1, j+NumElts))
3562 return false;
3563 }
Evan Cheng39623da2006-04-20 08:58:49 +00003564 }
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003565 // Process the next 128 bits.
3566 Start += NumLaneElts;
3567 End += NumLaneElts;
Evan Cheng4fcb9222006-03-28 02:43:26 +00003568 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003569 return true;
3570}
3571
Nate Begeman9008ca62009-04-27 18:41:29 +00003572bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3573 SmallVector<int, 8> M;
3574 N->getMask(M);
3575 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003576}
3577
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003578/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3579/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3580/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003581static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003582 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003583 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003584 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003585
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003586 // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern
3587 // FIXME: Need a better way to get rid of this, there's no latency difference
3588 // between UNPCKLPD and MOVDDUP, the later should always be checked first and
3589 // the former later. We should also remove the "_undef" special mask.
3590 if (NumElems == 4 && VT.getSizeInBits() == 256)
3591 return false;
3592
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003593 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3594 // independently on 128-bit lanes.
3595 unsigned NumLanes = VT.getSizeInBits() / 128;
3596 unsigned NumLaneElts = NumElems / NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003597
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003598 for (unsigned s = 0; s < NumLanes; ++s) {
3599 for (unsigned i = s * NumLaneElts, j = s * NumLaneElts;
3600 i != NumLaneElts * (s + 1);
David Greenea20244d2011-03-02 17:23:43 +00003601 i += 2, ++j) {
3602 int BitI = Mask[i];
3603 int BitI1 = Mask[i+1];
3604
3605 if (!isUndefOrEqual(BitI, j))
3606 return false;
3607 if (!isUndefOrEqual(BitI1, j))
3608 return false;
3609 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003610 }
David Greenea20244d2011-03-02 17:23:43 +00003611
Rafael Espindola15684b22009-04-24 12:40:33 +00003612 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003613}
3614
Nate Begeman9008ca62009-04-27 18:41:29 +00003615bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3616 SmallVector<int, 8> M;
3617 N->getMask(M);
3618 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3619}
3620
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003621/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3622/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3623/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003624static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003625 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003626 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3627 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003628
Nate Begeman9008ca62009-04-27 18:41:29 +00003629 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3630 int BitI = Mask[i];
3631 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003632 if (!isUndefOrEqual(BitI, j))
3633 return false;
3634 if (!isUndefOrEqual(BitI1, j))
3635 return false;
3636 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003637 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003638}
3639
Nate Begeman9008ca62009-04-27 18:41:29 +00003640bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3641 SmallVector<int, 8> M;
3642 N->getMask(M);
3643 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3644}
3645
Evan Cheng017dcc62006-04-21 01:05:10 +00003646/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3647/// specifies a shuffle of elements that is suitable for input to MOVSS,
3648/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003649static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003650 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003651 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003652
3653 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003654
Nate Begeman9008ca62009-04-27 18:41:29 +00003655 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003656 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003657
Nate Begeman9008ca62009-04-27 18:41:29 +00003658 for (int i = 1; i < NumElts; ++i)
3659 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003660 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003661
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003662 return true;
3663}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003664
Nate Begeman9008ca62009-04-27 18:41:29 +00003665bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3666 SmallVector<int, 8> M;
3667 N->getMask(M);
3668 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003669}
3670
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003671/// isVPERM2F128Mask - Match 256-bit shuffles where the elements are considered
3672/// as permutations between 128-bit chunks or halves. As an example: this
3673/// shuffle bellow:
3674/// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
3675/// The first half comes from the second half of V1 and the second half from the
3676/// the second half of V2.
3677static bool isVPERM2F128Mask(const SmallVectorImpl<int> &Mask, EVT VT,
3678 const X86Subtarget *Subtarget) {
3679 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256)
3680 return false;
3681
3682 // The shuffle result is divided into half A and half B. In total the two
3683 // sources have 4 halves, namely: C, D, E, F. The final values of A and
3684 // B must come from C, D, E or F.
3685 int HalfSize = VT.getVectorNumElements()/2;
3686 bool MatchA = false, MatchB = false;
3687
3688 // Check if A comes from one of C, D, E, F.
3689 for (int Half = 0; Half < 4; ++Half) {
3690 if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
3691 MatchA = true;
3692 break;
3693 }
3694 }
3695
3696 // Check if B comes from one of C, D, E, F.
3697 for (int Half = 0; Half < 4; ++Half) {
3698 if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) {
3699 MatchB = true;
3700 break;
3701 }
3702 }
3703
3704 return MatchA && MatchB;
3705}
3706
3707/// getShuffleVPERM2F128Immediate - Return the appropriate immediate to shuffle
3708/// the specified VECTOR_MASK mask with VPERM2F128 instructions.
3709static unsigned getShuffleVPERM2F128Immediate(SDNode *N) {
3710 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3711 EVT VT = SVOp->getValueType(0);
3712
3713 int HalfSize = VT.getVectorNumElements()/2;
3714
3715 int FstHalf = 0, SndHalf = 0;
3716 for (int i = 0; i < HalfSize; ++i) {
3717 if (SVOp->getMaskElt(i) > 0) {
3718 FstHalf = SVOp->getMaskElt(i)/HalfSize;
3719 break;
3720 }
3721 }
3722 for (int i = HalfSize; i < HalfSize*2; ++i) {
3723 if (SVOp->getMaskElt(i) > 0) {
3724 SndHalf = SVOp->getMaskElt(i)/HalfSize;
3725 break;
3726 }
3727 }
3728
3729 return (FstHalf | (SndHalf << 4));
3730}
3731
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003732/// isVPERMILPDMask - Return true if the specified VECTOR_SHUFFLE operand
3733/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
3734/// Note that VPERMIL mask matching is different depending whether theunderlying
3735/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3736/// to the same elements of the low, but to the higher half of the source.
3737/// In VPERMILPD the two lanes could be shuffled independently of each other
3738/// with the same restriction that lanes can't be crossed.
3739static bool isVPERMILPDMask(const SmallVectorImpl<int> &Mask, EVT VT,
3740 const X86Subtarget *Subtarget) {
3741 int NumElts = VT.getVectorNumElements();
3742 int NumLanes = VT.getSizeInBits()/128;
3743
3744 if (!Subtarget->hasAVX())
3745 return false;
3746
3747 // Match any permutation of 128-bit vector with 64-bit types
3748 if (NumLanes == 1 && NumElts != 2)
3749 return false;
3750
3751 // Only match 256-bit with 32 types
3752 if (VT.getSizeInBits() == 256 && NumElts != 4)
3753 return false;
3754
3755 // The mask on the high lane is independent of the low. Both can match
3756 // any element in inside its own lane, but can't cross.
3757 int LaneSize = NumElts/NumLanes;
3758 for (int l = 0; l < NumLanes; ++l)
3759 for (int i = l*LaneSize; i < LaneSize*(l+1); ++i) {
3760 int LaneStart = l*LaneSize;
3761 if (!isUndefOrInRange(Mask[i], LaneStart, LaneStart+LaneSize))
3762 return false;
3763 }
3764
3765 return true;
3766}
3767
3768/// isVPERMILPSMask - Return true if the specified VECTOR_SHUFFLE operand
3769/// specifies a shuffle of elements that is suitable for input to VPERMILPS*.
3770/// Note that VPERMIL mask matching is different depending whether theunderlying
3771/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3772/// to the same elements of the low, but to the higher half of the source.
3773/// In VPERMILPD the two lanes could be shuffled independently of each other
3774/// with the same restriction that lanes can't be crossed.
3775static bool isVPERMILPSMask(const SmallVectorImpl<int> &Mask, EVT VT,
3776 const X86Subtarget *Subtarget) {
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003777 unsigned NumElts = VT.getVectorNumElements();
3778 unsigned NumLanes = VT.getSizeInBits()/128;
3779
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003780 if (!Subtarget->hasAVX())
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003781 return false;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003782
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003783 // Match any permutation of 128-bit vector with 32-bit types
3784 if (NumLanes == 1 && NumElts != 4)
3785 return false;
3786
3787 // Only match 256-bit with 32 types
3788 if (VT.getSizeInBits() == 256 && NumElts != 8)
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003789 return false;
3790
3791 // The mask on the high lane should be the same as the low. Actually,
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003792 // they can differ if any of the corresponding index in a lane is undef
3793 // and the other stays in range.
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003794 int LaneSize = NumElts/NumLanes;
3795 for (int i = 0; i < LaneSize; ++i) {
3796 int HighElt = i+LaneSize;
Bruno Cardoso Lopes155a92a2011-08-10 01:54:17 +00003797 bool HighValid = isUndefOrInRange(Mask[HighElt], LaneSize, NumElts);
3798 bool LowValid = isUndefOrInRange(Mask[i], 0, LaneSize);
3799
3800 if (!HighValid || !LowValid)
3801 return false;
3802 if (Mask[i] < 0 || Mask[HighElt] < 0)
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003803 continue;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003804 if (Mask[HighElt]-Mask[i] != LaneSize)
3805 return false;
3806 }
3807
3808 return true;
3809}
3810
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003811/// getShuffleVPERMILPSImmediate - Return the appropriate immediate to shuffle
3812/// the specified VECTOR_MASK mask with VPERMILPS* instructions.
3813static unsigned getShuffleVPERMILPSImmediate(SDNode *N) {
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003814 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3815 EVT VT = SVOp->getValueType(0);
3816
3817 int NumElts = VT.getVectorNumElements();
3818 int NumLanes = VT.getSizeInBits()/128;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003819 int LaneSize = NumElts/NumLanes;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003820
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003821 // Although the mask is equal for both lanes do it twice to get the cases
3822 // where a mask will match because the same mask element is undef on the
3823 // first half but valid on the second. This would get pathological cases
3824 // such as: shuffle <u, 0, 1, 2, 4, 4, 5, 6>, which is completely valid.
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003825 unsigned Mask = 0;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003826 for (int l = 0; l < NumLanes; ++l) {
3827 for (int i = 0; i < LaneSize; ++i) {
3828 int MaskElt = SVOp->getMaskElt(i+(l*LaneSize));
3829 if (MaskElt < 0)
3830 continue;
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003831 if (MaskElt >= LaneSize)
3832 MaskElt -= LaneSize;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003833 Mask |= MaskElt << (i*2);
3834 }
Bruno Cardoso Lopes377baa52011-07-29 01:31:04 +00003835 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003836
3837 return Mask;
3838}
3839
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003840/// getShuffleVPERMILPDImmediate - Return the appropriate immediate to shuffle
3841/// the specified VECTOR_MASK mask with VPERMILPD* instructions.
3842static unsigned getShuffleVPERMILPDImmediate(SDNode *N) {
3843 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3844 EVT VT = SVOp->getValueType(0);
3845
3846 int NumElts = VT.getVectorNumElements();
3847 int NumLanes = VT.getSizeInBits()/128;
3848
3849 unsigned Mask = 0;
3850 int LaneSize = NumElts/NumLanes;
3851 for (int l = 0; l < NumLanes; ++l)
Bruno Cardoso Lopes377baa52011-07-29 01:31:04 +00003852 for (int i = l*LaneSize; i < LaneSize*(l+1); ++i) {
3853 int MaskElt = SVOp->getMaskElt(i);
3854 if (MaskElt < 0)
3855 continue;
3856 Mask |= (MaskElt-l*LaneSize) << i;
3857 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003858
3859 return Mask;
3860}
3861
Evan Cheng017dcc62006-04-21 01:05:10 +00003862/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3863/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003864/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003865static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003866 bool V2IsSplat = false, bool V2IsUndef = false) {
3867 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003868 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003869 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003870
Nate Begeman9008ca62009-04-27 18:41:29 +00003871 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003872 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003873
Nate Begeman9008ca62009-04-27 18:41:29 +00003874 for (int i = 1; i < NumOps; ++i)
3875 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3876 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3877 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003878 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003879
Evan Cheng39623da2006-04-20 08:58:49 +00003880 return true;
3881}
3882
Nate Begeman9008ca62009-04-27 18:41:29 +00003883static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003884 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003885 SmallVector<int, 8> M;
3886 N->getMask(M);
3887 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003888}
3889
Evan Chengd9539472006-04-14 21:59:03 +00003890/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3891/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003892/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
3893bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N,
3894 const X86Subtarget *Subtarget) {
3895 if (!Subtarget->hasSSE3() && !Subtarget->hasAVX())
Evan Chengd9539472006-04-14 21:59:03 +00003896 return false;
3897
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003898 // The second vector must be undef
3899 if (N->getOperand(1).getOpcode() != ISD::UNDEF)
3900 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003901
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003902 EVT VT = N->getValueType(0);
3903 unsigned NumElems = VT.getVectorNumElements();
3904
3905 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3906 (VT.getSizeInBits() == 256 && NumElems != 8))
3907 return false;
3908
3909 // "i+1" is the value the indexed mask element must have
3910 for (unsigned i = 0; i < NumElems; i += 2)
3911 if (!isUndefOrEqual(N->getMaskElt(i), i+1) ||
3912 !isUndefOrEqual(N->getMaskElt(i+1), i+1))
Nate Begeman9008ca62009-04-27 18:41:29 +00003913 return false;
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003914
3915 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003916}
3917
3918/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3919/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003920/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
3921bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N,
3922 const X86Subtarget *Subtarget) {
3923 if (!Subtarget->hasSSE3() && !Subtarget->hasAVX())
Evan Chengd9539472006-04-14 21:59:03 +00003924 return false;
3925
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003926 // The second vector must be undef
3927 if (N->getOperand(1).getOpcode() != ISD::UNDEF)
3928 return false;
3929
3930 EVT VT = N->getValueType(0);
3931 unsigned NumElems = VT.getVectorNumElements();
3932
3933 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3934 (VT.getSizeInBits() == 256 && NumElems != 8))
3935 return false;
3936
3937 // "i" is the value the indexed mask element must have
3938 for (unsigned i = 0; i < NumElems; i += 2)
3939 if (!isUndefOrEqual(N->getMaskElt(i), i) ||
3940 !isUndefOrEqual(N->getMaskElt(i+1), i))
Nate Begeman9008ca62009-04-27 18:41:29 +00003941 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003942
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003943 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003944}
3945
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003946/// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand
3947/// specifies a shuffle of elements that is suitable for input to 256-bit
3948/// version of MOVDDUP.
3949static bool isMOVDDUPYMask(ShuffleVectorSDNode *N,
3950 const X86Subtarget *Subtarget) {
3951 EVT VT = N->getValueType(0);
3952 int NumElts = VT.getVectorNumElements();
3953 bool V2IsUndef = N->getOperand(1).getOpcode() == ISD::UNDEF;
3954
3955 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256 ||
3956 !V2IsUndef || NumElts != 4)
3957 return false;
3958
3959 for (int i = 0; i != NumElts/2; ++i)
3960 if (!isUndefOrEqual(N->getMaskElt(i), 0))
3961 return false;
3962 for (int i = NumElts/2; i != NumElts; ++i)
3963 if (!isUndefOrEqual(N->getMaskElt(i), NumElts/2))
3964 return false;
3965 return true;
3966}
3967
Evan Cheng0b457f02008-09-25 20:50:48 +00003968/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003969/// specifies a shuffle of elements that is suitable for input to 128-bit
3970/// version of MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003971bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003972 EVT VT = N->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00003973
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003974 if (VT.getSizeInBits() != 128)
3975 return false;
3976
3977 int e = VT.getVectorNumElements() / 2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003978 for (int i = 0; i < e; ++i)
3979 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003980 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003981 for (int i = 0; i < e; ++i)
3982 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003983 return false;
3984 return true;
3985}
3986
David Greenec38a03e2011-02-03 15:50:00 +00003987/// isVEXTRACTF128Index - Return true if the specified
3988/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3989/// suitable for input to VEXTRACTF128.
3990bool X86::isVEXTRACTF128Index(SDNode *N) {
3991 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3992 return false;
3993
3994 // The index should be aligned on a 128-bit boundary.
3995 uint64_t Index =
3996 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3997
3998 unsigned VL = N->getValueType(0).getVectorNumElements();
3999 unsigned VBits = N->getValueType(0).getSizeInBits();
4000 unsigned ElSize = VBits / VL;
4001 bool Result = (Index * ElSize) % 128 == 0;
4002
4003 return Result;
4004}
4005
David Greeneccacdc12011-02-04 16:08:29 +00004006/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
4007/// operand specifies a subvector insert that is suitable for input to
4008/// VINSERTF128.
4009bool X86::isVINSERTF128Index(SDNode *N) {
4010 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4011 return false;
4012
4013 // The index should be aligned on a 128-bit boundary.
4014 uint64_t Index =
4015 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
4016
4017 unsigned VL = N->getValueType(0).getVectorNumElements();
4018 unsigned VBits = N->getValueType(0).getSizeInBits();
4019 unsigned ElSize = VBits / VL;
4020 bool Result = (Index * ElSize) % 128 == 0;
4021
4022 return Result;
4023}
4024
Evan Cheng63d33002006-03-22 08:01:21 +00004025/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004026/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00004027unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004028 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
4029 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
4030
Evan Chengb9df0ca2006-03-22 02:53:00 +00004031 unsigned Shift = (NumOperands == 4) ? 2 : 1;
4032 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00004033 for (int i = 0; i < NumOperands; ++i) {
4034 int Val = SVOp->getMaskElt(NumOperands-i-1);
4035 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00004036 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00004037 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00004038 if (i != NumOperands - 1)
4039 Mask <<= Shift;
4040 }
Evan Cheng63d33002006-03-22 08:01:21 +00004041 return Mask;
4042}
4043
Evan Cheng506d3df2006-03-29 23:07:14 +00004044/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004045/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00004046unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004047 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00004048 unsigned Mask = 0;
4049 // 8 nodes, but we only care about the last 4.
4050 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004051 int Val = SVOp->getMaskElt(i);
4052 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00004053 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00004054 if (i != 4)
4055 Mask <<= 2;
4056 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004057 return Mask;
4058}
4059
4060/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004061/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00004062unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004063 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00004064 unsigned Mask = 0;
4065 // 8 nodes, but we only care about the first 4.
4066 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004067 int Val = SVOp->getMaskElt(i);
4068 if (Val >= 0)
4069 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00004070 if (i != 0)
4071 Mask <<= 2;
4072 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004073 return Mask;
4074}
4075
Nate Begemana09008b2009-10-19 02:17:23 +00004076/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
4077/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
4078unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
4079 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
4080 EVT VVT = N->getValueType(0);
4081 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
4082 int Val = 0;
4083
4084 unsigned i, e;
4085 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
4086 Val = SVOp->getMaskElt(i);
4087 if (Val >= 0)
4088 break;
4089 }
Eli Friedman63f8dde2011-07-25 21:36:45 +00004090 assert(Val - i > 0 && "PALIGNR imm should be positive");
Nate Begemana09008b2009-10-19 02:17:23 +00004091 return (Val - i) * EltSize;
4092}
4093
David Greenec38a03e2011-02-03 15:50:00 +00004094/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
4095/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
4096/// instructions.
4097unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
4098 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4099 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
4100
4101 uint64_t Index =
4102 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4103
4104 EVT VecVT = N->getOperand(0).getValueType();
4105 EVT ElVT = VecVT.getVectorElementType();
4106
4107 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004108 return Index / NumElemsPerChunk;
4109}
4110
David Greeneccacdc12011-02-04 16:08:29 +00004111/// getInsertVINSERTF128Immediate - Return the appropriate immediate
4112/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
4113/// instructions.
4114unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
4115 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4116 llvm_unreachable("Illegal insert subvector for VINSERTF128");
4117
4118 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00004119 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00004120
4121 EVT VecVT = N->getValueType(0);
4122 EVT ElVT = VecVT.getVectorElementType();
4123
4124 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004125 return Index / NumElemsPerChunk;
4126}
4127
Evan Cheng37b73872009-07-30 08:33:02 +00004128/// isZeroNode - Returns true if Elt is a constant zero or a floating point
4129/// constant +0.0.
4130bool X86::isZeroNode(SDValue Elt) {
4131 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00004132 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00004133 (isa<ConstantFPSDNode>(Elt) &&
4134 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
4135}
4136
Nate Begeman9008ca62009-04-27 18:41:29 +00004137/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
4138/// their permute mask.
4139static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
4140 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004141 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004142 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004143 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00004144
Nate Begeman5a5ca152009-04-29 05:20:52 +00004145 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004146 int idx = SVOp->getMaskElt(i);
4147 if (idx < 0)
4148 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004149 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00004150 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004151 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004152 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004153 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004154 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
4155 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004156}
4157
Evan Cheng779ccea2007-12-07 21:30:01 +00004158/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
4159/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00004160static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00004161 unsigned NumElems = VT.getVectorNumElements();
4162 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004163 int idx = Mask[i];
4164 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004165 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004166 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00004167 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004168 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004169 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004170 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004171}
4172
Evan Cheng533a0aa2006-04-19 20:35:22 +00004173/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
4174/// match movhlps. The lower half elements should come from upper half of
4175/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004176/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00004177static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004178 EVT VT = Op->getValueType(0);
4179 if (VT.getSizeInBits() != 128)
4180 return false;
4181 if (VT.getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00004182 return false;
4183 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004184 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004185 return false;
4186 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004187 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004188 return false;
4189 return true;
4190}
4191
Evan Cheng5ced1d82006-04-06 23:23:56 +00004192/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00004193/// is promoted to a vector. It also returns the LoadSDNode by reference if
4194/// required.
4195static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00004196 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
4197 return false;
4198 N = N->getOperand(0).getNode();
4199 if (!ISD::isNON_EXTLoad(N))
4200 return false;
4201 if (LD)
4202 *LD = cast<LoadSDNode>(N);
4203 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004204}
4205
Evan Cheng533a0aa2006-04-19 20:35:22 +00004206/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4207/// match movlp{s|d}. The lower half elements should come from lower half of
4208/// V1 (and in order), and the upper half elements should come from the upper
4209/// half of V2 (and in order). And since V1 will become the source of the
4210/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004211static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
4212 ShuffleVectorSDNode *Op) {
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004213 EVT VT = Op->getValueType(0);
4214 if (VT.getSizeInBits() != 128)
4215 return false;
4216
Evan Cheng466685d2006-10-09 20:57:25 +00004217 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004218 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00004219 // Is V2 is a vector load, don't do this transformation. We will try to use
4220 // load folding shufps op.
4221 if (ISD::isNON_EXTLoad(V2))
4222 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004223
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004224 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004225
Evan Cheng533a0aa2006-04-19 20:35:22 +00004226 if (NumElems != 2 && NumElems != 4)
4227 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004228 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004229 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004230 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004231 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004232 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004233 return false;
4234 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004235}
4236
Evan Cheng39623da2006-04-20 08:58:49 +00004237/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4238/// all the same.
4239static bool isSplatVector(SDNode *N) {
4240 if (N->getOpcode() != ISD::BUILD_VECTOR)
4241 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004242
Dan Gohman475871a2008-07-27 21:46:04 +00004243 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00004244 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4245 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00004246 return false;
4247 return true;
4248}
4249
Evan Cheng213d2cf2007-05-17 18:45:50 +00004250/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00004251/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00004252/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00004253static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00004254 SDValue V1 = N->getOperand(0);
4255 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004256 unsigned NumElems = N->getValueType(0).getVectorNumElements();
4257 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004258 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004259 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004260 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00004261 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4262 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004263 if (Opc != ISD::BUILD_VECTOR ||
4264 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00004265 return false;
4266 } else if (Idx >= 0) {
4267 unsigned Opc = V1.getOpcode();
4268 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4269 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004270 if (Opc != ISD::BUILD_VECTOR ||
4271 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00004272 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00004273 }
4274 }
4275 return true;
4276}
4277
4278/// getZeroVector - Returns a vector of specified type with all zero elements.
4279///
Owen Andersone50ed302009-08-10 22:56:29 +00004280static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00004281 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004282 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004283
Dale Johannesen0488fb62010-09-30 23:57:10 +00004284 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004285 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00004286 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00004287 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004288 if (HasSSE2) { // SSE2
4289 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4290 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4291 } else { // SSE1
4292 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4293 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4294 }
4295 } else if (VT.getSizeInBits() == 256) { // AVX
4296 // 256-bit logic and arithmetic instructions in AVX are
4297 // all floating-point, no support for integer ops. Default
4298 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00004299 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004300 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4301 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00004302 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004303 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004304}
4305
Chris Lattner8a594482007-11-25 00:24:49 +00004306/// getOnesVector - Returns a vector of specified type with all bits set.
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004307/// Always build ones vectors as <4 x i32>. For 256-bit types, use two
4308/// <4 x i32> inserted in a <8 x i32> appropriately. Then bitcast to their
4309/// original type, ensuring they get CSE'd.
Owen Andersone50ed302009-08-10 22:56:29 +00004310static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004311 assert(VT.isVector() && "Expected a vector type");
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004312 assert((VT.is128BitVector() || VT.is256BitVector())
4313 && "Expected a 128-bit or 256-bit vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004314
Owen Anderson825b72b2009-08-11 20:47:22 +00004315 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004316 SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
4317 Cst, Cst, Cst, Cst);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004318
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004319 if (VT.is256BitVector()) {
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004320 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, MVT::v8i32),
4321 Vec, DAG.getConstant(0, MVT::i32), DAG, dl);
4322 Vec = Insert128BitVector(InsV, Vec,
4323 DAG.getConstant(4 /* NumElems/2 */, MVT::i32), DAG, dl);
4324 }
4325
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004326 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00004327}
4328
Evan Cheng39623da2006-04-20 08:58:49 +00004329/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4330/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00004331static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004332 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004333 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004334
Evan Cheng39623da2006-04-20 08:58:49 +00004335 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00004336 SmallVector<int, 8> MaskVec;
4337 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00004338
Nate Begeman5a5ca152009-04-29 05:20:52 +00004339 for (unsigned i = 0; i != NumElems; ++i) {
4340 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004341 MaskVec[i] = NumElems;
4342 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00004343 }
Evan Cheng39623da2006-04-20 08:58:49 +00004344 }
Evan Cheng39623da2006-04-20 08:58:49 +00004345 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00004346 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
4347 SVOp->getOperand(1), &MaskVec[0]);
4348 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00004349}
4350
Evan Cheng017dcc62006-04-21 01:05:10 +00004351/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4352/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00004353static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004354 SDValue V2) {
4355 unsigned NumElems = VT.getVectorNumElements();
4356 SmallVector<int, 8> Mask;
4357 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00004358 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004359 Mask.push_back(i);
4360 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00004361}
4362
Nate Begeman9008ca62009-04-27 18:41:29 +00004363/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004364static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004365 SDValue V2) {
4366 unsigned NumElems = VT.getVectorNumElements();
4367 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00004368 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004369 Mask.push_back(i);
4370 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00004371 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004372 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00004373}
4374
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004375/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004376static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004377 SDValue V2) {
4378 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00004379 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00004380 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00004381 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004382 Mask.push_back(i + Half);
4383 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00004384 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004385 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004386}
4387
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004388// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004389// a generic shuffle instruction because the target has no such instructions.
4390// Generate shuffles which repeat i16 and i8 several times until they can be
4391// represented by v4f32 and then be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004392static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004393 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004394 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004395 DebugLoc dl = V.getDebugLoc();
Rafael Espindola15684b22009-04-24 12:40:33 +00004396
Nate Begeman9008ca62009-04-27 18:41:29 +00004397 while (NumElems > 4) {
4398 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004399 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004400 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004401 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004402 EltNo -= NumElems/2;
4403 }
4404 NumElems >>= 1;
4405 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004406 return V;
4407}
Eric Christopherfd179292009-08-27 18:07:15 +00004408
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004409/// getLegalSplat - Generate a legal splat with supported x86 shuffles
4410static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
4411 EVT VT = V.getValueType();
4412 DebugLoc dl = V.getDebugLoc();
4413 assert((VT.getSizeInBits() == 128 || VT.getSizeInBits() == 256)
4414 && "Vector size not supported");
4415
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004416 if (VT.getSizeInBits() == 128) {
4417 V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004418 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004419 V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
4420 &SplatMask[0]);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004421 } else {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004422 // To use VPERMILPS to splat scalars, the second half of indicies must
4423 // refer to the higher part, which is a duplication of the lower one,
4424 // because VPERMILPS can only handle in-lane permutations.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004425 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
4426 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004427
4428 V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
4429 V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
4430 &SplatMask[0]);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004431 }
4432
4433 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4434}
4435
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004436/// PromoteSplat - Splat is promoted to target supported vector shuffles.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004437static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4438 EVT SrcVT = SV->getValueType(0);
4439 SDValue V1 = SV->getOperand(0);
4440 DebugLoc dl = SV->getDebugLoc();
4441
4442 int EltNo = SV->getSplatIndex();
4443 int NumElems = SrcVT.getVectorNumElements();
4444 unsigned Size = SrcVT.getSizeInBits();
4445
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004446 assert(((Size == 128 && NumElems > 4) || Size == 256) &&
4447 "Unknown how to promote splat for type");
4448
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004449 // Extract the 128-bit part containing the splat element and update
4450 // the splat element index when it refers to the higher register.
4451 if (Size == 256) {
4452 unsigned Idx = (EltNo > NumElems/2) ? NumElems/2 : 0;
4453 V1 = Extract128BitVector(V1, DAG.getConstant(Idx, MVT::i32), DAG, dl);
4454 if (Idx > 0)
4455 EltNo -= NumElems/2;
4456 }
4457
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004458 // All i16 and i8 vector types can't be used directly by a generic shuffle
4459 // instruction because the target has no such instruction. Generate shuffles
4460 // which repeat i16 and i8 several times until they fit in i32, and then can
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004461 // be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004462 EVT EltVT = SrcVT.getVectorElementType();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004463 if (EltVT == MVT::i8 || EltVT == MVT::i16)
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004464 V1 = PromoteSplati8i16(V1, DAG, EltNo);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004465
4466 // Recreate the 256-bit vector and place the same 128-bit vector
4467 // into the low and high part. This is necessary because we want
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004468 // to use VPERM* to shuffle the vectors
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004469 if (Size == 256) {
4470 SDValue InsV = Insert128BitVector(DAG.getUNDEF(SrcVT), V1,
4471 DAG.getConstant(0, MVT::i32), DAG, dl);
4472 V1 = Insert128BitVector(InsV, V1,
4473 DAG.getConstant(NumElems/2, MVT::i32), DAG, dl);
4474 }
4475
4476 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004477}
4478
Evan Chengba05f722006-04-21 23:03:30 +00004479/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004480/// vector of zero or undef vector. This produces a shuffle where the low
4481/// element of V2 is swizzled into the zero/undef vector, landing at element
4482/// 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 +00004483static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00004484 bool isZero, bool HasSSE2,
4485 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004486 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00004487 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00004488 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
4489 unsigned NumElems = VT.getVectorNumElements();
4490 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004491 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004492 // If this is the insertion idx, put the low elt of V2 here.
4493 MaskVec.push_back(i == Idx ? NumElems : i);
4494 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004495}
4496
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004497/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4498/// element of the result of the vector shuffle.
Benjamin Kramer050db522011-03-26 12:38:19 +00004499static SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
4500 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004501 if (Depth == 6)
4502 return SDValue(); // Limit search depth.
4503
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004504 SDValue V = SDValue(N, 0);
4505 EVT VT = V.getValueType();
4506 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004507
4508 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4509 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
4510 Index = SV->getMaskElt(Index);
4511
4512 if (Index < 0)
4513 return DAG.getUNDEF(VT.getVectorElementType());
4514
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004515 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004516 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004517 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004518 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004519
4520 // Recurse into target specific vector shuffles to find scalars.
4521 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004522 int NumElems = VT.getVectorNumElements();
4523 SmallVector<unsigned, 16> ShuffleMask;
4524 SDValue ImmN;
4525
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004526 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004527 case X86ISD::SHUFPS:
4528 case X86ISD::SHUFPD:
4529 ImmN = N->getOperand(N->getNumOperands()-1);
4530 DecodeSHUFPSMask(NumElems,
4531 cast<ConstantSDNode>(ImmN)->getZExtValue(),
4532 ShuffleMask);
4533 break;
4534 case X86ISD::PUNPCKHBW:
4535 case X86ISD::PUNPCKHWD:
4536 case X86ISD::PUNPCKHDQ:
4537 case X86ISD::PUNPCKHQDQ:
4538 DecodePUNPCKHMask(NumElems, ShuffleMask);
4539 break;
4540 case X86ISD::UNPCKHPS:
4541 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00004542 case X86ISD::VUNPCKHPSY:
4543 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004544 DecodeUNPCKHPMask(NumElems, ShuffleMask);
4545 break;
4546 case X86ISD::PUNPCKLBW:
4547 case X86ISD::PUNPCKLWD:
4548 case X86ISD::PUNPCKLDQ:
4549 case X86ISD::PUNPCKLQDQ:
David Greenec4db4e52011-02-28 19:06:56 +00004550 DecodePUNPCKLMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004551 break;
4552 case X86ISD::UNPCKLPS:
4553 case X86ISD::UNPCKLPD:
David Greenec4db4e52011-02-28 19:06:56 +00004554 case X86ISD::VUNPCKLPSY:
4555 case X86ISD::VUNPCKLPDY:
4556 DecodeUNPCKLPMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004557 break;
4558 case X86ISD::MOVHLPS:
4559 DecodeMOVHLPSMask(NumElems, ShuffleMask);
4560 break;
4561 case X86ISD::MOVLHPS:
4562 DecodeMOVLHPSMask(NumElems, ShuffleMask);
4563 break;
4564 case X86ISD::PSHUFD:
4565 ImmN = N->getOperand(N->getNumOperands()-1);
4566 DecodePSHUFMask(NumElems,
4567 cast<ConstantSDNode>(ImmN)->getZExtValue(),
4568 ShuffleMask);
4569 break;
4570 case X86ISD::PSHUFHW:
4571 ImmN = N->getOperand(N->getNumOperands()-1);
4572 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4573 ShuffleMask);
4574 break;
4575 case X86ISD::PSHUFLW:
4576 ImmN = N->getOperand(N->getNumOperands()-1);
4577 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4578 ShuffleMask);
4579 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004580 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004581 case X86ISD::MOVSD: {
4582 // The index 0 always comes from the first element of the second source,
4583 // this is why MOVSS and MOVSD are used in the first place. The other
4584 // elements come from the other positions of the first source vector.
4585 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004586 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
4587 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004588 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00004589 case X86ISD::VPERMILPS:
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00004590 ImmN = N->getOperand(N->getNumOperands()-1);
Bruno Cardoso Lopes2eb4c2b2011-07-29 01:31:11 +00004591 DecodeVPERMILPSMask(4, cast<ConstantSDNode>(ImmN)->getZExtValue(),
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00004592 ShuffleMask);
Bruno Cardoso Lopes2eb4c2b2011-07-29 01:31:11 +00004593 break;
4594 case X86ISD::VPERMILPSY:
4595 ImmN = N->getOperand(N->getNumOperands()-1);
4596 DecodeVPERMILPSMask(8, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4597 ShuffleMask);
4598 break;
4599 case X86ISD::VPERMILPD:
4600 ImmN = N->getOperand(N->getNumOperands()-1);
4601 DecodeVPERMILPDMask(2, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4602 ShuffleMask);
4603 break;
4604 case X86ISD::VPERMILPDY:
4605 ImmN = N->getOperand(N->getNumOperands()-1);
4606 DecodeVPERMILPDMask(4, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4607 ShuffleMask);
4608 break;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00004609 case X86ISD::VPERM2F128:
4610 ImmN = N->getOperand(N->getNumOperands()-1);
4611 DecodeVPERM2F128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4612 ShuffleMask);
4613 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004614 default:
4615 assert("not implemented for target shuffle node");
4616 return SDValue();
4617 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004618
4619 Index = ShuffleMask[Index];
4620 if (Index < 0)
4621 return DAG.getUNDEF(VT.getVectorElementType());
4622
4623 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
4624 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
4625 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004626 }
4627
4628 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004629 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004630 V = V.getOperand(0);
4631 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004632 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004633
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004634 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004635 return SDValue();
4636 }
4637
4638 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4639 return (Index == 0) ? V.getOperand(0)
4640 : DAG.getUNDEF(VT.getVectorElementType());
4641
4642 if (V.getOpcode() == ISD::BUILD_VECTOR)
4643 return V.getOperand(Index);
4644
4645 return SDValue();
4646}
4647
4648/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4649/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004650/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004651static
4652unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
4653 bool ZerosFromLeft, SelectionDAG &DAG) {
4654 int i = 0;
4655
4656 while (i < NumElems) {
4657 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004658 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004659 if (!(Elt.getNode() &&
4660 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4661 break;
4662 ++i;
4663 }
4664
4665 return i;
4666}
4667
4668/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
4669/// MaskE correspond consecutively to elements from one of the vector operands,
4670/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4671static
4672bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
4673 int OpIdx, int NumElems, unsigned &OpNum) {
4674 bool SeenV1 = false;
4675 bool SeenV2 = false;
4676
4677 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
4678 int Idx = SVOp->getMaskElt(i);
4679 // Ignore undef indicies
4680 if (Idx < 0)
4681 continue;
4682
4683 if (Idx < NumElems)
4684 SeenV1 = true;
4685 else
4686 SeenV2 = true;
4687
4688 // Only accept consecutive elements from the same vector
4689 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4690 return false;
4691 }
4692
4693 OpNum = SeenV1 ? 0 : 1;
4694 return true;
4695}
4696
4697/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4698/// logical left shift of a vector.
4699static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4700 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4701 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4702 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4703 false /* check zeros from right */, DAG);
4704 unsigned OpSrc;
4705
4706 if (!NumZeros)
4707 return false;
4708
4709 // Considering the elements in the mask that are not consecutive zeros,
4710 // check if they consecutively come from only one of the source vectors.
4711 //
4712 // V1 = {X, A, B, C} 0
4713 // \ \ \ /
4714 // vector_shuffle V1, V2 <1, 2, 3, X>
4715 //
4716 if (!isShuffleMaskConsecutive(SVOp,
4717 0, // Mask Start Index
4718 NumElems-NumZeros-1, // Mask End Index
4719 NumZeros, // Where to start looking in the src vector
4720 NumElems, // Number of elements in vector
4721 OpSrc)) // Which source operand ?
4722 return false;
4723
4724 isLeft = false;
4725 ShAmt = NumZeros;
4726 ShVal = SVOp->getOperand(OpSrc);
4727 return true;
4728}
4729
4730/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4731/// logical left shift of a vector.
4732static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4733 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4734 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4735 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4736 true /* check zeros from left */, DAG);
4737 unsigned OpSrc;
4738
4739 if (!NumZeros)
4740 return false;
4741
4742 // Considering the elements in the mask that are not consecutive zeros,
4743 // check if they consecutively come from only one of the source vectors.
4744 //
4745 // 0 { A, B, X, X } = V2
4746 // / \ / /
4747 // vector_shuffle V1, V2 <X, X, 4, 5>
4748 //
4749 if (!isShuffleMaskConsecutive(SVOp,
4750 NumZeros, // Mask Start Index
4751 NumElems-1, // Mask End Index
4752 0, // Where to start looking in the src vector
4753 NumElems, // Number of elements in vector
4754 OpSrc)) // Which source operand ?
4755 return false;
4756
4757 isLeft = true;
4758 ShAmt = NumZeros;
4759 ShVal = SVOp->getOperand(OpSrc);
4760 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004761}
4762
4763/// isVectorShift - Returns true if the shuffle can be implemented as a
4764/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004765static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004766 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004767 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4768 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4769 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004770
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004771 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004772}
4773
Evan Chengc78d3b42006-04-24 18:01:45 +00004774/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4775///
Dan Gohman475871a2008-07-27 21:46:04 +00004776static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004777 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004778 SelectionDAG &DAG,
4779 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004780 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004781 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004782
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004783 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004784 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004785 bool First = true;
4786 for (unsigned i = 0; i < 16; ++i) {
4787 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4788 if (ThisIsNonZero && First) {
4789 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004790 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004791 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004792 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004793 First = false;
4794 }
4795
4796 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004797 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004798 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4799 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004800 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004801 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004802 }
4803 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004804 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4805 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4806 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004807 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004808 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004809 } else
4810 ThisElt = LastElt;
4811
Gabor Greifba36cb52008-08-28 21:40:38 +00004812 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004813 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004814 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004815 }
4816 }
4817
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004818 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004819}
4820
Bill Wendlinga348c562007-03-22 18:42:45 +00004821/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004822///
Dan Gohman475871a2008-07-27 21:46:04 +00004823static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004824 unsigned NumNonZero, unsigned NumZero,
4825 SelectionDAG &DAG,
4826 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004827 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004828 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004829
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004830 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004831 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004832 bool First = true;
4833 for (unsigned i = 0; i < 8; ++i) {
4834 bool isNonZero = (NonZeros & (1 << i)) != 0;
4835 if (isNonZero) {
4836 if (First) {
4837 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004838 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004839 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004840 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004841 First = false;
4842 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004843 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004844 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004845 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004846 }
4847 }
4848
4849 return V;
4850}
4851
Evan Chengf26ffe92008-05-29 08:22:04 +00004852/// getVShift - Return a vector logical shift node.
4853///
Owen Andersone50ed302009-08-10 22:56:29 +00004854static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004855 unsigned NumBits, SelectionDAG &DAG,
4856 const TargetLowering &TLI, DebugLoc dl) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004857 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00004858 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004859 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4860 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004861 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004862 DAG.getConstant(NumBits,
4863 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004864}
4865
Dan Gohman475871a2008-07-27 21:46:04 +00004866SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004867X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004868 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004869
Evan Chengc3630942009-12-09 21:00:30 +00004870 // Check if the scalar load can be widened into a vector load. And if
4871 // the address is "base + cst" see if the cst can be "absorbed" into
4872 // the shuffle mask.
4873 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4874 SDValue Ptr = LD->getBasePtr();
4875 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4876 return SDValue();
4877 EVT PVT = LD->getValueType(0);
4878 if (PVT != MVT::i32 && PVT != MVT::f32)
4879 return SDValue();
4880
4881 int FI = -1;
4882 int64_t Offset = 0;
4883 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4884 FI = FINode->getIndex();
4885 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004886 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004887 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4888 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4889 Offset = Ptr.getConstantOperandVal(1);
4890 Ptr = Ptr.getOperand(0);
4891 } else {
4892 return SDValue();
4893 }
4894
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004895 // FIXME: 256-bit vector instructions don't require a strict alignment,
4896 // improve this code to support it better.
4897 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00004898 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004899 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00004900 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004901 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00004902 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004903 // Can't change the alignment. FIXME: It's possible to compute
4904 // the exact stack offset and reference FI + adjust offset instead.
4905 // If someone *really* cares about this. That's the way to implement it.
4906 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004907 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004908 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00004909 }
4910 }
4911
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004912 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00004913 // Ptr + (Offset & ~15).
4914 if (Offset < 0)
4915 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004916 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00004917 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004918 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00004919 if (StartOffset)
4920 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4921 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4922
4923 int EltNo = (Offset - StartOffset) >> 2;
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004924 int NumElems = VT.getVectorNumElements();
4925
4926 EVT CanonVT = VT.getSizeInBits() == 128 ? MVT::v4i32 : MVT::v8i32;
4927 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
4928 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00004929 LD->getPointerInfo().getWithOffset(StartOffset),
David Greene67c9d422010-02-15 16:53:33 +00004930 false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004931
4932 // Canonicalize it to a v4i32 or v8i32 shuffle.
4933 SmallVector<int, 8> Mask;
4934 for (int i = 0; i < NumElems; ++i)
4935 Mask.push_back(EltNo);
4936
4937 V1 = DAG.getNode(ISD::BITCAST, dl, CanonVT, V1);
4938 return DAG.getNode(ISD::BITCAST, dl, NVT,
4939 DAG.getVectorShuffle(CanonVT, dl, V1,
4940 DAG.getUNDEF(CanonVT),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00004941 }
4942
4943 return SDValue();
4944}
4945
Michael J. Spencerec38de22010-10-10 22:04:20 +00004946/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4947/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004948/// load which has the same value as a build_vector whose operands are 'elts'.
4949///
4950/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004951///
Nate Begeman1449f292010-03-24 22:19:06 +00004952/// FIXME: we'd also like to handle the case where the last elements are zero
4953/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4954/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004955static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004956 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004957 EVT EltVT = VT.getVectorElementType();
4958 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004959
Nate Begemanfdea31a2010-03-24 20:49:50 +00004960 LoadSDNode *LDBase = NULL;
4961 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004962
Nate Begeman1449f292010-03-24 22:19:06 +00004963 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004964 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004965 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004966 for (unsigned i = 0; i < NumElems; ++i) {
4967 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004968
Nate Begemanfdea31a2010-03-24 20:49:50 +00004969 if (!Elt.getNode() ||
4970 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4971 return SDValue();
4972 if (!LDBase) {
4973 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4974 return SDValue();
4975 LDBase = cast<LoadSDNode>(Elt.getNode());
4976 LastLoadedElt = i;
4977 continue;
4978 }
4979 if (Elt.getOpcode() == ISD::UNDEF)
4980 continue;
4981
4982 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4983 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4984 return SDValue();
4985 LastLoadedElt = i;
4986 }
Nate Begeman1449f292010-03-24 22:19:06 +00004987
4988 // If we have found an entire vector of loads and undefs, then return a large
4989 // load of the entire vector width starting at the base pointer. If we found
4990 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004991 if (LastLoadedElt == NumElems - 1) {
4992 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00004993 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004994 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004995 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00004996 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004997 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004998 LDBase->isVolatile(), LDBase->isNonTemporal(),
4999 LDBase->getAlignment());
Eli Friedman61cc47e2011-07-26 21:02:58 +00005000 } else if (NumElems == 4 && LastLoadedElt == 1 &&
5001 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005002 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
5003 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Chris Lattner88641552010-09-22 00:34:38 +00005004 SDValue ResNode = DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys,
5005 Ops, 2, MVT::i32,
5006 LDBase->getMemOperand());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005007 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00005008 }
5009 return SDValue();
5010}
5011
Evan Chengc3630942009-12-09 21:00:30 +00005012SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005013X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005014 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00005015
David Greenef125a292011-02-08 19:04:41 +00005016 EVT VT = Op.getValueType();
5017 EVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00005018 unsigned NumElems = Op.getNumOperands();
5019
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005020 // Vectors containing all zeros can be matched by pxor and xorps later
5021 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5022 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
5023 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00005024 if (Op.getValueType() == MVT::v4i32 ||
5025 Op.getValueType() == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00005026 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005027
Dale Johannesenace16102009-02-03 19:33:06 +00005028 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00005029 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005030
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005031 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
5032 // vectors or broken into v4i32 operations on 256-bit vectors.
5033 if (ISD::isBuildVectorAllOnes(Op.getNode())) {
5034 if (Op.getValueType() == MVT::v4i32)
5035 return Op;
5036
5037 return getOnesVector(Op.getValueType(), DAG, dl);
5038 }
5039
Owen Andersone50ed302009-08-10 22:56:29 +00005040 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005041
Evan Cheng0db9fe62006-04-25 20:13:52 +00005042 unsigned NumZero = 0;
5043 unsigned NumNonZero = 0;
5044 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005045 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005046 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005047 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005048 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00005049 if (Elt.getOpcode() == ISD::UNDEF)
5050 continue;
5051 Values.insert(Elt);
5052 if (Elt.getOpcode() != ISD::Constant &&
5053 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005054 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00005055 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00005056 NumZero++;
5057 else {
5058 NonZeros |= (1 << i);
5059 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005060 }
5061 }
5062
Chris Lattner97a2a562010-08-26 05:24:29 +00005063 // All undef vector. Return an UNDEF. All zero vectors were handled above.
5064 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00005065 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005066
Chris Lattner67f453a2008-03-09 05:42:06 +00005067 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00005068 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005069 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00005070 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005071
Chris Lattner62098042008-03-09 01:05:04 +00005072 // If this is an insertion of an i64 value on x86-32, and if the top bits of
5073 // the value are obviously zero, truncate the value to i32 and do the
5074 // insertion that way. Only do this if the value is non-constant or if the
5075 // value is a constant being inserted into element 0. It is cheaper to do
5076 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00005077 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00005078 (!IsAllConstants || Idx == 0)) {
5079 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00005080 // Handle SSE only.
5081 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
5082 EVT VecVT = MVT::v4i32;
5083 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00005084
Chris Lattner62098042008-03-09 01:05:04 +00005085 // Truncate the value (which may itself be a constant) to i32, and
5086 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00005087 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00005088 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00005089 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
5090 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005091
Chris Lattner62098042008-03-09 01:05:04 +00005092 // Now we have our 32-bit value zero extended in the low element of
5093 // a vector. If Idx != 0, swizzle it into place.
5094 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005095 SmallVector<int, 4> Mask;
5096 Mask.push_back(Idx);
5097 for (unsigned i = 1; i != VecElts; ++i)
5098 Mask.push_back(i);
5099 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00005100 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00005101 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00005102 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005103 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00005104 }
5105 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005106
Chris Lattner19f79692008-03-08 22:59:52 +00005107 // If we have a constant or non-constant insertion into the low element of
5108 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5109 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00005110 // depending on what the source datatype is.
5111 if (Idx == 0) {
5112 if (NumZero == 0) {
5113 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00005114 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
5115 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00005116 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5117 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
5118 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
5119 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00005120 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
5121 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005122 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
5123 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00005124 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
5125 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
5126 Subtarget->hasSSE2(), DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005127 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00005128 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005129 }
Evan Chengf26ffe92008-05-29 08:22:04 +00005130
5131 // Is it a vector logical left shift?
5132 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00005133 X86::isZeroNode(Op.getOperand(0)) &&
5134 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005135 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00005136 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00005137 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005138 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00005139 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005140 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005141
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005142 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00005143 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005144
Chris Lattner19f79692008-03-08 22:59:52 +00005145 // Otherwise, if this is a vector with i32 or f32 elements, and the element
5146 // is a non-constant being inserted into an element other than the low one,
5147 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
5148 // movd/movss) to move this into the low element, then shuffle it into
5149 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005150 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00005151 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00005152
Evan Cheng0db9fe62006-04-25 20:13:52 +00005153 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00005154 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
5155 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00005156 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005157 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00005158 MaskVec.push_back(i == Idx ? 0 : 1);
5159 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005160 }
5161 }
5162
Chris Lattner67f453a2008-03-09 05:42:06 +00005163 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00005164 if (Values.size() == 1) {
5165 if (EVTBits == 32) {
5166 // Instead of a shuffle like this:
5167 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5168 // Check if it's possible to issue this instead.
5169 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
5170 unsigned Idx = CountTrailingZeros_32(NonZeros);
5171 SDValue Item = Op.getOperand(Idx);
5172 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5173 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5174 }
Dan Gohman475871a2008-07-27 21:46:04 +00005175 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005176 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005177
Dan Gohmana3941172007-07-24 22:55:08 +00005178 // A vector full of immediates; various special cases are already
5179 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005180 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00005181 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00005182
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005183 // For AVX-length vectors, build the individual 128-bit pieces and use
5184 // shuffles to put them in place.
5185 if (VT.getSizeInBits() == 256 && !ISD::isBuildVectorAllZeros(Op.getNode())) {
5186 SmallVector<SDValue, 32> V;
5187 for (unsigned i = 0; i < NumElems; ++i)
5188 V.push_back(Op.getOperand(i));
5189
5190 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
5191
5192 // Build both the lower and upper subvector.
5193 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
5194 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
5195 NumElems/2);
5196
5197 // Recreate the wider vector with the lower and upper part.
Bruno Cardoso Lopes15d03fb2011-07-28 01:26:53 +00005198 SDValue Vec = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), Lower,
5199 DAG.getConstant(0, MVT::i32), DAG, dl);
5200 return Insert128BitVector(Vec, Upper, DAG.getConstant(NumElems/2, MVT::i32),
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005201 DAG, dl);
5202 }
5203
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005204 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005205 if (EVTBits == 64) {
5206 if (NumNonZero == 1) {
5207 // One half is zero or undef.
5208 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005209 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005210 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00005211 return getShuffleVectorZeroOrUndef(V2, Idx, true,
5212 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005213 }
Dan Gohman475871a2008-07-27 21:46:04 +00005214 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005215 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005216
5217 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005218 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005219 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00005220 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005221 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005222 }
5223
Bill Wendling826f36f2007-03-28 00:57:11 +00005224 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005225 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00005226 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005227 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005228 }
5229
5230 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00005231 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00005232 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005233 if (NumElems == 4 && NumZero > 0) {
5234 for (unsigned i = 0; i < 4; ++i) {
5235 bool isZero = !(NonZeros & (1 << i));
5236 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00005237 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005238 else
Dale Johannesenace16102009-02-03 19:33:06 +00005239 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005240 }
5241
5242 for (unsigned i = 0; i < 2; ++i) {
5243 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5244 default: break;
5245 case 0:
5246 V[i] = V[i*2]; // Must be a zero vector.
5247 break;
5248 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005249 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005250 break;
5251 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005252 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005253 break;
5254 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005255 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005256 break;
5257 }
5258 }
5259
Nate Begeman9008ca62009-04-27 18:41:29 +00005260 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005261 bool Reverse = (NonZeros & 0x3) == 2;
5262 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005263 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005264 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5265 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005266 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
5267 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005268 }
5269
Nate Begemanfdea31a2010-03-24 20:49:50 +00005270 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
5271 // Check for a build vector of consecutive loads.
5272 for (unsigned i = 0; i < NumElems; ++i)
5273 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005274
Nate Begemanfdea31a2010-03-24 20:49:50 +00005275 // Check for elements which are consecutive loads.
5276 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5277 if (LD.getNode())
5278 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005279
5280 // For SSE 4.1, use insertps to put the high elements into the low element.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005281 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005282 SDValue Result;
5283 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5284 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5285 else
5286 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005287
Chris Lattner24faf612010-08-28 17:59:08 +00005288 for (unsigned i = 1; i < NumElems; ++i) {
5289 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5290 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005291 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005292 }
5293 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005294 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005295
Chris Lattner6e80e442010-08-28 17:15:43 +00005296 // Otherwise, expand into a number of unpckl*, start by extending each of
5297 // our (non-undef) elements to the full vector width with the element in the
5298 // bottom slot of the vector (which generates no code for SSE).
5299 for (unsigned i = 0; i < NumElems; ++i) {
5300 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5301 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5302 else
5303 V[i] = DAG.getUNDEF(VT);
5304 }
5305
5306 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005307 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5308 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5309 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005310 unsigned EltStride = NumElems >> 1;
5311 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005312 for (unsigned i = 0; i < EltStride; ++i) {
5313 // If V[i+EltStride] is undef and this is the first round of mixing,
5314 // then it is safe to just drop this shuffle: V[i] is already in the
5315 // right place, the one element (since it's the first round) being
5316 // inserted as undef can be dropped. This isn't safe for successive
5317 // rounds because they will permute elements within both vectors.
5318 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5319 EltStride == NumElems/2)
5320 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005321
Chris Lattner6e80e442010-08-28 17:15:43 +00005322 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005323 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005324 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005325 }
5326 return V[0];
5327 }
Dan Gohman475871a2008-07-27 21:46:04 +00005328 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005329}
5330
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005331// LowerMMXCONCAT_VECTORS - We support concatenate two MMX registers and place
5332// them in a MMX register. This is better than doing a stack convert.
5333static SDValue LowerMMXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005334 DebugLoc dl = Op.getDebugLoc();
5335 EVT ResVT = Op.getValueType();
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005336
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005337 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
5338 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
5339 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005340 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005341 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
5342 InVec = Op.getOperand(1);
5343 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5344 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005345 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005346 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
5347 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
5348 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005349 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005350 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
5351 Mask[0] = 0; Mask[1] = 2;
5352 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
5353 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005354 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005355}
5356
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005357// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5358// to create 256-bit vectors from two other 128-bit ones.
5359static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5360 DebugLoc dl = Op.getDebugLoc();
5361 EVT ResVT = Op.getValueType();
5362
5363 assert(ResVT.getSizeInBits() == 256 && "Value type must be 256-bit wide");
5364
5365 SDValue V1 = Op.getOperand(0);
5366 SDValue V2 = Op.getOperand(1);
5367 unsigned NumElems = ResVT.getVectorNumElements();
5368
5369 SDValue V = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, ResVT), V1,
5370 DAG.getConstant(0, MVT::i32), DAG, dl);
5371 return Insert128BitVector(V, V2, DAG.getConstant(NumElems/2, MVT::i32),
5372 DAG, dl);
5373}
5374
5375SDValue
5376X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005377 EVT ResVT = Op.getValueType();
5378
5379 assert(Op.getNumOperands() == 2);
5380 assert((ResVT.getSizeInBits() == 128 || ResVT.getSizeInBits() == 256) &&
5381 "Unsupported CONCAT_VECTORS for value type");
5382
5383 // We support concatenate two MMX registers and place them in a MMX register.
5384 // This is better than doing a stack convert.
5385 if (ResVT.is128BitVector())
5386 return LowerMMXCONCAT_VECTORS(Op, DAG);
5387
5388 // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
5389 // from two other 128-bit ones.
5390 return LowerAVXCONCAT_VECTORS(Op, DAG);
5391}
5392
Nate Begemanb9a47b82009-02-23 08:49:38 +00005393// v8i16 shuffles - Prefer shuffles in the following order:
5394// 1. [all] pshuflw, pshufhw, optional move
5395// 2. [ssse3] 1 x pshufb
5396// 3. [ssse3] 2 x pshufb + 1 x por
5397// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005398SDValue
5399X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
5400 SelectionDAG &DAG) const {
5401 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00005402 SDValue V1 = SVOp->getOperand(0);
5403 SDValue V2 = SVOp->getOperand(1);
5404 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005405 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00005406
Nate Begemanb9a47b82009-02-23 08:49:38 +00005407 // Determine if more than 1 of the words in each of the low and high quadwords
5408 // of the result come from the same quadword of one of the two inputs. Undef
5409 // mask values count as coming from any quadword, for better codegen.
5410 SmallVector<unsigned, 4> LoQuad(4);
5411 SmallVector<unsigned, 4> HiQuad(4);
5412 BitVector InputQuads(4);
5413 for (unsigned i = 0; i < 8; ++i) {
5414 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00005415 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005416 MaskVals.push_back(EltIdx);
5417 if (EltIdx < 0) {
5418 ++Quad[0];
5419 ++Quad[1];
5420 ++Quad[2];
5421 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00005422 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005423 }
5424 ++Quad[EltIdx / 4];
5425 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00005426 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005427
Nate Begemanb9a47b82009-02-23 08:49:38 +00005428 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005429 unsigned MaxQuad = 1;
5430 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005431 if (LoQuad[i] > MaxQuad) {
5432 BestLoQuad = i;
5433 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005434 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005435 }
5436
Nate Begemanb9a47b82009-02-23 08:49:38 +00005437 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005438 MaxQuad = 1;
5439 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005440 if (HiQuad[i] > MaxQuad) {
5441 BestHiQuad = i;
5442 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005443 }
5444 }
5445
Nate Begemanb9a47b82009-02-23 08:49:38 +00005446 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00005447 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00005448 // single pshufb instruction is necessary. If There are more than 2 input
5449 // quads, disable the next transformation since it does not help SSSE3.
5450 bool V1Used = InputQuads[0] || InputQuads[1];
5451 bool V2Used = InputQuads[2] || InputQuads[3];
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005452 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005453 if (InputQuads.count() == 2 && V1Used && V2Used) {
5454 BestLoQuad = InputQuads.find_first();
5455 BestHiQuad = InputQuads.find_next(BestLoQuad);
5456 }
5457 if (InputQuads.count() > 2) {
5458 BestLoQuad = -1;
5459 BestHiQuad = -1;
5460 }
5461 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005462
Nate Begemanb9a47b82009-02-23 08:49:38 +00005463 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5464 // the shuffle mask. If a quad is scored as -1, that means that it contains
5465 // words from all 4 input quadwords.
5466 SDValue NewV;
5467 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005468 SmallVector<int, 8> MaskV;
5469 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
5470 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00005471 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005472 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
5473 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
5474 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005475
Nate Begemanb9a47b82009-02-23 08:49:38 +00005476 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5477 // source words for the shuffle, to aid later transformations.
5478 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00005479 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00005480 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005481 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00005482 if (idx != (int)i)
5483 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005484 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00005485 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005486 AllWordsInNewV = false;
5487 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00005488 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005489
Nate Begemanb9a47b82009-02-23 08:49:38 +00005490 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5491 if (AllWordsInNewV) {
5492 for (int i = 0; i != 8; ++i) {
5493 int idx = MaskVals[i];
5494 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005495 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005496 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005497 if ((idx != i) && idx < 4)
5498 pshufhw = false;
5499 if ((idx != i) && idx > 3)
5500 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00005501 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005502 V1 = NewV;
5503 V2Used = false;
5504 BestLoQuad = 0;
5505 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005506 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005507
Nate Begemanb9a47b82009-02-23 08:49:38 +00005508 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5509 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005510 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005511 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5512 unsigned TargetMask = 0;
5513 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005514 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005515 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
5516 X86::getShufflePSHUFLWImmediate(NewV.getNode());
5517 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005518 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005519 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005520 }
Eric Christopherfd179292009-08-27 18:07:15 +00005521
Nate Begemanb9a47b82009-02-23 08:49:38 +00005522 // If we have SSSE3, and all words of the result are from 1 input vector,
5523 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5524 // is present, fall back to case 4.
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005525 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005526 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005527
Nate Begemanb9a47b82009-02-23 08:49:38 +00005528 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005529 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005530 // mask, and elements that come from V1 in the V2 mask, so that the two
5531 // results can be OR'd together.
5532 bool TwoInputs = V1Used && V2Used;
5533 for (unsigned i = 0; i != 8; ++i) {
5534 int EltIdx = MaskVals[i] * 2;
5535 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005536 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5537 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005538 continue;
5539 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005540 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
5541 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005542 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005543 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005544 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005545 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005546 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005547 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005548 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005549
Nate Begemanb9a47b82009-02-23 08:49:38 +00005550 // Calculate the shuffle mask for the second input, shuffle it, and
5551 // OR it with the first shuffled input.
5552 pshufbMask.clear();
5553 for (unsigned i = 0; i != 8; ++i) {
5554 int EltIdx = MaskVals[i] * 2;
5555 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005556 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5557 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005558 continue;
5559 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005560 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
5561 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005562 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005563 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005564 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005565 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005566 MVT::v16i8, &pshufbMask[0], 16));
5567 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005568 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005569 }
5570
5571 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5572 // and update MaskVals with new element order.
5573 BitVector InOrder(8);
5574 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005575 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005576 for (int i = 0; i != 4; ++i) {
5577 int idx = MaskVals[i];
5578 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005579 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005580 InOrder.set(i);
5581 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005582 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005583 InOrder.set(i);
5584 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005585 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005586 }
5587 }
5588 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005589 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00005590 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005591 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005592
5593 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
5594 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
5595 NewV.getOperand(0),
5596 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
5597 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005598 }
Eric Christopherfd179292009-08-27 18:07:15 +00005599
Nate Begemanb9a47b82009-02-23 08:49:38 +00005600 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5601 // and update MaskVals with the new element order.
5602 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005603 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005604 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005605 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005606 for (unsigned i = 4; i != 8; ++i) {
5607 int idx = MaskVals[i];
5608 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005609 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005610 InOrder.set(i);
5611 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005612 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005613 InOrder.set(i);
5614 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005615 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005616 }
5617 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005618 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005619 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005620
5621 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
5622 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
5623 NewV.getOperand(0),
5624 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
5625 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005626 }
Eric Christopherfd179292009-08-27 18:07:15 +00005627
Nate Begemanb9a47b82009-02-23 08:49:38 +00005628 // In case BestHi & BestLo were both -1, which means each quadword has a word
5629 // from each of the four input quadwords, calculate the InOrder bitvector now
5630 // before falling through to the insert/extract cleanup.
5631 if (BestLoQuad == -1 && BestHiQuad == -1) {
5632 NewV = V1;
5633 for (int i = 0; i != 8; ++i)
5634 if (MaskVals[i] < 0 || MaskVals[i] == i)
5635 InOrder.set(i);
5636 }
Eric Christopherfd179292009-08-27 18:07:15 +00005637
Nate Begemanb9a47b82009-02-23 08:49:38 +00005638 // The other elements are put in the right place using pextrw and pinsrw.
5639 for (unsigned i = 0; i != 8; ++i) {
5640 if (InOrder[i])
5641 continue;
5642 int EltIdx = MaskVals[i];
5643 if (EltIdx < 0)
5644 continue;
5645 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00005646 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005647 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00005648 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005649 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005650 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005651 DAG.getIntPtrConstant(i));
5652 }
5653 return NewV;
5654}
5655
5656// v16i8 shuffles - Prefer shuffles in the following order:
5657// 1. [ssse3] 1 x pshufb
5658// 2. [ssse3] 2 x pshufb + 1 x por
5659// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5660static
Nate Begeman9008ca62009-04-27 18:41:29 +00005661SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005662 SelectionDAG &DAG,
5663 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005664 SDValue V1 = SVOp->getOperand(0);
5665 SDValue V2 = SVOp->getOperand(1);
5666 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005667 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00005668 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00005669
Nate Begemanb9a47b82009-02-23 08:49:38 +00005670 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005671 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005672 // present, fall back to case 3.
5673 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
5674 bool V1Only = true;
5675 bool V2Only = true;
5676 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005677 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00005678 if (EltIdx < 0)
5679 continue;
5680 if (EltIdx < 16)
5681 V2Only = false;
5682 else
5683 V1Only = false;
5684 }
Eric Christopherfd179292009-08-27 18:07:15 +00005685
Nate Begemanb9a47b82009-02-23 08:49:38 +00005686 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
5687 if (TLI.getSubtarget()->hasSSSE3()) {
5688 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005689
Nate Begemanb9a47b82009-02-23 08:49:38 +00005690 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005691 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005692 //
5693 // Otherwise, we have elements from both input vectors, and must zero out
5694 // elements that come from V2 in the first mask, and V1 in the second mask
5695 // so that we can OR them together.
5696 bool TwoInputs = !(V1Only || V2Only);
5697 for (unsigned i = 0; i != 16; ++i) {
5698 int EltIdx = MaskVals[i];
5699 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005700 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005701 continue;
5702 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005703 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005704 }
5705 // If all the elements are from V2, assign it to V1 and return after
5706 // building the first pshufb.
5707 if (V2Only)
5708 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00005709 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005710 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005711 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005712 if (!TwoInputs)
5713 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005714
Nate Begemanb9a47b82009-02-23 08:49:38 +00005715 // Calculate the shuffle mask for the second input, shuffle it, and
5716 // OR it with the first shuffled input.
5717 pshufbMask.clear();
5718 for (unsigned i = 0; i != 16; ++i) {
5719 int EltIdx = MaskVals[i];
5720 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005721 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005722 continue;
5723 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005724 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005725 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005726 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005727 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005728 MVT::v16i8, &pshufbMask[0], 16));
5729 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005730 }
Eric Christopherfd179292009-08-27 18:07:15 +00005731
Nate Begemanb9a47b82009-02-23 08:49:38 +00005732 // No SSSE3 - Calculate in place words and then fix all out of place words
5733 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5734 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005735 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5736 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005737 SDValue NewV = V2Only ? V2 : V1;
5738 for (int i = 0; i != 8; ++i) {
5739 int Elt0 = MaskVals[i*2];
5740 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005741
Nate Begemanb9a47b82009-02-23 08:49:38 +00005742 // This word of the result is all undef, skip it.
5743 if (Elt0 < 0 && Elt1 < 0)
5744 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005745
Nate Begemanb9a47b82009-02-23 08:49:38 +00005746 // This word of the result is already in the correct place, skip it.
5747 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
5748 continue;
5749 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
5750 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005751
Nate Begemanb9a47b82009-02-23 08:49:38 +00005752 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5753 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5754 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005755
5756 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5757 // using a single extract together, load it and store it.
5758 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005759 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005760 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005761 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005762 DAG.getIntPtrConstant(i));
5763 continue;
5764 }
5765
Nate Begemanb9a47b82009-02-23 08:49:38 +00005766 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00005767 // source byte is not also odd, shift the extracted word left 8 bits
5768 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005769 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005770 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005771 DAG.getIntPtrConstant(Elt1 / 2));
5772 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005773 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00005774 DAG.getConstant(8,
5775 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005776 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005777 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
5778 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005779 }
5780 // If Elt0 is defined, extract it from the appropriate source. If the
5781 // source byte is not also even, shift the extracted word right 8 bits. If
5782 // Elt1 was also defined, OR the extracted values together before
5783 // inserting them in the result.
5784 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005785 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005786 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
5787 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005788 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00005789 DAG.getConstant(8,
5790 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005791 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005792 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
5793 DAG.getConstant(0x00FF, MVT::i16));
5794 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00005795 : InsElt0;
5796 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005797 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005798 DAG.getIntPtrConstant(i));
5799 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005800 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005801}
5802
Evan Cheng7a831ce2007-12-15 03:00:47 +00005803/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005804/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00005805/// done when every pair / quad of shuffle mask elements point to elements in
5806/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005807/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00005808static
Nate Begeman9008ca62009-04-27 18:41:29 +00005809SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005810 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00005811 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00005812 SDValue V1 = SVOp->getOperand(0);
5813 SDValue V2 = SVOp->getOperand(1);
5814 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00005815 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005816 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00005817 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005818 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005819 case MVT::v4f32: NewVT = MVT::v2f64; break;
5820 case MVT::v4i32: NewVT = MVT::v2i64; break;
5821 case MVT::v8i16: NewVT = MVT::v4i32; break;
5822 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00005823 }
5824
Nate Begeman9008ca62009-04-27 18:41:29 +00005825 int Scale = NumElems / NewWidth;
5826 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00005827 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005828 int StartIdx = -1;
5829 for (int j = 0; j < Scale; ++j) {
5830 int EltIdx = SVOp->getMaskElt(i+j);
5831 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005832 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00005833 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00005834 StartIdx = EltIdx - (EltIdx % Scale);
5835 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00005836 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005837 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005838 if (StartIdx == -1)
5839 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00005840 else
Nate Begeman9008ca62009-04-27 18:41:29 +00005841 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005842 }
5843
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005844 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
5845 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00005846 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005847}
5848
Evan Chengd880b972008-05-09 21:53:03 +00005849/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005850///
Owen Andersone50ed302009-08-10 22:56:29 +00005851static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00005852 SDValue SrcOp, SelectionDAG &DAG,
5853 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005854 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005855 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00005856 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00005857 LD = dyn_cast<LoadSDNode>(SrcOp);
5858 if (!LD) {
5859 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
5860 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00005861 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00005862 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00005863 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005864 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00005865 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005866 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00005867 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005868 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005869 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
5870 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
5871 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00005872 SrcOp.getOperand(0)
5873 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005874 }
5875 }
5876 }
5877
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005878 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005879 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005880 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00005881 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005882}
5883
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00005884/// areShuffleHalvesWithinDisjointLanes - Check whether each half of a vector
5885/// shuffle node referes to only one lane in the sources.
5886static bool areShuffleHalvesWithinDisjointLanes(ShuffleVectorSDNode *SVOp) {
5887 EVT VT = SVOp->getValueType(0);
5888 int NumElems = VT.getVectorNumElements();
5889 int HalfSize = NumElems/2;
5890 SmallVector<int, 16> M;
5891 SVOp->getMask(M);
5892 bool MatchA = false, MatchB = false;
5893
5894 for (int l = 0; l < NumElems*2; l += HalfSize) {
5895 if (isUndefOrInRange(M, 0, HalfSize, l, l+HalfSize)) {
5896 MatchA = true;
5897 break;
5898 }
5899 }
5900
5901 for (int l = 0; l < NumElems*2; l += HalfSize) {
5902 if (isUndefOrInRange(M, HalfSize, HalfSize, l, l+HalfSize)) {
5903 MatchB = true;
5904 break;
5905 }
5906 }
5907
5908 return MatchA && MatchB;
5909}
5910
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00005911/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
5912/// which could not be matched by any known target speficic shuffle
5913static SDValue
5914LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00005915 if (areShuffleHalvesWithinDisjointLanes(SVOp)) {
5916 // If each half of a vector shuffle node referes to only one lane in the
5917 // source vectors, extract each used 128-bit lane and shuffle them using
5918 // 128-bit shuffles. Then, concatenate the results. Otherwise leave
5919 // the work to the legalizer.
5920 DebugLoc dl = SVOp->getDebugLoc();
5921 EVT VT = SVOp->getValueType(0);
5922 int NumElems = VT.getVectorNumElements();
5923 int HalfSize = NumElems/2;
5924
5925 // Extract the reference for each half
5926 int FstVecExtractIdx = 0, SndVecExtractIdx = 0;
5927 int FstVecOpNum = 0, SndVecOpNum = 0;
5928 for (int i = 0; i < HalfSize; ++i) {
5929 int Elt = SVOp->getMaskElt(i);
5930 if (SVOp->getMaskElt(i) < 0)
5931 continue;
5932 FstVecOpNum = Elt/NumElems;
5933 FstVecExtractIdx = Elt % NumElems < HalfSize ? 0 : HalfSize;
5934 break;
5935 }
5936 for (int i = HalfSize; i < NumElems; ++i) {
5937 int Elt = SVOp->getMaskElt(i);
5938 if (SVOp->getMaskElt(i) < 0)
5939 continue;
5940 SndVecOpNum = Elt/NumElems;
5941 SndVecExtractIdx = Elt % NumElems < HalfSize ? 0 : HalfSize;
5942 break;
5943 }
5944
5945 // Extract the subvectors
5946 SDValue V1 = Extract128BitVector(SVOp->getOperand(FstVecOpNum),
5947 DAG.getConstant(FstVecExtractIdx, MVT::i32), DAG, dl);
5948 SDValue V2 = Extract128BitVector(SVOp->getOperand(SndVecOpNum),
5949 DAG.getConstant(SndVecExtractIdx, MVT::i32), DAG, dl);
5950
5951 // Generate 128-bit shuffles
5952 SmallVector<int, 16> MaskV1, MaskV2;
5953 for (int i = 0; i < HalfSize; ++i) {
5954 int Elt = SVOp->getMaskElt(i);
5955 MaskV1.push_back(Elt < 0 ? Elt : Elt % HalfSize);
5956 }
5957 for (int i = HalfSize; i < NumElems; ++i) {
5958 int Elt = SVOp->getMaskElt(i);
5959 MaskV2.push_back(Elt < 0 ? Elt : Elt % HalfSize);
5960 }
5961
5962 EVT NVT = V1.getValueType();
5963 V1 = DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &MaskV1[0]);
5964 V2 = DAG.getVectorShuffle(NVT, dl, V2, DAG.getUNDEF(NVT), &MaskV2[0]);
5965
5966 // Concatenate the result back
5967 SDValue V = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), V1,
5968 DAG.getConstant(0, MVT::i32), DAG, dl);
5969 return Insert128BitVector(V, V2, DAG.getConstant(NumElems/2, MVT::i32),
5970 DAG, dl);
5971 }
5972
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00005973 return SDValue();
5974}
5975
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00005976/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
5977/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00005978static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00005979LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005980 SDValue V1 = SVOp->getOperand(0);
5981 SDValue V2 = SVOp->getOperand(1);
5982 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005983 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00005984
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00005985 assert(VT.getSizeInBits() == 128 && "Unsupported vector size");
5986
Evan Chengace3c172008-07-22 21:13:36 +00005987 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00005988 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00005989 SmallVector<int, 8> Mask1(4U, -1);
5990 SmallVector<int, 8> PermMask;
5991 SVOp->getMask(PermMask);
5992
Evan Chengace3c172008-07-22 21:13:36 +00005993 unsigned NumHi = 0;
5994 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00005995 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005996 int Idx = PermMask[i];
5997 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005998 Locs[i] = std::make_pair(-1, -1);
5999 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006000 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
6001 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006002 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00006003 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006004 NumLo++;
6005 } else {
6006 Locs[i] = std::make_pair(1, NumHi);
6007 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00006008 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006009 NumHi++;
6010 }
6011 }
6012 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006013
Evan Chengace3c172008-07-22 21:13:36 +00006014 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006015 // If no more than two elements come from either vector. This can be
6016 // implemented with two shuffles. First shuffle gather the elements.
6017 // The second shuffle, which takes the first shuffle as both of its
6018 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00006019 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006020
Nate Begeman9008ca62009-04-27 18:41:29 +00006021 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00006022
Evan Chengace3c172008-07-22 21:13:36 +00006023 for (unsigned i = 0; i != 4; ++i) {
6024 if (Locs[i].first == -1)
6025 continue;
6026 else {
6027 unsigned Idx = (i < 2) ? 0 : 4;
6028 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006029 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006030 }
6031 }
6032
Nate Begeman9008ca62009-04-27 18:41:29 +00006033 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006034 } else if (NumLo == 3 || NumHi == 3) {
6035 // Otherwise, we must have three elements from one vector, call it X, and
6036 // one element from the other, call it Y. First, use a shufps to build an
6037 // intermediate vector with the one element from Y and the element from X
6038 // that will be in the same half in the final destination (the indexes don't
6039 // matter). Then, use a shufps to build the final vector, taking the half
6040 // containing the element from Y from the intermediate, and the other half
6041 // from X.
6042 if (NumHi == 3) {
6043 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00006044 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006045 std::swap(V1, V2);
6046 }
6047
6048 // Find the element from V2.
6049 unsigned HiIndex;
6050 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006051 int Val = PermMask[HiIndex];
6052 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006053 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006054 if (Val >= 4)
6055 break;
6056 }
6057
Nate Begeman9008ca62009-04-27 18:41:29 +00006058 Mask1[0] = PermMask[HiIndex];
6059 Mask1[1] = -1;
6060 Mask1[2] = PermMask[HiIndex^1];
6061 Mask1[3] = -1;
6062 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006063
6064 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006065 Mask1[0] = PermMask[0];
6066 Mask1[1] = PermMask[1];
6067 Mask1[2] = HiIndex & 1 ? 6 : 4;
6068 Mask1[3] = HiIndex & 1 ? 4 : 6;
6069 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006070 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006071 Mask1[0] = HiIndex & 1 ? 2 : 0;
6072 Mask1[1] = HiIndex & 1 ? 0 : 2;
6073 Mask1[2] = PermMask[2];
6074 Mask1[3] = PermMask[3];
6075 if (Mask1[2] >= 0)
6076 Mask1[2] += 4;
6077 if (Mask1[3] >= 0)
6078 Mask1[3] += 4;
6079 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006080 }
Evan Chengace3c172008-07-22 21:13:36 +00006081 }
6082
6083 // Break it into (shuffle shuffle_hi, shuffle_lo).
6084 Locs.clear();
David Greenec4db4e52011-02-28 19:06:56 +00006085 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00006086 SmallVector<int,8> LoMask(4U, -1);
6087 SmallVector<int,8> HiMask(4U, -1);
6088
6089 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00006090 unsigned MaskIdx = 0;
6091 unsigned LoIdx = 0;
6092 unsigned HiIdx = 2;
6093 for (unsigned i = 0; i != 4; ++i) {
6094 if (i == 2) {
6095 MaskPtr = &HiMask;
6096 MaskIdx = 1;
6097 LoIdx = 0;
6098 HiIdx = 2;
6099 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006100 int Idx = PermMask[i];
6101 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006102 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00006103 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006104 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00006105 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006106 LoIdx++;
6107 } else {
6108 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00006109 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006110 HiIdx++;
6111 }
6112 }
6113
Nate Begeman9008ca62009-04-27 18:41:29 +00006114 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6115 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
6116 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00006117 for (unsigned i = 0; i != 4; ++i) {
6118 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006119 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00006120 } else {
6121 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006122 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00006123 }
6124 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006125 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006126}
6127
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006128static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006129 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006130 V = V.getOperand(0);
6131 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6132 V = V.getOperand(0);
6133 if (MayFoldLoad(V))
6134 return true;
6135 return false;
6136}
6137
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006138// FIXME: the version above should always be used. Since there's
6139// a bug where several vector shuffles can't be folded because the
6140// DAG is not updated during lowering and a node claims to have two
6141// uses while it only has one, use this version, and let isel match
6142// another instruction if the load really happens to have more than
6143// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00006144// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006145static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006146 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006147 V = V.getOperand(0);
6148 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6149 V = V.getOperand(0);
6150 if (ISD::isNormalLoad(V.getNode()))
6151 return true;
6152 return false;
6153}
6154
6155/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
6156/// a vector extract, and if both can be later optimized into a single load.
6157/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
6158/// here because otherwise a target specific shuffle node is going to be
6159/// emitted for this shuffle, and the optimization not done.
6160/// FIXME: This is probably not the best approach, but fix the problem
6161/// until the right path is decided.
6162static
6163bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
6164 const TargetLowering &TLI) {
6165 EVT VT = V.getValueType();
6166 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
6167
6168 // Be sure that the vector shuffle is present in a pattern like this:
6169 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
6170 if (!V.hasOneUse())
6171 return false;
6172
6173 SDNode *N = *V.getNode()->use_begin();
6174 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
6175 return false;
6176
6177 SDValue EltNo = N->getOperand(1);
6178 if (!isa<ConstantSDNode>(EltNo))
6179 return false;
6180
6181 // If the bit convert changed the number of elements, it is unsafe
6182 // to examine the mask.
6183 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006184 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006185 EVT SrcVT = V.getOperand(0).getValueType();
6186 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
6187 return false;
6188 V = V.getOperand(0);
6189 HasShuffleIntoBitcast = true;
6190 }
6191
6192 // Select the input vector, guarding against out of range extract vector.
6193 unsigned NumElems = VT.getVectorNumElements();
6194 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
6195 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
6196 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
6197
6198 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006199 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006200 V = V.getOperand(0);
6201
6202 if (ISD::isNormalLoad(V.getNode())) {
6203 // Is the original load suitable?
6204 LoadSDNode *LN0 = cast<LoadSDNode>(V);
6205
6206 // FIXME: avoid the multi-use bug that is preventing lots of
6207 // of foldings to be detected, this is still wrong of course, but
6208 // give the temporary desired behavior, and if it happens that
6209 // the load has real more uses, during isel it will not fold, and
6210 // will generate poor code.
6211 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
6212 return false;
6213
6214 if (!HasShuffleIntoBitcast)
6215 return true;
6216
6217 // If there's a bitcast before the shuffle, check if the load type and
6218 // alignment is valid.
6219 unsigned Align = LN0->getAlignment();
6220 unsigned NewAlign =
6221 TLI.getTargetData()->getABITypeAlignment(
6222 VT.getTypeForEVT(*DAG.getContext()));
6223
6224 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
6225 return false;
6226 }
6227
6228 return true;
6229}
6230
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006231static
Evan Cheng835580f2010-10-07 20:50:20 +00006232SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
6233 EVT VT = Op.getValueType();
6234
6235 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006236 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6237 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006238 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6239 V1, DAG));
6240}
6241
6242static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006243SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
6244 bool HasSSE2) {
6245 SDValue V1 = Op.getOperand(0);
6246 SDValue V2 = Op.getOperand(1);
6247 EVT VT = Op.getValueType();
6248
6249 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6250
6251 if (HasSSE2 && VT == MVT::v2f64)
6252 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6253
Evan Cheng0899f5c2011-08-31 02:05:24 +00006254 // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
6255 return DAG.getNode(ISD::BITCAST, dl, VT,
6256 getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
6257 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
6258 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006259}
6260
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006261static
6262SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
6263 SDValue V1 = Op.getOperand(0);
6264 SDValue V2 = Op.getOperand(1);
6265 EVT VT = Op.getValueType();
6266
6267 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6268 "unsupported shuffle type");
6269
6270 if (V2.getOpcode() == ISD::UNDEF)
6271 V2 = V1;
6272
6273 // v4i32 or v4f32
6274 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6275}
6276
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006277static inline unsigned getSHUFPOpcode(EVT VT) {
6278 switch(VT.getSimpleVT().SimpleTy) {
6279 case MVT::v8i32: // Use fp unit for int unpack.
6280 case MVT::v8f32:
6281 case MVT::v4i32: // Use fp unit for int unpack.
6282 case MVT::v4f32: return X86ISD::SHUFPS;
6283 case MVT::v4i64: // Use fp unit for int unpack.
6284 case MVT::v4f64:
6285 case MVT::v2i64: // Use fp unit for int unpack.
6286 case MVT::v2f64: return X86ISD::SHUFPD;
6287 default:
6288 llvm_unreachable("Unknown type for shufp*");
6289 }
6290 return 0;
6291}
6292
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006293static
6294SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
6295 SDValue V1 = Op.getOperand(0);
6296 SDValue V2 = Op.getOperand(1);
6297 EVT VT = Op.getValueType();
6298 unsigned NumElems = VT.getVectorNumElements();
6299
6300 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6301 // operand of these instructions is only memory, so check if there's a
6302 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6303 // same masks.
6304 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006305
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006306 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006307 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006308 CanFoldLoad = true;
6309
6310 // When V1 is a load, it can be folded later into a store in isel, example:
6311 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6312 // turns into:
6313 // (MOVLPSmr addr:$src1, VR128:$src2)
6314 // So, recognize this potential and also use MOVLPS or MOVLPD
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006315 if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006316 CanFoldLoad = true;
6317
Eric Christopher893a8822011-02-20 05:04:42 +00006318 // Both of them can't be memory operations though.
6319 if (MayFoldVectorLoad(V1) && MayFoldVectorLoad(V2))
6320 CanFoldLoad = false;
Owen Anderson95771af2011-02-25 21:41:48 +00006321
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006322 if (CanFoldLoad) {
6323 if (HasSSE2 && NumElems == 2)
6324 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6325
6326 if (NumElems == 4)
6327 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
6328 }
6329
6330 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6331 // movl and movlp will both match v2i64, but v2i64 is never matched by
6332 // movl earlier because we make it strict to avoid messing with the movlp load
6333 // folding logic (see the code above getMOVLP call). Match it here then,
6334 // this is horrible, but will stay like this until we move all shuffle
6335 // matching to x86 specific nodes. Note that for the 1st condition all
6336 // types are matched with movsd.
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006337 if (HasSSE2) {
6338 if (NumElems == 2)
6339 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006340 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006341 }
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006342
6343 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6344
6345 // Invert the operand order and use SHUFPS to match it.
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006346 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V2, V1,
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006347 X86::getShuffleSHUFImmediate(SVOp), DAG);
6348}
6349
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006350static inline unsigned getUNPCKLOpcode(EVT VT) {
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006351 switch(VT.getSimpleVT().SimpleTy) {
6352 case MVT::v4i32: return X86ISD::PUNPCKLDQ;
6353 case MVT::v2i64: return X86ISD::PUNPCKLQDQ;
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00006354 case MVT::v4f32: return X86ISD::UNPCKLPS;
6355 case MVT::v2f64: return X86ISD::UNPCKLPD;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006356 case MVT::v8i32: // Use fp unit for int unpack.
David Greenec4db4e52011-02-28 19:06:56 +00006357 case MVT::v8f32: return X86ISD::VUNPCKLPSY;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006358 case MVT::v4i64: // Use fp unit for int unpack.
David Greenec4db4e52011-02-28 19:06:56 +00006359 case MVT::v4f64: return X86ISD::VUNPCKLPDY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006360 case MVT::v16i8: return X86ISD::PUNPCKLBW;
6361 case MVT::v8i16: return X86ISD::PUNPCKLWD;
6362 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00006363 llvm_unreachable("Unknown type for unpckl");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006364 }
6365 return 0;
6366}
6367
6368static inline unsigned getUNPCKHOpcode(EVT VT) {
6369 switch(VT.getSimpleVT().SimpleTy) {
6370 case MVT::v4i32: return X86ISD::PUNPCKHDQ;
6371 case MVT::v2i64: return X86ISD::PUNPCKHQDQ;
6372 case MVT::v4f32: return X86ISD::UNPCKHPS;
6373 case MVT::v2f64: return X86ISD::UNPCKHPD;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006374 case MVT::v8i32: // Use fp unit for int unpack.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00006375 case MVT::v8f32: return X86ISD::VUNPCKHPSY;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006376 case MVT::v4i64: // Use fp unit for int unpack.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00006377 case MVT::v4f64: return X86ISD::VUNPCKHPDY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006378 case MVT::v16i8: return X86ISD::PUNPCKHBW;
6379 case MVT::v8i16: return X86ISD::PUNPCKHWD;
6380 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00006381 llvm_unreachable("Unknown type for unpckh");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006382 }
6383 return 0;
6384}
6385
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006386static inline unsigned getVPERMILOpcode(EVT VT) {
6387 switch(VT.getSimpleVT().SimpleTy) {
6388 case MVT::v4i32:
6389 case MVT::v4f32: return X86ISD::VPERMILPS;
6390 case MVT::v2i64:
6391 case MVT::v2f64: return X86ISD::VPERMILPD;
6392 case MVT::v8i32:
6393 case MVT::v8f32: return X86ISD::VPERMILPSY;
6394 case MVT::v4i64:
6395 case MVT::v4f64: return X86ISD::VPERMILPDY;
6396 default:
6397 llvm_unreachable("Unknown type for vpermil");
6398 }
6399 return 0;
6400}
6401
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006402/// isVectorBroadcast - Check if the node chain is suitable to be xformed to
6403/// a vbroadcast node. The nodes are suitable whenever we can fold a load coming
6404/// from a 32 or 64 bit scalar. Update Op to the desired load to be folded.
6405static bool isVectorBroadcast(SDValue &Op) {
6406 EVT VT = Op.getValueType();
6407 bool Is256 = VT.getSizeInBits() == 256;
6408
6409 assert((VT.getSizeInBits() == 128 || Is256) &&
6410 "Unsupported type for vbroadcast node");
6411
6412 SDValue V = Op;
6413 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
6414 V = V.getOperand(0);
6415
6416 if (Is256 && !(V.hasOneUse() &&
6417 V.getOpcode() == ISD::INSERT_SUBVECTOR &&
6418 V.getOperand(0).getOpcode() == ISD::UNDEF))
6419 return false;
6420
6421 if (Is256)
6422 V = V.getOperand(1);
Bruno Cardoso Lopesa39ccdb2011-09-01 18:15:06 +00006423
6424 if (!V.hasOneUse())
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006425 return false;
6426
6427 // Check the source scalar_to_vector type. 256-bit broadcasts are
6428 // supported for 32/64-bit sizes, while 128-bit ones are only supported
6429 // for 32-bit scalars.
Bruno Cardoso Lopesa39ccdb2011-09-01 18:15:06 +00006430 if (V.getOpcode() != ISD::SCALAR_TO_VECTOR)
6431 return false;
6432
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006433 unsigned ScalarSize = V.getOperand(0).getValueType().getSizeInBits();
6434 if (ScalarSize != 32 && ScalarSize != 64)
6435 return false;
6436 if (!Is256 && ScalarSize == 64)
6437 return false;
6438
6439 V = V.getOperand(0);
6440 if (!MayFoldLoad(V))
6441 return false;
6442
6443 // Return the load node
6444 Op = V;
6445 return true;
6446}
6447
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006448static
6449SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006450 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006451 const X86Subtarget *Subtarget) {
6452 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6453 EVT VT = Op.getValueType();
6454 DebugLoc dl = Op.getDebugLoc();
6455 SDValue V1 = Op.getOperand(0);
6456 SDValue V2 = Op.getOperand(1);
6457
6458 if (isZeroShuffle(SVOp))
6459 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
6460
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006461 // Handle splat operations
6462 if (SVOp->isSplat()) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006463 unsigned NumElem = VT.getVectorNumElements();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006464 int Size = VT.getSizeInBits();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006465 // Special case, this is the only place now where it's allowed to return
6466 // a vector_shuffle operation without using a target specific node, because
6467 // *hopefully* it will be optimized away by the dag combiner. FIXME: should
6468 // this be moved to DAGCombine instead?
6469 if (NumElem <= 4 && CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006470 return Op;
6471
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006472 // Use vbroadcast whenever the splat comes from a foldable load
6473 if (Subtarget->hasAVX() && isVectorBroadcast(V1))
6474 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, V1);
6475
Bruno Cardoso Lopes6a32adc2011-07-25 23:05:25 +00006476 // Handle splats by matching through known shuffle masks
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006477 if ((Size == 128 && NumElem <= 4) ||
6478 (Size == 256 && NumElem < 8))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006479 return SDValue();
6480
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00006481 // All remaning splats are promoted to target supported vector shuffles.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006482 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006483 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006484
6485 // If the shuffle can be profitably rewritten as a narrower shuffle, then
6486 // do it!
6487 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
6488 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6489 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006490 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006491 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
6492 // FIXME: Figure out a cleaner way to do this.
6493 // Try to make use of movq to zero out the top part.
6494 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
6495 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6496 if (NewOp.getNode()) {
6497 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
6498 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
6499 DAG, Subtarget, dl);
6500 }
6501 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
6502 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6503 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
6504 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
6505 DAG, Subtarget, dl);
6506 }
6507 }
6508 return SDValue();
6509}
6510
Dan Gohman475871a2008-07-27 21:46:04 +00006511SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006512X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00006513 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00006514 SDValue V1 = Op.getOperand(0);
6515 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006516 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006517 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006518 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006519 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006520 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
6521 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00006522 bool V1IsSplat = false;
6523 bool V2IsSplat = false;
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006524 bool HasSSE2 = Subtarget->hasSSE2() || Subtarget->hasAVX();
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006525 bool HasSSE3 = Subtarget->hasSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006526 bool HasSSSE3 = Subtarget->hasSSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006527 MachineFunction &MF = DAG.getMachineFunction();
6528 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006529
Dale Johannesen0488fb62010-09-30 23:57:10 +00006530 // Shuffle operations on MMX not supported.
6531 if (isMMX)
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00006532 return Op;
6533
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006534 // Vector shuffle lowering takes 3 steps:
6535 //
6536 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
6537 // narrowing and commutation of operands should be handled.
6538 // 2) Matching of shuffles with known shuffle masks to x86 target specific
6539 // shuffle nodes.
6540 // 3) Rewriting of unmatched masks into new generic shuffle operations,
6541 // so the shuffle can be broken into other shuffles and the legalizer can
6542 // try the lowering again.
6543 //
6544 // The general ideia is that no vector_shuffle operation should be left to
6545 // be matched during isel, all of them must be converted to a target specific
6546 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00006547
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006548 // Normalize the input vectors. Here splats, zeroed vectors, profitable
6549 // narrowing and commutation of operands should be handled. The actual code
6550 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006551 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006552 if (NewOp.getNode())
6553 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00006554
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006555 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
6556 // unpckh_undef). Only use pshufd if speed is more important than size.
6557 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006558 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006559 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
Rafael Espindola23e31012011-07-22 18:56:05 +00006560 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00006561
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006562 if (X86::isMOVDDUPMask(SVOp) && HasSSE3 && V2IsUndef &&
Dale Johannesen0488fb62010-09-30 23:57:10 +00006563 RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00006564 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006565
Dale Johannesen0488fb62010-09-30 23:57:10 +00006566 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006567 return getMOVHighToLow(Op, dl, DAG);
6568
6569 // Use to match splats
6570 if (HasSSE2 && X86::isUNPCKHMask(SVOp) && V2IsUndef &&
6571 (VT == MVT::v2f64 || VT == MVT::v2i64))
6572 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
6573
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006574 if (X86::isPSHUFDMask(SVOp)) {
6575 // The actual implementation will match the mask in the if above and then
6576 // during isel it can match several different instructions, not only pshufd
6577 // as its name says, sad but true, emulate the behavior for now...
6578 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
6579 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
6580
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006581 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
6582
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006583 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006584 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
6585
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006586 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V1,
6587 TargetMask, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006588 }
Eric Christopherfd179292009-08-27 18:07:15 +00006589
Evan Chengf26ffe92008-05-29 08:22:04 +00006590 // Check if this can be converted into a logical shift.
6591 bool isLeft = false;
6592 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00006593 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00006594 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00006595 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00006596 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006597 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00006598 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006599 EVT EltVT = VT.getVectorElementType();
6600 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006601 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006602 }
Eric Christopherfd179292009-08-27 18:07:15 +00006603
Nate Begeman9008ca62009-04-27 18:41:29 +00006604 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006605 if (V1IsUndef)
6606 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00006607 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00006608 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00006609 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006610 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006611 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6612
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006613 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006614 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6615 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00006616 }
Eric Christopherfd179292009-08-27 18:07:15 +00006617
Nate Begeman9008ca62009-04-27 18:41:29 +00006618 // FIXME: fold these into legal mask.
Dale Johannesen0488fb62010-09-30 23:57:10 +00006619 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp))
6620 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006621
Dale Johannesen0488fb62010-09-30 23:57:10 +00006622 if (X86::isMOVHLPSMask(SVOp))
6623 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006624
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00006625 if (X86::isMOVSHDUPMask(SVOp, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006626 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006627
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00006628 if (X86::isMOVSLDUPMask(SVOp, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006629 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00006630
Dale Johannesen0488fb62010-09-30 23:57:10 +00006631 if (X86::isMOVLPMask(SVOp))
6632 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006633
Nate Begeman9008ca62009-04-27 18:41:29 +00006634 if (ShouldXformToMOVHLPS(SVOp) ||
6635 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
6636 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006637
Evan Chengf26ffe92008-05-29 08:22:04 +00006638 if (isShift) {
6639 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006640 EVT EltVT = VT.getVectorElementType();
6641 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006642 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006643 }
Eric Christopherfd179292009-08-27 18:07:15 +00006644
Evan Cheng9eca5e82006-10-25 21:49:50 +00006645 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00006646 // FIXME: This should also accept a bitcast of a splat? Be careful, not
6647 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00006648 V1IsSplat = isSplatVector(V1.getNode());
6649 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006650
Chris Lattner8a594482007-11-25 00:24:49 +00006651 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00006652 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006653 Op = CommuteVectorShuffle(SVOp, DAG);
6654 SVOp = cast<ShuffleVectorSDNode>(Op);
6655 V1 = SVOp->getOperand(0);
6656 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00006657 std::swap(V1IsSplat, V2IsSplat);
6658 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006659 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00006660 }
6661
Nate Begeman9008ca62009-04-27 18:41:29 +00006662 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
6663 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00006664 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00006665 return V1;
6666 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6667 // the instruction selector will not match, so get a canonical MOVL with
6668 // swapped operands to undo the commute.
6669 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006670 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006671
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006672 if (X86::isUNPCKLMask(SVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006673 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006674
6675 if (X86::isUNPCKHMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006676 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006677
Evan Cheng9bbbb982006-10-25 20:48:19 +00006678 if (V2IsSplat) {
6679 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006680 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00006681 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00006682 SDValue NewMask = NormalizeMask(SVOp, DAG);
6683 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
6684 if (NSVOp != SVOp) {
6685 if (X86::isUNPCKLMask(NSVOp, true)) {
6686 return NewMask;
6687 } else if (X86::isUNPCKHMask(NSVOp, true)) {
6688 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006689 }
6690 }
6691 }
6692
Evan Cheng9eca5e82006-10-25 21:49:50 +00006693 if (Commuted) {
6694 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006695 // FIXME: this seems wrong.
6696 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
6697 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006698
6699 if (X86::isUNPCKLMask(NewSVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006700 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V2, V1, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006701
6702 if (X86::isUNPCKHMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006703 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V2, V1, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006704 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006705
Nate Begeman9008ca62009-04-27 18:41:29 +00006706 // Normalize the node to match x86 shuffle ops if needed
Dale Johannesen0488fb62010-09-30 23:57:10 +00006707 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
Nate Begeman9008ca62009-04-27 18:41:29 +00006708 return CommuteVectorShuffle(SVOp, DAG);
6709
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006710 // The checks below are all present in isShuffleMaskLegal, but they are
6711 // inlined here right now to enable us to directly emit target specific
6712 // nodes, and remove one by one until they don't return Op anymore.
6713 SmallVector<int, 16> M;
6714 SVOp->getMask(M);
6715
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006716 if (isPALIGNRMask(M, VT, HasSSSE3))
6717 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
6718 X86::getShufflePALIGNRImmediate(SVOp),
6719 DAG);
6720
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006721 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6722 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00006723 if (VT == MVT::v2f64)
6724 return getTargetShuffleNode(X86ISD::UNPCKLPD, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006725 if (VT == MVT::v2i64)
6726 return getTargetShuffleNode(X86ISD::PUNPCKLQDQ, dl, VT, V1, V1, DAG);
6727 }
6728
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006729 if (isPSHUFHWMask(M, VT))
6730 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
6731 X86::getShufflePSHUFHWImmediate(SVOp),
6732 DAG);
6733
6734 if (isPSHUFLWMask(M, VT))
6735 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
6736 X86::getShufflePSHUFLWImmediate(SVOp),
6737 DAG);
6738
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006739 if (isSHUFPMask(M, VT))
6740 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6741 X86::getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00006742
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006743 if (X86::isUNPCKL_v_undef_Mask(SVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006744 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006745 if (X86::isUNPCKH_v_undef_Mask(SVOp))
Rafael Espindola23e31012011-07-22 18:56:05 +00006746 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006747
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006748 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006749 // Generate target specific nodes for 128 or 256-bit shuffles only
6750 // supported in the AVX instruction set.
6751 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006752
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006753 // Handle VMOVDDUPY permutations
6754 if (isMOVDDUPYMask(SVOp, Subtarget))
6755 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
6756
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006757 // Handle VPERMILPS* permutations
6758 if (isVPERMILPSMask(M, VT, Subtarget))
6759 return getTargetShuffleNode(getVPERMILOpcode(VT), dl, VT, V1,
6760 getShuffleVPERMILPSImmediate(SVOp), DAG);
6761
6762 // Handle VPERMILPD* permutations
6763 if (isVPERMILPDMask(M, VT, Subtarget))
6764 return getTargetShuffleNode(getVPERMILOpcode(VT), dl, VT, V1,
6765 getShuffleVPERMILPDImmediate(SVOp), DAG);
6766
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00006767 // Handle VPERM2F128 permutations
6768 if (isVPERM2F128Mask(M, VT, Subtarget))
6769 return getTargetShuffleNode(X86ISD::VPERM2F128, dl, VT, V1, V2,
6770 getShuffleVPERM2F128Immediate(SVOp), DAG);
6771
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006772 // Handle VSHUFPSY permutations
6773 if (isVSHUFPSYMask(M, VT, Subtarget))
6774 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6775 getShuffleVSHUFPSYImmediate(SVOp), DAG);
6776
6777 // Handle VSHUFPDY permutations
6778 if (isVSHUFPDYMask(M, VT, Subtarget))
6779 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6780 getShuffleVSHUFPDYImmediate(SVOp), DAG);
6781
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006782 //===--------------------------------------------------------------------===//
6783 // Since no target specific shuffle was selected for this generic one,
6784 // lower it into other known shuffles. FIXME: this isn't true yet, but
6785 // this is the plan.
6786 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00006787
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006788 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
6789 if (VT == MVT::v8i16) {
6790 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
6791 if (NewOp.getNode())
6792 return NewOp;
6793 }
6794
6795 if (VT == MVT::v16i8) {
6796 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
6797 if (NewOp.getNode())
6798 return NewOp;
6799 }
6800
6801 // Handle all 128-bit wide vectors with 4 elements, and match them with
6802 // several different shuffle types.
6803 if (NumElems == 4 && VT.getSizeInBits() == 128)
6804 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
6805
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006806 // Handle general 256-bit shuffles
6807 if (VT.is256BitVector())
6808 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
6809
Dan Gohman475871a2008-07-27 21:46:04 +00006810 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006811}
6812
Dan Gohman475871a2008-07-27 21:46:04 +00006813SDValue
6814X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00006815 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006816 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006817 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006818
6819 if (Op.getOperand(0).getValueType().getSizeInBits() != 128)
6820 return SDValue();
6821
Duncan Sands83ec4b62008-06-06 12:08:01 +00006822 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006823 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006824 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006825 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006826 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006827 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006828 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00006829 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
6830 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
6831 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006832 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6833 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006834 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006835 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00006836 Op.getOperand(0)),
6837 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006838 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006839 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006840 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006841 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006842 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00006843 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00006844 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
6845 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006846 // result has a single use which is a store or a bitcast to i32. And in
6847 // the case of a store, it's not worth it if the index is a constant 0,
6848 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00006849 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00006850 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00006851 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006852 if ((User->getOpcode() != ISD::STORE ||
6853 (isa<ConstantSDNode>(Op.getOperand(1)) &&
6854 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006855 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00006856 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00006857 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00006858 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006859 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00006860 Op.getOperand(0)),
6861 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006862 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Owen Anderson825b72b2009-08-11 20:47:22 +00006863 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00006864 // ExtractPS works with constant index.
6865 if (isa<ConstantSDNode>(Op.getOperand(1)))
6866 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006867 }
Dan Gohman475871a2008-07-27 21:46:04 +00006868 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006869}
6870
6871
Dan Gohman475871a2008-07-27 21:46:04 +00006872SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006873X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
6874 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006875 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00006876 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006877
David Greene74a579d2011-02-10 16:57:36 +00006878 SDValue Vec = Op.getOperand(0);
6879 EVT VecVT = Vec.getValueType();
6880
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006881 // If this is a 256-bit vector result, first extract the 128-bit vector and
6882 // then extract the element from the 128-bit vector.
6883 if (VecVT.getSizeInBits() == 256) {
David Greene74a579d2011-02-10 16:57:36 +00006884 DebugLoc dl = Op.getNode()->getDebugLoc();
6885 unsigned NumElems = VecVT.getVectorNumElements();
6886 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00006887 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
6888
6889 // Get the 128-bit vector.
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006890 bool Upper = IdxVal >= NumElems/2;
6891 Vec = Extract128BitVector(Vec,
6892 DAG.getConstant(Upper ? NumElems/2 : 0, MVT::i32), DAG, dl);
David Greene74a579d2011-02-10 16:57:36 +00006893
David Greene74a579d2011-02-10 16:57:36 +00006894 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006895 Upper ? DAG.getConstant(IdxVal-NumElems/2, MVT::i32) : Idx);
David Greene74a579d2011-02-10 16:57:36 +00006896 }
6897
6898 assert(Vec.getValueSizeInBits() <= 128 && "Unexpected vector length");
6899
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006900 if (Subtarget->hasSSE41() || Subtarget->hasAVX()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006901 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006902 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00006903 return Res;
6904 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00006905
Owen Andersone50ed302009-08-10 22:56:29 +00006906 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006907 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006908 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006909 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00006910 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006911 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006912 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006913 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6914 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006915 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006916 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00006917 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006918 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00006919 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00006920 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006921 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00006922 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006923 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006924 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006925 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006926 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006927 if (Idx == 0)
6928 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00006929
Evan Cheng0db9fe62006-04-25 20:13:52 +00006930 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00006931 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006932 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006933 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006934 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006935 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006936 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00006937 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006938 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
6939 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
6940 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006941 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006942 if (Idx == 0)
6943 return Op;
6944
6945 // UNPCKHPD the element to the lowest double word, then movsd.
6946 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
6947 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00006948 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006949 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006950 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006951 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006952 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006953 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006954 }
6955
Dan Gohman475871a2008-07-27 21:46:04 +00006956 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006957}
6958
Dan Gohman475871a2008-07-27 21:46:04 +00006959SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006960X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
6961 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006962 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006963 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006964 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006965
Dan Gohman475871a2008-07-27 21:46:04 +00006966 SDValue N0 = Op.getOperand(0);
6967 SDValue N1 = Op.getOperand(1);
6968 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00006969
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006970 if (VT.getSizeInBits() == 256)
6971 return SDValue();
6972
Dan Gohman8a55ce42009-09-23 21:02:20 +00006973 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00006974 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006975 unsigned Opc;
6976 if (VT == MVT::v8i16)
6977 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006978 else if (VT == MVT::v16i8)
6979 Opc = X86ISD::PINSRB;
6980 else
6981 Opc = X86ISD::PINSRB;
6982
Nate Begeman14d12ca2008-02-11 04:19:36 +00006983 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
6984 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006985 if (N1.getValueType() != MVT::i32)
6986 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6987 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006988 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00006989 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006990 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006991 // Bits [7:6] of the constant are the source select. This will always be
6992 // zero here. The DAG Combiner may combine an extract_elt index into these
6993 // bits. For example (insert (extract, 3), 2) could be matched by putting
6994 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00006995 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00006996 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00006997 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00006998 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006999 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00007000 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00007001 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00007002 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00007003 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00007004 // PINSR* works with constant index.
7005 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007006 }
Dan Gohman475871a2008-07-27 21:46:04 +00007007 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007008}
7009
Dan Gohman475871a2008-07-27 21:46:04 +00007010SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007011X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007012 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007013 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007014
David Greene6b381262011-02-09 15:32:06 +00007015 DebugLoc dl = Op.getDebugLoc();
7016 SDValue N0 = Op.getOperand(0);
7017 SDValue N1 = Op.getOperand(1);
7018 SDValue N2 = Op.getOperand(2);
7019
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007020 // If this is a 256-bit vector result, first extract the 128-bit vector,
7021 // insert the element into the extracted half and then place it back.
7022 if (VT.getSizeInBits() == 256) {
David Greene6b381262011-02-09 15:32:06 +00007023 if (!isa<ConstantSDNode>(N2))
7024 return SDValue();
7025
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007026 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00007027 unsigned NumElems = VT.getVectorNumElements();
7028 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007029 bool Upper = IdxVal >= NumElems/2;
7030 SDValue Ins128Idx = DAG.getConstant(Upper ? NumElems/2 : 0, MVT::i32);
7031 SDValue V = Extract128BitVector(N0, Ins128Idx, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007032
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007033 // Insert the element into the desired half.
7034 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V,
7035 N1, Upper ? DAG.getConstant(IdxVal-NumElems/2, MVT::i32) : N2);
David Greene6b381262011-02-09 15:32:06 +00007036
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007037 // Insert the changed part back to the 256-bit vector
7038 return Insert128BitVector(N0, V, Ins128Idx, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007039 }
7040
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007041 if (Subtarget->hasSSE41() || Subtarget->hasAVX())
Nate Begeman14d12ca2008-02-11 04:19:36 +00007042 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
7043
Dan Gohman8a55ce42009-09-23 21:02:20 +00007044 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00007045 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00007046
Dan Gohman8a55ce42009-09-23 21:02:20 +00007047 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00007048 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
7049 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007050 if (N1.getValueType() != MVT::i32)
7051 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7052 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007053 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00007054 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007055 }
Dan Gohman475871a2008-07-27 21:46:04 +00007056 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007057}
7058
Dan Gohman475871a2008-07-27 21:46:04 +00007059SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007060X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007061 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007062 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00007063 EVT OpVT = Op.getValueType();
7064
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007065 // If this is a 256-bit vector result, first insert into a 128-bit
7066 // vector and then insert into the 256-bit vector.
7067 if (OpVT.getSizeInBits() > 128) {
7068 // Insert into a 128-bit vector.
7069 EVT VT128 = EVT::getVectorVT(*Context,
7070 OpVT.getVectorElementType(),
7071 OpVT.getVectorNumElements() / 2);
7072
7073 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
7074
7075 // Insert the 128-bit vector.
7076 return Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, OpVT), Op,
7077 DAG.getConstant(0, MVT::i32),
7078 DAG, dl);
7079 }
7080
Chris Lattnerf172ecd2010-07-04 23:07:25 +00007081 if (Op.getValueType() == MVT::v1i64 &&
7082 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00007083 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00007084
Owen Anderson825b72b2009-08-11 20:47:22 +00007085 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00007086 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
7087 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007088 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00007089 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007090}
7091
David Greene91585092011-01-26 15:38:49 +00007092// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
7093// a simple subregister reference or explicit instructions to grab
7094// upper bits of a vector.
7095SDValue
7096X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7097 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00007098 DebugLoc dl = Op.getNode()->getDebugLoc();
7099 SDValue Vec = Op.getNode()->getOperand(0);
7100 SDValue Idx = Op.getNode()->getOperand(1);
7101
7102 if (Op.getNode()->getValueType(0).getSizeInBits() == 128
7103 && Vec.getNode()->getValueType(0).getSizeInBits() == 256) {
7104 return Extract128BitVector(Vec, Idx, DAG, dl);
7105 }
David Greene91585092011-01-26 15:38:49 +00007106 }
7107 return SDValue();
7108}
7109
David Greenecfe33c42011-01-26 19:13:22 +00007110// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
7111// simple superregister reference or explicit instructions to insert
7112// the upper bits of a vector.
7113SDValue
7114X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7115 if (Subtarget->hasAVX()) {
7116 DebugLoc dl = Op.getNode()->getDebugLoc();
7117 SDValue Vec = Op.getNode()->getOperand(0);
7118 SDValue SubVec = Op.getNode()->getOperand(1);
7119 SDValue Idx = Op.getNode()->getOperand(2);
7120
7121 if (Op.getNode()->getValueType(0).getSizeInBits() == 256
7122 && SubVec.getNode()->getValueType(0).getSizeInBits() == 128) {
David Greenea5f26012011-02-07 19:36:54 +00007123 return Insert128BitVector(Vec, SubVec, Idx, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00007124 }
7125 }
7126 return SDValue();
7127}
7128
Bill Wendling056292f2008-09-16 21:48:12 +00007129// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7130// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7131// one of the above mentioned nodes. It has to be wrapped because otherwise
7132// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7133// be used to form addressing mode. These wrapped nodes will be selected
7134// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00007135SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007136X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007137 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007138
Chris Lattner41621a22009-06-26 19:22:52 +00007139 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7140 // global base reg.
7141 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007142 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007143 CodeModel::Model M = getTargetMachine().getCodeModel();
7144
Chris Lattner4f066492009-07-11 20:29:19 +00007145 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007146 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007147 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007148 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007149 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007150 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007151 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007152
Evan Cheng1606e8e2009-03-13 07:51:59 +00007153 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00007154 CP->getAlignment(),
7155 CP->getOffset(), OpFlag);
7156 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00007157 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007158 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00007159 if (OpFlag) {
7160 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007161 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007162 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007163 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007164 }
7165
7166 return Result;
7167}
7168
Dan Gohmand858e902010-04-17 15:26:15 +00007169SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007170 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007171
Chris Lattner18c59872009-06-27 04:16:01 +00007172 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7173 // global base reg.
7174 unsigned char OpFlag = 0;
7175 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007176 CodeModel::Model M = getTargetMachine().getCodeModel();
7177
Chris Lattner4f066492009-07-11 20:29:19 +00007178 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007179 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007180 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007181 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007182 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007183 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007184 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007185
Chris Lattner18c59872009-06-27 04:16:01 +00007186 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7187 OpFlag);
7188 DebugLoc DL = JT->getDebugLoc();
7189 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007190
Chris Lattner18c59872009-06-27 04:16:01 +00007191 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00007192 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00007193 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7194 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007195 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007196 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007197
Chris Lattner18c59872009-06-27 04:16:01 +00007198 return Result;
7199}
7200
7201SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007202X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007203 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00007204
Chris Lattner18c59872009-06-27 04:16:01 +00007205 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7206 // global base reg.
7207 unsigned char OpFlag = 0;
7208 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007209 CodeModel::Model M = getTargetMachine().getCodeModel();
7210
Chris Lattner4f066492009-07-11 20:29:19 +00007211 if (Subtarget->isPICStyleRIPRel() &&
Eli Friedman586272d2011-08-11 01:48:05 +00007212 (M == CodeModel::Small || M == CodeModel::Kernel)) {
7213 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
7214 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00007215 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00007216 } else if (Subtarget->isPICStyleGOT()) {
7217 OpFlag = X86II::MO_GOT;
7218 } else if (Subtarget->isPICStyleStubPIC()) {
7219 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
7220 } else if (Subtarget->isPICStyleStubNoDynamic()) {
7221 OpFlag = X86II::MO_DARWIN_NONLAZY;
7222 }
Eric Christopherfd179292009-08-27 18:07:15 +00007223
Chris Lattner18c59872009-06-27 04:16:01 +00007224 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00007225
Chris Lattner18c59872009-06-27 04:16:01 +00007226 DebugLoc DL = Op.getDebugLoc();
7227 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007228
7229
Chris Lattner18c59872009-06-27 04:16:01 +00007230 // With PIC, the address is actually $g + Offset.
7231 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007232 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007233 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7234 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007235 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007236 Result);
7237 }
Eric Christopherfd179292009-08-27 18:07:15 +00007238
Eli Friedman586272d2011-08-11 01:48:05 +00007239 // For symbols that require a load from a stub to get the address, emit the
7240 // load.
7241 if (isGlobalStubReference(OpFlag))
7242 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
7243 MachinePointerInfo::getGOT(), false, false, 0);
7244
Chris Lattner18c59872009-06-27 04:16:01 +00007245 return Result;
7246}
7247
Dan Gohman475871a2008-07-27 21:46:04 +00007248SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007249X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007250 // Create the TargetBlockAddressAddress node.
7251 unsigned char OpFlags =
7252 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007253 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007254 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00007255 DebugLoc dl = Op.getDebugLoc();
7256 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
7257 /*isTarget=*/true, OpFlags);
7258
Dan Gohmanf705adb2009-10-30 01:28:02 +00007259 if (Subtarget->isPICStyleRIPRel() &&
7260 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007261 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7262 else
7263 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007264
Dan Gohman29cbade2009-11-20 23:18:13 +00007265 // With PIC, the address is actually $g + Offset.
7266 if (isGlobalRelativeToPICBase(OpFlags)) {
7267 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7268 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7269 Result);
7270 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007271
7272 return Result;
7273}
7274
7275SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00007276X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00007277 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00007278 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007279 // Create the TargetGlobalAddress node, folding in the constant
7280 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007281 unsigned char OpFlags =
7282 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007283 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007284 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007285 if (OpFlags == X86II::MO_NO_FLAG &&
7286 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007287 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007288 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007289 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007290 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007291 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007292 }
Eric Christopherfd179292009-08-27 18:07:15 +00007293
Chris Lattner4f066492009-07-11 20:29:19 +00007294 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007295 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007296 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7297 else
7298 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007299
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007300 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007301 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007302 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7303 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007304 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007305 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007306
Chris Lattner36c25012009-07-10 07:34:39 +00007307 // For globals that require a load from a stub to get the address, emit the
7308 // load.
7309 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007310 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00007311 MachinePointerInfo::getGOT(), false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007312
Dan Gohman6520e202008-10-18 02:06:02 +00007313 // If there was a non-zero offset that we didn't fold, create an explicit
7314 // addition for it.
7315 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007316 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007317 DAG.getConstant(Offset, getPointerTy()));
7318
Evan Cheng0db9fe62006-04-25 20:13:52 +00007319 return Result;
7320}
7321
Evan Chengda43bcf2008-09-24 00:05:32 +00007322SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007323X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007324 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007325 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007326 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007327}
7328
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007329static SDValue
7330GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007331 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00007332 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007333 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007334 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007335 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007336 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007337 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007338 GA->getOffset(),
7339 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007340 if (InFlag) {
7341 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00007342 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007343 } else {
7344 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00007345 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007346 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007347
7348 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007349 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007350
Rafael Espindola15f1b662009-04-24 12:59:40 +00007351 SDValue Flag = Chain.getValue(1);
7352 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007353}
7354
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007355// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007356static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007357LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007358 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007359 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00007360 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
7361 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007362 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007363 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007364 InFlag = Chain.getValue(1);
7365
Chris Lattnerb903bed2009-06-26 21:20:29 +00007366 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007367}
7368
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007369// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007370static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007371LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007372 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007373 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7374 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007375}
7376
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007377// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
7378// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007379static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007380 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00007381 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007382 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00007383
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007384 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
7385 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
7386 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00007387
Michael J. Spencerec38de22010-10-10 22:04:20 +00007388 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007389 DAG.getIntPtrConstant(0),
7390 MachinePointerInfo(Ptr), false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00007391
Chris Lattnerb903bed2009-06-26 21:20:29 +00007392 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007393 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
7394 // initialexec.
7395 unsigned WrapperKind = X86ISD::Wrapper;
7396 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007397 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00007398 } else if (is64Bit) {
7399 assert(model == TLSModel::InitialExec);
7400 OperandFlags = X86II::MO_GOTTPOFF;
7401 WrapperKind = X86ISD::WrapperRIP;
7402 } else {
7403 assert(model == TLSModel::InitialExec);
7404 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00007405 }
Eric Christopherfd179292009-08-27 18:07:15 +00007406
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007407 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
7408 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00007409 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00007410 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007411 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007412 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007413
Rafael Espindola9a580232009-02-27 13:37:18 +00007414 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007415 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00007416 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007417
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007418 // The address of the thread local variable is the add of the thread
7419 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00007420 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007421}
7422
Dan Gohman475871a2008-07-27 21:46:04 +00007423SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007424X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00007425
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007426 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00007427 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00007428
Eric Christopher30ef0e52010-06-03 04:07:48 +00007429 if (Subtarget->isTargetELF()) {
7430 // TODO: implement the "local dynamic" model
7431 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00007432
Eric Christopher30ef0e52010-06-03 04:07:48 +00007433 // If GV is an alias then use the aliasee for determining
7434 // thread-localness.
7435 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
7436 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007437
7438 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00007439 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00007440
Eric Christopher30ef0e52010-06-03 04:07:48 +00007441 switch (model) {
7442 case TLSModel::GeneralDynamic:
7443 case TLSModel::LocalDynamic: // not implemented
7444 if (Subtarget->is64Bit())
7445 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
7446 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00007447
Eric Christopher30ef0e52010-06-03 04:07:48 +00007448 case TLSModel::InitialExec:
7449 case TLSModel::LocalExec:
7450 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
7451 Subtarget->is64Bit());
7452 }
7453 } else if (Subtarget->isTargetDarwin()) {
7454 // Darwin only has one model of TLS. Lower to that.
7455 unsigned char OpFlag = 0;
7456 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7457 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007458
Eric Christopher30ef0e52010-06-03 04:07:48 +00007459 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7460 // global base reg.
7461 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7462 !Subtarget->is64Bit();
7463 if (PIC32)
7464 OpFlag = X86II::MO_TLVP_PIC_BASE;
7465 else
7466 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007467 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007468 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00007469 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007470 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007471 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007472
Eric Christopher30ef0e52010-06-03 04:07:48 +00007473 // With PIC32, the address is actually $g + Offset.
7474 if (PIC32)
7475 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7476 DAG.getNode(X86ISD::GlobalBaseReg,
7477 DebugLoc(), getPointerTy()),
7478 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007479
Eric Christopher30ef0e52010-06-03 04:07:48 +00007480 // Lowering the machine isd will make sure everything is in the right
7481 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007482 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007483 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00007484 SDValue Args[] = { Chain, Offset };
7485 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007486
Eric Christopher30ef0e52010-06-03 04:07:48 +00007487 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7488 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7489 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007490
Eric Christopher30ef0e52010-06-03 04:07:48 +00007491 // And our return value (tls address) is in the standard call return value
7492 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007493 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
7494 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007495 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007496
Eric Christopher30ef0e52010-06-03 04:07:48 +00007497 assert(false &&
7498 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00007499
Torok Edwinc23197a2009-07-14 16:55:14 +00007500 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00007501 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007502}
7503
Evan Cheng0db9fe62006-04-25 20:13:52 +00007504
Nadav Rotem43012222011-05-11 08:12:09 +00007505/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00007506/// take a 2 x i32 value to shift plus a shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +00007507SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00007508 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00007509 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007510 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007511 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007512 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00007513 SDValue ShOpLo = Op.getOperand(0);
7514 SDValue ShOpHi = Op.getOperand(1);
7515 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00007516 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00007517 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00007518 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00007519
Dan Gohman475871a2008-07-27 21:46:04 +00007520 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007521 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007522 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
7523 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007524 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007525 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
7526 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007527 }
Evan Chenge3413162006-01-09 18:33:28 +00007528
Owen Anderson825b72b2009-08-11 20:47:22 +00007529 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
7530 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00007531 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00007532 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00007533
Dan Gohman475871a2008-07-27 21:46:04 +00007534 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00007535 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00007536 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
7537 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00007538
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007539 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007540 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7541 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007542 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007543 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7544 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007545 }
7546
Dan Gohman475871a2008-07-27 21:46:04 +00007547 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00007548 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007549}
Evan Chenga3195e82006-01-12 22:54:21 +00007550
Dan Gohmand858e902010-04-17 15:26:15 +00007551SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
7552 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007553 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00007554
Dale Johannesen0488fb62010-09-30 23:57:10 +00007555 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007556 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007557
Owen Anderson825b72b2009-08-11 20:47:22 +00007558 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00007559 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007560
Eli Friedman36df4992009-05-27 00:47:34 +00007561 // These are really Legal; return the operand so the caller accepts it as
7562 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007563 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00007564 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00007565 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00007566 Subtarget->is64Bit()) {
7567 return Op;
7568 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007569
Bruno Cardoso Lopesa511b8e2011-08-09 17:39:01 +00007570 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007571 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007572 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00007573 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007574 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00007575 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00007576 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007577 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007578 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007579 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
7580}
Evan Cheng0db9fe62006-04-25 20:13:52 +00007581
Owen Andersone50ed302009-08-10 22:56:29 +00007582SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007583 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00007584 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007585 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00007586 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00007587 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00007588 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007589 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007590 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00007591 else
Owen Anderson825b72b2009-08-11 20:47:22 +00007592 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007593
Chris Lattner492a43e2010-09-22 01:28:21 +00007594 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007595
Stuart Hastings84be9582011-06-02 15:57:11 +00007596 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
7597 MachineMemOperand *MMO;
7598 if (FI) {
7599 int SSFI = FI->getIndex();
7600 MMO =
7601 DAG.getMachineFunction()
7602 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7603 MachineMemOperand::MOLoad, ByteSize, ByteSize);
7604 } else {
7605 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
7606 StackSlot = StackSlot.getOperand(1);
7607 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007608 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007609 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
7610 X86ISD::FILD, DL,
7611 Tys, Ops, array_lengthof(Ops),
7612 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007613
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007614 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007615 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00007616 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007617
7618 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
7619 // shouldn't be necessary except that RFP cannot be live across
7620 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007621 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007622 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
7623 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007624 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00007625 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007626 SDValue Ops[] = {
7627 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
7628 };
Chris Lattner492a43e2010-09-22 01:28:21 +00007629 MachineMemOperand *MMO =
7630 DAG.getMachineFunction()
7631 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007632 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007633
Chris Lattner492a43e2010-09-22 01:28:21 +00007634 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
7635 Ops, array_lengthof(Ops),
7636 Op.getValueType(), MMO);
7637 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007638 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007639 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007640 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007641
Evan Cheng0db9fe62006-04-25 20:13:52 +00007642 return Result;
7643}
7644
Bill Wendling8b8a6362009-01-17 03:56:04 +00007645// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007646SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
7647 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00007648 // This algorithm is not obvious. Here it is in C code, more or less:
7649 /*
7650 double uint64_to_double( uint32_t hi, uint32_t lo ) {
7651 static const __m128i exp = { 0x4330000045300000ULL, 0 };
7652 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00007653
Bill Wendling8b8a6362009-01-17 03:56:04 +00007654 // Copy ints to xmm registers.
7655 __m128i xh = _mm_cvtsi32_si128( hi );
7656 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00007657
Bill Wendling8b8a6362009-01-17 03:56:04 +00007658 // Combine into low half of a single xmm register.
7659 __m128i x = _mm_unpacklo_epi32( xh, xl );
7660 __m128d d;
7661 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00007662
Bill Wendling8b8a6362009-01-17 03:56:04 +00007663 // Merge in appropriate exponents to give the integer bits the right
7664 // magnitude.
7665 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00007666
Bill Wendling8b8a6362009-01-17 03:56:04 +00007667 // Subtract away the biases to deal with the IEEE-754 double precision
7668 // implicit 1.
7669 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00007670
Bill Wendling8b8a6362009-01-17 03:56:04 +00007671 // All conversions up to here are exact. The correctly rounded result is
7672 // calculated using the current rounding mode using the following
7673 // horizontal add.
7674 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
7675 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
7676 // store doesn't really need to be here (except
7677 // maybe to zero the other double)
7678 return sd;
7679 }
7680 */
Dale Johannesen040225f2008-10-21 23:07:49 +00007681
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007682 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00007683 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00007684
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007685 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00007686 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00007687 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
7688 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
7689 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
7690 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00007691 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007692 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007693
Bill Wendling8b8a6362009-01-17 03:56:04 +00007694 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00007695 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007696 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00007697 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007698 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00007699 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007700 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007701
Owen Anderson825b72b2009-08-11 20:47:22 +00007702 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7703 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00007704 Op.getOperand(0),
7705 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007706 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7707 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00007708 Op.getOperand(0),
7709 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007710 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
7711 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00007712 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007713 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00007714 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007715 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
Owen Anderson825b72b2009-08-11 20:47:22 +00007716 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00007717 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007718 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00007719 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007720
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007721 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00007722 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00007723 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
7724 DAG.getUNDEF(MVT::v2f64), ShufMask);
7725 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
7726 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007727 DAG.getIntPtrConstant(0));
7728}
7729
Bill Wendling8b8a6362009-01-17 03:56:04 +00007730// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007731SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
7732 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007733 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007734 // FP constant to bias correct the final result.
7735 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00007736 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007737
7738 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00007739 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00007740 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007741
Eli Friedmanf3704762011-08-29 21:15:46 +00007742 // Zero out the upper parts of the register.
7743 Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget->hasSSE2(), DAG);
7744
Owen Anderson825b72b2009-08-11 20:47:22 +00007745 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007746 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007747 DAG.getIntPtrConstant(0));
7748
7749 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007750 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007751 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007752 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007753 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007754 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007755 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007756 MVT::v2f64, Bias)));
7757 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007758 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007759 DAG.getIntPtrConstant(0));
7760
7761 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007762 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007763
7764 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00007765 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00007766
Owen Anderson825b72b2009-08-11 20:47:22 +00007767 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007768 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00007769 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007770 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007771 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00007772 }
7773
7774 // Handle final rounding.
7775 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00007776}
7777
Dan Gohmand858e902010-04-17 15:26:15 +00007778SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
7779 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00007780 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007781 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007782
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007783 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00007784 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
7785 // the optimization here.
7786 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00007787 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00007788
Owen Andersone50ed302009-08-10 22:56:29 +00007789 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007790 EVT DstVT = Op.getValueType();
7791 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007792 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007793 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007794 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00007795
7796 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00007797 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007798 if (SrcVT == MVT::i32) {
7799 SDValue WordOff = DAG.getConstant(4, getPointerTy());
7800 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
7801 getPointerTy(), StackSlot, WordOff);
7802 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007803 StackSlot, MachinePointerInfo(),
7804 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007805 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007806 OffsetSlot, MachinePointerInfo(),
7807 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007808 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
7809 return Fild;
7810 }
7811
7812 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
7813 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007814 StackSlot, MachinePointerInfo(),
7815 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007816 // For i64 source, we need to add the appropriate power of 2 if the input
7817 // was negative. This is the same as the optimization in
7818 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
7819 // we must be careful to do the computation in x87 extended precision, not
7820 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00007821 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
7822 MachineMemOperand *MMO =
7823 DAG.getMachineFunction()
7824 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7825 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007826
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007827 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
7828 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007829 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
7830 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007831
7832 APInt FF(32, 0x5F800000ULL);
7833
7834 // Check whether the sign bit is set.
7835 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
7836 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
7837 ISD::SETLT);
7838
7839 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
7840 SDValue FudgePtr = DAG.getConstantPool(
7841 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
7842 getPointerTy());
7843
7844 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
7845 SDValue Zero = DAG.getIntPtrConstant(0);
7846 SDValue Four = DAG.getIntPtrConstant(4);
7847 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
7848 Zero, Four);
7849 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
7850
7851 // Load the value out, extending it from f32 to f80.
7852 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00007853 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00007854 FudgePtr, MachinePointerInfo::getConstantPool(),
7855 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007856 // Extend everything to 80 bits to force it to be done on x87.
7857 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
7858 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007859}
7860
Dan Gohman475871a2008-07-27 21:46:04 +00007861std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00007862FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00007863 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00007864
Owen Andersone50ed302009-08-10 22:56:29 +00007865 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00007866
7867 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007868 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
7869 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00007870 }
7871
Owen Anderson825b72b2009-08-11 20:47:22 +00007872 assert(DstTy.getSimpleVT() <= MVT::i64 &&
7873 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00007874 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00007875
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007876 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007877 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00007878 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007879 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00007880 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00007881 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00007882 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007883 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007884
Evan Cheng87c89352007-10-15 20:11:21 +00007885 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
7886 // stack slot.
7887 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00007888 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00007889 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007890 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00007891
Michael J. Spencerec38de22010-10-10 22:04:20 +00007892
7893
Evan Cheng0db9fe62006-04-25 20:13:52 +00007894 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00007895 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007896 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007897 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
7898 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
7899 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007900 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007901
Dan Gohman475871a2008-07-27 21:46:04 +00007902 SDValue Chain = DAG.getEntryNode();
7903 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00007904 EVT TheVT = Op.getOperand(0).getValueType();
7905 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007906 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00007907 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007908 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007909 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007910 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00007911 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00007912 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00007913 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00007914
Chris Lattner492a43e2010-09-22 01:28:21 +00007915 MachineMemOperand *MMO =
7916 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7917 MachineMemOperand::MOLoad, MemSize, MemSize);
7918 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
7919 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007920 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00007921 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007922 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
7923 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007924
Chris Lattner07290932010-09-22 01:05:16 +00007925 MachineMemOperand *MMO =
7926 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7927 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007928
Evan Cheng0db9fe62006-04-25 20:13:52 +00007929 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00007930 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00007931 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
7932 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00007933
Chris Lattner27a6c732007-11-24 07:07:01 +00007934 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007935}
7936
Dan Gohmand858e902010-04-17 15:26:15 +00007937SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
7938 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00007939 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007940 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007941
Eli Friedman948e95a2009-05-23 09:59:16 +00007942 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00007943 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00007944 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
7945 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00007946
Chris Lattner27a6c732007-11-24 07:07:01 +00007947 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007948 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007949 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00007950}
7951
Dan Gohmand858e902010-04-17 15:26:15 +00007952SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
7953 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00007954 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
7955 SDValue FIST = Vals.first, StackSlot = Vals.second;
7956 assert(FIST.getNode() && "Unexpected failure");
7957
7958 // Load the result.
7959 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007960 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007961}
7962
Dan Gohmand858e902010-04-17 15:26:15 +00007963SDValue X86TargetLowering::LowerFABS(SDValue Op,
7964 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007965 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007966 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007967 EVT VT = Op.getValueType();
7968 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007969 if (VT.isVector())
7970 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007971 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007972 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007973 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00007974 CV.push_back(C);
7975 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007976 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007977 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00007978 CV.push_back(C);
7979 CV.push_back(C);
7980 CV.push_back(C);
7981 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007982 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007983 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007984 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007985 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007986 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007987 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007988 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007989}
7990
Dan Gohmand858e902010-04-17 15:26:15 +00007991SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007992 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007993 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007994 EVT VT = Op.getValueType();
7995 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00007996 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00007997 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007998 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007999 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008000 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00008001 CV.push_back(C);
8002 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008003 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008004 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00008005 CV.push_back(C);
8006 CV.push_back(C);
8007 CV.push_back(C);
8008 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008009 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008010 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008011 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008012 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008013 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00008014 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00008015 if (VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008016 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008017 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008018 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00008019 Op.getOperand(0)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008020 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00008021 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00008022 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00008023 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008024}
8025
Dan Gohmand858e902010-04-17 15:26:15 +00008026SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008027 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00008028 SDValue Op0 = Op.getOperand(0);
8029 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008030 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008031 EVT VT = Op.getValueType();
8032 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00008033
8034 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008035 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008036 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008037 SrcVT = VT;
8038 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008039 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008040 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008041 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008042 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008043 }
8044
8045 // At this point the operands and the result should have the same
8046 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00008047
Evan Cheng68c47cb2007-01-05 07:55:56 +00008048 // First get the sign bit of second operand.
8049 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008050 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008051 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
8052 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008053 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008054 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
8055 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8056 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8057 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008058 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008059 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008060 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008061 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008062 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00008063 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008064 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008065
8066 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008067 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008068 // Op0 is MVT::f32, Op1 is MVT::f64.
8069 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
8070 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
8071 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008072 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00008073 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00008074 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008075 }
8076
Evan Cheng73d6cf12007-01-05 21:37:56 +00008077 // Clear first operand sign bit.
8078 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00008079 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008080 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
8081 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008082 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008083 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
8084 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8085 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8086 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008087 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008088 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008089 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008090 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008091 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00008092 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008093 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008094
8095 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00008096 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008097}
8098
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008099SDValue X86TargetLowering::LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) const {
8100 SDValue N0 = Op.getOperand(0);
8101 DebugLoc dl = Op.getDebugLoc();
8102 EVT VT = Op.getValueType();
8103
8104 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
8105 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
8106 DAG.getConstant(1, VT));
8107 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
8108}
8109
Dan Gohman076aee32009-03-04 19:44:21 +00008110/// Emit nodes that will be selected as "test Op0,Op0", or something
8111/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008112SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008113 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008114 DebugLoc dl = Op.getDebugLoc();
8115
Dan Gohman31125812009-03-07 01:58:32 +00008116 // CF and OF aren't always set the way we want. Determine which
8117 // of these we need.
8118 bool NeedCF = false;
8119 bool NeedOF = false;
8120 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008121 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00008122 case X86::COND_A: case X86::COND_AE:
8123 case X86::COND_B: case X86::COND_BE:
8124 NeedCF = true;
8125 break;
8126 case X86::COND_G: case X86::COND_GE:
8127 case X86::COND_L: case X86::COND_LE:
8128 case X86::COND_O: case X86::COND_NO:
8129 NeedOF = true;
8130 break;
Dan Gohman31125812009-03-07 01:58:32 +00008131 }
8132
Dan Gohman076aee32009-03-04 19:44:21 +00008133 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00008134 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
8135 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008136 if (Op.getResNo() != 0 || NeedOF || NeedCF)
8137 // Emit a CMP with 0, which is the TEST pattern.
8138 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8139 DAG.getConstant(0, Op.getValueType()));
8140
8141 unsigned Opcode = 0;
8142 unsigned NumOperands = 0;
8143 switch (Op.getNode()->getOpcode()) {
8144 case ISD::ADD:
8145 // Due to an isel shortcoming, be conservative if this add is likely to be
8146 // selected as part of a load-modify-store instruction. When the root node
8147 // in a match is a store, isel doesn't know how to remap non-chain non-flag
8148 // uses of other nodes in the match, such as the ADD in this case. This
8149 // leads to the ADD being left around and reselected, with the result being
8150 // two adds in the output. Alas, even if none our users are stores, that
8151 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
8152 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
8153 // climbing the DAG back to the root, and it doesn't seem to be worth the
8154 // effort.
8155 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Dan Gohman076aee32009-03-04 19:44:21 +00008156 UE = Op.getNode()->use_end(); UI != UE; ++UI)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008157 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
8158 goto default_case;
8159
8160 if (ConstantSDNode *C =
8161 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
8162 // An add of one will be selected as an INC.
8163 if (C->getAPIntValue() == 1) {
8164 Opcode = X86ISD::INC;
8165 NumOperands = 1;
8166 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00008167 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008168
8169 // An add of negative one (subtract of one) will be selected as a DEC.
8170 if (C->getAPIntValue().isAllOnesValue()) {
8171 Opcode = X86ISD::DEC;
8172 NumOperands = 1;
8173 break;
8174 }
Dan Gohman076aee32009-03-04 19:44:21 +00008175 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008176
8177 // Otherwise use a regular EFLAGS-setting add.
8178 Opcode = X86ISD::ADD;
8179 NumOperands = 2;
8180 break;
8181 case ISD::AND: {
8182 // If the primary and result isn't used, don't bother using X86ISD::AND,
8183 // because a TEST instruction will be better.
8184 bool NonFlagUse = false;
8185 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8186 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
8187 SDNode *User = *UI;
8188 unsigned UOpNo = UI.getOperandNo();
8189 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
8190 // Look pass truncate.
8191 UOpNo = User->use_begin().getOperandNo();
8192 User = *User->use_begin();
8193 }
8194
8195 if (User->getOpcode() != ISD::BRCOND &&
8196 User->getOpcode() != ISD::SETCC &&
8197 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
8198 NonFlagUse = true;
8199 break;
8200 }
Dan Gohman076aee32009-03-04 19:44:21 +00008201 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008202
8203 if (!NonFlagUse)
8204 break;
8205 }
8206 // FALL THROUGH
8207 case ISD::SUB:
8208 case ISD::OR:
8209 case ISD::XOR:
8210 // Due to the ISEL shortcoming noted above, be conservative if this op is
8211 // likely to be selected as part of a load-modify-store instruction.
8212 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8213 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8214 if (UI->getOpcode() == ISD::STORE)
8215 goto default_case;
8216
8217 // Otherwise use a regular EFLAGS-setting instruction.
8218 switch (Op.getNode()->getOpcode()) {
8219 default: llvm_unreachable("unexpected operator!");
8220 case ISD::SUB: Opcode = X86ISD::SUB; break;
8221 case ISD::OR: Opcode = X86ISD::OR; break;
8222 case ISD::XOR: Opcode = X86ISD::XOR; break;
8223 case ISD::AND: Opcode = X86ISD::AND; break;
8224 }
8225
8226 NumOperands = 2;
8227 break;
8228 case X86ISD::ADD:
8229 case X86ISD::SUB:
8230 case X86ISD::INC:
8231 case X86ISD::DEC:
8232 case X86ISD::OR:
8233 case X86ISD::XOR:
8234 case X86ISD::AND:
8235 return SDValue(Op.getNode(), 1);
8236 default:
8237 default_case:
8238 break;
Dan Gohman076aee32009-03-04 19:44:21 +00008239 }
8240
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008241 if (Opcode == 0)
8242 // Emit a CMP with 0, which is the TEST pattern.
8243 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8244 DAG.getConstant(0, Op.getValueType()));
8245
8246 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
8247 SmallVector<SDValue, 4> Ops;
8248 for (unsigned i = 0; i != NumOperands; ++i)
8249 Ops.push_back(Op.getOperand(i));
8250
8251 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
8252 DAG.ReplaceAllUsesWith(Op, New);
8253 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00008254}
8255
8256/// Emit nodes that will be selected as "cmp Op0,Op1", or something
8257/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008258SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008259 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008260 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
8261 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00008262 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00008263
8264 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00008265 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00008266}
8267
Evan Chengd40d03e2010-01-06 19:38:29 +00008268/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
8269/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00008270SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
8271 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008272 SDValue Op0 = And.getOperand(0);
8273 SDValue Op1 = And.getOperand(1);
8274 if (Op0.getOpcode() == ISD::TRUNCATE)
8275 Op0 = Op0.getOperand(0);
8276 if (Op1.getOpcode() == ISD::TRUNCATE)
8277 Op1 = Op1.getOperand(0);
8278
Evan Chengd40d03e2010-01-06 19:38:29 +00008279 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008280 if (Op1.getOpcode() == ISD::SHL)
8281 std::swap(Op0, Op1);
8282 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008283 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
8284 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008285 // If we looked past a truncate, check that it's only truncating away
8286 // known zeros.
8287 unsigned BitWidth = Op0.getValueSizeInBits();
8288 unsigned AndBitWidth = And.getValueSizeInBits();
8289 if (BitWidth > AndBitWidth) {
8290 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
8291 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
8292 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
8293 return SDValue();
8294 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008295 LHS = Op1;
8296 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00008297 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008298 } else if (Op1.getOpcode() == ISD::Constant) {
8299 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
8300 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00008301 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
8302 LHS = AndLHS.getOperand(0);
8303 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008304 }
Evan Chengd40d03e2010-01-06 19:38:29 +00008305 }
Evan Cheng0488db92007-09-25 01:57:46 +00008306
Evan Chengd40d03e2010-01-06 19:38:29 +00008307 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00008308 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00008309 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00008310 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00008311 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00008312 // Also promote i16 to i32 for performance / code size reason.
8313 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00008314 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00008315 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00008316
Evan Chengd40d03e2010-01-06 19:38:29 +00008317 // If the operand types disagree, extend the shift amount to match. Since
8318 // BT ignores high bits (like shifts) we can use anyextend.
8319 if (LHS.getValueType() != RHS.getValueType())
8320 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008321
Evan Chengd40d03e2010-01-06 19:38:29 +00008322 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
8323 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
8324 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8325 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00008326 }
8327
Evan Cheng54de3ea2010-01-05 06:52:31 +00008328 return SDValue();
8329}
8330
Dan Gohmand858e902010-04-17 15:26:15 +00008331SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Duncan Sands28b77e92011-09-06 19:07:46 +00008332
8333 if (Op.getValueType().isVector()) return LowerVSETCC(Op, DAG);
8334
Evan Cheng54de3ea2010-01-05 06:52:31 +00008335 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
8336 SDValue Op0 = Op.getOperand(0);
8337 SDValue Op1 = Op.getOperand(1);
8338 DebugLoc dl = Op.getDebugLoc();
8339 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
8340
8341 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00008342 // Lower (X & (1 << N)) == 0 to BT(X, N).
8343 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
8344 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00008345 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008346 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00008347 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008348 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
8349 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
8350 if (NewSetCC.getNode())
8351 return NewSetCC;
8352 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00008353
Chris Lattner481eebc2010-12-19 21:23:48 +00008354 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
8355 // these.
8356 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00008357 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00008358 cast<ConstantSDNode>(Op1)->isNullValue()) &&
8359 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008360
Chris Lattner481eebc2010-12-19 21:23:48 +00008361 // If the input is a setcc, then reuse the input setcc or use a new one with
8362 // the inverted condition.
8363 if (Op0.getOpcode() == X86ISD::SETCC) {
8364 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
8365 bool Invert = (CC == ISD::SETNE) ^
8366 cast<ConstantSDNode>(Op1)->isNullValue();
8367 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008368
Evan Cheng2c755ba2010-02-27 07:36:59 +00008369 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00008370 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8371 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
8372 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008373 }
8374
Evan Chenge5b51ac2010-04-17 06:13:15 +00008375 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00008376 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008377 if (X86CC == X86::COND_INVALID)
8378 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008379
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008380 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00008381 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008382 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00008383}
8384
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008385// Lower256IntVETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
8386// ones, and then concatenate the result back.
8387static SDValue Lower256IntVETCC(SDValue Op, SelectionDAG &DAG) {
8388 EVT VT = Op.getValueType();
8389
Duncan Sands28b77e92011-09-06 19:07:46 +00008390 assert(VT.getSizeInBits() == 256 && Op.getOpcode() == ISD::SETCC &&
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008391 "Unsupported value type for operation");
8392
8393 int NumElems = VT.getVectorNumElements();
8394 DebugLoc dl = Op.getDebugLoc();
8395 SDValue CC = Op.getOperand(2);
8396 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
8397 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
8398
8399 // Extract the LHS vectors
8400 SDValue LHS = Op.getOperand(0);
8401 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
8402 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
8403
8404 // Extract the RHS vectors
8405 SDValue RHS = Op.getOperand(1);
8406 SDValue RHS1 = Extract128BitVector(RHS, Idx0, DAG, dl);
8407 SDValue RHS2 = Extract128BitVector(RHS, Idx1, DAG, dl);
8408
8409 // Issue the operation on the smaller types and concatenate the result back
8410 MVT EltVT = VT.getVectorElementType().getSimpleVT();
8411 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
8412 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
8413 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
8414 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
8415}
8416
8417
Dan Gohmand858e902010-04-17 15:26:15 +00008418SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008419 SDValue Cond;
8420 SDValue Op0 = Op.getOperand(0);
8421 SDValue Op1 = Op.getOperand(1);
8422 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00008423 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00008424 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
8425 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008426 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00008427
8428 if (isFP) {
8429 unsigned SSECC = 8;
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008430 EVT EltVT = Op0.getValueType().getVectorElementType();
8431 assert(EltVT == MVT::f32 || EltVT == MVT::f64);
8432
8433 unsigned Opc = EltVT == MVT::f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00008434 bool Swap = false;
8435
8436 switch (SetCCOpcode) {
8437 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008438 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00008439 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00008440 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00008441 case ISD::SETGT: Swap = true; // Fallthrough
8442 case ISD::SETLT:
8443 case ISD::SETOLT: SSECC = 1; break;
8444 case ISD::SETOGE:
8445 case ISD::SETGE: Swap = true; // Fallthrough
8446 case ISD::SETLE:
8447 case ISD::SETOLE: SSECC = 2; break;
8448 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008449 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00008450 case ISD::SETNE: SSECC = 4; break;
8451 case ISD::SETULE: Swap = true;
8452 case ISD::SETUGE: SSECC = 5; break;
8453 case ISD::SETULT: Swap = true;
8454 case ISD::SETUGT: SSECC = 6; break;
8455 case ISD::SETO: SSECC = 7; break;
8456 }
8457 if (Swap)
8458 std::swap(Op0, Op1);
8459
Nate Begemanfb8ead02008-07-25 19:05:58 +00008460 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00008461 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00008462 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00008463 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00008464 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
8465 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00008466 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00008467 }
8468 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00008469 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00008470 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
8471 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00008472 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00008473 }
Torok Edwinc23197a2009-07-14 16:55:14 +00008474 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00008475 }
8476 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00008477 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00008478 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008479
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008480 // Break 256-bit integer vector compare into smaller ones.
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008481 if (!isFP && VT.getSizeInBits() == 256)
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008482 return Lower256IntVETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008483
Nate Begeman30a0de92008-07-17 16:51:19 +00008484 // We are handling one of the integer comparisons here. Since SSE only has
8485 // GT and EQ comparisons for integer, swapping operands and multiple
8486 // operations may be required for some comparisons.
8487 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
8488 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00008489
Owen Anderson825b72b2009-08-11 20:47:22 +00008490 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00008491 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00008492 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00008493 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00008494 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
8495 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008496 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008497
Nate Begeman30a0de92008-07-17 16:51:19 +00008498 switch (SetCCOpcode) {
8499 default: break;
8500 case ISD::SETNE: Invert = true;
8501 case ISD::SETEQ: Opc = EQOpc; break;
8502 case ISD::SETLT: Swap = true;
8503 case ISD::SETGT: Opc = GTOpc; break;
8504 case ISD::SETGE: Swap = true;
8505 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
8506 case ISD::SETULT: Swap = true;
8507 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
8508 case ISD::SETUGE: Swap = true;
8509 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
8510 }
8511 if (Swap)
8512 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008513
Nate Begeman30a0de92008-07-17 16:51:19 +00008514 // Since SSE has no unsigned integer comparisons, we need to flip the sign
8515 // bits of the inputs before performing those operations.
8516 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00008517 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00008518 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
8519 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00008520 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00008521 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
8522 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00008523 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
8524 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00008525 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008526
Dale Johannesenace16102009-02-03 19:33:06 +00008527 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00008528
8529 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00008530 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00008531 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00008532
Nate Begeman30a0de92008-07-17 16:51:19 +00008533 return Result;
8534}
Evan Cheng0488db92007-09-25 01:57:46 +00008535
Evan Cheng370e5342008-12-03 08:38:43 +00008536// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00008537static bool isX86LogicalCmp(SDValue Op) {
8538 unsigned Opc = Op.getNode()->getOpcode();
8539 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
8540 return true;
8541 if (Op.getResNo() == 1 &&
8542 (Opc == X86ISD::ADD ||
8543 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00008544 Opc == X86ISD::ADC ||
8545 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00008546 Opc == X86ISD::SMUL ||
8547 Opc == X86ISD::UMUL ||
8548 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00008549 Opc == X86ISD::DEC ||
8550 Opc == X86ISD::OR ||
8551 Opc == X86ISD::XOR ||
8552 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00008553 return true;
8554
Chris Lattner9637d5b2010-12-05 07:49:54 +00008555 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
8556 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008557
Dan Gohman076aee32009-03-04 19:44:21 +00008558 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00008559}
8560
Chris Lattnera2b56002010-12-05 01:23:24 +00008561static bool isZero(SDValue V) {
8562 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8563 return C && C->isNullValue();
8564}
8565
Chris Lattner96908b12010-12-05 02:00:51 +00008566static bool isAllOnes(SDValue V) {
8567 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8568 return C && C->isAllOnesValue();
8569}
8570
Dan Gohmand858e902010-04-17 15:26:15 +00008571SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008572 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008573 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00008574 SDValue Op1 = Op.getOperand(1);
8575 SDValue Op2 = Op.getOperand(2);
8576 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008577 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00008578
Dan Gohman1a492952009-10-20 16:22:37 +00008579 if (Cond.getOpcode() == ISD::SETCC) {
8580 SDValue NewCond = LowerSETCC(Cond, DAG);
8581 if (NewCond.getNode())
8582 Cond = NewCond;
8583 }
Evan Cheng734503b2006-09-11 02:19:56 +00008584
Chris Lattnera2b56002010-12-05 01:23:24 +00008585 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008586 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00008587 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008588 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008589 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00008590 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
8591 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008592 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008593
Chris Lattnera2b56002010-12-05 01:23:24 +00008594 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008595
8596 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00008597 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
8598 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00008599
8600 SDValue CmpOp0 = Cmp.getOperand(0);
8601 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
8602 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008603
Chris Lattner96908b12010-12-05 02:00:51 +00008604 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00008605 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8606 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008607
Chris Lattner96908b12010-12-05 02:00:51 +00008608 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
8609 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008610
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008611 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00008612 if (N2C == 0 || !N2C->isNullValue())
8613 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
8614 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008615 }
8616 }
8617
Chris Lattnera2b56002010-12-05 01:23:24 +00008618 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00008619 if (Cond.getOpcode() == ISD::AND &&
8620 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8621 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008622 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008623 Cond = Cond.getOperand(0);
8624 }
8625
Evan Cheng3f41d662007-10-08 22:16:29 +00008626 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8627 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00008628 if (Cond.getOpcode() == X86ISD::SETCC ||
8629 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008630 CC = Cond.getOperand(0);
8631
Dan Gohman475871a2008-07-27 21:46:04 +00008632 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008633 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00008634 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00008635
Evan Cheng3f41d662007-10-08 22:16:29 +00008636 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008637 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00008638 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00008639 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00008640
Chris Lattnerd1980a52009-03-12 06:52:53 +00008641 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
8642 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00008643 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008644 addTest = false;
8645 }
8646 }
8647
8648 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008649 // Look pass the truncate.
8650 if (Cond.getOpcode() == ISD::TRUNCATE)
8651 Cond = Cond.getOperand(0);
8652
8653 // We know the result of AND is compared against zero. Try to match
8654 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00008655 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00008656 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00008657 if (NewSetCC.getNode()) {
8658 CC = NewSetCC.getOperand(0);
8659 Cond = NewSetCC.getOperand(1);
8660 addTest = false;
8661 }
8662 }
8663 }
8664
8665 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008666 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00008667 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00008668 }
8669
Benjamin Kramere915ff32010-12-22 23:09:28 +00008670 // a < b ? -1 : 0 -> RES = ~setcc_carry
8671 // a < b ? 0 : -1 -> RES = setcc_carry
8672 // a >= b ? -1 : 0 -> RES = setcc_carry
8673 // a >= b ? 0 : -1 -> RES = ~setcc_carry
8674 if (Cond.getOpcode() == X86ISD::CMP) {
8675 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
8676
8677 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
8678 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
8679 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8680 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
8681 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
8682 return DAG.getNOT(DL, Res, Res.getValueType());
8683 return Res;
8684 }
8685 }
8686
Evan Cheng0488db92007-09-25 01:57:46 +00008687 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
8688 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008689 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008690 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00008691 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00008692}
8693
Nadav Rotemffe3e7d2011-09-08 08:11:19 +00008694SDValue X86TargetLowering::LowerVSELECT(SDValue Op, SelectionDAG &DAG) const {
8695 SDValue Cond = Op.getOperand(0);
8696 SDValue Op1 = Op.getOperand(1);
8697 SDValue Op2 = Op.getOperand(2);
8698 DebugLoc DL = Op.getDebugLoc();
8699
Bruno Cardoso Lopes814c6ce2011-09-08 18:05:08 +00008700 SDValue Ops[] = {Op1, Op2, Cond};
Nadav Rotemffe3e7d2011-09-08 08:11:19 +00008701
8702 assert(Op1.getValueType().isVector() && "Op1 must be a vector");
8703 assert(Op2.getValueType().isVector() && "Op2 must be a vector");
8704 assert(Cond.getValueType().isVector() && "Cond must be a vector");
8705 assert(Op1.getValueType() == Op2.getValueType() && "Type mismatch");
8706
8707 switch (Op1.getValueType().getSimpleVT().SimpleTy) {
8708 default: break;
8709 case MVT::v2i64: return DAG.getNode(X86ISD::BLENDVPD, DL, Op1.getValueType(), Ops, array_lengthof(Ops));
8710 case MVT::v2f64: return DAG.getNode(X86ISD::BLENDVPD, DL, Op1.getValueType(), Ops, array_lengthof(Ops));
8711 case MVT::v4i32: return DAG.getNode(X86ISD::BLENDVPS, DL, Op1.getValueType(), Ops, array_lengthof(Ops));
8712 case MVT::v4f32: return DAG.getNode(X86ISD::BLENDVPS, DL, Op1.getValueType(), Ops, array_lengthof(Ops));
8713 case MVT::v16i8: return DAG.getNode(X86ISD::PBLENDVB, DL, Op1.getValueType(), Ops, array_lengthof(Ops));
8714 }
8715
8716 return SDValue();
8717}
8718
8719
Evan Cheng370e5342008-12-03 08:38:43 +00008720// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
8721// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
8722// from the AND / OR.
8723static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
8724 Opc = Op.getOpcode();
8725 if (Opc != ISD::OR && Opc != ISD::AND)
8726 return false;
8727 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8728 Op.getOperand(0).hasOneUse() &&
8729 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
8730 Op.getOperand(1).hasOneUse());
8731}
8732
Evan Cheng961d6d42009-02-02 08:19:07 +00008733// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
8734// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00008735static bool isXor1OfSetCC(SDValue Op) {
8736 if (Op.getOpcode() != ISD::XOR)
8737 return false;
8738 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
8739 if (N1C && N1C->getAPIntValue() == 1) {
8740 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8741 Op.getOperand(0).hasOneUse();
8742 }
8743 return false;
8744}
8745
Dan Gohmand858e902010-04-17 15:26:15 +00008746SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008747 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008748 SDValue Chain = Op.getOperand(0);
8749 SDValue Cond = Op.getOperand(1);
8750 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008751 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008752 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00008753
Dan Gohman1a492952009-10-20 16:22:37 +00008754 if (Cond.getOpcode() == ISD::SETCC) {
8755 SDValue NewCond = LowerSETCC(Cond, DAG);
8756 if (NewCond.getNode())
8757 Cond = NewCond;
8758 }
Chris Lattnere55484e2008-12-25 05:34:37 +00008759#if 0
8760 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00008761 else if (Cond.getOpcode() == X86ISD::ADD ||
8762 Cond.getOpcode() == X86ISD::SUB ||
8763 Cond.getOpcode() == X86ISD::SMUL ||
8764 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00008765 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00008766#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00008767
Evan Chengad9c0a32009-12-15 00:53:42 +00008768 // Look pass (and (setcc_carry (cmp ...)), 1).
8769 if (Cond.getOpcode() == ISD::AND &&
8770 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8771 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008772 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008773 Cond = Cond.getOperand(0);
8774 }
8775
Evan Cheng3f41d662007-10-08 22:16:29 +00008776 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8777 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00008778 if (Cond.getOpcode() == X86ISD::SETCC ||
8779 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008780 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008781
Dan Gohman475871a2008-07-27 21:46:04 +00008782 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008783 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00008784 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00008785 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00008786 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008787 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00008788 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00008789 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00008790 default: break;
8791 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00008792 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00008793 // These can only come from an arithmetic instruction with overflow,
8794 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00008795 Cond = Cond.getNode()->getOperand(1);
8796 addTest = false;
8797 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00008798 }
Evan Cheng0488db92007-09-25 01:57:46 +00008799 }
Evan Cheng370e5342008-12-03 08:38:43 +00008800 } else {
8801 unsigned CondOpc;
8802 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
8803 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00008804 if (CondOpc == ISD::OR) {
8805 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
8806 // two branches instead of an explicit OR instruction with a
8807 // separate test.
8808 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00008809 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00008810 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008811 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00008812 Chain, Dest, CC, Cmp);
8813 CC = Cond.getOperand(1).getOperand(0);
8814 Cond = Cmp;
8815 addTest = false;
8816 }
8817 } else { // ISD::AND
8818 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
8819 // two branches instead of an explicit AND instruction with a
8820 // separate test. However, we only do this if this block doesn't
8821 // have a fall-through edge, because this requires an explicit
8822 // jmp when the condition is false.
8823 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00008824 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00008825 Op.getNode()->hasOneUse()) {
8826 X86::CondCode CCode =
8827 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
8828 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008829 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00008830 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00008831 // Look for an unconditional branch following this conditional branch.
8832 // We need this because we need to reverse the successors in order
8833 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00008834 if (User->getOpcode() == ISD::BR) {
8835 SDValue FalseBB = User->getOperand(1);
8836 SDNode *NewBR =
8837 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00008838 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00008839 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00008840 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00008841
Dale Johannesene4d209d2009-02-03 20:21:25 +00008842 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00008843 Chain, Dest, CC, Cmp);
8844 X86::CondCode CCode =
8845 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
8846 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008847 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00008848 Cond = Cmp;
8849 addTest = false;
8850 }
8851 }
Dan Gohman279c22e2008-10-21 03:29:32 +00008852 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00008853 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
8854 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
8855 // It should be transformed during dag combiner except when the condition
8856 // is set by a arithmetics with overflow node.
8857 X86::CondCode CCode =
8858 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
8859 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008860 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00008861 Cond = Cond.getOperand(0).getOperand(1);
8862 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00008863 }
Evan Cheng0488db92007-09-25 01:57:46 +00008864 }
8865
8866 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008867 // Look pass the truncate.
8868 if (Cond.getOpcode() == ISD::TRUNCATE)
8869 Cond = Cond.getOperand(0);
8870
8871 // We know the result of AND is compared against zero. Try to match
8872 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00008873 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008874 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
8875 if (NewSetCC.getNode()) {
8876 CC = NewSetCC.getOperand(0);
8877 Cond = NewSetCC.getOperand(1);
8878 addTest = false;
8879 }
8880 }
8881 }
8882
8883 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008884 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00008885 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00008886 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00008887 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00008888 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00008889}
8890
Anton Korobeynikove060b532007-04-17 19:34:00 +00008891
8892// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
8893// Calls to _alloca is needed to probe the stack when allocating more than 4k
8894// bytes in one go. Touching the stack at 4K increments is necessary to ensure
8895// that the guard pages used by the OS virtual memory manager are allocated in
8896// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00008897SDValue
8898X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008899 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008900 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
8901 EnableSegmentedStacks) &&
8902 "This should be used only on Windows targets or when segmented stacks "
Rafael Espindola96428ce2011-09-06 18:43:08 +00008903 "are being used");
8904 assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008905 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008906
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008907 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00008908 SDValue Chain = Op.getOperand(0);
8909 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008910 // FIXME: Ensure alignment here
8911
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008912 bool Is64Bit = Subtarget->is64Bit();
8913 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008914
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008915 if (EnableSegmentedStacks) {
8916 MachineFunction &MF = DAG.getMachineFunction();
8917 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008918
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008919 if (Is64Bit) {
8920 // The 64 bit implementation of segmented stacks needs to clobber both r10
Rafael Espindola96428ce2011-09-06 18:43:08 +00008921 // r11. This makes it impossible to use it along with nested parameters.
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008922 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00008923
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008924 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
8925 I != E; I++)
8926 if (I->hasNestAttr())
8927 report_fatal_error("Cannot use segmented stacks with functions that "
8928 "have nested arguments.");
8929 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00008930
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008931 const TargetRegisterClass *AddrRegClass =
8932 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
8933 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
8934 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
8935 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
8936 DAG.getRegister(Vreg, SPTy));
8937 SDValue Ops1[2] = { Value, Chain };
8938 return DAG.getMergeValues(Ops1, 2, dl);
8939 } else {
8940 SDValue Flag;
8941 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008942
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008943 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
8944 Flag = Chain.getValue(1);
8945 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008946
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008947 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
8948 Flag = Chain.getValue(1);
8949
8950 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
8951
8952 SDValue Ops1[2] = { Chain.getValue(0), Chain };
8953 return DAG.getMergeValues(Ops1, 2, dl);
8954 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008955}
8956
Dan Gohmand858e902010-04-17 15:26:15 +00008957SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00008958 MachineFunction &MF = DAG.getMachineFunction();
8959 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
8960
Dan Gohman69de1932008-02-06 22:27:42 +00008961 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00008962 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00008963
Anton Korobeynikove7beda12010-10-03 22:52:07 +00008964 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00008965 // vastart just stores the address of the VarArgsFrameIndex slot into the
8966 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00008967 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
8968 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00008969 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
8970 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008971 }
8972
8973 // __va_list_tag:
8974 // gp_offset (0 - 6 * 8)
8975 // fp_offset (48 - 48 + 8 * 16)
8976 // overflow_arg_area (point to parameters coming in memory).
8977 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00008978 SmallVector<SDValue, 8> MemOps;
8979 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00008980 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00008981 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00008982 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
8983 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008984 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008985 MemOps.push_back(Store);
8986
8987 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00008988 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008989 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008990 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00008991 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
8992 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008993 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008994 MemOps.push_back(Store);
8995
8996 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00008997 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008998 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00008999 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9000 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009001 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
9002 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00009003 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009004 MemOps.push_back(Store);
9005
9006 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00009007 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009008 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00009009 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
9010 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009011 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
9012 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009013 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009014 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00009015 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00009016}
9017
Dan Gohmand858e902010-04-17 15:26:15 +00009018SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00009019 assert(Subtarget->is64Bit() &&
9020 "LowerVAARG only handles 64-bit va_arg!");
9021 assert((Subtarget->isTargetLinux() ||
9022 Subtarget->isTargetDarwin()) &&
9023 "Unhandled target in LowerVAARG");
9024 assert(Op.getNode()->getNumOperands() == 4);
9025 SDValue Chain = Op.getOperand(0);
9026 SDValue SrcPtr = Op.getOperand(1);
9027 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
9028 unsigned Align = Op.getConstantOperandVal(3);
9029 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00009030
Dan Gohman320afb82010-10-12 18:00:49 +00009031 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009032 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Dan Gohman320afb82010-10-12 18:00:49 +00009033 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
9034 uint8_t ArgMode;
9035
9036 // Decide which area this value should be read from.
9037 // TODO: Implement the AMD64 ABI in its entirety. This simple
9038 // selection mechanism works only for the basic types.
9039 if (ArgVT == MVT::f80) {
9040 llvm_unreachable("va_arg for f80 not yet implemented");
9041 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
9042 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
9043 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
9044 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
9045 } else {
9046 llvm_unreachable("Unhandled argument type in LowerVAARG");
9047 }
9048
9049 if (ArgMode == 2) {
9050 // Sanity Check: Make sure using fp_offset makes sense.
Michael J. Spencer87b86652010-10-19 07:32:42 +00009051 assert(!UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00009052 !(DAG.getMachineFunction()
9053 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00009054 Subtarget->hasXMM());
Dan Gohman320afb82010-10-12 18:00:49 +00009055 }
9056
9057 // Insert VAARG_64 node into the DAG
9058 // VAARG_64 returns two values: Variable Argument Address, Chain
9059 SmallVector<SDValue, 11> InstOps;
9060 InstOps.push_back(Chain);
9061 InstOps.push_back(SrcPtr);
9062 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
9063 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
9064 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
9065 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
9066 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
9067 VTs, &InstOps[0], InstOps.size(),
9068 MVT::i64,
9069 MachinePointerInfo(SV),
9070 /*Align=*/0,
9071 /*Volatile=*/false,
9072 /*ReadMem=*/true,
9073 /*WriteMem=*/true);
9074 Chain = VAARG.getValue(1);
9075
9076 // Load the next argument and return it
9077 return DAG.getLoad(ArgVT, dl,
9078 Chain,
9079 VAARG,
9080 MachinePointerInfo(),
9081 false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00009082}
9083
Dan Gohmand858e902010-04-17 15:26:15 +00009084SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00009085 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00009086 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00009087 SDValue Chain = Op.getOperand(0);
9088 SDValue DstPtr = Op.getOperand(1);
9089 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00009090 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
9091 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00009092 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00009093
Chris Lattnere72f2022010-09-21 05:40:29 +00009094 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00009095 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00009096 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00009097 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00009098}
9099
Dan Gohman475871a2008-07-27 21:46:04 +00009100SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00009101X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009102 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009103 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00009104 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00009105 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00009106 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00009107 case Intrinsic::x86_sse_comieq_ss:
9108 case Intrinsic::x86_sse_comilt_ss:
9109 case Intrinsic::x86_sse_comile_ss:
9110 case Intrinsic::x86_sse_comigt_ss:
9111 case Intrinsic::x86_sse_comige_ss:
9112 case Intrinsic::x86_sse_comineq_ss:
9113 case Intrinsic::x86_sse_ucomieq_ss:
9114 case Intrinsic::x86_sse_ucomilt_ss:
9115 case Intrinsic::x86_sse_ucomile_ss:
9116 case Intrinsic::x86_sse_ucomigt_ss:
9117 case Intrinsic::x86_sse_ucomige_ss:
9118 case Intrinsic::x86_sse_ucomineq_ss:
9119 case Intrinsic::x86_sse2_comieq_sd:
9120 case Intrinsic::x86_sse2_comilt_sd:
9121 case Intrinsic::x86_sse2_comile_sd:
9122 case Intrinsic::x86_sse2_comigt_sd:
9123 case Intrinsic::x86_sse2_comige_sd:
9124 case Intrinsic::x86_sse2_comineq_sd:
9125 case Intrinsic::x86_sse2_ucomieq_sd:
9126 case Intrinsic::x86_sse2_ucomilt_sd:
9127 case Intrinsic::x86_sse2_ucomile_sd:
9128 case Intrinsic::x86_sse2_ucomigt_sd:
9129 case Intrinsic::x86_sse2_ucomige_sd:
9130 case Intrinsic::x86_sse2_ucomineq_sd: {
9131 unsigned Opc = 0;
9132 ISD::CondCode CC = ISD::SETCC_INVALID;
9133 switch (IntNo) {
9134 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009135 case Intrinsic::x86_sse_comieq_ss:
9136 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009137 Opc = X86ISD::COMI;
9138 CC = ISD::SETEQ;
9139 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009140 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009141 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009142 Opc = X86ISD::COMI;
9143 CC = ISD::SETLT;
9144 break;
9145 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009146 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009147 Opc = X86ISD::COMI;
9148 CC = ISD::SETLE;
9149 break;
9150 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009151 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009152 Opc = X86ISD::COMI;
9153 CC = ISD::SETGT;
9154 break;
9155 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009156 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009157 Opc = X86ISD::COMI;
9158 CC = ISD::SETGE;
9159 break;
9160 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009161 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009162 Opc = X86ISD::COMI;
9163 CC = ISD::SETNE;
9164 break;
9165 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009166 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009167 Opc = X86ISD::UCOMI;
9168 CC = ISD::SETEQ;
9169 break;
9170 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009171 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009172 Opc = X86ISD::UCOMI;
9173 CC = ISD::SETLT;
9174 break;
9175 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009176 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009177 Opc = X86ISD::UCOMI;
9178 CC = ISD::SETLE;
9179 break;
9180 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009181 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009182 Opc = X86ISD::UCOMI;
9183 CC = ISD::SETGT;
9184 break;
9185 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009186 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009187 Opc = X86ISD::UCOMI;
9188 CC = ISD::SETGE;
9189 break;
9190 case Intrinsic::x86_sse_ucomineq_ss:
9191 case Intrinsic::x86_sse2_ucomineq_sd:
9192 Opc = X86ISD::UCOMI;
9193 CC = ISD::SETNE;
9194 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009195 }
Evan Cheng734503b2006-09-11 02:19:56 +00009196
Dan Gohman475871a2008-07-27 21:46:04 +00009197 SDValue LHS = Op.getOperand(1);
9198 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00009199 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00009200 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00009201 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
9202 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9203 DAG.getConstant(X86CC, MVT::i8), Cond);
9204 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00009205 }
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009206 // ptest and testp intrinsics. The intrinsic these come from are designed to
9207 // return an integer value, not just an instruction so lower it to the ptest
9208 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00009209 case Intrinsic::x86_sse41_ptestz:
9210 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009211 case Intrinsic::x86_sse41_ptestnzc:
9212 case Intrinsic::x86_avx_ptestz_256:
9213 case Intrinsic::x86_avx_ptestc_256:
9214 case Intrinsic::x86_avx_ptestnzc_256:
9215 case Intrinsic::x86_avx_vtestz_ps:
9216 case Intrinsic::x86_avx_vtestc_ps:
9217 case Intrinsic::x86_avx_vtestnzc_ps:
9218 case Intrinsic::x86_avx_vtestz_pd:
9219 case Intrinsic::x86_avx_vtestc_pd:
9220 case Intrinsic::x86_avx_vtestnzc_pd:
9221 case Intrinsic::x86_avx_vtestz_ps_256:
9222 case Intrinsic::x86_avx_vtestc_ps_256:
9223 case Intrinsic::x86_avx_vtestnzc_ps_256:
9224 case Intrinsic::x86_avx_vtestz_pd_256:
9225 case Intrinsic::x86_avx_vtestc_pd_256:
9226 case Intrinsic::x86_avx_vtestnzc_pd_256: {
9227 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00009228 unsigned X86CC = 0;
9229 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00009230 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009231 case Intrinsic::x86_avx_vtestz_ps:
9232 case Intrinsic::x86_avx_vtestz_pd:
9233 case Intrinsic::x86_avx_vtestz_ps_256:
9234 case Intrinsic::x86_avx_vtestz_pd_256:
9235 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009236 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009237 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009238 // ZF = 1
9239 X86CC = X86::COND_E;
9240 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009241 case Intrinsic::x86_avx_vtestc_ps:
9242 case Intrinsic::x86_avx_vtestc_pd:
9243 case Intrinsic::x86_avx_vtestc_ps_256:
9244 case Intrinsic::x86_avx_vtestc_pd_256:
9245 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009246 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009247 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009248 // CF = 1
9249 X86CC = X86::COND_B;
9250 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009251 case Intrinsic::x86_avx_vtestnzc_ps:
9252 case Intrinsic::x86_avx_vtestnzc_pd:
9253 case Intrinsic::x86_avx_vtestnzc_ps_256:
9254 case Intrinsic::x86_avx_vtestnzc_pd_256:
9255 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00009256 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009257 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009258 // ZF and CF = 0
9259 X86CC = X86::COND_A;
9260 break;
9261 }
Eric Christopherfd179292009-08-27 18:07:15 +00009262
Eric Christopher71c67532009-07-29 00:28:05 +00009263 SDValue LHS = Op.getOperand(1);
9264 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009265 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
9266 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00009267 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
9268 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
9269 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00009270 }
Evan Cheng5759f972008-05-04 09:15:50 +00009271
9272 // Fix vector shift instructions where the last operand is a non-immediate
9273 // i32 value.
9274 case Intrinsic::x86_sse2_pslli_w:
9275 case Intrinsic::x86_sse2_pslli_d:
9276 case Intrinsic::x86_sse2_pslli_q:
9277 case Intrinsic::x86_sse2_psrli_w:
9278 case Intrinsic::x86_sse2_psrli_d:
9279 case Intrinsic::x86_sse2_psrli_q:
9280 case Intrinsic::x86_sse2_psrai_w:
9281 case Intrinsic::x86_sse2_psrai_d:
9282 case Intrinsic::x86_mmx_pslli_w:
9283 case Intrinsic::x86_mmx_pslli_d:
9284 case Intrinsic::x86_mmx_pslli_q:
9285 case Intrinsic::x86_mmx_psrli_w:
9286 case Intrinsic::x86_mmx_psrli_d:
9287 case Intrinsic::x86_mmx_psrli_q:
9288 case Intrinsic::x86_mmx_psrai_w:
9289 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00009290 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00009291 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00009292 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00009293
9294 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00009295 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00009296 switch (IntNo) {
9297 case Intrinsic::x86_sse2_pslli_w:
9298 NewIntNo = Intrinsic::x86_sse2_psll_w;
9299 break;
9300 case Intrinsic::x86_sse2_pslli_d:
9301 NewIntNo = Intrinsic::x86_sse2_psll_d;
9302 break;
9303 case Intrinsic::x86_sse2_pslli_q:
9304 NewIntNo = Intrinsic::x86_sse2_psll_q;
9305 break;
9306 case Intrinsic::x86_sse2_psrli_w:
9307 NewIntNo = Intrinsic::x86_sse2_psrl_w;
9308 break;
9309 case Intrinsic::x86_sse2_psrli_d:
9310 NewIntNo = Intrinsic::x86_sse2_psrl_d;
9311 break;
9312 case Intrinsic::x86_sse2_psrli_q:
9313 NewIntNo = Intrinsic::x86_sse2_psrl_q;
9314 break;
9315 case Intrinsic::x86_sse2_psrai_w:
9316 NewIntNo = Intrinsic::x86_sse2_psra_w;
9317 break;
9318 case Intrinsic::x86_sse2_psrai_d:
9319 NewIntNo = Intrinsic::x86_sse2_psra_d;
9320 break;
9321 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00009322 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00009323 switch (IntNo) {
9324 case Intrinsic::x86_mmx_pslli_w:
9325 NewIntNo = Intrinsic::x86_mmx_psll_w;
9326 break;
9327 case Intrinsic::x86_mmx_pslli_d:
9328 NewIntNo = Intrinsic::x86_mmx_psll_d;
9329 break;
9330 case Intrinsic::x86_mmx_pslli_q:
9331 NewIntNo = Intrinsic::x86_mmx_psll_q;
9332 break;
9333 case Intrinsic::x86_mmx_psrli_w:
9334 NewIntNo = Intrinsic::x86_mmx_psrl_w;
9335 break;
9336 case Intrinsic::x86_mmx_psrli_d:
9337 NewIntNo = Intrinsic::x86_mmx_psrl_d;
9338 break;
9339 case Intrinsic::x86_mmx_psrli_q:
9340 NewIntNo = Intrinsic::x86_mmx_psrl_q;
9341 break;
9342 case Intrinsic::x86_mmx_psrai_w:
9343 NewIntNo = Intrinsic::x86_mmx_psra_w;
9344 break;
9345 case Intrinsic::x86_mmx_psrai_d:
9346 NewIntNo = Intrinsic::x86_mmx_psra_d;
9347 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00009348 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00009349 }
9350 break;
9351 }
9352 }
Mon P Wangefa42202009-09-03 19:56:25 +00009353
9354 // The vector shift intrinsics with scalars uses 32b shift amounts but
9355 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
9356 // to be zero.
9357 SDValue ShOps[4];
9358 ShOps[0] = ShAmt;
9359 ShOps[1] = DAG.getConstant(0, MVT::i32);
9360 if (ShAmtVT == MVT::v4i32) {
9361 ShOps[2] = DAG.getUNDEF(MVT::i32);
9362 ShOps[3] = DAG.getUNDEF(MVT::i32);
9363 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
9364 } else {
9365 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00009366// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00009367 }
9368
Owen Andersone50ed302009-08-10 22:56:29 +00009369 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009370 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009371 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009372 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00009373 Op.getOperand(1), ShAmt);
9374 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00009375 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00009376}
Evan Cheng72261582005-12-20 06:22:03 +00009377
Dan Gohmand858e902010-04-17 15:26:15 +00009378SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
9379 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00009380 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9381 MFI->setReturnAddressIsTaken(true);
9382
Bill Wendling64e87322009-01-16 19:25:27 +00009383 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009384 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00009385
9386 if (Depth > 0) {
9387 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
9388 SDValue Offset =
9389 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00009390 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009391 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00009392 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009393 FrameAddr, Offset),
Chris Lattner51abfe42010-09-21 06:02:19 +00009394 MachinePointerInfo(), false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00009395 }
9396
9397 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00009398 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00009399 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Chris Lattner51abfe42010-09-21 06:02:19 +00009400 RetAddrFI, MachinePointerInfo(), false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00009401}
9402
Dan Gohmand858e902010-04-17 15:26:15 +00009403SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00009404 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9405 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00009406
Owen Andersone50ed302009-08-10 22:56:29 +00009407 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009408 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00009409 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
9410 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00009411 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00009412 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00009413 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
9414 MachinePointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +00009415 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00009416 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00009417}
9418
Dan Gohman475871a2008-07-27 21:46:04 +00009419SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009420 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00009421 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009422}
9423
Dan Gohmand858e902010-04-17 15:26:15 +00009424SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009425 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00009426 SDValue Chain = Op.getOperand(0);
9427 SDValue Offset = Op.getOperand(1);
9428 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009429 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009430
Dan Gohmand8816272010-08-11 18:14:00 +00009431 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
9432 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
9433 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009434 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009435
Dan Gohmand8816272010-08-11 18:14:00 +00009436 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
9437 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009438 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009439 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
9440 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00009441 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009442 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009443
Dale Johannesene4d209d2009-02-03 20:21:25 +00009444 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00009445 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009446 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009447}
9448
Duncan Sands4a544a72011-09-06 13:37:06 +00009449SDValue X86TargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op,
9450 SelectionDAG &DAG) const {
9451 return Op.getOperand(0);
9452}
9453
9454SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
9455 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00009456 SDValue Root = Op.getOperand(0);
9457 SDValue Trmp = Op.getOperand(1); // trampoline
9458 SDValue FPtr = Op.getOperand(2); // nested function
9459 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009460 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009461
Dan Gohman69de1932008-02-06 22:27:42 +00009462 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009463
9464 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00009465 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00009466
9467 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00009468 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
9469 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00009470
Evan Cheng0e6a0522011-07-18 20:57:22 +00009471 const unsigned char N86R10 = X86_MC::getX86RegNum(X86::R10);
9472 const unsigned char N86R11 = X86_MC::getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00009473
9474 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
9475
9476 // Load the pointer to the nested function into R11.
9477 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00009478 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00009479 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009480 Addr, MachinePointerInfo(TrmpAddr),
9481 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009482
Owen Anderson825b72b2009-08-11 20:47:22 +00009483 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9484 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009485 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
9486 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00009487 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00009488
9489 // Load the 'nest' parameter value into R10.
9490 // R10 is specified in X86CallingConv.td
9491 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00009492 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9493 DAG.getConstant(10, MVT::i64));
9494 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009495 Addr, MachinePointerInfo(TrmpAddr, 10),
9496 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009497
Owen Anderson825b72b2009-08-11 20:47:22 +00009498 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9499 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009500 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
9501 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00009502 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00009503
9504 // Jump to the nested function.
9505 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00009506 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9507 DAG.getConstant(20, MVT::i64));
9508 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009509 Addr, MachinePointerInfo(TrmpAddr, 20),
9510 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009511
9512 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00009513 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9514 DAG.getConstant(22, MVT::i64));
9515 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00009516 MachinePointerInfo(TrmpAddr, 22),
9517 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009518
Duncan Sands4a544a72011-09-06 13:37:06 +00009519 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009520 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00009521 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00009522 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00009523 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00009524 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009525
9526 switch (CC) {
9527 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00009528 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00009529 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00009530 case CallingConv::X86_StdCall: {
9531 // Pass 'nest' parameter in ECX.
9532 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00009533 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009534
9535 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009536 FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00009537 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009538
Chris Lattner58d74912008-03-12 17:45:29 +00009539 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00009540 unsigned InRegCount = 0;
9541 unsigned Idx = 1;
9542
9543 for (FunctionType::param_iterator I = FTy->param_begin(),
9544 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00009545 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00009546 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00009547 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009548
9549 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00009550 report_fatal_error("Nest register in use - reduce number of inreg"
9551 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00009552 }
9553 }
9554 break;
9555 }
9556 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00009557 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00009558 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00009559 // Pass 'nest' parameter in EAX.
9560 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00009561 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009562 break;
9563 }
9564
Dan Gohman475871a2008-07-27 21:46:04 +00009565 SDValue OutChains[4];
9566 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009567
Owen Anderson825b72b2009-08-11 20:47:22 +00009568 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9569 DAG.getConstant(10, MVT::i32));
9570 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009571
Chris Lattnera62fe662010-02-05 19:20:30 +00009572 // This is storing the opcode for MOV32ri.
9573 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Evan Cheng0e6a0522011-07-18 20:57:22 +00009574 const unsigned char N86Reg = X86_MC::getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009575 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00009576 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009577 Trmp, MachinePointerInfo(TrmpAddr),
9578 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009579
Owen Anderson825b72b2009-08-11 20:47:22 +00009580 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9581 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009582 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
9583 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00009584 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009585
Chris Lattnera62fe662010-02-05 19:20:30 +00009586 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00009587 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9588 DAG.getConstant(5, MVT::i32));
9589 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00009590 MachinePointerInfo(TrmpAddr, 5),
9591 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009592
Owen Anderson825b72b2009-08-11 20:47:22 +00009593 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9594 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009595 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
9596 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00009597 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009598
Duncan Sands4a544a72011-09-06 13:37:06 +00009599 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009600 }
9601}
9602
Dan Gohmand858e902010-04-17 15:26:15 +00009603SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
9604 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009605 /*
9606 The rounding mode is in bits 11:10 of FPSR, and has the following
9607 settings:
9608 00 Round to nearest
9609 01 Round to -inf
9610 10 Round to +inf
9611 11 Round to 0
9612
9613 FLT_ROUNDS, on the other hand, expects the following:
9614 -1 Undefined
9615 0 Round to 0
9616 1 Round to nearest
9617 2 Round to +inf
9618 3 Round to -inf
9619
9620 To perform the conversion, we do:
9621 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
9622 */
9623
9624 MachineFunction &MF = DAG.getMachineFunction();
9625 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00009626 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009627 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00009628 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00009629 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009630
9631 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00009632 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00009633 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009634
Michael J. Spencerec38de22010-10-10 22:04:20 +00009635
Chris Lattner2156b792010-09-22 01:11:26 +00009636 MachineMemOperand *MMO =
9637 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
9638 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009639
Chris Lattner2156b792010-09-22 01:11:26 +00009640 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
9641 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
9642 DAG.getVTList(MVT::Other),
9643 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009644
9645 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00009646 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Chris Lattner51abfe42010-09-21 06:02:19 +00009647 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009648
9649 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00009650 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00009651 DAG.getNode(ISD::SRL, DL, MVT::i16,
9652 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00009653 CWD, DAG.getConstant(0x800, MVT::i16)),
9654 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00009655 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00009656 DAG.getNode(ISD::SRL, DL, MVT::i16,
9657 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00009658 CWD, DAG.getConstant(0x400, MVT::i16)),
9659 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009660
Dan Gohman475871a2008-07-27 21:46:04 +00009661 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00009662 DAG.getNode(ISD::AND, DL, MVT::i16,
9663 DAG.getNode(ISD::ADD, DL, MVT::i16,
9664 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00009665 DAG.getConstant(1, MVT::i16)),
9666 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009667
9668
Duncan Sands83ec4b62008-06-06 12:08:01 +00009669 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00009670 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009671}
9672
Dan Gohmand858e902010-04-17 15:26:15 +00009673SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009674 EVT VT = Op.getValueType();
9675 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009676 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009677 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00009678
9679 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009680 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00009681 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00009682 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00009683 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009684 }
Evan Cheng18efe262007-12-14 02:13:44 +00009685
Evan Cheng152804e2007-12-14 08:30:15 +00009686 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00009687 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009688 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00009689
9690 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00009691 SDValue Ops[] = {
9692 Op,
9693 DAG.getConstant(NumBits+NumBits-1, OpVT),
9694 DAG.getConstant(X86::COND_E, MVT::i8),
9695 Op.getValue(1)
9696 };
9697 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00009698
9699 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00009700 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00009701
Owen Anderson825b72b2009-08-11 20:47:22 +00009702 if (VT == MVT::i8)
9703 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009704 return Op;
9705}
9706
Dan Gohmand858e902010-04-17 15:26:15 +00009707SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009708 EVT VT = Op.getValueType();
9709 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009710 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009711 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00009712
9713 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009714 if (VT == MVT::i8) {
9715 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00009716 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009717 }
Evan Cheng152804e2007-12-14 08:30:15 +00009718
9719 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00009720 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009721 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00009722
9723 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00009724 SDValue Ops[] = {
9725 Op,
9726 DAG.getConstant(NumBits, OpVT),
9727 DAG.getConstant(X86::COND_E, MVT::i8),
9728 Op.getValue(1)
9729 };
9730 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00009731
Owen Anderson825b72b2009-08-11 20:47:22 +00009732 if (VT == MVT::i8)
9733 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009734 return Op;
9735}
9736
Craig Topper13894fa2011-08-24 06:14:18 +00009737// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
9738// ones, and then concatenate the result back.
9739static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00009740 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +00009741
9742 assert(VT.getSizeInBits() == 256 && VT.isInteger() &&
9743 "Unsupported value type for operation");
9744
9745 int NumElems = VT.getVectorNumElements();
9746 DebugLoc dl = Op.getDebugLoc();
9747 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
9748 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
9749
9750 // Extract the LHS vectors
9751 SDValue LHS = Op.getOperand(0);
9752 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
9753 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
9754
9755 // Extract the RHS vectors
9756 SDValue RHS = Op.getOperand(1);
9757 SDValue RHS1 = Extract128BitVector(RHS, Idx0, DAG, dl);
9758 SDValue RHS2 = Extract128BitVector(RHS, Idx1, DAG, dl);
9759
9760 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9761 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
9762
9763 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
9764 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
9765 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
9766}
9767
9768SDValue X86TargetLowering::LowerADD(SDValue Op, SelectionDAG &DAG) const {
9769 assert(Op.getValueType().getSizeInBits() == 256 &&
9770 Op.getValueType().isInteger() &&
9771 "Only handle AVX 256-bit vector integer operation");
9772 return Lower256IntArith(Op, DAG);
9773}
9774
9775SDValue X86TargetLowering::LowerSUB(SDValue Op, SelectionDAG &DAG) const {
9776 assert(Op.getValueType().getSizeInBits() == 256 &&
9777 Op.getValueType().isInteger() &&
9778 "Only handle AVX 256-bit vector integer operation");
9779 return Lower256IntArith(Op, DAG);
9780}
9781
9782SDValue X86TargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
9783 EVT VT = Op.getValueType();
9784
9785 // Decompose 256-bit ops into smaller 128-bit ops.
9786 if (VT.getSizeInBits() == 256)
9787 return Lower256IntArith(Op, DAG);
9788
Owen Anderson825b72b2009-08-11 20:47:22 +00009789 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009790 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00009791
Mon P Wangaf9b9522008-12-18 21:42:19 +00009792 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
9793 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
9794 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
9795 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
9796 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
9797 //
9798 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
9799 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
9800 // return AloBlo + AloBhi + AhiBlo;
9801
9802 SDValue A = Op.getOperand(0);
9803 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009804
Dale Johannesene4d209d2009-02-03 20:21:25 +00009805 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009806 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
9807 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009808 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009809 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
9810 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009811 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009812 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00009813 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009814 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009815 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00009816 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009817 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009818 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00009819 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009820 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009821 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
9822 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009823 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009824 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
9825 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009826 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
9827 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00009828 return Res;
9829}
9830
Nadav Rotem43012222011-05-11 08:12:09 +00009831SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
9832
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009833 EVT VT = Op.getValueType();
9834 DebugLoc dl = Op.getDebugLoc();
9835 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +00009836 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009837 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009838
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00009839 if (!(Subtarget->hasSSE2() || Subtarget->hasAVX()))
9840 return SDValue();
9841
9842 // Decompose 256-bit shifts into smaller 128-bit shifts.
9843 if (VT.getSizeInBits() == 256) {
9844 int NumElems = VT.getVectorNumElements();
9845 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9846 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
9847
9848 // Extract the two vectors
9849 SDValue V1 = Extract128BitVector(R, DAG.getConstant(0, MVT::i32), DAG, dl);
9850 SDValue V2 = Extract128BitVector(R, DAG.getConstant(NumElems/2, MVT::i32),
9851 DAG, dl);
9852
9853 // Recreate the shift amount vectors
Bruno Cardoso Lopes0dd80b02011-08-17 22:12:20 +00009854 SDValue Amt1, Amt2;
9855 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
9856 // Constant shift amount
9857 SmallVector<SDValue, 4> Amt1Csts;
9858 SmallVector<SDValue, 4> Amt2Csts;
9859 for (int i = 0; i < NumElems/2; ++i)
9860 Amt1Csts.push_back(Amt->getOperand(i));
9861 for (int i = NumElems/2; i < NumElems; ++i)
9862 Amt2Csts.push_back(Amt->getOperand(i));
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00009863
Bruno Cardoso Lopes0dd80b02011-08-17 22:12:20 +00009864 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
9865 &Amt1Csts[0], NumElems/2);
9866 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
9867 &Amt2Csts[0], NumElems/2);
9868 } else {
9869 // Variable shift amount
9870 Amt1 = Extract128BitVector(Amt, DAG.getConstant(0, MVT::i32), DAG, dl);
9871 Amt2 = Extract128BitVector(Amt, DAG.getConstant(NumElems/2, MVT::i32),
9872 DAG, dl);
9873 }
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00009874
9875 // Issue new vector shifts for the smaller types
9876 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
9877 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
9878
9879 // Concatenate the result back
9880 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
9881 }
Nate Begeman51409212010-07-28 00:21:48 +00009882
Nadav Rotem43012222011-05-11 08:12:09 +00009883 // Optimize shl/srl/sra with constant shift amount.
9884 if (isSplatVector(Amt.getNode())) {
9885 SDValue SclrAmt = Amt->getOperand(0);
9886 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
9887 uint64_t ShiftAmt = C->getZExtValue();
9888
9889 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SHL)
9890 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9891 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
9892 R, DAG.getConstant(ShiftAmt, MVT::i32));
9893
9894 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SHL)
9895 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9896 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
9897 R, DAG.getConstant(ShiftAmt, MVT::i32));
9898
9899 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SHL)
9900 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9901 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
9902 R, DAG.getConstant(ShiftAmt, MVT::i32));
9903
9904 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SRL)
9905 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9906 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
9907 R, DAG.getConstant(ShiftAmt, MVT::i32));
9908
9909 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRL)
9910 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9911 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
9912 R, DAG.getConstant(ShiftAmt, MVT::i32));
9913
9914 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRL)
9915 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9916 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
9917 R, DAG.getConstant(ShiftAmt, MVT::i32));
9918
9919 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRA)
9920 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9921 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
9922 R, DAG.getConstant(ShiftAmt, MVT::i32));
9923
9924 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRA)
9925 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9926 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
9927 R, DAG.getConstant(ShiftAmt, MVT::i32));
9928 }
9929 }
9930
9931 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +00009932 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +00009933 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9934 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
9935 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
9936
9937 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009938
Nate Begeman51409212010-07-28 00:21:48 +00009939 std::vector<Constant*> CV(4, CI);
9940 Constant *C = ConstantVector::get(CV);
9941 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
9942 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00009943 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00009944 false, false, 16);
9945
9946 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009947 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +00009948 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
9949 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
9950 }
Nadav Rotem43012222011-05-11 08:12:09 +00009951 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +00009952 // a = a << 5;
9953 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9954 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
9955 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
9956
9957 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
9958 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
9959
9960 std::vector<Constant*> CVM1(16, CM1);
9961 std::vector<Constant*> CVM2(16, CM2);
9962 Constant *C = ConstantVector::get(CVM1);
9963 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
9964 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00009965 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00009966 false, false, 16);
9967
9968 // r = pblendv(r, psllw(r & (char16)15, 4), a);
9969 M = DAG.getNode(ISD::AND, dl, VT, R, M);
9970 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9971 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
9972 DAG.getConstant(4, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00009973 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00009974 // a += a
9975 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009976
Nate Begeman51409212010-07-28 00:21:48 +00009977 C = ConstantVector::get(CVM2);
9978 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
9979 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00009980 MachinePointerInfo::getConstantPool(),
Chris Lattner51abfe42010-09-21 06:02:19 +00009981 false, false, 16);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009982
Nate Begeman51409212010-07-28 00:21:48 +00009983 // r = pblendv(r, psllw(r & (char16)63, 2), a);
9984 M = DAG.getNode(ISD::AND, dl, VT, R, M);
9985 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9986 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
9987 DAG.getConstant(2, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00009988 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00009989 // a += a
9990 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009991
Nate Begeman51409212010-07-28 00:21:48 +00009992 // return pblendv(r, r+r, a);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009993 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT,
Nate Begeman51409212010-07-28 00:21:48 +00009994 R, DAG.getNode(ISD::ADD, dl, VT, R, R), Op);
9995 return R;
9996 }
9997 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009998}
Mon P Wangaf9b9522008-12-18 21:42:19 +00009999
Dan Gohmand858e902010-04-17 15:26:15 +000010000SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +000010001 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
10002 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +000010003 // looks for this combo and may remove the "setcc" instruction if the "setcc"
10004 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +000010005 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +000010006 SDValue LHS = N->getOperand(0);
10007 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +000010008 unsigned BaseOp = 0;
10009 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010010 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +000010011 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010012 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +000010013 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +000010014 // A subtract of one will be selected as a INC. Note that INC doesn't
10015 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010016 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10017 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010018 BaseOp = X86ISD::INC;
10019 Cond = X86::COND_O;
10020 break;
10021 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010022 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +000010023 Cond = X86::COND_O;
10024 break;
10025 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010026 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +000010027 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010028 break;
10029 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +000010030 // A subtract of one will be selected as a DEC. Note that DEC doesn't
10031 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010032 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10033 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010034 BaseOp = X86ISD::DEC;
10035 Cond = X86::COND_O;
10036 break;
10037 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010038 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +000010039 Cond = X86::COND_O;
10040 break;
10041 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010042 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +000010043 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010044 break;
10045 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000010046 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000010047 Cond = X86::COND_O;
10048 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010049 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
10050 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
10051 MVT::i32);
10052 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010053
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010054 SDValue SetCC =
10055 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10056 DAG.getConstant(X86::COND_O, MVT::i32),
10057 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010058
Dan Gohman6e5fda22011-07-22 18:45:15 +000010059 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010060 }
Bill Wendling74c37652008-12-09 22:08:41 +000010061 }
Bill Wendling3fafd932008-11-26 22:37:40 +000010062
Bill Wendling61edeb52008-12-02 01:06:39 +000010063 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010064 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010065 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000010066
Bill Wendling61edeb52008-12-02 01:06:39 +000010067 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010068 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
10069 DAG.getConstant(Cond, MVT::i32),
10070 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000010071
Dan Gohman6e5fda22011-07-22 18:45:15 +000010072 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000010073}
10074
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010075SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op, SelectionDAG &DAG) const{
10076 DebugLoc dl = Op.getDebugLoc();
10077 SDNode* Node = Op.getNode();
10078 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
10079 EVT VT = Node->getValueType(0);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010080 if (Subtarget->hasSSE2() && VT.isVector()) {
10081 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
10082 ExtraVT.getScalarType().getSizeInBits();
10083 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
10084
10085 unsigned SHLIntrinsicsID = 0;
10086 unsigned SRAIntrinsicsID = 0;
10087 switch (VT.getSimpleVT().SimpleTy) {
10088 default:
10089 return SDValue();
10090 case MVT::v2i64: {
10091 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_q;
10092 SRAIntrinsicsID = 0;
10093 break;
10094 }
10095 case MVT::v4i32: {
10096 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_d;
10097 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_d;
10098 break;
10099 }
10100 case MVT::v8i16: {
10101 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_w;
10102 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_w;
10103 break;
10104 }
10105 }
10106
10107 SDValue Tmp1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10108 DAG.getConstant(SHLIntrinsicsID, MVT::i32),
10109 Node->getOperand(0), ShAmt);
10110
10111 // In case of 1 bit sext, no need to shr
10112 if (ExtraVT.getScalarType().getSizeInBits() == 1) return Tmp1;
10113
10114 if (SRAIntrinsicsID) {
10115 Tmp1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10116 DAG.getConstant(SRAIntrinsicsID, MVT::i32),
10117 Tmp1, ShAmt);
10118 }
10119 return Tmp1;
10120 }
10121
10122 return SDValue();
10123}
10124
10125
Eric Christopher9a9d2752010-07-22 02:48:34 +000010126SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
10127 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010128
Eric Christopher77ed1352011-07-08 00:04:56 +000010129 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
10130 // There isn't any reason to disable it if the target processor supports it.
10131 if (!Subtarget->hasSSE2() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +000010132 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +000010133 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +000010134 SDValue Ops[] = {
10135 DAG.getRegister(X86::ESP, MVT::i32), // Base
10136 DAG.getTargetConstant(1, MVT::i8), // Scale
10137 DAG.getRegister(0, MVT::i32), // Index
10138 DAG.getTargetConstant(0, MVT::i32), // Disp
10139 DAG.getRegister(0, MVT::i32), // Segment.
10140 Zero,
10141 Chain
10142 };
Michael J. Spencerec38de22010-10-10 22:04:20 +000010143 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +000010144 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
10145 array_lengthof(Ops));
10146 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +000010147 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010148
Eric Christopher9a9d2752010-07-22 02:48:34 +000010149 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +000010150 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +000010151 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010152
Chris Lattner132929a2010-08-14 17:26:09 +000010153 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10154 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
10155 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
10156 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010157
Chris Lattner132929a2010-08-14 17:26:09 +000010158 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
10159 if (!Op1 && !Op2 && !Op3 && Op4)
10160 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010161
Chris Lattner132929a2010-08-14 17:26:09 +000010162 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
10163 if (Op1 && !Op2 && !Op3 && !Op4)
10164 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010165
10166 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +000010167 // (MFENCE)>;
10168 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +000010169}
10170
Eli Friedman14648462011-07-27 22:21:52 +000010171SDValue X86TargetLowering::LowerATOMIC_FENCE(SDValue Op,
10172 SelectionDAG &DAG) const {
10173 DebugLoc dl = Op.getDebugLoc();
10174 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
10175 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
10176 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
10177 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
10178
10179 // The only fence that needs an instruction is a sequentially-consistent
10180 // cross-thread fence.
10181 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
10182 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
10183 // no-sse2). There isn't any reason to disable it if the target processor
10184 // supports it.
10185 if (Subtarget->hasSSE2() || Subtarget->is64Bit())
10186 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
10187
10188 SDValue Chain = Op.getOperand(0);
10189 SDValue Zero = DAG.getConstant(0, MVT::i32);
10190 SDValue Ops[] = {
10191 DAG.getRegister(X86::ESP, MVT::i32), // Base
10192 DAG.getTargetConstant(1, MVT::i8), // Scale
10193 DAG.getRegister(0, MVT::i32), // Index
10194 DAG.getTargetConstant(0, MVT::i32), // Disp
10195 DAG.getRegister(0, MVT::i32), // Segment.
10196 Zero,
10197 Chain
10198 };
10199 SDNode *Res =
10200 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
10201 array_lengthof(Ops));
10202 return SDValue(Res, 0);
10203 }
10204
10205 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
10206 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
10207}
10208
10209
Dan Gohmand858e902010-04-17 15:26:15 +000010210SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010211 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010212 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000010213 unsigned Reg = 0;
10214 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000010215 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000010216 default:
10217 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000010218 case MVT::i8: Reg = X86::AL; size = 1; break;
10219 case MVT::i16: Reg = X86::AX; size = 2; break;
10220 case MVT::i32: Reg = X86::EAX; size = 4; break;
10221 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000010222 assert(Subtarget->is64Bit() && "Node not type legal!");
10223 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000010224 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000010225 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010226 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000010227 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000010228 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000010229 Op.getOperand(1),
10230 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000010231 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000010232 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010233 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010234 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
10235 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
10236 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000010237 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010238 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000010239 return cpOut;
10240}
10241
Duncan Sands1607f052008-12-01 11:39:25 +000010242SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010243 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +000010244 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010245 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000010246 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010247 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +000010248 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010249 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
10250 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000010251 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000010252 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
10253 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000010254 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000010255 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000010256 rdx.getValue(1)
10257 };
Dale Johannesene4d209d2009-02-03 20:21:25 +000010258 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010259}
10260
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010261SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +000010262 SelectionDAG &DAG) const {
10263 EVT SrcVT = Op.getOperand(0).getValueType();
10264 EVT DstVT = Op.getValueType();
Chris Lattner2a786eb2010-12-19 20:19:20 +000010265 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
10266 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000010267 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000010268 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010269 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000010270 // i64 <=> MMX conversions are Legal.
10271 if (SrcVT==MVT::i64 && DstVT.isVector())
10272 return Op;
10273 if (DstVT==MVT::i64 && SrcVT.isVector())
10274 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000010275 // MMX <=> MMX conversions are Legal.
10276 if (SrcVT.isVector() && DstVT.isVector())
10277 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000010278 // All other conversions need to be expanded.
10279 return SDValue();
10280}
Chris Lattner5b856542010-12-20 00:59:46 +000010281
Dan Gohmand858e902010-04-17 15:26:15 +000010282SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000010283 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +000010284 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000010285 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010286 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000010287 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010288 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010289 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000010290 Node->getOperand(0),
10291 Node->getOperand(1), negOp,
10292 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000010293 cast<AtomicSDNode>(Node)->getAlignment(),
10294 cast<AtomicSDNode>(Node)->getOrdering(),
10295 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000010296}
10297
Eli Friedman327236c2011-08-24 20:50:09 +000010298static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
10299 SDNode *Node = Op.getNode();
10300 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010301 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000010302
10303 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010304 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
10305 // FIXME: On 32-bit, store -> fist or movq would be more efficient
10306 // (The only way to get a 16-byte store is cmpxchg16b)
10307 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
10308 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
10309 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000010310 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
10311 cast<AtomicSDNode>(Node)->getMemoryVT(),
10312 Node->getOperand(0),
10313 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010314 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000010315 cast<AtomicSDNode>(Node)->getOrdering(),
10316 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000010317 return Swap.getValue(1);
10318 }
10319 // Other atomic stores have a simple pattern.
10320 return Op;
10321}
10322
Chris Lattner5b856542010-12-20 00:59:46 +000010323static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
10324 EVT VT = Op.getNode()->getValueType(0);
10325
10326 // Let legalize expand this if it isn't a legal type yet.
10327 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
10328 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010329
Chris Lattner5b856542010-12-20 00:59:46 +000010330 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010331
Chris Lattner5b856542010-12-20 00:59:46 +000010332 unsigned Opc;
10333 bool ExtraOp = false;
10334 switch (Op.getOpcode()) {
10335 default: assert(0 && "Invalid code");
10336 case ISD::ADDC: Opc = X86ISD::ADD; break;
10337 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
10338 case ISD::SUBC: Opc = X86ISD::SUB; break;
10339 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
10340 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010341
Chris Lattner5b856542010-12-20 00:59:46 +000010342 if (!ExtraOp)
10343 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
10344 Op.getOperand(1));
10345 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
10346 Op.getOperand(1), Op.getOperand(2));
10347}
10348
Evan Cheng0db9fe62006-04-25 20:13:52 +000010349/// LowerOperation - Provide custom lowering hooks for some operations.
10350///
Dan Gohmand858e902010-04-17 15:26:15 +000010351SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000010352 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010353 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010354 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Eric Christopher9a9d2752010-07-22 02:48:34 +000010355 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Eli Friedman14648462011-07-27 22:21:52 +000010356 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010357 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
10358 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000010359 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010360 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000010361 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010362 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
10363 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
10364 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +000010365 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +000010366 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010367 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
10368 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
10369 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000010370 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000010371 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000010372 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010373 case ISD::SHL_PARTS:
10374 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000010375 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010376 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000010377 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010378 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000010379 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010380 case ISD::FABS: return LowerFABS(Op, DAG);
10381 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000010382 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000010383 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000010384 case ISD::SETCC: return LowerSETCC(Op, DAG);
10385 case ISD::SELECT: return LowerSELECT(Op, DAG);
Nadav Rotemffe3e7d2011-09-08 08:11:19 +000010386 case ISD::VSELECT: return LowerVSELECT(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000010387 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010388 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010389 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000010390 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +000010391 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010392 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000010393 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
10394 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010395 case ISD::FRAME_TO_ARGS_OFFSET:
10396 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010397 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010398 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +000010399 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
10400 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000010401 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000010402 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
10403 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000010404 case ISD::MUL: return LowerMUL(Op, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000010405 case ISD::SRA:
10406 case ISD::SRL:
10407 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000010408 case ISD::SADDO:
10409 case ISD::UADDO:
10410 case ISD::SSUBO:
10411 case ISD::USUBO:
10412 case ISD::SMULO:
10413 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +000010414 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010415 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000010416 case ISD::ADDC:
10417 case ISD::ADDE:
10418 case ISD::SUBC:
10419 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000010420 case ISD::ADD: return LowerADD(Op, DAG);
10421 case ISD::SUB: return LowerSUB(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010422 }
Chris Lattner27a6c732007-11-24 07:07:01 +000010423}
10424
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010425static void ReplaceATOMIC_LOAD(SDNode *Node,
10426 SmallVectorImpl<SDValue> &Results,
10427 SelectionDAG &DAG) {
10428 DebugLoc dl = Node->getDebugLoc();
10429 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
10430
10431 // Convert wide load -> cmpxchg8b/cmpxchg16b
10432 // FIXME: On 32-bit, load -> fild or movq would be more efficient
10433 // (The only way to get a 16-byte load is cmpxchg16b)
10434 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000010435 SDValue Zero = DAG.getConstant(0, VT);
10436 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010437 Node->getOperand(0),
10438 Node->getOperand(1), Zero, Zero,
10439 cast<AtomicSDNode>(Node)->getMemOperand(),
10440 cast<AtomicSDNode>(Node)->getOrdering(),
10441 cast<AtomicSDNode>(Node)->getSynchScope());
10442 Results.push_back(Swap.getValue(0));
10443 Results.push_back(Swap.getValue(1));
10444}
10445
Duncan Sands1607f052008-12-01 11:39:25 +000010446void X86TargetLowering::
10447ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000010448 SelectionDAG &DAG, unsigned NewOp) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010449 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010450 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +000010451 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000010452
10453 SDValue Chain = Node->getOperand(0);
10454 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010455 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010456 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000010457 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010458 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000010459 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000010460 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000010461 SDValue Result =
10462 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
10463 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000010464 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000010465 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010466 Results.push_back(Result.getValue(2));
10467}
10468
Duncan Sands126d9072008-07-04 11:47:58 +000010469/// ReplaceNodeResults - Replace a node with an illegal result type
10470/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000010471void X86TargetLowering::ReplaceNodeResults(SDNode *N,
10472 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000010473 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010474 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +000010475 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000010476 default:
Duncan Sands1607f052008-12-01 11:39:25 +000010477 assert(false && "Do not know how to custom type legalize this operation!");
10478 return;
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010479 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000010480 case ISD::ADDC:
10481 case ISD::ADDE:
10482 case ISD::SUBC:
10483 case ISD::SUBE:
10484 // We don't want to expand or promote these.
10485 return;
Duncan Sands1607f052008-12-01 11:39:25 +000010486 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +000010487 std::pair<SDValue,SDValue> Vals =
10488 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +000010489 SDValue FIST = Vals.first, StackSlot = Vals.second;
10490 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000010491 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000010492 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +000010493 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
10494 MachinePointerInfo(), false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +000010495 }
10496 return;
10497 }
10498 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010499 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000010500 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010501 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010502 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000010503 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000010504 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010505 eax.getValue(2));
10506 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
10507 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +000010508 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010509 Results.push_back(edx.getValue(1));
10510 return;
10511 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010512 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000010513 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000010514 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000010515 bool Regs64bit = T == MVT::i128;
10516 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000010517 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000010518 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
10519 DAG.getConstant(0, HalfT));
10520 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
10521 DAG.getConstant(1, HalfT));
10522 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
10523 Regs64bit ? X86::RAX : X86::EAX,
10524 cpInL, SDValue());
10525 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
10526 Regs64bit ? X86::RDX : X86::EDX,
10527 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000010528 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000010529 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
10530 DAG.getConstant(0, HalfT));
10531 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
10532 DAG.getConstant(1, HalfT));
10533 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
10534 Regs64bit ? X86::RBX : X86::EBX,
10535 swapInL, cpInH.getValue(1));
10536 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
10537 Regs64bit ? X86::RCX : X86::ECX,
10538 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000010539 SDValue Ops[] = { swapInH.getValue(0),
10540 N->getOperand(1),
10541 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010542 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000010543 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000010544 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
10545 X86ISD::LCMPXCHG8_DAG;
10546 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000010547 Ops, 3, T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000010548 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
10549 Regs64bit ? X86::RAX : X86::EAX,
10550 HalfT, Result.getValue(1));
10551 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
10552 Regs64bit ? X86::RDX : X86::EDX,
10553 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000010554 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000010555 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010556 Results.push_back(cpOutH.getValue(1));
10557 return;
10558 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010559 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +000010560 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
10561 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010562 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +000010563 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
10564 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010565 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +000010566 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
10567 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010568 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +000010569 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
10570 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010571 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +000010572 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
10573 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010574 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +000010575 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
10576 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010577 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +000010578 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
10579 return;
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010580 case ISD::ATOMIC_LOAD:
10581 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000010582 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000010583}
10584
Evan Cheng72261582005-12-20 06:22:03 +000010585const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
10586 switch (Opcode) {
10587 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000010588 case X86ISD::BSF: return "X86ISD::BSF";
10589 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000010590 case X86ISD::SHLD: return "X86ISD::SHLD";
10591 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000010592 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000010593 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000010594 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000010595 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000010596 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000010597 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000010598 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
10599 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
10600 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000010601 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000010602 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000010603 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000010604 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000010605 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000010606 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000010607 case X86ISD::COMI: return "X86ISD::COMI";
10608 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000010609 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000010610 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000010611 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
10612 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000010613 case X86ISD::CMOV: return "X86ISD::CMOV";
10614 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000010615 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000010616 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
10617 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000010618 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000010619 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000010620 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000010621 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000010622 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000010623 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
10624 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000010625 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000010626 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000010627 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Nate Begemanb65c1752010-12-17 22:55:37 +000010628 case X86ISD::PSIGNB: return "X86ISD::PSIGNB";
10629 case X86ISD::PSIGNW: return "X86ISD::PSIGNW";
10630 case X86ISD::PSIGND: return "X86ISD::PSIGND";
Nate Begeman672fb622010-12-20 22:04:24 +000010631 case X86ISD::PBLENDVB: return "X86ISD::PBLENDVB";
Evan Cheng8ca29322006-11-10 21:43:37 +000010632 case X86ISD::FMAX: return "X86ISD::FMAX";
10633 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +000010634 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
10635 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000010636 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000010637 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010638 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000010639 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010640 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +000010641 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
10642 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010643 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
10644 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
10645 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
10646 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
10647 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
10648 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000010649 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
10650 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +000010651 case X86ISD::VSHL: return "X86ISD::VSHL";
10652 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +000010653 case X86ISD::CMPPD: return "X86ISD::CMPPD";
10654 case X86ISD::CMPPS: return "X86ISD::CMPPS";
10655 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
10656 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
10657 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
10658 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
10659 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
10660 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
10661 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
10662 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010663 case X86ISD::ADD: return "X86ISD::ADD";
10664 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000010665 case X86ISD::ADC: return "X86ISD::ADC";
10666 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000010667 case X86ISD::SMUL: return "X86ISD::SMUL";
10668 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000010669 case X86ISD::INC: return "X86ISD::INC";
10670 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000010671 case X86ISD::OR: return "X86ISD::OR";
10672 case X86ISD::XOR: return "X86ISD::XOR";
10673 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +000010674 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000010675 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010676 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010677 case X86ISD::PALIGN: return "X86ISD::PALIGN";
10678 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
10679 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
10680 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
10681 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
10682 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
10683 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
10684 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
10685 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010686 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000010687 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010688 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000010689 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
10690 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010691 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
10692 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
10693 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
10694 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
10695 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
10696 case X86ISD::MOVSD: return "X86ISD::MOVSD";
10697 case X86ISD::MOVSS: return "X86ISD::MOVSS";
10698 case X86ISD::UNPCKLPS: return "X86ISD::UNPCKLPS";
10699 case X86ISD::UNPCKLPD: return "X86ISD::UNPCKLPD";
David Greenefbf05d32011-02-22 23:31:46 +000010700 case X86ISD::VUNPCKLPDY: return "X86ISD::VUNPCKLPDY";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010701 case X86ISD::UNPCKHPS: return "X86ISD::UNPCKHPS";
10702 case X86ISD::UNPCKHPD: return "X86ISD::UNPCKHPD";
10703 case X86ISD::PUNPCKLBW: return "X86ISD::PUNPCKLBW";
10704 case X86ISD::PUNPCKLWD: return "X86ISD::PUNPCKLWD";
10705 case X86ISD::PUNPCKLDQ: return "X86ISD::PUNPCKLDQ";
10706 case X86ISD::PUNPCKLQDQ: return "X86ISD::PUNPCKLQDQ";
10707 case X86ISD::PUNPCKHBW: return "X86ISD::PUNPCKHBW";
10708 case X86ISD::PUNPCKHWD: return "X86ISD::PUNPCKHWD";
10709 case X86ISD::PUNPCKHDQ: return "X86ISD::PUNPCKHDQ";
10710 case X86ISD::PUNPCKHQDQ: return "X86ISD::PUNPCKHQDQ";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000010711 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +000010712 case X86ISD::VPERMILPS: return "X86ISD::VPERMILPS";
10713 case X86ISD::VPERMILPSY: return "X86ISD::VPERMILPSY";
10714 case X86ISD::VPERMILPD: return "X86ISD::VPERMILPD";
10715 case X86ISD::VPERMILPDY: return "X86ISD::VPERMILPDY";
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +000010716 case X86ISD::VPERM2F128: return "X86ISD::VPERM2F128";
Dan Gohmand6708ea2009-08-15 01:38:56 +000010717 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000010718 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010719 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000010720 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000010721 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +000010722 }
10723}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010724
Chris Lattnerc9addb72007-03-30 23:15:24 +000010725// isLegalAddressingMode - Return true if the addressing mode represented
10726// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000010727bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010728 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000010729 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010730 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000010731 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000010732
Chris Lattnerc9addb72007-03-30 23:15:24 +000010733 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010734 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000010735 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000010736
Chris Lattnerc9addb72007-03-30 23:15:24 +000010737 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000010738 unsigned GVFlags =
10739 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010740
Chris Lattnerdfed4132009-07-10 07:38:24 +000010741 // If a reference to this global requires an extra load, we can't fold it.
10742 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000010743 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010744
Chris Lattnerdfed4132009-07-10 07:38:24 +000010745 // If BaseGV requires a register for the PIC base, we cannot also have a
10746 // BaseReg specified.
10747 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000010748 return false;
Evan Cheng52787842007-08-01 23:46:47 +000010749
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010750 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000010751 if ((M != CodeModel::Small || R != Reloc::Static) &&
10752 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010753 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000010754 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010755
Chris Lattnerc9addb72007-03-30 23:15:24 +000010756 switch (AM.Scale) {
10757 case 0:
10758 case 1:
10759 case 2:
10760 case 4:
10761 case 8:
10762 // These scales always work.
10763 break;
10764 case 3:
10765 case 5:
10766 case 9:
10767 // These scales are formed with basereg+scalereg. Only accept if there is
10768 // no basereg yet.
10769 if (AM.HasBaseReg)
10770 return false;
10771 break;
10772 default: // Other stuff never works.
10773 return false;
10774 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010775
Chris Lattnerc9addb72007-03-30 23:15:24 +000010776 return true;
10777}
10778
10779
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010780bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000010781 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000010782 return false;
Evan Chenge127a732007-10-29 07:57:50 +000010783 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
10784 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000010785 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +000010786 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000010787 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +000010788}
10789
Owen Andersone50ed302009-08-10 22:56:29 +000010790bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000010791 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000010792 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000010793 unsigned NumBits1 = VT1.getSizeInBits();
10794 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000010795 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +000010796 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000010797 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000010798}
Evan Cheng2bd122c2007-10-26 01:56:11 +000010799
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010800bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000010801 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000010802 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000010803}
10804
Owen Andersone50ed302009-08-10 22:56:29 +000010805bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000010806 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000010807 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000010808}
10809
Owen Andersone50ed302009-08-10 22:56:29 +000010810bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000010811 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000010812 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000010813}
10814
Evan Cheng60c07e12006-07-05 22:17:51 +000010815/// isShuffleMaskLegal - Targets can use this to indicate that they only
10816/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
10817/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
10818/// are assumed to be legal.
10819bool
Eric Christopherfd179292009-08-27 18:07:15 +000010820X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000010821 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000010822 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000010823 if (VT.getSizeInBits() == 64)
Eric Christophercff6f852010-04-15 01:40:20 +000010824 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3());
Nate Begeman9008ca62009-04-27 18:41:29 +000010825
Nate Begemana09008b2009-10-19 02:17:23 +000010826 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000010827 return (VT.getVectorNumElements() == 2 ||
10828 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
10829 isMOVLMask(M, VT) ||
10830 isSHUFPMask(M, VT) ||
10831 isPSHUFDMask(M, VT) ||
10832 isPSHUFHWMask(M, VT) ||
10833 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +000010834 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000010835 isUNPCKLMask(M, VT) ||
10836 isUNPCKHMask(M, VT) ||
10837 isUNPCKL_v_undef_Mask(M, VT) ||
10838 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +000010839}
10840
Dan Gohman7d8143f2008-04-09 20:09:42 +000010841bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000010842X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000010843 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000010844 unsigned NumElts = VT.getVectorNumElements();
10845 // FIXME: This collection of masks seems suspect.
10846 if (NumElts == 2)
10847 return true;
10848 if (NumElts == 4 && VT.getSizeInBits() == 128) {
10849 return (isMOVLMask(Mask, VT) ||
10850 isCommutedMOVLMask(Mask, VT, true) ||
10851 isSHUFPMask(Mask, VT) ||
10852 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +000010853 }
10854 return false;
10855}
10856
10857//===----------------------------------------------------------------------===//
10858// X86 Scheduler Hooks
10859//===----------------------------------------------------------------------===//
10860
Mon P Wang63307c32008-05-05 19:05:59 +000010861// private utility function
10862MachineBasicBlock *
10863X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
10864 MachineBasicBlock *MBB,
10865 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010866 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010867 unsigned LoadOpc,
10868 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010869 unsigned notOpc,
10870 unsigned EAXreg,
10871 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000010872 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000010873 // For the atomic bitwise operator, we generate
10874 // thisMBB:
10875 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000010876 // ld t1 = [bitinstr.addr]
10877 // op t2 = t1, [bitinstr.val]
10878 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000010879 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
10880 // bz newMBB
10881 // fallthrough -->nextMBB
10882 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10883 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000010884 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000010885 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000010886
Mon P Wang63307c32008-05-05 19:05:59 +000010887 /// First build the CFG
10888 MachineFunction *F = MBB->getParent();
10889 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000010890 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
10891 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
10892 F->insert(MBBIter, newMBB);
10893 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010894
Dan Gohman14152b42010-07-06 20:24:04 +000010895 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
10896 nextMBB->splice(nextMBB->begin(), thisMBB,
10897 llvm::next(MachineBasicBlock::iterator(bInstr)),
10898 thisMBB->end());
10899 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010900
Mon P Wang63307c32008-05-05 19:05:59 +000010901 // Update thisMBB to fall through to newMBB
10902 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010903
Mon P Wang63307c32008-05-05 19:05:59 +000010904 // newMBB jumps to itself and fall through to nextMBB
10905 newMBB->addSuccessor(nextMBB);
10906 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010907
Mon P Wang63307c32008-05-05 19:05:59 +000010908 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010909 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000010910 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +000010911 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000010912 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010913 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000010914 int numArgs = bInstr->getNumOperands() - 1;
10915 for (int i=0; i < numArgs; ++i)
10916 argOpers[i] = &bInstr->getOperand(i+1);
10917
10918 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010919 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000010920 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000010921
Dale Johannesen140be2d2008-08-19 18:47:28 +000010922 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010923 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000010924 for (int i=0; i <= lastAddrIndx; ++i)
10925 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010926
Dale Johannesen140be2d2008-08-19 18:47:28 +000010927 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010928 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010929 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010930 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010931 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010932 tt = t1;
10933
Dale Johannesen140be2d2008-08-19 18:47:28 +000010934 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +000010935 assert((argOpers[valArgIndx]->isReg() ||
10936 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000010937 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +000010938 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000010939 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000010940 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000010941 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010942 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +000010943 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010944
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010945 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +000010946 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +000010947
Dale Johannesene4d209d2009-02-03 20:21:25 +000010948 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +000010949 for (int i=0; i <= lastAddrIndx; ++i)
10950 (*MIB).addOperand(*argOpers[i]);
10951 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +000010952 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000010953 (*MIB).setMemRefs(bInstr->memoperands_begin(),
10954 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +000010955
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010956 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +000010957 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +000010958
Mon P Wang63307c32008-05-05 19:05:59 +000010959 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000010960 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000010961
Dan Gohman14152b42010-07-06 20:24:04 +000010962 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000010963 return nextMBB;
10964}
10965
Dale Johannesen1b54c7f2008-10-03 19:41:08 +000010966// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +000010967MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010968X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
10969 MachineBasicBlock *MBB,
10970 unsigned regOpcL,
10971 unsigned regOpcH,
10972 unsigned immOpcL,
10973 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000010974 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010975 // For the atomic bitwise operator, we generate
10976 // thisMBB (instructions are in pairs, except cmpxchg8b)
10977 // ld t1,t2 = [bitinstr.addr]
10978 // newMBB:
10979 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
10980 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +000010981 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010982 // mov ECX, EBX <- t5, t6
10983 // mov EAX, EDX <- t1, t2
10984 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
10985 // mov t3, t4 <- EAX, EDX
10986 // bz newMBB
10987 // result in out1, out2
10988 // fallthrough -->nextMBB
10989
10990 const TargetRegisterClass *RC = X86::GR32RegisterClass;
10991 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010992 const unsigned NotOpc = X86::NOT32r;
10993 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10994 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
10995 MachineFunction::iterator MBBIter = MBB;
10996 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000010997
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010998 /// First build the CFG
10999 MachineFunction *F = MBB->getParent();
11000 MachineBasicBlock *thisMBB = MBB;
11001 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11002 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11003 F->insert(MBBIter, newMBB);
11004 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011005
Dan Gohman14152b42010-07-06 20:24:04 +000011006 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11007 nextMBB->splice(nextMBB->begin(), thisMBB,
11008 llvm::next(MachineBasicBlock::iterator(bInstr)),
11009 thisMBB->end());
11010 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011011
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011012 // Update thisMBB to fall through to newMBB
11013 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011014
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011015 // newMBB jumps to itself and fall through to nextMBB
11016 newMBB->addSuccessor(nextMBB);
11017 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011018
Dale Johannesene4d209d2009-02-03 20:21:25 +000011019 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011020 // Insert instructions into newMBB based on incoming instruction
11021 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011022 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011023 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011024 MachineOperand& dest1Oper = bInstr->getOperand(0);
11025 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011026 MachineOperand* argOpers[2 + X86::AddrNumOperands];
11027 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011028 argOpers[i] = &bInstr->getOperand(i+2);
11029
Dan Gohman71ea4e52010-05-14 21:01:44 +000011030 // We use some of the operands multiple times, so conservatively just
11031 // clear any kill flags that might be present.
11032 if (argOpers[i]->isReg() && argOpers[i]->isUse())
11033 argOpers[i]->setIsKill(false);
11034 }
11035
Evan Chengad5b52f2010-01-08 19:14:57 +000011036 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011037 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +000011038
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011039 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011040 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011041 for (int i=0; i <= lastAddrIndx; ++i)
11042 (*MIB).addOperand(*argOpers[i]);
11043 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011044 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +000011045 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +000011046 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011047 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +000011048 MachineOperand newOp3 = *(argOpers[3]);
11049 if (newOp3.isImm())
11050 newOp3.setImm(newOp3.getImm()+4);
11051 else
11052 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011053 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +000011054 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011055
11056 // t3/4 are defined later, at the bottom of the loop
11057 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
11058 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011059 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011060 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011061 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011062 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
11063
Evan Cheng306b4ca2010-01-08 23:41:50 +000011064 // The subsequent operations should be using the destination registers of
11065 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +000011066 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +000011067 t1 = F->getRegInfo().createVirtualRegister(RC);
11068 t2 = F->getRegInfo().createVirtualRegister(RC);
11069 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
11070 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011071 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +000011072 t1 = dest1Oper.getReg();
11073 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011074 }
11075
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011076 int valArgIndx = lastAddrIndx + 1;
11077 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +000011078 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011079 "invalid operand");
11080 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
11081 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011082 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011083 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011084 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011085 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +000011086 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011087 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011088 (*MIB).addOperand(*argOpers[valArgIndx]);
11089 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011090 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011091 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011092 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011093 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011094 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011095 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011096 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +000011097 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011098 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011099 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011100
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011101 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011102 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011103 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011104 MIB.addReg(t2);
11105
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011106 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011107 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011108 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011109 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +000011110
Dale Johannesene4d209d2009-02-03 20:21:25 +000011111 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011112 for (int i=0; i <= lastAddrIndx; ++i)
11113 (*MIB).addOperand(*argOpers[i]);
11114
11115 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011116 (*MIB).setMemRefs(bInstr->memoperands_begin(),
11117 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011118
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011119 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011120 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011121 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011122 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000011123
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011124 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011125 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011126
Dan Gohman14152b42010-07-06 20:24:04 +000011127 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011128 return nextMBB;
11129}
11130
11131// private utility function
11132MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +000011133X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
11134 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011135 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000011136 // For the atomic min/max operator, we generate
11137 // thisMBB:
11138 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000011139 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +000011140 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +000011141 // cmp t1, t2
11142 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +000011143 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000011144 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
11145 // bz newMBB
11146 // fallthrough -->nextMBB
11147 //
11148 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11149 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011150 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000011151 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011152
Mon P Wang63307c32008-05-05 19:05:59 +000011153 /// First build the CFG
11154 MachineFunction *F = MBB->getParent();
11155 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011156 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11157 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11158 F->insert(MBBIter, newMBB);
11159 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011160
Dan Gohman14152b42010-07-06 20:24:04 +000011161 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11162 nextMBB->splice(nextMBB->begin(), thisMBB,
11163 llvm::next(MachineBasicBlock::iterator(mInstr)),
11164 thisMBB->end());
11165 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011166
Mon P Wang63307c32008-05-05 19:05:59 +000011167 // Update thisMBB to fall through to newMBB
11168 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011169
Mon P Wang63307c32008-05-05 19:05:59 +000011170 // newMBB jumps to newMBB and fall through to nextMBB
11171 newMBB->addSuccessor(nextMBB);
11172 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011173
Dale Johannesene4d209d2009-02-03 20:21:25 +000011174 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000011175 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011176 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011177 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +000011178 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011179 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000011180 int numArgs = mInstr->getNumOperands() - 1;
11181 for (int i=0; i < numArgs; ++i)
11182 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +000011183
Mon P Wang63307c32008-05-05 19:05:59 +000011184 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011185 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011186 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000011187
Mon P Wangab3e7472008-05-05 22:56:23 +000011188 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011189 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011190 for (int i=0; i <= lastAddrIndx; ++i)
11191 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +000011192
Mon P Wang63307c32008-05-05 19:05:59 +000011193 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +000011194 assert((argOpers[valArgIndx]->isReg() ||
11195 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000011196 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +000011197
11198 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +000011199 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011200 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +000011201 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011202 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000011203 (*MIB).addOperand(*argOpers[valArgIndx]);
11204
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011205 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +000011206 MIB.addReg(t1);
11207
Dale Johannesene4d209d2009-02-03 20:21:25 +000011208 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +000011209 MIB.addReg(t1);
11210 MIB.addReg(t2);
11211
11212 // Generate movc
11213 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011214 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +000011215 MIB.addReg(t2);
11216 MIB.addReg(t1);
11217
11218 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +000011219 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +000011220 for (int i=0; i <= lastAddrIndx; ++i)
11221 (*MIB).addOperand(*argOpers[i]);
11222 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +000011223 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011224 (*MIB).setMemRefs(mInstr->memoperands_begin(),
11225 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +000011226
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011227 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +000011228 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +000011229
Mon P Wang63307c32008-05-05 19:05:59 +000011230 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011231 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000011232
Dan Gohman14152b42010-07-06 20:24:04 +000011233 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000011234 return nextMBB;
11235}
11236
Eric Christopherf83a5de2009-08-27 18:08:16 +000011237// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011238// or XMM0_V32I8 in AVX all of this code can be replaced with that
11239// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +000011240MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +000011241X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +000011242 unsigned numArgs, bool memArg) const {
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011243 assert((Subtarget->hasSSE42() || Subtarget->hasAVX()) &&
11244 "Target must have SSE4.2 or AVX features enabled");
11245
Eric Christopherb120ab42009-08-18 22:50:32 +000011246 DebugLoc dl = MI->getDebugLoc();
11247 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +000011248 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011249 if (!Subtarget->hasAVX()) {
11250 if (memArg)
11251 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
11252 else
11253 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
11254 } else {
11255 if (memArg)
11256 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
11257 else
11258 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
11259 }
Eric Christopherb120ab42009-08-18 22:50:32 +000011260
Eric Christopher41c902f2010-11-30 08:20:21 +000011261 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +000011262 for (unsigned i = 0; i < numArgs; ++i) {
11263 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +000011264 if (!(Op.isReg() && Op.isImplicit()))
11265 MIB.addOperand(Op);
11266 }
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011267 BuildMI(*BB, MI, dl,
11268 TII->get(Subtarget->hasAVX() ? X86::VMOVAPSrr : X86::MOVAPSrr),
11269 MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000011270 .addReg(X86::XMM0);
11271
Dan Gohman14152b42010-07-06 20:24:04 +000011272 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000011273 return BB;
11274}
11275
11276MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +000011277X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000011278 DebugLoc dl = MI->getDebugLoc();
11279 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011280
Eric Christopher228232b2010-11-30 07:20:12 +000011281 // Address into RAX/EAX, other two args into ECX, EDX.
11282 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
11283 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
11284 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
11285 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000011286 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011287
Eric Christopher228232b2010-11-30 07:20:12 +000011288 unsigned ValOps = X86::AddrNumOperands;
11289 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
11290 .addReg(MI->getOperand(ValOps).getReg());
11291 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
11292 .addReg(MI->getOperand(ValOps+1).getReg());
11293
11294 // The instruction doesn't actually take any operands though.
11295 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011296
Eric Christopher228232b2010-11-30 07:20:12 +000011297 MI->eraseFromParent(); // The pseudo is gone now.
11298 return BB;
11299}
11300
11301MachineBasicBlock *
11302X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000011303 DebugLoc dl = MI->getDebugLoc();
11304 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011305
Eric Christopher228232b2010-11-30 07:20:12 +000011306 // First arg in ECX, the second in EAX.
11307 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
11308 .addReg(MI->getOperand(0).getReg());
11309 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
11310 .addReg(MI->getOperand(1).getReg());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011311
Eric Christopher228232b2010-11-30 07:20:12 +000011312 // The instruction doesn't actually take any operands though.
11313 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011314
Eric Christopher228232b2010-11-30 07:20:12 +000011315 MI->eraseFromParent(); // The pseudo is gone now.
11316 return BB;
11317}
11318
11319MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000011320X86TargetLowering::EmitVAARG64WithCustomInserter(
11321 MachineInstr *MI,
11322 MachineBasicBlock *MBB) const {
11323 // Emit va_arg instruction on X86-64.
11324
11325 // Operands to this pseudo-instruction:
11326 // 0 ) Output : destination address (reg)
11327 // 1-5) Input : va_list address (addr, i64mem)
11328 // 6 ) ArgSize : Size (in bytes) of vararg type
11329 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
11330 // 8 ) Align : Alignment of type
11331 // 9 ) EFLAGS (implicit-def)
11332
11333 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
11334 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
11335
11336 unsigned DestReg = MI->getOperand(0).getReg();
11337 MachineOperand &Base = MI->getOperand(1);
11338 MachineOperand &Scale = MI->getOperand(2);
11339 MachineOperand &Index = MI->getOperand(3);
11340 MachineOperand &Disp = MI->getOperand(4);
11341 MachineOperand &Segment = MI->getOperand(5);
11342 unsigned ArgSize = MI->getOperand(6).getImm();
11343 unsigned ArgMode = MI->getOperand(7).getImm();
11344 unsigned Align = MI->getOperand(8).getImm();
11345
11346 // Memory Reference
11347 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
11348 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
11349 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
11350
11351 // Machine Information
11352 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11353 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
11354 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
11355 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
11356 DebugLoc DL = MI->getDebugLoc();
11357
11358 // struct va_list {
11359 // i32 gp_offset
11360 // i32 fp_offset
11361 // i64 overflow_area (address)
11362 // i64 reg_save_area (address)
11363 // }
11364 // sizeof(va_list) = 24
11365 // alignment(va_list) = 8
11366
11367 unsigned TotalNumIntRegs = 6;
11368 unsigned TotalNumXMMRegs = 8;
11369 bool UseGPOffset = (ArgMode == 1);
11370 bool UseFPOffset = (ArgMode == 2);
11371 unsigned MaxOffset = TotalNumIntRegs * 8 +
11372 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
11373
11374 /* Align ArgSize to a multiple of 8 */
11375 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
11376 bool NeedsAlign = (Align > 8);
11377
11378 MachineBasicBlock *thisMBB = MBB;
11379 MachineBasicBlock *overflowMBB;
11380 MachineBasicBlock *offsetMBB;
11381 MachineBasicBlock *endMBB;
11382
11383 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
11384 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
11385 unsigned OffsetReg = 0;
11386
11387 if (!UseGPOffset && !UseFPOffset) {
11388 // If we only pull from the overflow region, we don't create a branch.
11389 // We don't need to alter control flow.
11390 OffsetDestReg = 0; // unused
11391 OverflowDestReg = DestReg;
11392
11393 offsetMBB = NULL;
11394 overflowMBB = thisMBB;
11395 endMBB = thisMBB;
11396 } else {
11397 // First emit code to check if gp_offset (or fp_offset) is below the bound.
11398 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
11399 // If not, pull from overflow_area. (branch to overflowMBB)
11400 //
11401 // thisMBB
11402 // | .
11403 // | .
11404 // offsetMBB overflowMBB
11405 // | .
11406 // | .
11407 // endMBB
11408
11409 // Registers for the PHI in endMBB
11410 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
11411 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
11412
11413 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11414 MachineFunction *MF = MBB->getParent();
11415 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11416 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11417 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11418
11419 MachineFunction::iterator MBBIter = MBB;
11420 ++MBBIter;
11421
11422 // Insert the new basic blocks
11423 MF->insert(MBBIter, offsetMBB);
11424 MF->insert(MBBIter, overflowMBB);
11425 MF->insert(MBBIter, endMBB);
11426
11427 // Transfer the remainder of MBB and its successor edges to endMBB.
11428 endMBB->splice(endMBB->begin(), thisMBB,
11429 llvm::next(MachineBasicBlock::iterator(MI)),
11430 thisMBB->end());
11431 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
11432
11433 // Make offsetMBB and overflowMBB successors of thisMBB
11434 thisMBB->addSuccessor(offsetMBB);
11435 thisMBB->addSuccessor(overflowMBB);
11436
11437 // endMBB is a successor of both offsetMBB and overflowMBB
11438 offsetMBB->addSuccessor(endMBB);
11439 overflowMBB->addSuccessor(endMBB);
11440
11441 // Load the offset value into a register
11442 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
11443 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
11444 .addOperand(Base)
11445 .addOperand(Scale)
11446 .addOperand(Index)
11447 .addDisp(Disp, UseFPOffset ? 4 : 0)
11448 .addOperand(Segment)
11449 .setMemRefs(MMOBegin, MMOEnd);
11450
11451 // Check if there is enough room left to pull this argument.
11452 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
11453 .addReg(OffsetReg)
11454 .addImm(MaxOffset + 8 - ArgSizeA8);
11455
11456 // Branch to "overflowMBB" if offset >= max
11457 // Fall through to "offsetMBB" otherwise
11458 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
11459 .addMBB(overflowMBB);
11460 }
11461
11462 // In offsetMBB, emit code to use the reg_save_area.
11463 if (offsetMBB) {
11464 assert(OffsetReg != 0);
11465
11466 // Read the reg_save_area address.
11467 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
11468 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
11469 .addOperand(Base)
11470 .addOperand(Scale)
11471 .addOperand(Index)
11472 .addDisp(Disp, 16)
11473 .addOperand(Segment)
11474 .setMemRefs(MMOBegin, MMOEnd);
11475
11476 // Zero-extend the offset
11477 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
11478 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
11479 .addImm(0)
11480 .addReg(OffsetReg)
11481 .addImm(X86::sub_32bit);
11482
11483 // Add the offset to the reg_save_area to get the final address.
11484 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
11485 .addReg(OffsetReg64)
11486 .addReg(RegSaveReg);
11487
11488 // Compute the offset for the next argument
11489 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
11490 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
11491 .addReg(OffsetReg)
11492 .addImm(UseFPOffset ? 16 : 8);
11493
11494 // Store it back into the va_list.
11495 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
11496 .addOperand(Base)
11497 .addOperand(Scale)
11498 .addOperand(Index)
11499 .addDisp(Disp, UseFPOffset ? 4 : 0)
11500 .addOperand(Segment)
11501 .addReg(NextOffsetReg)
11502 .setMemRefs(MMOBegin, MMOEnd);
11503
11504 // Jump to endMBB
11505 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
11506 .addMBB(endMBB);
11507 }
11508
11509 //
11510 // Emit code to use overflow area
11511 //
11512
11513 // Load the overflow_area address into a register.
11514 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
11515 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
11516 .addOperand(Base)
11517 .addOperand(Scale)
11518 .addOperand(Index)
11519 .addDisp(Disp, 8)
11520 .addOperand(Segment)
11521 .setMemRefs(MMOBegin, MMOEnd);
11522
11523 // If we need to align it, do so. Otherwise, just copy the address
11524 // to OverflowDestReg.
11525 if (NeedsAlign) {
11526 // Align the overflow address
11527 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
11528 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
11529
11530 // aligned_addr = (addr + (align-1)) & ~(align-1)
11531 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
11532 .addReg(OverflowAddrReg)
11533 .addImm(Align-1);
11534
11535 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
11536 .addReg(TmpReg)
11537 .addImm(~(uint64_t)(Align-1));
11538 } else {
11539 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
11540 .addReg(OverflowAddrReg);
11541 }
11542
11543 // Compute the next overflow address after this argument.
11544 // (the overflow address should be kept 8-byte aligned)
11545 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
11546 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
11547 .addReg(OverflowDestReg)
11548 .addImm(ArgSizeA8);
11549
11550 // Store the new overflow address.
11551 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
11552 .addOperand(Base)
11553 .addOperand(Scale)
11554 .addOperand(Index)
11555 .addDisp(Disp, 8)
11556 .addOperand(Segment)
11557 .addReg(NextAddrReg)
11558 .setMemRefs(MMOBegin, MMOEnd);
11559
11560 // If we branched, emit the PHI to the front of endMBB.
11561 if (offsetMBB) {
11562 BuildMI(*endMBB, endMBB->begin(), DL,
11563 TII->get(X86::PHI), DestReg)
11564 .addReg(OffsetDestReg).addMBB(offsetMBB)
11565 .addReg(OverflowDestReg).addMBB(overflowMBB);
11566 }
11567
11568 // Erase the pseudo instruction
11569 MI->eraseFromParent();
11570
11571 return endMBB;
11572}
11573
11574MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000011575X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
11576 MachineInstr *MI,
11577 MachineBasicBlock *MBB) const {
11578 // Emit code to save XMM registers to the stack. The ABI says that the
11579 // number of registers to save is given in %al, so it's theoretically
11580 // possible to do an indirect jump trick to avoid saving all of them,
11581 // however this code takes a simpler approach and just executes all
11582 // of the stores if %al is non-zero. It's less code, and it's probably
11583 // easier on the hardware branch predictor, and stores aren't all that
11584 // expensive anyway.
11585
11586 // Create the new basic blocks. One block contains all the XMM stores,
11587 // and one block is the final destination regardless of whether any
11588 // stores were performed.
11589 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11590 MachineFunction *F = MBB->getParent();
11591 MachineFunction::iterator MBBIter = MBB;
11592 ++MBBIter;
11593 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
11594 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
11595 F->insert(MBBIter, XMMSaveMBB);
11596 F->insert(MBBIter, EndMBB);
11597
Dan Gohman14152b42010-07-06 20:24:04 +000011598 // Transfer the remainder of MBB and its successor edges to EndMBB.
11599 EndMBB->splice(EndMBB->begin(), MBB,
11600 llvm::next(MachineBasicBlock::iterator(MI)),
11601 MBB->end());
11602 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
11603
Dan Gohmand6708ea2009-08-15 01:38:56 +000011604 // The original block will now fall through to the XMM save block.
11605 MBB->addSuccessor(XMMSaveMBB);
11606 // The XMMSaveMBB will fall through to the end block.
11607 XMMSaveMBB->addSuccessor(EndMBB);
11608
11609 // Now add the instructions.
11610 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11611 DebugLoc DL = MI->getDebugLoc();
11612
11613 unsigned CountReg = MI->getOperand(0).getReg();
11614 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
11615 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
11616
11617 if (!Subtarget->isTargetWin64()) {
11618 // If %al is 0, branch around the XMM save block.
11619 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011620 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000011621 MBB->addSuccessor(EndMBB);
11622 }
11623
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011624 unsigned MOVOpc = Subtarget->hasAVX() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000011625 // In the XMM save block, save all the XMM argument registers.
11626 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
11627 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000011628 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000011629 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000011630 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000011631 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000011632 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011633 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000011634 .addFrameIndex(RegSaveFrameIndex)
11635 .addImm(/*Scale=*/1)
11636 .addReg(/*IndexReg=*/0)
11637 .addImm(/*Disp=*/Offset)
11638 .addReg(/*Segment=*/0)
11639 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000011640 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000011641 }
11642
Dan Gohman14152b42010-07-06 20:24:04 +000011643 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000011644
11645 return EndMBB;
11646}
Mon P Wang63307c32008-05-05 19:05:59 +000011647
Evan Cheng60c07e12006-07-05 22:17:51 +000011648MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000011649X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000011650 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000011651 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11652 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000011653
Chris Lattner52600972009-09-02 05:57:00 +000011654 // To "insert" a SELECT_CC instruction, we actually have to insert the
11655 // diamond control-flow pattern. The incoming instruction knows the
11656 // destination vreg to set, the condition code register to branch on, the
11657 // true/false values to select between, and a branch opcode to use.
11658 const BasicBlock *LLVM_BB = BB->getBasicBlock();
11659 MachineFunction::iterator It = BB;
11660 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000011661
Chris Lattner52600972009-09-02 05:57:00 +000011662 // thisMBB:
11663 // ...
11664 // TrueVal = ...
11665 // cmpTY ccX, r1, r2
11666 // bCC copy1MBB
11667 // fallthrough --> copy0MBB
11668 MachineBasicBlock *thisMBB = BB;
11669 MachineFunction *F = BB->getParent();
11670 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
11671 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000011672 F->insert(It, copy0MBB);
11673 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000011674
Bill Wendling730c07e2010-06-25 20:48:10 +000011675 // If the EFLAGS register isn't dead in the terminator, then claim that it's
11676 // live into the sink and copy blocks.
Jakob Stoklund Olesen4a1b9d82011-09-02 23:52:49 +000011677 if (!MI->killsRegister(X86::EFLAGS)) {
11678 copy0MBB->addLiveIn(X86::EFLAGS);
11679 sinkMBB->addLiveIn(X86::EFLAGS);
Bill Wendling730c07e2010-06-25 20:48:10 +000011680 }
11681
Dan Gohman14152b42010-07-06 20:24:04 +000011682 // Transfer the remainder of BB and its successor edges to sinkMBB.
11683 sinkMBB->splice(sinkMBB->begin(), BB,
11684 llvm::next(MachineBasicBlock::iterator(MI)),
11685 BB->end());
11686 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
11687
11688 // Add the true and fallthrough blocks as its successors.
11689 BB->addSuccessor(copy0MBB);
11690 BB->addSuccessor(sinkMBB);
11691
11692 // Create the conditional branch instruction.
11693 unsigned Opc =
11694 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
11695 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
11696
Chris Lattner52600972009-09-02 05:57:00 +000011697 // copy0MBB:
11698 // %FalseValue = ...
11699 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000011700 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000011701
Chris Lattner52600972009-09-02 05:57:00 +000011702 // sinkMBB:
11703 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
11704 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000011705 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
11706 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000011707 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
11708 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
11709
Dan Gohman14152b42010-07-06 20:24:04 +000011710 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000011711 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000011712}
11713
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011714MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000011715X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
11716 bool Is64Bit) const {
11717 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11718 DebugLoc DL = MI->getDebugLoc();
11719 MachineFunction *MF = BB->getParent();
11720 const BasicBlock *LLVM_BB = BB->getBasicBlock();
11721
11722 assert(EnableSegmentedStacks);
11723
11724 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
11725 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
11726
11727 // BB:
11728 // ... [Till the alloca]
11729 // If stacklet is not large enough, jump to mallocMBB
11730 //
11731 // bumpMBB:
11732 // Allocate by subtracting from RSP
11733 // Jump to continueMBB
11734 //
11735 // mallocMBB:
11736 // Allocate by call to runtime
11737 //
11738 // continueMBB:
11739 // ...
11740 // [rest of original BB]
11741 //
11742
11743 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11744 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11745 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11746
11747 MachineRegisterInfo &MRI = MF->getRegInfo();
11748 const TargetRegisterClass *AddrRegClass =
11749 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
11750
11751 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
11752 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
11753 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
11754 sizeVReg = MI->getOperand(1).getReg(),
11755 physSPReg = Is64Bit ? X86::RSP : X86::ESP;
11756
11757 MachineFunction::iterator MBBIter = BB;
11758 ++MBBIter;
11759
11760 MF->insert(MBBIter, bumpMBB);
11761 MF->insert(MBBIter, mallocMBB);
11762 MF->insert(MBBIter, continueMBB);
11763
11764 continueMBB->splice(continueMBB->begin(), BB, llvm::next
11765 (MachineBasicBlock::iterator(MI)), BB->end());
11766 continueMBB->transferSuccessorsAndUpdatePHIs(BB);
11767
11768 // Add code to the main basic block to check if the stack limit has been hit,
11769 // and if so, jump to mallocMBB otherwise to bumpMBB.
11770 BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
11771 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), tmpSPVReg)
11772 .addReg(tmpSPVReg).addReg(sizeVReg);
11773 BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
11774 .addReg(0).addImm(0).addReg(0).addImm(TlsOffset).addReg(TlsReg)
11775 .addReg(tmpSPVReg);
11776 BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
11777
11778 // bumpMBB simply decreases the stack pointer, since we know the current
11779 // stacklet has enough space.
11780 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
11781 .addReg(tmpSPVReg);
11782 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
11783 .addReg(tmpSPVReg);
11784 BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
11785
11786 // Calls into a routine in libgcc to allocate more space from the heap.
11787 if (Is64Bit) {
11788 BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
11789 .addReg(sizeVReg);
11790 BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
11791 .addExternalSymbol("__morestack_allocate_stack_space").addReg(X86::RDI);
11792 } else {
11793 BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
11794 .addImm(12);
11795 BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
11796 BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
11797 .addExternalSymbol("__morestack_allocate_stack_space");
11798 }
11799
11800 if (!Is64Bit)
11801 BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
11802 .addImm(16);
11803
11804 BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
11805 .addReg(Is64Bit ? X86::RAX : X86::EAX);
11806 BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
11807
11808 // Set up the CFG correctly.
11809 BB->addSuccessor(bumpMBB);
11810 BB->addSuccessor(mallocMBB);
11811 mallocMBB->addSuccessor(continueMBB);
11812 bumpMBB->addSuccessor(continueMBB);
11813
11814 // Take care of the PHI nodes.
11815 BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
11816 MI->getOperand(0).getReg())
11817 .addReg(mallocPtrVReg).addMBB(mallocMBB)
11818 .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
11819
11820 // Delete the original pseudo instruction.
11821 MI->eraseFromParent();
11822
11823 // And we're done.
11824 return continueMBB;
11825}
11826
11827MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000011828X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000011829 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011830 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11831 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011832
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000011833 assert(!Subtarget->isTargetEnvMacho());
11834
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011835 // The lowering is pretty easy: we're just emitting the call to _alloca. The
11836 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011837
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000011838 if (Subtarget->isTargetWin64()) {
11839 if (Subtarget->isTargetCygMing()) {
11840 // ___chkstk(Mingw64):
11841 // Clobbers R10, R11, RAX and EFLAGS.
11842 // Updates RSP.
11843 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
11844 .addExternalSymbol("___chkstk")
11845 .addReg(X86::RAX, RegState::Implicit)
11846 .addReg(X86::RSP, RegState::Implicit)
11847 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
11848 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
11849 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
11850 } else {
11851 // __chkstk(MSVCRT): does not update stack pointer.
11852 // Clobbers R10, R11 and EFLAGS.
11853 // FIXME: RAX(allocated size) might be reused and not killed.
11854 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
11855 .addExternalSymbol("__chkstk")
11856 .addReg(X86::RAX, RegState::Implicit)
11857 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
11858 // RAX has the offset to subtracted from RSP.
11859 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
11860 .addReg(X86::RSP)
11861 .addReg(X86::RAX);
11862 }
11863 } else {
11864 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000011865 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
11866
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000011867 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
11868 .addExternalSymbol(StackProbeSymbol)
11869 .addReg(X86::EAX, RegState::Implicit)
11870 .addReg(X86::ESP, RegState::Implicit)
11871 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
11872 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
11873 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
11874 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011875
Dan Gohman14152b42010-07-06 20:24:04 +000011876 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011877 return BB;
11878}
Chris Lattner52600972009-09-02 05:57:00 +000011879
11880MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000011881X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
11882 MachineBasicBlock *BB) const {
11883 // This is pretty easy. We're taking the value that we received from
11884 // our load from the relocation, sticking it in either RDI (x86-64)
11885 // or EAX and doing an indirect call. The return value will then
11886 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000011887 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000011888 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000011889 DebugLoc DL = MI->getDebugLoc();
11890 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000011891
11892 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000011893 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000011894
Eric Christopher30ef0e52010-06-03 04:07:48 +000011895 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000011896 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
11897 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000011898 .addReg(X86::RIP)
11899 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000011900 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000011901 MI->getOperand(3).getTargetFlags())
11902 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000011903 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000011904 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +000011905 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000011906 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
11907 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000011908 .addReg(0)
11909 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000011910 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000011911 MI->getOperand(3).getTargetFlags())
11912 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000011913 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000011914 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000011915 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000011916 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
11917 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000011918 .addReg(TII->getGlobalBaseReg(F))
11919 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000011920 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000011921 MI->getOperand(3).getTargetFlags())
11922 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000011923 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000011924 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000011925 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000011926
Dan Gohman14152b42010-07-06 20:24:04 +000011927 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000011928 return BB;
11929}
11930
11931MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000011932X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000011933 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000011934 switch (MI->getOpcode()) {
11935 default: assert(false && "Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000011936 case X86::TAILJMPd64:
11937 case X86::TAILJMPr64:
11938 case X86::TAILJMPm64:
11939 assert(!"TAILJMP64 would not be touched here.");
11940 case X86::TCRETURNdi64:
11941 case X86::TCRETURNri64:
11942 case X86::TCRETURNmi64:
11943 // Defs of TCRETURNxx64 has Win64's callee-saved registers, as subset.
11944 // On AMD64, additional defs should be added before register allocation.
11945 if (!Subtarget->isTargetWin64()) {
11946 MI->addRegisterDefined(X86::RSI);
11947 MI->addRegisterDefined(X86::RDI);
11948 MI->addRegisterDefined(X86::XMM6);
11949 MI->addRegisterDefined(X86::XMM7);
11950 MI->addRegisterDefined(X86::XMM8);
11951 MI->addRegisterDefined(X86::XMM9);
11952 MI->addRegisterDefined(X86::XMM10);
11953 MI->addRegisterDefined(X86::XMM11);
11954 MI->addRegisterDefined(X86::XMM12);
11955 MI->addRegisterDefined(X86::XMM13);
11956 MI->addRegisterDefined(X86::XMM14);
11957 MI->addRegisterDefined(X86::XMM15);
11958 }
11959 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000011960 case X86::WIN_ALLOCA:
11961 return EmitLoweredWinAlloca(MI, BB);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000011962 case X86::SEG_ALLOCA_32:
11963 return EmitLoweredSegAlloca(MI, BB, false);
11964 case X86::SEG_ALLOCA_64:
11965 return EmitLoweredSegAlloca(MI, BB, true);
Eric Christopher30ef0e52010-06-03 04:07:48 +000011966 case X86::TLSCall_32:
11967 case X86::TLSCall_64:
11968 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000011969 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000011970 case X86::CMOV_FR32:
11971 case X86::CMOV_FR64:
11972 case X86::CMOV_V4F32:
11973 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000011974 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000011975 case X86::CMOV_V8F32:
11976 case X86::CMOV_V4F64:
11977 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000011978 case X86::CMOV_GR16:
11979 case X86::CMOV_GR32:
11980 case X86::CMOV_RFP32:
11981 case X86::CMOV_RFP64:
11982 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000011983 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000011984
Dale Johannesen849f2142007-07-03 00:53:03 +000011985 case X86::FP32_TO_INT16_IN_MEM:
11986 case X86::FP32_TO_INT32_IN_MEM:
11987 case X86::FP32_TO_INT64_IN_MEM:
11988 case X86::FP64_TO_INT16_IN_MEM:
11989 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000011990 case X86::FP64_TO_INT64_IN_MEM:
11991 case X86::FP80_TO_INT16_IN_MEM:
11992 case X86::FP80_TO_INT32_IN_MEM:
11993 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000011994 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11995 DebugLoc DL = MI->getDebugLoc();
11996
Evan Cheng60c07e12006-07-05 22:17:51 +000011997 // Change the floating point control register to use "round towards zero"
11998 // mode when truncating to an integer value.
11999 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000012000 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000012001 addFrameReference(BuildMI(*BB, MI, DL,
12002 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012003
12004 // Load the old value of the high byte of the control word...
12005 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000012006 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000012007 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000012008 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012009
12010 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000012011 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012012 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000012013
12014 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000012015 addFrameReference(BuildMI(*BB, MI, DL,
12016 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012017
12018 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000012019 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012020 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000012021
12022 // Get the X86 opcode to use.
12023 unsigned Opc;
12024 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000012025 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000012026 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
12027 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
12028 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
12029 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
12030 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
12031 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000012032 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
12033 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
12034 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000012035 }
12036
12037 X86AddressMode AM;
12038 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000012039 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012040 AM.BaseType = X86AddressMode::RegBase;
12041 AM.Base.Reg = Op.getReg();
12042 } else {
12043 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000012044 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000012045 }
12046 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000012047 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012048 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012049 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000012050 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012051 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012052 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000012053 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012054 AM.GV = Op.getGlobal();
12055 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000012056 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012057 }
Dan Gohman14152b42010-07-06 20:24:04 +000012058 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000012059 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000012060
12061 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000012062 addFrameReference(BuildMI(*BB, MI, DL,
12063 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012064
Dan Gohman14152b42010-07-06 20:24:04 +000012065 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000012066 return BB;
12067 }
Eric Christopherb120ab42009-08-18 22:50:32 +000012068 // String/text processing lowering.
12069 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012070 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000012071 return EmitPCMP(MI, BB, 3, false /* in-mem */);
12072 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012073 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000012074 return EmitPCMP(MI, BB, 3, true /* in-mem */);
12075 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012076 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000012077 return EmitPCMP(MI, BB, 5, false /* in mem */);
12078 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012079 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000012080 return EmitPCMP(MI, BB, 5, true /* in mem */);
12081
Eric Christopher228232b2010-11-30 07:20:12 +000012082 // Thread synchronization.
12083 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012084 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000012085 case X86::MWAIT:
12086 return EmitMwait(MI, BB);
12087
Eric Christopherb120ab42009-08-18 22:50:32 +000012088 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000012089 case X86::ATOMAND32:
12090 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012091 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012092 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012093 X86::NOT32r, X86::EAX,
12094 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000012095 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000012096 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
12097 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012098 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012099 X86::NOT32r, X86::EAX,
12100 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000012101 case X86::ATOMXOR32:
12102 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012103 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012104 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012105 X86::NOT32r, X86::EAX,
12106 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000012107 case X86::ATOMNAND32:
12108 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012109 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012110 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012111 X86::NOT32r, X86::EAX,
12112 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000012113 case X86::ATOMMIN32:
12114 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
12115 case X86::ATOMMAX32:
12116 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
12117 case X86::ATOMUMIN32:
12118 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
12119 case X86::ATOMUMAX32:
12120 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000012121
12122 case X86::ATOMAND16:
12123 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
12124 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012125 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012126 X86::NOT16r, X86::AX,
12127 X86::GR16RegisterClass);
12128 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000012129 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012130 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012131 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012132 X86::NOT16r, X86::AX,
12133 X86::GR16RegisterClass);
12134 case X86::ATOMXOR16:
12135 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
12136 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012137 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012138 X86::NOT16r, X86::AX,
12139 X86::GR16RegisterClass);
12140 case X86::ATOMNAND16:
12141 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
12142 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012143 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012144 X86::NOT16r, X86::AX,
12145 X86::GR16RegisterClass, true);
12146 case X86::ATOMMIN16:
12147 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
12148 case X86::ATOMMAX16:
12149 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
12150 case X86::ATOMUMIN16:
12151 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
12152 case X86::ATOMUMAX16:
12153 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
12154
12155 case X86::ATOMAND8:
12156 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
12157 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012158 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012159 X86::NOT8r, X86::AL,
12160 X86::GR8RegisterClass);
12161 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000012162 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012163 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012164 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012165 X86::NOT8r, X86::AL,
12166 X86::GR8RegisterClass);
12167 case X86::ATOMXOR8:
12168 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
12169 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012170 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012171 X86::NOT8r, X86::AL,
12172 X86::GR8RegisterClass);
12173 case X86::ATOMNAND8:
12174 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
12175 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012176 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012177 X86::NOT8r, X86::AL,
12178 X86::GR8RegisterClass, true);
12179 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012180 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000012181 case X86::ATOMAND64:
12182 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012183 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012184 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012185 X86::NOT64r, X86::RAX,
12186 X86::GR64RegisterClass);
12187 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000012188 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
12189 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012190 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012191 X86::NOT64r, X86::RAX,
12192 X86::GR64RegisterClass);
12193 case X86::ATOMXOR64:
12194 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012195 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012196 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012197 X86::NOT64r, X86::RAX,
12198 X86::GR64RegisterClass);
12199 case X86::ATOMNAND64:
12200 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
12201 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012202 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012203 X86::NOT64r, X86::RAX,
12204 X86::GR64RegisterClass, true);
12205 case X86::ATOMMIN64:
12206 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
12207 case X86::ATOMMAX64:
12208 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
12209 case X86::ATOMUMIN64:
12210 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
12211 case X86::ATOMUMAX64:
12212 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012213
12214 // This group does 64-bit operations on a 32-bit host.
12215 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012216 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012217 X86::AND32rr, X86::AND32rr,
12218 X86::AND32ri, X86::AND32ri,
12219 false);
12220 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012221 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012222 X86::OR32rr, X86::OR32rr,
12223 X86::OR32ri, X86::OR32ri,
12224 false);
12225 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012226 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012227 X86::XOR32rr, X86::XOR32rr,
12228 X86::XOR32ri, X86::XOR32ri,
12229 false);
12230 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012231 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012232 X86::AND32rr, X86::AND32rr,
12233 X86::AND32ri, X86::AND32ri,
12234 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012235 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012236 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012237 X86::ADD32rr, X86::ADC32rr,
12238 X86::ADD32ri, X86::ADC32ri,
12239 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012240 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012241 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012242 X86::SUB32rr, X86::SBB32rr,
12243 X86::SUB32ri, X86::SBB32ri,
12244 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000012245 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012246 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000012247 X86::MOV32rr, X86::MOV32rr,
12248 X86::MOV32ri, X86::MOV32ri,
12249 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012250 case X86::VASTART_SAVE_XMM_REGS:
12251 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000012252
12253 case X86::VAARG_64:
12254 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000012255 }
12256}
12257
12258//===----------------------------------------------------------------------===//
12259// X86 Optimization Hooks
12260//===----------------------------------------------------------------------===//
12261
Dan Gohman475871a2008-07-27 21:46:04 +000012262void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000012263 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000012264 APInt &KnownZero,
12265 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000012266 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000012267 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012268 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000012269 assert((Opc >= ISD::BUILTIN_OP_END ||
12270 Opc == ISD::INTRINSIC_WO_CHAIN ||
12271 Opc == ISD::INTRINSIC_W_CHAIN ||
12272 Opc == ISD::INTRINSIC_VOID) &&
12273 "Should use MaskedValueIsZero if you don't know whether Op"
12274 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012275
Dan Gohmanf4f92f52008-02-13 23:07:24 +000012276 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012277 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000012278 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012279 case X86ISD::ADD:
12280 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000012281 case X86ISD::ADC:
12282 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012283 case X86ISD::SMUL:
12284 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000012285 case X86ISD::INC:
12286 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000012287 case X86ISD::OR:
12288 case X86ISD::XOR:
12289 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012290 // These nodes' second result is a boolean.
12291 if (Op.getResNo() == 0)
12292 break;
12293 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012294 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000012295 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
12296 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000012297 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012298 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012299}
Chris Lattner259e97c2006-01-31 19:43:35 +000012300
Owen Andersonbc146b02010-09-21 20:42:50 +000012301unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
12302 unsigned Depth) const {
12303 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
12304 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
12305 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000012306
Owen Andersonbc146b02010-09-21 20:42:50 +000012307 // Fallback case.
12308 return 1;
12309}
12310
Evan Cheng206ee9d2006-07-07 08:33:52 +000012311/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000012312/// node is a GlobalAddress + offset.
12313bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000012314 const GlobalValue* &GA,
12315 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000012316 if (N->getOpcode() == X86ISD::Wrapper) {
12317 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000012318 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000012319 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000012320 return true;
12321 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000012322 }
Evan Chengad4196b2008-05-12 19:56:52 +000012323 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000012324}
12325
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012326/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
12327/// same as extracting the high 128-bit part of 256-bit vector and then
12328/// inserting the result into the low part of a new 256-bit vector
12329static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
12330 EVT VT = SVOp->getValueType(0);
12331 int NumElems = VT.getVectorNumElements();
12332
12333 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
12334 for (int i = 0, j = NumElems/2; i < NumElems/2; ++i, ++j)
12335 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
12336 SVOp->getMaskElt(j) >= 0)
12337 return false;
12338
12339 return true;
12340}
12341
12342/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
12343/// same as extracting the low 128-bit part of 256-bit vector and then
12344/// inserting the result into the high part of a new 256-bit vector
12345static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
12346 EVT VT = SVOp->getValueType(0);
12347 int NumElems = VT.getVectorNumElements();
12348
12349 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
12350 for (int i = NumElems/2, j = 0; i < NumElems; ++i, ++j)
12351 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
12352 SVOp->getMaskElt(j) >= 0)
12353 return false;
12354
12355 return true;
12356}
12357
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012358/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
12359static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
12360 TargetLowering::DAGCombinerInfo &DCI) {
12361 DebugLoc dl = N->getDebugLoc();
12362 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
12363 SDValue V1 = SVOp->getOperand(0);
12364 SDValue V2 = SVOp->getOperand(1);
12365 EVT VT = SVOp->getValueType(0);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012366 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012367
12368 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
12369 V2.getOpcode() == ISD::CONCAT_VECTORS) {
12370 //
12371 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000012372 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012373 // V UNDEF BUILD_VECTOR UNDEF
12374 // \ / \ /
12375 // CONCAT_VECTOR CONCAT_VECTOR
12376 // \ /
12377 // \ /
12378 // RESULT: V + zero extended
12379 //
12380 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
12381 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
12382 V1.getOperand(1).getOpcode() != ISD::UNDEF)
12383 return SDValue();
12384
12385 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
12386 return SDValue();
12387
12388 // To match the shuffle mask, the first half of the mask should
12389 // be exactly the first vector, and all the rest a splat with the
12390 // first element of the second one.
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012391 for (int i = 0; i < NumElems/2; ++i)
12392 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
12393 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
12394 return SDValue();
12395
12396 // Emit a zeroed vector and insert the desired subvector on its
12397 // first half.
12398 SDValue Zeros = getZeroVector(VT, true /* HasSSE2 */, DAG, dl);
12399 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0),
12400 DAG.getConstant(0, MVT::i32), DAG, dl);
12401 return DCI.CombineTo(N, InsV);
12402 }
12403
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012404 //===--------------------------------------------------------------------===//
12405 // Combine some shuffles into subvector extracts and inserts:
12406 //
12407
12408 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
12409 if (isShuffleHigh128VectorInsertLow(SVOp)) {
12410 SDValue V = Extract128BitVector(V1, DAG.getConstant(NumElems/2, MVT::i32),
12411 DAG, dl);
12412 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT),
12413 V, DAG.getConstant(0, MVT::i32), DAG, dl);
12414 return DCI.CombineTo(N, InsV);
12415 }
12416
12417 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
12418 if (isShuffleLow128VectorInsertHigh(SVOp)) {
12419 SDValue V = Extract128BitVector(V1, DAG.getConstant(0, MVT::i32), DAG, dl);
12420 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT),
12421 V, DAG.getConstant(NumElems/2, MVT::i32), DAG, dl);
12422 return DCI.CombineTo(N, InsV);
12423 }
12424
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012425 return SDValue();
12426}
12427
12428/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000012429static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000012430 TargetLowering::DAGCombinerInfo &DCI,
12431 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000012432 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000012433 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000012434
Mon P Wanga0fd0d52010-12-19 23:55:53 +000012435 // Don't create instructions with illegal types after legalize types has run.
12436 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12437 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
12438 return SDValue();
12439
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000012440 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
12441 if (Subtarget->hasAVX() && VT.getSizeInBits() == 256 &&
12442 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012443 return PerformShuffleCombine256(N, DAG, DCI);
12444
12445 // Only handle 128 wide vector from here on.
12446 if (VT.getSizeInBits() != 128)
12447 return SDValue();
12448
12449 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
12450 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
12451 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000012452 SmallVector<SDValue, 16> Elts;
12453 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012454 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000012455
Nate Begemanfdea31a2010-03-24 20:49:50 +000012456 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000012457}
Evan Chengd880b972008-05-09 21:53:03 +000012458
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000012459/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
12460/// generation and convert it from being a bunch of shuffles and extracts
12461/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012462static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
12463 const TargetLowering &TLI) {
12464 SDValue InputVector = N->getOperand(0);
12465
12466 // Only operate on vectors of 4 elements, where the alternative shuffling
12467 // gets to be more expensive.
12468 if (InputVector.getValueType() != MVT::v4i32)
12469 return SDValue();
12470
12471 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
12472 // single use which is a sign-extend or zero-extend, and all elements are
12473 // used.
12474 SmallVector<SDNode *, 4> Uses;
12475 unsigned ExtractedElements = 0;
12476 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
12477 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
12478 if (UI.getUse().getResNo() != InputVector.getResNo())
12479 return SDValue();
12480
12481 SDNode *Extract = *UI;
12482 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
12483 return SDValue();
12484
12485 if (Extract->getValueType(0) != MVT::i32)
12486 return SDValue();
12487 if (!Extract->hasOneUse())
12488 return SDValue();
12489 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
12490 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
12491 return SDValue();
12492 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
12493 return SDValue();
12494
12495 // Record which element was extracted.
12496 ExtractedElements |=
12497 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
12498
12499 Uses.push_back(Extract);
12500 }
12501
12502 // If not all the elements were used, this may not be worthwhile.
12503 if (ExtractedElements != 15)
12504 return SDValue();
12505
12506 // Ok, we've now decided to do the transformation.
12507 DebugLoc dl = InputVector.getDebugLoc();
12508
12509 // Store the value to a temporary stack slot.
12510 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000012511 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
12512 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012513
12514 // Replace each use (extract) with a load of the appropriate element.
12515 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
12516 UE = Uses.end(); UI != UE; ++UI) {
12517 SDNode *Extract = *UI;
12518
Nadav Rotem86694292011-05-17 08:31:57 +000012519 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012520 SDValue Idx = Extract->getOperand(1);
12521 unsigned EltSize =
12522 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
12523 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
12524 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
12525
Nadav Rotem86694292011-05-17 08:31:57 +000012526 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000012527 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012528
12529 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000012530 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000012531 ScalarAddr, MachinePointerInfo(),
12532 false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012533
12534 // Replace the exact with the load.
12535 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
12536 }
12537
12538 // The replacement was made in place; don't return anything.
12539 return SDValue();
12540}
12541
Chris Lattner83e6c992006-10-04 06:57:07 +000012542/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000012543static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000012544 const X86Subtarget *Subtarget) {
12545 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000012546 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000012547 // Get the LHS/RHS of the select.
12548 SDValue LHS = N->getOperand(1);
12549 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +000012550
Dan Gohman670e5392009-09-21 18:03:22 +000012551 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000012552 // instructions match the semantics of the common C idiom x<y?x:y but not
12553 // x<=y?x:y, because of how they handle negative zero (which can be
12554 // ignored in unsafe-math mode).
Chris Lattner83e6c992006-10-04 06:57:07 +000012555 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +000012556 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +000012557 Cond.getOpcode() == ISD::SETCC) {
12558 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012559
Chris Lattner47b4ce82009-03-11 05:48:52 +000012560 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000012561 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000012562 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
12563 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000012564 switch (CC) {
12565 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000012566 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000012567 // Converting this to a min would handle NaNs incorrectly, and swapping
12568 // the operands would cause it to handle comparisons between positive
12569 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000012570 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000012571 if (!UnsafeFPMath &&
12572 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
12573 break;
12574 std::swap(LHS, RHS);
12575 }
Dan Gohman670e5392009-09-21 18:03:22 +000012576 Opcode = X86ISD::FMIN;
12577 break;
12578 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000012579 // Converting this to a min would handle comparisons between positive
12580 // and negative zero incorrectly.
12581 if (!UnsafeFPMath &&
12582 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
12583 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012584 Opcode = X86ISD::FMIN;
12585 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000012586 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000012587 // Converting this to a min would handle both negative zeros and NaNs
12588 // incorrectly, but we can swap the operands to fix both.
12589 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012590 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012591 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000012592 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012593 Opcode = X86ISD::FMIN;
12594 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012595
Dan Gohman670e5392009-09-21 18:03:22 +000012596 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012597 // Converting this to a max would handle comparisons between positive
12598 // and negative zero incorrectly.
12599 if (!UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000012600 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012601 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012602 Opcode = X86ISD::FMAX;
12603 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000012604 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000012605 // Converting this to a max would handle NaNs incorrectly, and swapping
12606 // the operands would cause it to handle comparisons between positive
12607 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000012608 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000012609 if (!UnsafeFPMath &&
12610 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
12611 break;
12612 std::swap(LHS, RHS);
12613 }
Dan Gohman670e5392009-09-21 18:03:22 +000012614 Opcode = X86ISD::FMAX;
12615 break;
12616 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012617 // Converting this to a max would handle both negative zeros and NaNs
12618 // incorrectly, but we can swap the operands to fix both.
12619 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012620 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012621 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012622 case ISD::SETGE:
12623 Opcode = X86ISD::FMAX;
12624 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000012625 }
Dan Gohman670e5392009-09-21 18:03:22 +000012626 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000012627 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
12628 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000012629 switch (CC) {
12630 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000012631 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012632 // Converting this to a min would handle comparisons between positive
12633 // and negative zero incorrectly, and swapping the operands would
12634 // cause it to handle NaNs incorrectly.
12635 if (!UnsafeFPMath &&
12636 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000012637 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012638 break;
12639 std::swap(LHS, RHS);
12640 }
Dan Gohman670e5392009-09-21 18:03:22 +000012641 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000012642 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012643 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000012644 // Converting this to a min would handle NaNs incorrectly.
12645 if (!UnsafeFPMath &&
12646 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
12647 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012648 Opcode = X86ISD::FMIN;
12649 break;
12650 case ISD::SETUGE:
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::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012655 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012656 case ISD::SETGE:
12657 Opcode = X86ISD::FMIN;
12658 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012659
Dan Gohman670e5392009-09-21 18:03:22 +000012660 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000012661 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000012662 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012663 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012664 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000012665 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012666 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000012667 // Converting this to a max would handle comparisons between positive
12668 // and negative zero incorrectly, and swapping the operands would
12669 // cause it to handle NaNs incorrectly.
12670 if (!UnsafeFPMath &&
12671 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000012672 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012673 break;
12674 std::swap(LHS, RHS);
12675 }
Dan Gohman670e5392009-09-21 18:03:22 +000012676 Opcode = X86ISD::FMAX;
12677 break;
12678 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000012679 // Converting this to a max would handle both negative zeros and NaNs
12680 // incorrectly, but we can swap the operands to fix both.
12681 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012682 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012683 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000012684 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012685 Opcode = X86ISD::FMAX;
12686 break;
12687 }
Chris Lattner83e6c992006-10-04 06:57:07 +000012688 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012689
Chris Lattner47b4ce82009-03-11 05:48:52 +000012690 if (Opcode)
12691 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000012692 }
Eric Christopherfd179292009-08-27 18:07:15 +000012693
Chris Lattnerd1980a52009-03-12 06:52:53 +000012694 // If this is a select between two integer constants, try to do some
12695 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000012696 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
12697 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000012698 // Don't do this for crazy integer types.
12699 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
12700 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000012701 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000012702 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000012703
Chris Lattnercee56e72009-03-13 05:53:31 +000012704 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000012705 // Efficiently invertible.
12706 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
12707 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
12708 isa<ConstantSDNode>(Cond.getOperand(1))))) {
12709 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000012710 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000012711 }
Eric Christopherfd179292009-08-27 18:07:15 +000012712
Chris Lattnerd1980a52009-03-12 06:52:53 +000012713 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000012714 if (FalseC->getAPIntValue() == 0 &&
12715 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000012716 if (NeedsCondInvert) // Invert the condition if needed.
12717 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
12718 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000012719
Chris Lattnerd1980a52009-03-12 06:52:53 +000012720 // Zero extend the condition if needed.
12721 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000012722
Chris Lattnercee56e72009-03-13 05:53:31 +000012723 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000012724 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000012725 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000012726 }
Eric Christopherfd179292009-08-27 18:07:15 +000012727
Chris Lattner97a29a52009-03-13 05:22:11 +000012728 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000012729 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000012730 if (NeedsCondInvert) // Invert the condition if needed.
12731 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
12732 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000012733
Chris Lattner97a29a52009-03-13 05:22:11 +000012734 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000012735 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
12736 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000012737 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000012738 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000012739 }
Eric Christopherfd179292009-08-27 18:07:15 +000012740
Chris Lattnercee56e72009-03-13 05:53:31 +000012741 // Optimize cases that will turn into an LEA instruction. This requires
12742 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000012743 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000012744 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000012745 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000012746
Chris Lattnercee56e72009-03-13 05:53:31 +000012747 bool isFastMultiplier = false;
12748 if (Diff < 10) {
12749 switch ((unsigned char)Diff) {
12750 default: break;
12751 case 1: // result = add base, cond
12752 case 2: // result = lea base( , cond*2)
12753 case 3: // result = lea base(cond, cond*2)
12754 case 4: // result = lea base( , cond*4)
12755 case 5: // result = lea base(cond, cond*4)
12756 case 8: // result = lea base( , cond*8)
12757 case 9: // result = lea base(cond, cond*8)
12758 isFastMultiplier = true;
12759 break;
12760 }
12761 }
Eric Christopherfd179292009-08-27 18:07:15 +000012762
Chris Lattnercee56e72009-03-13 05:53:31 +000012763 if (isFastMultiplier) {
12764 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
12765 if (NeedsCondInvert) // Invert the condition if needed.
12766 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
12767 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000012768
Chris Lattnercee56e72009-03-13 05:53:31 +000012769 // Zero extend the condition if needed.
12770 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
12771 Cond);
12772 // Scale the condition by the difference.
12773 if (Diff != 1)
12774 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
12775 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000012776
Chris Lattnercee56e72009-03-13 05:53:31 +000012777 // Add the base if non-zero.
12778 if (FalseC->getAPIntValue() != 0)
12779 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
12780 SDValue(FalseC, 0));
12781 return Cond;
12782 }
Eric Christopherfd179292009-08-27 18:07:15 +000012783 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000012784 }
12785 }
Eric Christopherfd179292009-08-27 18:07:15 +000012786
Dan Gohman475871a2008-07-27 21:46:04 +000012787 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000012788}
12789
Chris Lattnerd1980a52009-03-12 06:52:53 +000012790/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
12791static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
12792 TargetLowering::DAGCombinerInfo &DCI) {
12793 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000012794
Chris Lattnerd1980a52009-03-12 06:52:53 +000012795 // If the flag operand isn't dead, don't touch this CMOV.
12796 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
12797 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000012798
Evan Chengb5a55d92011-05-24 01:48:22 +000012799 SDValue FalseOp = N->getOperand(0);
12800 SDValue TrueOp = N->getOperand(1);
12801 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
12802 SDValue Cond = N->getOperand(3);
12803 if (CC == X86::COND_E || CC == X86::COND_NE) {
12804 switch (Cond.getOpcode()) {
12805 default: break;
12806 case X86ISD::BSR:
12807 case X86ISD::BSF:
12808 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
12809 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
12810 return (CC == X86::COND_E) ? FalseOp : TrueOp;
12811 }
12812 }
12813
Chris Lattnerd1980a52009-03-12 06:52:53 +000012814 // If this is a select between two integer constants, try to do some
12815 // optimizations. Note that the operands are ordered the opposite of SELECT
12816 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000012817 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
12818 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000012819 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
12820 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000012821 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
12822 CC = X86::GetOppositeBranchCondition(CC);
12823 std::swap(TrueC, FalseC);
12824 }
Eric Christopherfd179292009-08-27 18:07:15 +000012825
Chris Lattnerd1980a52009-03-12 06:52:53 +000012826 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000012827 // This is efficient for any integer data type (including i8/i16) and
12828 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000012829 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012830 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
12831 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000012832
Chris Lattnerd1980a52009-03-12 06:52:53 +000012833 // Zero extend the condition if needed.
12834 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000012835
Chris Lattnerd1980a52009-03-12 06:52:53 +000012836 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
12837 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000012838 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000012839 if (N->getNumValues() == 2) // Dead flag value?
12840 return DCI.CombineTo(N, Cond, SDValue());
12841 return Cond;
12842 }
Eric Christopherfd179292009-08-27 18:07:15 +000012843
Chris Lattnercee56e72009-03-13 05:53:31 +000012844 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
12845 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000012846 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012847 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
12848 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000012849
Chris Lattner97a29a52009-03-13 05:22:11 +000012850 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000012851 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
12852 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000012853 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
12854 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000012855
Chris Lattner97a29a52009-03-13 05:22:11 +000012856 if (N->getNumValues() == 2) // Dead flag value?
12857 return DCI.CombineTo(N, Cond, SDValue());
12858 return Cond;
12859 }
Eric Christopherfd179292009-08-27 18:07:15 +000012860
Chris Lattnercee56e72009-03-13 05:53:31 +000012861 // Optimize cases that will turn into an LEA instruction. This requires
12862 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000012863 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000012864 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000012865 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000012866
Chris Lattnercee56e72009-03-13 05:53:31 +000012867 bool isFastMultiplier = false;
12868 if (Diff < 10) {
12869 switch ((unsigned char)Diff) {
12870 default: break;
12871 case 1: // result = add base, cond
12872 case 2: // result = lea base( , cond*2)
12873 case 3: // result = lea base(cond, cond*2)
12874 case 4: // result = lea base( , cond*4)
12875 case 5: // result = lea base(cond, cond*4)
12876 case 8: // result = lea base( , cond*8)
12877 case 9: // result = lea base(cond, cond*8)
12878 isFastMultiplier = true;
12879 break;
12880 }
12881 }
Eric Christopherfd179292009-08-27 18:07:15 +000012882
Chris Lattnercee56e72009-03-13 05:53:31 +000012883 if (isFastMultiplier) {
12884 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000012885 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
12886 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000012887 // Zero extend the condition if needed.
12888 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
12889 Cond);
12890 // Scale the condition by the difference.
12891 if (Diff != 1)
12892 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
12893 DAG.getConstant(Diff, Cond.getValueType()));
12894
12895 // Add the base if non-zero.
12896 if (FalseC->getAPIntValue() != 0)
12897 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
12898 SDValue(FalseC, 0));
12899 if (N->getNumValues() == 2) // Dead flag value?
12900 return DCI.CombineTo(N, Cond, SDValue());
12901 return Cond;
12902 }
Eric Christopherfd179292009-08-27 18:07:15 +000012903 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000012904 }
12905 }
12906 return SDValue();
12907}
12908
12909
Evan Cheng0b0cd912009-03-28 05:57:29 +000012910/// PerformMulCombine - Optimize a single multiply with constant into two
12911/// in order to implement it with two cheaper instructions, e.g.
12912/// LEA + SHL, LEA + LEA.
12913static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
12914 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000012915 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
12916 return SDValue();
12917
Owen Andersone50ed302009-08-10 22:56:29 +000012918 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012919 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000012920 return SDValue();
12921
12922 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
12923 if (!C)
12924 return SDValue();
12925 uint64_t MulAmt = C->getZExtValue();
12926 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
12927 return SDValue();
12928
12929 uint64_t MulAmt1 = 0;
12930 uint64_t MulAmt2 = 0;
12931 if ((MulAmt % 9) == 0) {
12932 MulAmt1 = 9;
12933 MulAmt2 = MulAmt / 9;
12934 } else if ((MulAmt % 5) == 0) {
12935 MulAmt1 = 5;
12936 MulAmt2 = MulAmt / 5;
12937 } else if ((MulAmt % 3) == 0) {
12938 MulAmt1 = 3;
12939 MulAmt2 = MulAmt / 3;
12940 }
12941 if (MulAmt2 &&
12942 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
12943 DebugLoc DL = N->getDebugLoc();
12944
12945 if (isPowerOf2_64(MulAmt2) &&
12946 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
12947 // If second multiplifer is pow2, issue it first. We want the multiply by
12948 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
12949 // is an add.
12950 std::swap(MulAmt1, MulAmt2);
12951
12952 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000012953 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000012954 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000012955 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000012956 else
Evan Cheng73f24c92009-03-30 21:36:47 +000012957 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000012958 DAG.getConstant(MulAmt1, VT));
12959
Eric Christopherfd179292009-08-27 18:07:15 +000012960 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000012961 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000012962 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000012963 else
Evan Cheng73f24c92009-03-30 21:36:47 +000012964 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000012965 DAG.getConstant(MulAmt2, VT));
12966
12967 // Do not add new nodes to DAG combiner worklist.
12968 DCI.CombineTo(N, NewMul, false);
12969 }
12970 return SDValue();
12971}
12972
Evan Chengad9c0a32009-12-15 00:53:42 +000012973static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
12974 SDValue N0 = N->getOperand(0);
12975 SDValue N1 = N->getOperand(1);
12976 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
12977 EVT VT = N0.getValueType();
12978
12979 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
12980 // since the result of setcc_c is all zero's or all ones.
12981 if (N1C && N0.getOpcode() == ISD::AND &&
12982 N0.getOperand(1).getOpcode() == ISD::Constant) {
12983 SDValue N00 = N0.getOperand(0);
12984 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
12985 ((N00.getOpcode() == ISD::ANY_EXTEND ||
12986 N00.getOpcode() == ISD::ZERO_EXTEND) &&
12987 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
12988 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
12989 APInt ShAmt = N1C->getAPIntValue();
12990 Mask = Mask.shl(ShAmt);
12991 if (Mask != 0)
12992 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
12993 N00, DAG.getConstant(Mask, VT));
12994 }
12995 }
12996
12997 return SDValue();
12998}
Evan Cheng0b0cd912009-03-28 05:57:29 +000012999
Nate Begeman740ab032009-01-26 00:52:55 +000013000/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
13001/// when possible.
13002static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
13003 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000013004 EVT VT = N->getValueType(0);
13005 if (!VT.isVector() && VT.isInteger() &&
13006 N->getOpcode() == ISD::SHL)
13007 return PerformSHLCombine(N, DAG);
13008
Nate Begeman740ab032009-01-26 00:52:55 +000013009 // On X86 with SSE2 support, we can transform this to a vector shift if
13010 // all elements are shifted by the same amount. We can't do this in legalize
13011 // because the a constant vector is typically transformed to a constant pool
13012 // so we have no knowledge of the shift amount.
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +000013013 if (!(Subtarget->hasSSE2() || Subtarget->hasAVX()))
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013014 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000013015
Owen Anderson825b72b2009-08-11 20:47:22 +000013016 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013017 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000013018
Mon P Wang3becd092009-01-28 08:12:05 +000013019 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000013020 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000013021 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000013022 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000013023 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
13024 unsigned NumElts = VT.getVectorNumElements();
13025 unsigned i = 0;
13026 for (; i != NumElts; ++i) {
13027 SDValue Arg = ShAmtOp.getOperand(i);
13028 if (Arg.getOpcode() == ISD::UNDEF) continue;
13029 BaseShAmt = Arg;
13030 break;
13031 }
13032 for (; i != NumElts; ++i) {
13033 SDValue Arg = ShAmtOp.getOperand(i);
13034 if (Arg.getOpcode() == ISD::UNDEF) continue;
13035 if (Arg != BaseShAmt) {
13036 return SDValue();
13037 }
13038 }
13039 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000013040 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000013041 SDValue InVec = ShAmtOp.getOperand(0);
13042 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
13043 unsigned NumElts = InVec.getValueType().getVectorNumElements();
13044 unsigned i = 0;
13045 for (; i != NumElts; ++i) {
13046 SDValue Arg = InVec.getOperand(i);
13047 if (Arg.getOpcode() == ISD::UNDEF) continue;
13048 BaseShAmt = Arg;
13049 break;
13050 }
13051 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
13052 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000013053 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000013054 if (C->getZExtValue() == SplatIdx)
13055 BaseShAmt = InVec.getOperand(1);
13056 }
13057 }
13058 if (BaseShAmt.getNode() == 0)
13059 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
13060 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000013061 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013062 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000013063
Mon P Wangefa42202009-09-03 19:56:25 +000013064 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000013065 if (EltVT.bitsGT(MVT::i32))
13066 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
13067 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000013068 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000013069
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013070 // The shift amount is identical so we can do a vector shift.
13071 SDValue ValOp = N->getOperand(0);
13072 switch (N->getOpcode()) {
13073 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000013074 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013075 break;
13076 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000013077 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013078 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013079 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013080 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013081 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013082 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013083 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013084 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013085 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013086 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013087 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013088 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013089 break;
13090 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000013091 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013092 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013093 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013094 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013095 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013096 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013097 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013098 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013099 break;
13100 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000013101 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013102 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013103 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013104 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013105 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013106 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013107 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013108 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013109 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013110 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013111 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013112 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013113 break;
Nate Begeman740ab032009-01-26 00:52:55 +000013114 }
13115 return SDValue();
13116}
13117
Nate Begemanb65c1752010-12-17 22:55:37 +000013118
Stuart Hastings865f0932011-06-03 23:53:54 +000013119// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
13120// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
13121// and friends. Likewise for OR -> CMPNEQSS.
13122static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
13123 TargetLowering::DAGCombinerInfo &DCI,
13124 const X86Subtarget *Subtarget) {
13125 unsigned opcode;
13126
13127 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
13128 // we're requiring SSE2 for both.
13129 if (Subtarget->hasSSE2() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
13130 SDValue N0 = N->getOperand(0);
13131 SDValue N1 = N->getOperand(1);
13132 SDValue CMP0 = N0->getOperand(1);
13133 SDValue CMP1 = N1->getOperand(1);
13134 DebugLoc DL = N->getDebugLoc();
13135
13136 // The SETCCs should both refer to the same CMP.
13137 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
13138 return SDValue();
13139
13140 SDValue CMP00 = CMP0->getOperand(0);
13141 SDValue CMP01 = CMP0->getOperand(1);
13142 EVT VT = CMP00.getValueType();
13143
13144 if (VT == MVT::f32 || VT == MVT::f64) {
13145 bool ExpectingFlags = false;
13146 // Check for any users that want flags:
13147 for (SDNode::use_iterator UI = N->use_begin(),
13148 UE = N->use_end();
13149 !ExpectingFlags && UI != UE; ++UI)
13150 switch (UI->getOpcode()) {
13151 default:
13152 case ISD::BR_CC:
13153 case ISD::BRCOND:
13154 case ISD::SELECT:
13155 ExpectingFlags = true;
13156 break;
13157 case ISD::CopyToReg:
13158 case ISD::SIGN_EXTEND:
13159 case ISD::ZERO_EXTEND:
13160 case ISD::ANY_EXTEND:
13161 break;
13162 }
13163
13164 if (!ExpectingFlags) {
13165 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
13166 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
13167
13168 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
13169 X86::CondCode tmp = cc0;
13170 cc0 = cc1;
13171 cc1 = tmp;
13172 }
13173
13174 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
13175 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
13176 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
13177 X86ISD::NodeType NTOperator = is64BitFP ?
13178 X86ISD::FSETCCsd : X86ISD::FSETCCss;
13179 // FIXME: need symbolic constants for these magic numbers.
13180 // See X86ATTInstPrinter.cpp:printSSECC().
13181 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
13182 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
13183 DAG.getConstant(x86cc, MVT::i8));
13184 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
13185 OnesOrZeroesF);
13186 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
13187 DAG.getConstant(1, MVT::i32));
13188 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
13189 return OneBitOfTruth;
13190 }
13191 }
13192 }
13193 }
13194 return SDValue();
13195}
13196
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013197/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
13198/// so it can be folded inside ANDNP.
13199static bool CanFoldXORWithAllOnes(const SDNode *N) {
13200 EVT VT = N->getValueType(0);
13201
13202 // Match direct AllOnes for 128 and 256-bit vectors
13203 if (ISD::isBuildVectorAllOnes(N))
13204 return true;
13205
13206 // Look through a bit convert.
13207 if (N->getOpcode() == ISD::BITCAST)
13208 N = N->getOperand(0).getNode();
13209
13210 // Sometimes the operand may come from a insert_subvector building a 256-bit
13211 // allones vector
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013212 if (VT.getSizeInBits() == 256 &&
Bill Wendling456a9252011-08-04 00:32:58 +000013213 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
13214 SDValue V1 = N->getOperand(0);
13215 SDValue V2 = N->getOperand(1);
13216
13217 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
13218 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
13219 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
13220 ISD::isBuildVectorAllOnes(V2.getNode()))
13221 return true;
13222 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013223
13224 return false;
13225}
13226
Nate Begemanb65c1752010-12-17 22:55:37 +000013227static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
13228 TargetLowering::DAGCombinerInfo &DCI,
13229 const X86Subtarget *Subtarget) {
13230 if (DCI.isBeforeLegalizeOps())
13231 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013232
Stuart Hastings865f0932011-06-03 23:53:54 +000013233 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
13234 if (R.getNode())
13235 return R;
13236
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000013237 // Want to form ANDNP nodes:
13238 // 1) In the hopes of then easily combining them with OR and AND nodes
13239 // to form PBLEND/PSIGN.
13240 // 2) To match ANDN packed intrinsics
Nate Begemanb65c1752010-12-17 22:55:37 +000013241 EVT VT = N->getValueType(0);
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000013242 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000013243 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013244
Nate Begemanb65c1752010-12-17 22:55:37 +000013245 SDValue N0 = N->getOperand(0);
13246 SDValue N1 = N->getOperand(1);
13247 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013248
Nate Begemanb65c1752010-12-17 22:55:37 +000013249 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013250 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013251 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
13252 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013253 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000013254
13255 // Check RHS for vnot
13256 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013257 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
13258 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013259 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013260
Nate Begemanb65c1752010-12-17 22:55:37 +000013261 return SDValue();
13262}
13263
Evan Cheng760d1942010-01-04 21:22:48 +000013264static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000013265 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000013266 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000013267 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000013268 return SDValue();
13269
Stuart Hastings865f0932011-06-03 23:53:54 +000013270 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
13271 if (R.getNode())
13272 return R;
13273
Evan Cheng760d1942010-01-04 21:22:48 +000013274 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000013275 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64 && VT != MVT::v2i64)
Evan Cheng760d1942010-01-04 21:22:48 +000013276 return SDValue();
13277
Evan Cheng760d1942010-01-04 21:22:48 +000013278 SDValue N0 = N->getOperand(0);
13279 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013280
Nate Begemanb65c1752010-12-17 22:55:37 +000013281 // look for psign/blend
13282 if (Subtarget->hasSSSE3()) {
13283 if (VT == MVT::v2i64) {
13284 // Canonicalize pandn to RHS
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013285 if (N0.getOpcode() == X86ISD::ANDNP)
Nate Begemanb65c1752010-12-17 22:55:37 +000013286 std::swap(N0, N1);
13287 // or (and (m, x), (pandn m, y))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013288 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
Nate Begemanb65c1752010-12-17 22:55:37 +000013289 SDValue Mask = N1.getOperand(0);
13290 SDValue X = N1.getOperand(1);
13291 SDValue Y;
13292 if (N0.getOperand(0) == Mask)
13293 Y = N0.getOperand(1);
13294 if (N0.getOperand(1) == Mask)
13295 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013296
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013297 // Check to see if the mask appeared in both the AND and ANDNP and
Nate Begemanb65c1752010-12-17 22:55:37 +000013298 if (!Y.getNode())
13299 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013300
Nate Begemanb65c1752010-12-17 22:55:37 +000013301 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
13302 if (Mask.getOpcode() != ISD::BITCAST ||
13303 X.getOpcode() != ISD::BITCAST ||
13304 Y.getOpcode() != ISD::BITCAST)
13305 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013306
Nate Begemanb65c1752010-12-17 22:55:37 +000013307 // Look through mask bitcast.
13308 Mask = Mask.getOperand(0);
13309 EVT MaskVT = Mask.getValueType();
13310
13311 // Validate that the Mask operand is a vector sra node. The sra node
13312 // will be an intrinsic.
13313 if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
13314 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013315
Nate Begemanb65c1752010-12-17 22:55:37 +000013316 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
13317 // there is no psrai.b
13318 switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
13319 case Intrinsic::x86_sse2_psrai_w:
13320 case Intrinsic::x86_sse2_psrai_d:
13321 break;
13322 default: return SDValue();
13323 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013324
Nate Begemanb65c1752010-12-17 22:55:37 +000013325 // Check that the SRA is all signbits.
13326 SDValue SraC = Mask.getOperand(2);
13327 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
13328 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
13329 if ((SraAmt + 1) != EltBits)
13330 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013331
Nate Begemanb65c1752010-12-17 22:55:37 +000013332 DebugLoc DL = N->getDebugLoc();
13333
13334 // Now we know we at least have a plendvb with the mask val. See if
13335 // we can form a psignb/w/d.
13336 // psign = x.type == y.type == mask.type && y = sub(0, x);
13337 X = X.getOperand(0);
13338 Y = Y.getOperand(0);
13339 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
13340 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
13341 X.getValueType() == MaskVT && X.getValueType() == Y.getValueType()){
13342 unsigned Opc = 0;
13343 switch (EltBits) {
13344 case 8: Opc = X86ISD::PSIGNB; break;
13345 case 16: Opc = X86ISD::PSIGNW; break;
13346 case 32: Opc = X86ISD::PSIGND; break;
13347 default: break;
13348 }
13349 if (Opc) {
13350 SDValue Sign = DAG.getNode(Opc, DL, MaskVT, X, Mask.getOperand(1));
13351 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Sign);
13352 }
13353 }
13354 // PBLENDVB only available on SSE 4.1
13355 if (!Subtarget->hasSSE41())
13356 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013357
Nate Begemanb65c1752010-12-17 22:55:37 +000013358 X = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, X);
13359 Y = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Y);
13360 Mask = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Mask);
Nate Begeman672fb622010-12-20 22:04:24 +000013361 Mask = DAG.getNode(X86ISD::PBLENDVB, DL, MVT::v16i8, X, Y, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000013362 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Mask);
13363 }
13364 }
13365 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013366
Nate Begemanb65c1752010-12-17 22:55:37 +000013367 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000013368 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
13369 std::swap(N0, N1);
13370 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
13371 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000013372 if (!N0.hasOneUse() || !N1.hasOneUse())
13373 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000013374
13375 SDValue ShAmt0 = N0.getOperand(1);
13376 if (ShAmt0.getValueType() != MVT::i8)
13377 return SDValue();
13378 SDValue ShAmt1 = N1.getOperand(1);
13379 if (ShAmt1.getValueType() != MVT::i8)
13380 return SDValue();
13381 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
13382 ShAmt0 = ShAmt0.getOperand(0);
13383 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
13384 ShAmt1 = ShAmt1.getOperand(0);
13385
13386 DebugLoc DL = N->getDebugLoc();
13387 unsigned Opc = X86ISD::SHLD;
13388 SDValue Op0 = N0.getOperand(0);
13389 SDValue Op1 = N1.getOperand(0);
13390 if (ShAmt0.getOpcode() == ISD::SUB) {
13391 Opc = X86ISD::SHRD;
13392 std::swap(Op0, Op1);
13393 std::swap(ShAmt0, ShAmt1);
13394 }
13395
Evan Cheng8b1190a2010-04-28 01:18:01 +000013396 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000013397 if (ShAmt1.getOpcode() == ISD::SUB) {
13398 SDValue Sum = ShAmt1.getOperand(0);
13399 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000013400 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
13401 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
13402 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
13403 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000013404 return DAG.getNode(Opc, DL, VT,
13405 Op0, Op1,
13406 DAG.getNode(ISD::TRUNCATE, DL,
13407 MVT::i8, ShAmt0));
13408 }
13409 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
13410 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
13411 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000013412 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000013413 return DAG.getNode(Opc, DL, VT,
13414 N0.getOperand(0), N1.getOperand(0),
13415 DAG.getNode(ISD::TRUNCATE, DL,
13416 MVT::i8, ShAmt0));
13417 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013418
Evan Cheng760d1942010-01-04 21:22:48 +000013419 return SDValue();
13420}
13421
Chris Lattner149a4e52008-02-22 02:09:43 +000013422/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000013423static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000013424 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000013425 StoreSDNode *St = cast<StoreSDNode>(N);
13426 EVT VT = St->getValue().getValueType();
13427 EVT StVT = St->getMemoryVT();
13428 DebugLoc dl = St->getDebugLoc();
Nadav Rotem5e742a32011-08-11 16:41:21 +000013429 SDValue StoredVal = St->getOperand(1);
13430 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13431
13432 // If we are saving a concatination of two XMM registers, perform two stores.
Nadav Rotem6236f7f2011-08-11 17:05:47 +000013433 // This is better in Sandy Bridge cause one 256-bit mem op is done via two
13434 // 128-bit ones. If in the future the cost becomes only one memory access the
13435 // first version would be better.
Nadav Rotem5e742a32011-08-11 16:41:21 +000013436 if (VT.getSizeInBits() == 256 &&
13437 StoredVal.getNode()->getOpcode() == ISD::CONCAT_VECTORS &&
13438 StoredVal.getNumOperands() == 2) {
13439
13440 SDValue Value0 = StoredVal.getOperand(0);
13441 SDValue Value1 = StoredVal.getOperand(1);
13442
13443 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
13444 SDValue Ptr0 = St->getBasePtr();
13445 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
13446
13447 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
13448 St->getPointerInfo(), St->isVolatile(),
13449 St->isNonTemporal(), St->getAlignment());
13450 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
13451 St->getPointerInfo(), St->isVolatile(),
13452 St->isNonTemporal(), St->getAlignment());
13453 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
13454 }
Nadav Rotem614061b2011-08-10 19:30:14 +000013455
13456 // Optimize trunc store (of multiple scalars) to shuffle and store.
13457 // First, pack all of the elements in one place. Next, store to memory
13458 // in fewer chunks.
13459 if (St->isTruncatingStore() && VT.isVector()) {
13460 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13461 unsigned NumElems = VT.getVectorNumElements();
13462 assert(StVT != VT && "Cannot truncate to the same type");
13463 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
13464 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
13465
13466 // From, To sizes and ElemCount must be pow of two
13467 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
13468 // We are going to use the original vector elt for storing.
13469 // accumulated smaller vector elements must be a multiple of bigger size.
13470 if (0 != (NumElems * ToSz) % FromSz) return SDValue();
13471 unsigned SizeRatio = FromSz / ToSz;
13472
13473 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
13474
13475 // Create a type on which we perform the shuffle
13476 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
13477 StVT.getScalarType(), NumElems*SizeRatio);
13478
13479 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
13480
13481 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
13482 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
13483 for (unsigned i = 0; i < NumElems; i++ ) ShuffleVec[i] = i * SizeRatio;
13484
13485 // Can't shuffle using an illegal type
13486 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
13487
13488 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
13489 DAG.getUNDEF(WideVec.getValueType()),
13490 ShuffleVec.data());
13491 // At this point all of the data is stored at the bottom of the
13492 // register. We now need to save it to mem.
13493
13494 // Find the largest store unit
13495 MVT StoreType = MVT::i8;
13496 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
13497 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
13498 MVT Tp = (MVT::SimpleValueType)tp;
13499 if (TLI.isTypeLegal(Tp) && StoreType.getSizeInBits() < NumElems * ToSz)
13500 StoreType = Tp;
13501 }
13502
13503 // Bitcast the original vector into a vector of store-size units
13504 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
13505 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
13506 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
13507 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
13508 SmallVector<SDValue, 8> Chains;
13509 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
13510 TLI.getPointerTy());
13511 SDValue Ptr = St->getBasePtr();
13512
13513 // Perform one or more big stores into memory.
13514 for (unsigned i = 0; i < (ToSz*NumElems)/StoreType.getSizeInBits() ; i++) {
13515 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
13516 StoreType, ShuffWide,
13517 DAG.getIntPtrConstant(i));
13518 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
13519 St->getPointerInfo(), St->isVolatile(),
13520 St->isNonTemporal(), St->getAlignment());
13521 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
13522 Chains.push_back(Ch);
13523 }
13524
13525 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
13526 Chains.size());
13527 }
13528
13529
Chris Lattner149a4e52008-02-22 02:09:43 +000013530 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
13531 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000013532 // A preferable solution to the general problem is to figure out the right
13533 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000013534
13535 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000013536 if (VT.getSizeInBits() != 64)
13537 return SDValue();
13538
Devang Patel578efa92009-06-05 21:57:13 +000013539 const Function *F = DAG.getMachineFunction().getFunction();
13540 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +000013541 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +000013542 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000013543 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000013544 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000013545 isa<LoadSDNode>(St->getValue()) &&
13546 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
13547 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000013548 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000013549 LoadSDNode *Ld = 0;
13550 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000013551 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000013552 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000013553 // Must be a store of a load. We currently handle two cases: the load
13554 // is a direct child, and it's under an intervening TokenFactor. It is
13555 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000013556 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000013557 Ld = cast<LoadSDNode>(St->getChain());
13558 else if (St->getValue().hasOneUse() &&
13559 ChainVal->getOpcode() == ISD::TokenFactor) {
13560 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000013561 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000013562 TokenFactorIndex = i;
13563 Ld = cast<LoadSDNode>(St->getValue());
13564 } else
13565 Ops.push_back(ChainVal->getOperand(i));
13566 }
13567 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000013568
Evan Cheng536e6672009-03-12 05:59:15 +000013569 if (!Ld || !ISD::isNormalLoad(Ld))
13570 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000013571
Evan Cheng536e6672009-03-12 05:59:15 +000013572 // If this is not the MMX case, i.e. we are just turning i64 load/store
13573 // into f64 load/store, avoid the transformation if there are multiple
13574 // uses of the loaded value.
13575 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
13576 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000013577
Evan Cheng536e6672009-03-12 05:59:15 +000013578 DebugLoc LdDL = Ld->getDebugLoc();
13579 DebugLoc StDL = N->getDebugLoc();
13580 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
13581 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
13582 // pair instead.
13583 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000013584 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000013585 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
13586 Ld->getPointerInfo(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000013587 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000013588 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000013589 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000013590 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000013591 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000013592 Ops.size());
13593 }
Evan Cheng536e6672009-03-12 05:59:15 +000013594 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000013595 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000013596 St->isVolatile(), St->isNonTemporal(),
13597 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000013598 }
Evan Cheng536e6672009-03-12 05:59:15 +000013599
13600 // Otherwise, lower to two pairs of 32-bit loads / stores.
13601 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000013602 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
13603 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000013604
Owen Anderson825b72b2009-08-11 20:47:22 +000013605 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000013606 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000013607 Ld->isVolatile(), Ld->isNonTemporal(),
13608 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000013609 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000013610 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000013611 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000013612 MinAlign(Ld->getAlignment(), 4));
13613
13614 SDValue NewChain = LoLd.getValue(1);
13615 if (TokenFactorIndex != -1) {
13616 Ops.push_back(LoLd);
13617 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000013618 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000013619 Ops.size());
13620 }
13621
13622 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000013623 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
13624 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000013625
13626 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000013627 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000013628 St->isVolatile(), St->isNonTemporal(),
13629 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000013630 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000013631 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000013632 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000013633 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000013634 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000013635 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000013636 }
Dan Gohman475871a2008-07-27 21:46:04 +000013637 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000013638}
13639
Chris Lattner6cf73262008-01-25 06:14:17 +000013640/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
13641/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000013642static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000013643 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
13644 // F[X]OR(0.0, x) -> x
13645 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000013646 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
13647 if (C->getValueAPF().isPosZero())
13648 return N->getOperand(1);
13649 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
13650 if (C->getValueAPF().isPosZero())
13651 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000013652 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000013653}
13654
13655/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000013656static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000013657 // FAND(0.0, x) -> 0.0
13658 // FAND(x, 0.0) -> 0.0
13659 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
13660 if (C->getValueAPF().isPosZero())
13661 return N->getOperand(0);
13662 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
13663 if (C->getValueAPF().isPosZero())
13664 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000013665 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000013666}
13667
Dan Gohmane5af2d32009-01-29 01:59:02 +000013668static SDValue PerformBTCombine(SDNode *N,
13669 SelectionDAG &DAG,
13670 TargetLowering::DAGCombinerInfo &DCI) {
13671 // BT ignores high bits in the bit index operand.
13672 SDValue Op1 = N->getOperand(1);
13673 if (Op1.hasOneUse()) {
13674 unsigned BitWidth = Op1.getValueSizeInBits();
13675 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
13676 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000013677 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
13678 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000013679 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000013680 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
13681 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
13682 DCI.CommitTargetLoweringOpt(TLO);
13683 }
13684 return SDValue();
13685}
Chris Lattner83e6c992006-10-04 06:57:07 +000013686
Eli Friedman7a5e5552009-06-07 06:52:44 +000013687static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
13688 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000013689 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000013690 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000013691 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000013692 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000013693 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000013694 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000013695 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000013696 }
13697 return SDValue();
13698}
13699
Evan Cheng2e489c42009-12-16 00:53:11 +000013700static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
13701 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
13702 // (and (i32 x86isd::setcc_carry), 1)
13703 // This eliminates the zext. This transformation is necessary because
13704 // ISD::SETCC is always legalized to i8.
13705 DebugLoc dl = N->getDebugLoc();
13706 SDValue N0 = N->getOperand(0);
13707 EVT VT = N->getValueType(0);
13708 if (N0.getOpcode() == ISD::AND &&
13709 N0.hasOneUse() &&
13710 N0.getOperand(0).hasOneUse()) {
13711 SDValue N00 = N0.getOperand(0);
13712 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
13713 return SDValue();
13714 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
13715 if (!C || C->getZExtValue() != 1)
13716 return SDValue();
13717 return DAG.getNode(ISD::AND, dl, VT,
13718 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
13719 N00.getOperand(0), N00.getOperand(1)),
13720 DAG.getConstant(1, VT));
13721 }
13722
13723 return SDValue();
13724}
13725
Chris Lattnerc19d1c32010-12-19 22:08:31 +000013726// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
13727static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
13728 unsigned X86CC = N->getConstantOperandVal(0);
13729 SDValue EFLAG = N->getOperand(1);
13730 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013731
Chris Lattnerc19d1c32010-12-19 22:08:31 +000013732 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
13733 // a zext and produces an all-ones bit which is more useful than 0/1 in some
13734 // cases.
13735 if (X86CC == X86::COND_B)
13736 return DAG.getNode(ISD::AND, DL, MVT::i8,
13737 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
13738 DAG.getConstant(X86CC, MVT::i8), EFLAG),
13739 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013740
Chris Lattnerc19d1c32010-12-19 22:08:31 +000013741 return SDValue();
13742}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013743
Benjamin Kramer1396c402011-06-18 11:09:41 +000013744static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
13745 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000013746 SDValue Op0 = N->getOperand(0);
13747 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
13748 // a 32-bit target where SSE doesn't support i64->FP operations.
13749 if (Op0.getOpcode() == ISD::LOAD) {
13750 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
13751 EVT VT = Ld->getValueType(0);
13752 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
13753 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
13754 !XTLI->getSubtarget()->is64Bit() &&
13755 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000013756 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
13757 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000013758 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
13759 return FILDChain;
13760 }
13761 }
13762 return SDValue();
13763}
13764
Chris Lattner23a01992010-12-20 01:37:09 +000013765// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
13766static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
13767 X86TargetLowering::DAGCombinerInfo &DCI) {
13768 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
13769 // the result is either zero or one (depending on the input carry bit).
13770 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
13771 if (X86::isZeroNode(N->getOperand(0)) &&
13772 X86::isZeroNode(N->getOperand(1)) &&
13773 // We don't have a good way to replace an EFLAGS use, so only do this when
13774 // dead right now.
13775 SDValue(N, 1).use_empty()) {
13776 DebugLoc DL = N->getDebugLoc();
13777 EVT VT = N->getValueType(0);
13778 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
13779 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
13780 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
13781 DAG.getConstant(X86::COND_B,MVT::i8),
13782 N->getOperand(2)),
13783 DAG.getConstant(1, VT));
13784 return DCI.CombineTo(N, Res1, CarryOut);
13785 }
13786
13787 return SDValue();
13788}
13789
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000013790// fold (add Y, (sete X, 0)) -> adc 0, Y
13791// (add Y, (setne X, 0)) -> sbb -1, Y
13792// (sub (sete X, 0), Y) -> sbb 0, Y
13793// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000013794static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000013795 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013796
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000013797 // Look through ZExts.
13798 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
13799 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
13800 return SDValue();
13801
13802 SDValue SetCC = Ext.getOperand(0);
13803 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
13804 return SDValue();
13805
13806 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
13807 if (CC != X86::COND_E && CC != X86::COND_NE)
13808 return SDValue();
13809
13810 SDValue Cmp = SetCC.getOperand(1);
13811 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000013812 !X86::isZeroNode(Cmp.getOperand(1)) ||
13813 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000013814 return SDValue();
13815
13816 SDValue CmpOp0 = Cmp.getOperand(0);
13817 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
13818 DAG.getConstant(1, CmpOp0.getValueType()));
13819
13820 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
13821 if (CC == X86::COND_NE)
13822 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
13823 DL, OtherVal.getValueType(), OtherVal,
13824 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
13825 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
13826 DL, OtherVal.getValueType(), OtherVal,
13827 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
13828}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000013829
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000013830static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG) {
13831 SDValue Op0 = N->getOperand(0);
13832 SDValue Op1 = N->getOperand(1);
13833
13834 // X86 can't encode an immediate LHS of a sub. See if we can push the
13835 // negation into a preceding instruction.
13836 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000013837 // If the RHS of the sub is a XOR with one use and a constant, invert the
13838 // immediate. Then add one to the LHS of the sub so we can turn
13839 // X-Y -> X+~Y+1, saving one register.
13840 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
13841 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000013842 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000013843 EVT VT = Op0.getValueType();
13844 SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
13845 Op1.getOperand(0),
13846 DAG.getConstant(~XorC, VT));
13847 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000013848 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000013849 }
13850 }
13851
13852 return OptimizeConditionalInDecrement(N, DAG);
13853}
13854
Dan Gohman475871a2008-07-27 21:46:04 +000013855SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000013856 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000013857 SelectionDAG &DAG = DCI.DAG;
13858 switch (N->getOpcode()) {
13859 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013860 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000013861 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +000013862 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000013863 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000013864 case ISD::ADD: return OptimizeConditionalInDecrement(N, DAG);
13865 case ISD::SUB: return PerformSubCombine(N, DAG);
Chris Lattner23a01992010-12-20 01:37:09 +000013866 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000013867 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000013868 case ISD::SHL:
13869 case ISD::SRA:
13870 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000013871 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000013872 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000013873 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000013874 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Chris Lattner6cf73262008-01-25 06:14:17 +000013875 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000013876 case X86ISD::FOR: return PerformFORCombine(N, DAG);
13877 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000013878 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000013879 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000013880 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000013881 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000013882 case X86ISD::SHUFPS: // Handle all target specific shuffles
13883 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000013884 case X86ISD::PALIGN:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000013885 case X86ISD::PUNPCKHBW:
13886 case X86ISD::PUNPCKHWD:
13887 case X86ISD::PUNPCKHDQ:
13888 case X86ISD::PUNPCKHQDQ:
13889 case X86ISD::UNPCKHPS:
13890 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +000013891 case X86ISD::VUNPCKHPSY:
13892 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000013893 case X86ISD::PUNPCKLBW:
13894 case X86ISD::PUNPCKLWD:
13895 case X86ISD::PUNPCKLDQ:
13896 case X86ISD::PUNPCKLQDQ:
13897 case X86ISD::UNPCKLPS:
13898 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +000013899 case X86ISD::VUNPCKLPSY:
13900 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000013901 case X86ISD::MOVHLPS:
13902 case X86ISD::MOVLHPS:
13903 case X86ISD::PSHUFD:
13904 case X86ISD::PSHUFHW:
13905 case X86ISD::PSHUFLW:
13906 case X86ISD::MOVSS:
13907 case X86ISD::MOVSD:
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +000013908 case X86ISD::VPERMILPS:
13909 case X86ISD::VPERMILPSY:
13910 case X86ISD::VPERMILPD:
13911 case X86ISD::VPERMILPDY:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +000013912 case X86ISD::VPERM2F128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000013913 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000013914 }
13915
Dan Gohman475871a2008-07-27 21:46:04 +000013916 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000013917}
13918
Evan Chenge5b51ac2010-04-17 06:13:15 +000013919/// isTypeDesirableForOp - Return true if the target has native support for
13920/// the specified value type and it is 'desirable' to use the type for the
13921/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
13922/// instruction encodings are longer and some i16 instructions are slow.
13923bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
13924 if (!isTypeLegal(VT))
13925 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000013926 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000013927 return true;
13928
13929 switch (Opc) {
13930 default:
13931 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000013932 case ISD::LOAD:
13933 case ISD::SIGN_EXTEND:
13934 case ISD::ZERO_EXTEND:
13935 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000013936 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000013937 case ISD::SRL:
13938 case ISD::SUB:
13939 case ISD::ADD:
13940 case ISD::MUL:
13941 case ISD::AND:
13942 case ISD::OR:
13943 case ISD::XOR:
13944 return false;
13945 }
13946}
13947
13948/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000013949/// beneficial for dag combiner to promote the specified node. If true, it
13950/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000013951bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000013952 EVT VT = Op.getValueType();
13953 if (VT != MVT::i16)
13954 return false;
13955
Evan Cheng4c26e932010-04-19 19:29:22 +000013956 bool Promote = false;
13957 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000013958 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000013959 default: break;
13960 case ISD::LOAD: {
13961 LoadSDNode *LD = cast<LoadSDNode>(Op);
13962 // If the non-extending load has a single use and it's not live out, then it
13963 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000013964 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
13965 Op.hasOneUse()*/) {
13966 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
13967 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
13968 // The only case where we'd want to promote LOAD (rather then it being
13969 // promoted as an operand is when it's only use is liveout.
13970 if (UI->getOpcode() != ISD::CopyToReg)
13971 return false;
13972 }
13973 }
Evan Cheng4c26e932010-04-19 19:29:22 +000013974 Promote = true;
13975 break;
13976 }
13977 case ISD::SIGN_EXTEND:
13978 case ISD::ZERO_EXTEND:
13979 case ISD::ANY_EXTEND:
13980 Promote = true;
13981 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000013982 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000013983 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000013984 SDValue N0 = Op.getOperand(0);
13985 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000013986 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000013987 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000013988 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000013989 break;
13990 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000013991 case ISD::ADD:
13992 case ISD::MUL:
13993 case ISD::AND:
13994 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000013995 case ISD::XOR:
13996 Commute = true;
13997 // fallthrough
13998 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000013999 SDValue N0 = Op.getOperand(0);
14000 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000014001 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014002 return false;
14003 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000014004 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014005 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000014006 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014007 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000014008 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014009 }
14010 }
14011
14012 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000014013 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014014}
14015
Evan Cheng60c07e12006-07-05 22:17:51 +000014016//===----------------------------------------------------------------------===//
14017// X86 Inline Assembly Support
14018//===----------------------------------------------------------------------===//
14019
Chris Lattnerb8105652009-07-20 17:51:36 +000014020bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
14021 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000014022
14023 std::string AsmStr = IA->getAsmString();
14024
14025 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000014026 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000014027 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000014028
14029 switch (AsmPieces.size()) {
14030 default: return false;
14031 case 1:
14032 AsmStr = AsmPieces[0];
14033 AsmPieces.clear();
14034 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
14035
Chris Lattner7a2bdde2011-04-15 05:18:47 +000014036 // FIXME: this should verify that we are targeting a 486 or better. If not,
Evan Cheng55d42002011-01-08 01:24:27 +000014037 // we will turn this bswap into something that will be lowered to logical ops
14038 // instead of emitting the bswap asm. For now, we don't support 486 or lower
14039 // so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000014040 // bswap $0
14041 if (AsmPieces.size() == 2 &&
14042 (AsmPieces[0] == "bswap" ||
14043 AsmPieces[0] == "bswapq" ||
14044 AsmPieces[0] == "bswapl") &&
14045 (AsmPieces[1] == "$0" ||
14046 AsmPieces[1] == "${0:q}")) {
14047 // No need to check constraints, nothing other than the equivalent of
14048 // "=r,0" would be valid here.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014049 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014050 if (!Ty || Ty->getBitWidth() % 16 != 0)
14051 return false;
14052 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000014053 }
14054 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000014055 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000014056 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000014057 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000014058 AsmPieces[1] == "$$8," &&
14059 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000014060 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
14061 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014062 const std::string &ConstraintsStr = IA->getConstraintString();
14063 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000014064 std::sort(AsmPieces.begin(), AsmPieces.end());
14065 if (AsmPieces.size() == 4 &&
14066 AsmPieces[0] == "~{cc}" &&
14067 AsmPieces[1] == "~{dirflag}" &&
14068 AsmPieces[2] == "~{flags}" &&
14069 AsmPieces[3] == "~{fpsr}") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014070 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014071 if (!Ty || Ty->getBitWidth() % 16 != 0)
14072 return false;
14073 return IntrinsicLowering::LowerToByteSwap(CI);
Dan Gohman0ef701e2010-03-04 19:58:08 +000014074 }
Chris Lattnerb8105652009-07-20 17:51:36 +000014075 }
14076 break;
14077 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000014078 if (CI->getType()->isIntegerTy(32) &&
14079 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
14080 SmallVector<StringRef, 4> Words;
14081 SplitString(AsmPieces[0], Words, " \t,");
14082 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
14083 Words[2] == "${0:w}") {
14084 Words.clear();
14085 SplitString(AsmPieces[1], Words, " \t,");
14086 if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
14087 Words[2] == "$0") {
14088 Words.clear();
14089 SplitString(AsmPieces[2], Words, " \t,");
14090 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
14091 Words[2] == "${0:w}") {
14092 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014093 const std::string &ConstraintsStr = IA->getConstraintString();
14094 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Peter Collingbourne948cf022010-11-13 19:54:30 +000014095 std::sort(AsmPieces.begin(), AsmPieces.end());
14096 if (AsmPieces.size() == 4 &&
14097 AsmPieces[0] == "~{cc}" &&
14098 AsmPieces[1] == "~{dirflag}" &&
14099 AsmPieces[2] == "~{flags}" &&
14100 AsmPieces[3] == "~{fpsr}") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014101 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014102 if (!Ty || Ty->getBitWidth() % 16 != 0)
14103 return false;
14104 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000014105 }
14106 }
14107 }
14108 }
14109 }
Evan Cheng55d42002011-01-08 01:24:27 +000014110
14111 if (CI->getType()->isIntegerTy(64)) {
14112 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
14113 if (Constraints.size() >= 2 &&
14114 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
14115 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
14116 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
14117 SmallVector<StringRef, 4> Words;
14118 SplitString(AsmPieces[0], Words, " \t");
14119 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
Chris Lattnerb8105652009-07-20 17:51:36 +000014120 Words.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014121 SplitString(AsmPieces[1], Words, " \t");
14122 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
14123 Words.clear();
14124 SplitString(AsmPieces[2], Words, " \t,");
14125 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
14126 Words[2] == "%edx") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014127 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014128 if (!Ty || Ty->getBitWidth() % 16 != 0)
14129 return false;
14130 return IntrinsicLowering::LowerToByteSwap(CI);
14131 }
Chris Lattnerb8105652009-07-20 17:51:36 +000014132 }
14133 }
14134 }
14135 }
14136 break;
14137 }
14138 return false;
14139}
14140
14141
14142
Chris Lattnerf4dff842006-07-11 02:54:03 +000014143/// getConstraintType - Given a constraint letter, return the type of
14144/// constraint it is for this target.
14145X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000014146X86TargetLowering::getConstraintType(const std::string &Constraint) const {
14147 if (Constraint.size() == 1) {
14148 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000014149 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000014150 case 'q':
14151 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000014152 case 'f':
14153 case 't':
14154 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000014155 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000014156 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000014157 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000014158 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000014159 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000014160 case 'a':
14161 case 'b':
14162 case 'c':
14163 case 'd':
14164 case 'S':
14165 case 'D':
14166 case 'A':
14167 return C_Register;
14168 case 'I':
14169 case 'J':
14170 case 'K':
14171 case 'L':
14172 case 'M':
14173 case 'N':
14174 case 'G':
14175 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000014176 case 'e':
14177 case 'Z':
14178 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000014179 default:
14180 break;
14181 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000014182 }
Chris Lattner4234f572007-03-25 02:14:49 +000014183 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000014184}
14185
John Thompson44ab89e2010-10-29 17:29:13 +000014186/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000014187/// This object must already have been set up with the operand type
14188/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000014189TargetLowering::ConstraintWeight
14190 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000014191 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000014192 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014193 Value *CallOperandVal = info.CallOperandVal;
14194 // If we don't have a value, we can't do a match,
14195 // but allow it at the lowest weight.
14196 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000014197 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014198 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000014199 // Look at the constraint type.
14200 switch (*constraint) {
14201 default:
John Thompson44ab89e2010-10-29 17:29:13 +000014202 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
14203 case 'R':
14204 case 'q':
14205 case 'Q':
14206 case 'a':
14207 case 'b':
14208 case 'c':
14209 case 'd':
14210 case 'S':
14211 case 'D':
14212 case 'A':
14213 if (CallOperandVal->getType()->isIntegerTy())
14214 weight = CW_SpecificReg;
14215 break;
14216 case 'f':
14217 case 't':
14218 case 'u':
14219 if (type->isFloatingPointTy())
14220 weight = CW_SpecificReg;
14221 break;
14222 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000014223 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000014224 weight = CW_SpecificReg;
14225 break;
14226 case 'x':
14227 case 'Y':
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014228 if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
John Thompson44ab89e2010-10-29 17:29:13 +000014229 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014230 break;
14231 case 'I':
14232 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
14233 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000014234 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014235 }
14236 break;
John Thompson44ab89e2010-10-29 17:29:13 +000014237 case 'J':
14238 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14239 if (C->getZExtValue() <= 63)
14240 weight = CW_Constant;
14241 }
14242 break;
14243 case 'K':
14244 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14245 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
14246 weight = CW_Constant;
14247 }
14248 break;
14249 case 'L':
14250 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14251 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
14252 weight = CW_Constant;
14253 }
14254 break;
14255 case 'M':
14256 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14257 if (C->getZExtValue() <= 3)
14258 weight = CW_Constant;
14259 }
14260 break;
14261 case 'N':
14262 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14263 if (C->getZExtValue() <= 0xff)
14264 weight = CW_Constant;
14265 }
14266 break;
14267 case 'G':
14268 case 'C':
14269 if (dyn_cast<ConstantFP>(CallOperandVal)) {
14270 weight = CW_Constant;
14271 }
14272 break;
14273 case 'e':
14274 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14275 if ((C->getSExtValue() >= -0x80000000LL) &&
14276 (C->getSExtValue() <= 0x7fffffffLL))
14277 weight = CW_Constant;
14278 }
14279 break;
14280 case 'Z':
14281 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14282 if (C->getZExtValue() <= 0xffffffff)
14283 weight = CW_Constant;
14284 }
14285 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014286 }
14287 return weight;
14288}
14289
Dale Johannesenba2a0b92008-01-29 02:21:21 +000014290/// LowerXConstraint - try to replace an X constraint, which matches anything,
14291/// with another that has more specific requirements based on the type of the
14292/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000014293const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000014294LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000014295 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
14296 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000014297 if (ConstraintVT.isFloatingPoint()) {
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014298 if (Subtarget->hasXMMInt())
Chris Lattner5e764232008-04-26 23:02:14 +000014299 return "Y";
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014300 if (Subtarget->hasXMM())
Chris Lattner5e764232008-04-26 23:02:14 +000014301 return "x";
14302 }
Scott Michelfdc40a02009-02-17 22:15:04 +000014303
Chris Lattner5e764232008-04-26 23:02:14 +000014304 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000014305}
14306
Chris Lattner48884cd2007-08-25 00:47:38 +000014307/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
14308/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000014309void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000014310 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000014311 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000014312 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000014313 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000014314
Eric Christopher100c8332011-06-02 23:16:42 +000014315 // Only support length 1 constraints for now.
14316 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000014317
Eric Christopher100c8332011-06-02 23:16:42 +000014318 char ConstraintLetter = Constraint[0];
14319 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000014320 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000014321 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000014322 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000014323 if (C->getZExtValue() <= 31) {
14324 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000014325 break;
14326 }
Devang Patel84f7fd22007-03-17 00:13:28 +000014327 }
Chris Lattner48884cd2007-08-25 00:47:38 +000014328 return;
Evan Cheng364091e2008-09-22 23:57:37 +000014329 case 'J':
14330 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000014331 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000014332 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
14333 break;
14334 }
14335 }
14336 return;
14337 case 'K':
14338 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000014339 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000014340 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
14341 break;
14342 }
14343 }
14344 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000014345 case 'N':
14346 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000014347 if (C->getZExtValue() <= 255) {
14348 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000014349 break;
14350 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000014351 }
Chris Lattner48884cd2007-08-25 00:47:38 +000014352 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000014353 case 'e': {
14354 // 32-bit signed value
14355 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000014356 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
14357 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000014358 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000014359 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000014360 break;
14361 }
14362 // FIXME gcc accepts some relocatable values here too, but only in certain
14363 // memory models; it's complicated.
14364 }
14365 return;
14366 }
14367 case 'Z': {
14368 // 32-bit unsigned value
14369 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000014370 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
14371 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000014372 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
14373 break;
14374 }
14375 }
14376 // FIXME gcc accepts some relocatable values here too, but only in certain
14377 // memory models; it's complicated.
14378 return;
14379 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000014380 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000014381 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000014382 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000014383 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000014384 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000014385 break;
14386 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014387
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000014388 // In any sort of PIC mode addresses need to be computed at runtime by
14389 // adding in a register or some sort of table lookup. These can't
14390 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000014391 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000014392 return;
14393
Chris Lattnerdc43a882007-05-03 16:52:29 +000014394 // If we are in non-pic codegen mode, we allow the address of a global (with
14395 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000014396 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000014397 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000014398
Chris Lattner49921962009-05-08 18:23:14 +000014399 // Match either (GA), (GA+C), (GA+C1+C2), etc.
14400 while (1) {
14401 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
14402 Offset += GA->getOffset();
14403 break;
14404 } else if (Op.getOpcode() == ISD::ADD) {
14405 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
14406 Offset += C->getZExtValue();
14407 Op = Op.getOperand(0);
14408 continue;
14409 }
14410 } else if (Op.getOpcode() == ISD::SUB) {
14411 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
14412 Offset += -C->getZExtValue();
14413 Op = Op.getOperand(0);
14414 continue;
14415 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000014416 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000014417
Chris Lattner49921962009-05-08 18:23:14 +000014418 // Otherwise, this isn't something we can handle, reject it.
14419 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000014420 }
Eric Christopherfd179292009-08-27 18:07:15 +000014421
Dan Gohman46510a72010-04-15 01:51:59 +000014422 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000014423 // If we require an extra load to get this address, as in PIC mode, we
14424 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000014425 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
14426 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000014427 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000014428
Devang Patel0d881da2010-07-06 22:08:15 +000014429 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
14430 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000014431 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000014432 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000014433 }
Scott Michelfdc40a02009-02-17 22:15:04 +000014434
Gabor Greifba36cb52008-08-28 21:40:38 +000014435 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000014436 Ops.push_back(Result);
14437 return;
14438 }
Dale Johannesen1784d162010-06-25 21:55:36 +000014439 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000014440}
14441
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014442std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000014443X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000014444 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000014445 // First, see if this is a constraint that directly corresponds to an LLVM
14446 // register class.
14447 if (Constraint.size() == 1) {
14448 // GCC Constraint Letters
14449 switch (Constraint[0]) {
14450 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000014451 // TODO: Slight differences here in allocation order and leaving
14452 // RIP in the class. Do they matter any more here than they do
14453 // in the normal allocation?
14454 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
14455 if (Subtarget->is64Bit()) {
Nick Lewycky9bf45d02011-07-08 00:19:27 +000014456 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000014457 return std::make_pair(0U, X86::GR32RegisterClass);
14458 else if (VT == MVT::i16)
14459 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000014460 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000014461 return std::make_pair(0U, X86::GR8RegisterClass);
Nick Lewycky9bf45d02011-07-08 00:19:27 +000014462 else if (VT == MVT::i64 || VT == MVT::f64)
Eric Christopherd176af82011-06-29 17:23:50 +000014463 return std::make_pair(0U, X86::GR64RegisterClass);
14464 break;
14465 }
14466 // 32-bit fallthrough
14467 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000014468 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000014469 return std::make_pair(0U, X86::GR32_ABCDRegisterClass);
14470 else if (VT == MVT::i16)
14471 return std::make_pair(0U, X86::GR16_ABCDRegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000014472 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000014473 return std::make_pair(0U, X86::GR8_ABCD_LRegisterClass);
14474 else if (VT == MVT::i64)
14475 return std::make_pair(0U, X86::GR64_ABCDRegisterClass);
14476 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000014477 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000014478 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000014479 if (VT == MVT::i8 || VT == MVT::i1)
Chris Lattner0f65cad2007-04-09 05:49:22 +000014480 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000014481 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000014482 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000014483 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000014484 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000014485 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000014486 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000014487 if (VT == MVT::i8 || VT == MVT::i1)
Dale Johannesen5f3663e2009-10-07 22:47:20 +000014488 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
14489 if (VT == MVT::i16)
14490 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
14491 if (VT == MVT::i32 || !Subtarget->is64Bit())
14492 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
14493 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000014494 case 'f': // FP Stack registers.
14495 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
14496 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000014497 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000014498 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000014499 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000014500 return std::make_pair(0U, X86::RFP64RegisterClass);
14501 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000014502 case 'y': // MMX_REGS if MMX allowed.
14503 if (!Subtarget->hasMMX()) break;
14504 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000014505 case 'Y': // SSE_REGS if SSE2 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014506 if (!Subtarget->hasXMMInt()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000014507 // FALL THROUGH.
14508 case 'x': // SSE_REGS if SSE1 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014509 if (!Subtarget->hasXMM()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000014510
Owen Anderson825b72b2009-08-11 20:47:22 +000014511 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000014512 default: break;
14513 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000014514 case MVT::f32:
14515 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000014516 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000014517 case MVT::f64:
14518 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000014519 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000014520 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000014521 case MVT::v16i8:
14522 case MVT::v8i16:
14523 case MVT::v4i32:
14524 case MVT::v2i64:
14525 case MVT::v4f32:
14526 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000014527 return std::make_pair(0U, X86::VR128RegisterClass);
14528 }
Chris Lattnerad043e82007-04-09 05:11:28 +000014529 break;
14530 }
14531 }
Scott Michelfdc40a02009-02-17 22:15:04 +000014532
Chris Lattnerf76d1802006-07-31 23:26:50 +000014533 // Use the default implementation in TargetLowering to convert the register
14534 // constraint into a member of a register class.
14535 std::pair<unsigned, const TargetRegisterClass*> Res;
14536 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000014537
14538 // Not found as a standard register?
14539 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000014540 // Map st(0) -> st(7) -> ST0
14541 if (Constraint.size() == 7 && Constraint[0] == '{' &&
14542 tolower(Constraint[1]) == 's' &&
14543 tolower(Constraint[2]) == 't' &&
14544 Constraint[3] == '(' &&
14545 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
14546 Constraint[5] == ')' &&
14547 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000014548
Chris Lattner56d77c72009-09-13 22:41:48 +000014549 Res.first = X86::ST0+Constraint[4]-'0';
14550 Res.second = X86::RFP80RegisterClass;
14551 return Res;
14552 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000014553
Chris Lattner56d77c72009-09-13 22:41:48 +000014554 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000014555 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000014556 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000014557 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000014558 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000014559 }
Chris Lattner56d77c72009-09-13 22:41:48 +000014560
14561 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000014562 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000014563 Res.first = X86::EFLAGS;
14564 Res.second = X86::CCRRegisterClass;
14565 return Res;
14566 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000014567
Dale Johannesen330169f2008-11-13 21:52:36 +000014568 // 'A' means EAX + EDX.
14569 if (Constraint == "A") {
14570 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000014571 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000014572 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000014573 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000014574 return Res;
14575 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014576
Chris Lattnerf76d1802006-07-31 23:26:50 +000014577 // Otherwise, check to see if this is a register class of the wrong value
14578 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
14579 // turn into {ax},{dx}.
14580 if (Res.second->hasType(VT))
14581 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014582
Chris Lattnerf76d1802006-07-31 23:26:50 +000014583 // All of the single-register GCC register classes map their values onto
14584 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
14585 // really want an 8-bit or 32-bit register, map to the appropriate register
14586 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000014587 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000014588 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000014589 unsigned DestReg = 0;
14590 switch (Res.first) {
14591 default: break;
14592 case X86::AX: DestReg = X86::AL; break;
14593 case X86::DX: DestReg = X86::DL; break;
14594 case X86::CX: DestReg = X86::CL; break;
14595 case X86::BX: DestReg = X86::BL; break;
14596 }
14597 if (DestReg) {
14598 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000014599 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000014600 }
Owen Anderson825b72b2009-08-11 20:47:22 +000014601 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000014602 unsigned DestReg = 0;
14603 switch (Res.first) {
14604 default: break;
14605 case X86::AX: DestReg = X86::EAX; break;
14606 case X86::DX: DestReg = X86::EDX; break;
14607 case X86::CX: DestReg = X86::ECX; break;
14608 case X86::BX: DestReg = X86::EBX; break;
14609 case X86::SI: DestReg = X86::ESI; break;
14610 case X86::DI: DestReg = X86::EDI; break;
14611 case X86::BP: DestReg = X86::EBP; break;
14612 case X86::SP: DestReg = X86::ESP; break;
14613 }
14614 if (DestReg) {
14615 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000014616 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000014617 }
Owen Anderson825b72b2009-08-11 20:47:22 +000014618 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000014619 unsigned DestReg = 0;
14620 switch (Res.first) {
14621 default: break;
14622 case X86::AX: DestReg = X86::RAX; break;
14623 case X86::DX: DestReg = X86::RDX; break;
14624 case X86::CX: DestReg = X86::RCX; break;
14625 case X86::BX: DestReg = X86::RBX; break;
14626 case X86::SI: DestReg = X86::RSI; break;
14627 case X86::DI: DestReg = X86::RDI; break;
14628 case X86::BP: DestReg = X86::RBP; break;
14629 case X86::SP: DestReg = X86::RSP; break;
14630 }
14631 if (DestReg) {
14632 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000014633 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000014634 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000014635 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000014636 } else if (Res.second == X86::FR32RegisterClass ||
14637 Res.second == X86::FR64RegisterClass ||
14638 Res.second == X86::VR128RegisterClass) {
14639 // Handle references to XMM physical registers that got mapped into the
14640 // wrong class. This can happen with constraints like {xmm0} where the
14641 // target independent register mapper will just pick the first match it can
14642 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000014643 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000014644 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000014645 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000014646 Res.second = X86::FR64RegisterClass;
14647 else if (X86::VR128RegisterClass->hasType(VT))
14648 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000014649 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014650
Chris Lattnerf76d1802006-07-31 23:26:50 +000014651 return Res;
14652}